Re: [Tutor] Index Out of Range?List

2017-05-03 Thread Alan Gauld via Tutor
On 04/05/17 00:32, Stephen P. Molnar wrote:

> import numpy as np
> 
> name = input("Enter Molecule ID: ")
> name = str(name)

You don't need the str(), input always returns a string.

> name_in =name[:]+'.lac.dat'

And you don't need the [:]. Just use

name_in = name + '.lac.dat'

> print(name_in)
> 
> atm_chg = []

> """
> atm_chg = open(name_in,'r')
> for line in atm_chg:
>  print(line, end=' ')
> """
This creates a 3 line string which is not assigned to any object.
It is not executable code and will not be executed. Maybe you
are doing it as a way of commenting out a block? If so it would be
better in a post to just delete it, it just adds confusion
otherwise. (Well, it confused me! :-)

> with open(name_in) as f:
>  # skip two lines
>  f.readline()
>  f.readline()
>  for line in f.readlines():
>  atm_chg.append(float( line.split()[-1] ))
> 
> p.asarray({atm_chg})

Where did p come from?
Should it be np? asarray() sounds like it might be a numpy thing.
And I'm not sure what the {atm_chg} is supposed to do - create
a single element set using your list maybe? I get an "unhashable"
error if I try it at the  prompt.

> When it is run I get:
> 
> IndexError: list index out of range

I'm pretty sure you get more than that, please post the full
error text, it's much harder to diagnose problems with just
the summary.

Since the only indexing you do is in this line

>  atm_chg.append(float( line.split()[-1] ))

I'll assume that's where the problem lies.
Try checking if the string is not empty before using it:

 for line in f.readlines():
 if line:
atm_chg.append(float( line.split()[-1] ))


> However, the Variable Explorer shows:

I have no idea what the Variable Explorer is?
Is it part of your IDE? Or of numpy?
If the IDE which IDE are you using?

>   [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
> [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
> [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
> [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
> 
> One line of which is exactly what I want as input to the next step in 
> the larger calculation.
> 
> Now, my question is how do I extract just one line of this file?

Any particular line? And which file are you talking about?
The data should be in the list variable, atm_chg.
In which case the first line is therefore:  atm_chg[0]

Or you can process each line using the usual for loop:

for line in atm_chg:
# use line here

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Index Out of Range?List

2017-05-03 Thread Steven D'Aprano
On Wed, May 03, 2017 at 07:32:53PM -0400, Stephen P. Molnar wrote:

[...]
> When it is run I get:
> 
> IndexError: list index out of range

That alone is useless to us. Please post the full traceback, starting 
from the line 

Traceback (most recent call last):

Among other things, it will show us the line of code that causes the 
error. Otherwise, we're just guessing.

You might also like to consider a simple debugging technique: print 
the index and the length of the list just before trying to use them. 
E.g. if you want to extract the nth item of a list, write:

print(n, len(a_list))
value = a_list[n]


(And yes, I completely agree that the error message should show that 
information, but it currently doesn't.)


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Index Out of Range?List

2017-05-03 Thread Stephen P. Molnar
I have a section of Python 3 code that is intended to be part of a 
larger program which essentially inputs the file:


LOEWDIN ATOMIC CHARGES
--
   0 C :   -0.780631
   1 H :0.114577
   2 Br:0.309802
   3 Cl:0.357316
   4 F :   -0.001065

The code is:

import numpy as np


name = input("Enter Molecule ID: ")
name = str(name)

name_in =name[:]+'.lac.dat'
print(name_in)

atm_chg = []
"""
atm_chg = open(name_in,'r')
for line in atm_chg:
print(line, end=' ')
"""
#all_contents = []
with open(name_in) as f:
# skip two lines
f.readline()
f.readline()
for line in f.readlines():
atm_chg.append(float( line.split()[-1] ))

p.asarray({atm_chg})

When it is run I get:

IndexError: list index out of range

However, the Variable Explorer shows:

 [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]

One line of which is exactly what I want as input to the next step in 
the larger calculation.


Now, my question is how do I extract just one line of this file?

Thanks in advance.

--
Stephen P. Molnar, Ph.D.Life is a fuzzy set
www.molecular-modeling.net  Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Bind DNS Zone config

2017-05-03 Thread boB Stepp
Hello Nathan!

On Wed, May 3, 2017 at 4:40 AM, Nathan D'Elboux
 wrote:

>
> What i am trying to do is have the below syntax somehow stored into a block
> of strings exactly in this syntax. I dont know what data type i should be
> trying to store this as, a string obviously but how would i preserve the
> syntax so it outputs in the identical way? The
>
> zone "." {
> 6 type master;
> 7 //type hint;
> 8 file "/etc/bind/db.root.honeypot";
> 9
>
>
>
>
> };

Do you know about triple quotes in Python?  They can be used when you
wish to have a multi-line string.  You can use either single quotes:

py3: my_string = '''Line one...
... Line two...
... Line three!'''
py3: print(my_string)
Line one...
Line two...
Line three!

Or you could use three double quotes:

py3: my_string = """Line one...
... Line two...
... Line three!"""
py3: print(my_string)
Line one...
Line two...
Line three!

> In between the "." I will be asking a user for input to insert a domain in
> plain ASCII and place it in that location of the zone file config and print
> it to STD OUT on the terminal. Perhaps later another version of this i will
> include writing directly to a file but for now i want to ensure the
> inserting of string into specific position works and just print it out.
>
> So im thinking of something along the lines of
>
> User_input_Zone = str(input("Enter the domain you wish to block: ")

Note:  The str() you use with input() is unnecessary.  input() always
returns a string; no need to convert it to a string.  On the other
hand if the input was an integer or a float, then you would need to
use int() or float(), respectively.

> Def zone_blacklist
> New_zone = "zone "{}" {, user_input_zone
> 6  type master;
> 7  //type hint;
> 8  file "/etc/bind/db.root.honeypot";
> 9 };"
> Print(new_zone)
>
> What i was thinking of doing is having a place holder of {} in place of
> where i want the domain to be inserted into but i dont know how to
> structure the syntax of a zone file in a function within

Have you looked into string formatting?  See:

https://docs.python.org/3/tutorial/inputoutput.html

in the Python Tutorial.  What you appear to be interested in falls a
bit farther down the page.  And you can always google Python 3 string
formatting.

HTH!

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Bind DNS Zone config

2017-05-03 Thread Nathan D'Elboux
Hi all,

I am new to python and have been studying Python 3 at uni and in my spare
time for the past 6 months or so.

I could probably do this in BASH given its on a linux system but am really
incorporating python into my daily work as much as i can, so i have chosen
python3 to implement this

What i am trying to do is have the below syntax somehow stored into a block
of strings exactly in this syntax. I dont know what data type i should be
trying to store this as, a string obviously but how would i preserve the
syntax so it outputs in the identical way? The

zone "." {
6 type master;
7 //type hint;
8 file "/etc/bind/db.root.honeypot";
9




};






In between the "." I will be asking a user for input to insert a domain in
plain ASCII and place it in that location of the zone file config and print
it to STD OUT on the terminal. Perhaps later another version of this i will
include writing directly to a file but for now i want to ensure the
inserting of string into specific position works and just print it out.

So im thinking of something along the lines of

User_input_Zone = str(input("Enter the domain you wish to block: ")

Def zone_blacklist
New_zone = "zone "{}" {, user_input_zone
6  type master;
7  //type hint;
8  file "/etc/bind/db.root.honeypot";
9 };"
Print(new_zone)

What i was thinking of doing is having a place holder of {} in place of
where i want the domain to be inserted into but i dont know how to
structure the syntax of a zone file in a function within

I apologise if its basic, i don't necessarily want the answer just given to
me but more of a clue of how someone else may structure it and i will
research it myself. Just want some guidance.

Thanks,
Nathan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] creating a cluster

2017-05-03 Thread Alan Gauld via Tutor
On 03/05/17 06:22, shubham goyal wrote:
> Hello tutors,
> 
> here i am trying to create a cluster on cloud using json data but its
> giving some error

>   File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
> raise URLError(err)
> urllib2.URLError: 

It says connection refused.
Do you have access to the URL? Can you connect using telnet/ssh
or a browser? This looks like a networking issue rather than
a Python one.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi all: How do I save a file in a designated folder?

2017-05-03 Thread Alan Gauld via Tutor
On 03/05/17 00:28, Cameron Simpson wrote:

>> And so forth? I assume you mean
>>
>> MMDD.png format?
>>
>> You should read about the strftime function in the time
> 
> Further to this, I would also advocate that you consider writing the 
> timestamp 
> from largest unit to smallest unit, like an ISO8601 timestamp, which 
> typically 
> looks like:
> 
>   MMDD.png

Yes, I absolutely second this advice and should have included it.
ISO dates make programming life so much easier!


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] creating a cluster

2017-05-03 Thread shubham goyal
Hello tutors,

here i am trying to create a cluster on cloud using json data but its
giving some error
can you walk me through it. thankyou


import urllib2
import requests
data="""{
  "spark_version": null,
  "presto_version": null,
  "label": [
"test"
  ],
  "disallow_cluster_termination": false,
  "force_tunnel": false,
  "enable_ganglia_monitoring": false,
  "node_bootstrap_file": null,
  "tunnel_server_ip": null,
  "ec2_settings": {
"aws_preferred_availability_zone": "Any",
"aws_region": "us-east-1",
"compute_validated": false,
"vpc_id": null,
"subnet_id": null,
"bastion_node_public_dns": null,
"master_elastic_ip": null,
"instance_tenancy": null,
"compute_secret_key": "",
"compute_access_key": "AKIAITFMYWE7A3BG5BDQ",
"use_account_compute_creds": true
  },
  "hadoop_settings": {
"use_hbase": null,
"use_spark": false,
"custom_config": null,
"use_hadoop2": false,
"use_qubole_placement_policy": true,
"is_ha": null,
"enable_rubix": false,
"fairscheduler_settings": {
  "default_pool": null
}
  },
  "node_configuration": {
"master_instance_type": "m1.large",
"slave_instance_type": "m1.xlarge",
"initial_nodes": 1,
"max_nodes": 1,
"idle_cluster_timeout": null,
"slave_request_type": "spot",
"fallback_to_ondemand": null,
"spot_instance_settings": {
  "maximum_bid_price_percentage": 100,
  "timeout_for_request": 10,
  "maximum_spot_instance_percentage": 50
},
"cluster_name": "qa_qbol_acc5572_cl67507"
  },
  "security_settings": {
"encrypted_ephemerals": false
  },
  "presto_settings": {
"enable_presto": false,
"custom_config": null
  },
  "spark_settings": {
"custom_config": null
  },
  "errors": [
"invalid_credentials"
  ],
  "datadog_settings": {
"datadog_api_token": null,
"datadog_app_token": null
  },
  "spark_s3_package_name": null,
  "zeppelin_s3_package_name": null,
  "engine_config": {},
  "zeppelin_interpreter_mode": null
}"""
u='auth_token'
p='d3d62d04584d49bbaf2a2430a453f3314fb03b38adcd4e58ac671f40638ac6cd'
url='https://qa.qubole.net/api/v1.3/clusters'
print url
headers={'u':'p'}
req = requests.post(url, data,headers)
#base64string = base64.encodestring('%s:%s' % (u, p)).replace('\n', '')
#req.add_header("Authorization", "Basic %s" %base64string)
for line in req:
print line
#f.close()











Output:


https://qubole.net/api/v1.3/clusters
Traceback (most recent call last):
  File "createCluster.py", line 83, in 
f = urllib2.urlopen(req)
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 431, in open
response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 449, in _open
'_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1240, in https_open
context=self._context)
  File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
raise URLError(err)
urllib2.URLError: 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor