Re: [Tutor] Code critique
Bo Morris wrote: > Thank you all for the helpful criticism. I wish I was able to catch on to > what you are suggesting more quickly. > > Based on your recommendations, I have come up with the following so far, > however I just dont see it as easily as I did while using the if/elif > statements. > > This is what I have so far. I can not figure out how to iterate through > the dictionary inserting each value where "png_file" should be and execute > the code for each ip address in the list. I was able to do it using the > if/elif statements, but I am afraid I am lost trying to do it another way. > > ipList = ['ip-1', 'ip-2', 'ip-3', 'ip-4', 'ip-5', 'ip-6', 'ip-7', 'ip-8', > 'ip-9'] > > host = {'3102EHD-01108':'3102EHD-01108.png', > '3102EHD-01109':'3102DHD-01109.png', > '3102EHD-MUTV-1082':'3102EHD-01082.png', > '3102DHD-01033':'3102DHD-MUTV-1033.png', > 'Encoder':'3102EHD-01302.png', > '3102DHD-01149':'3102DHD-01149.png', > '3102EHD-01125':'3102EHD-01125.png', > '3102DHD-01144':'3102DHD-01144.png', > '3102EHD-01105':'3102EHD-01105.png'} > > # iterate through the dictionary inserting the png file > def get_png_file(?): > process = get_png_file.get(hostname, png_file) > s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh') > sftp = s.open_sftp() > sftp.get('/Downloads/Hourly/'png_file,'/Downloads/Hourly/'png_file) > sftp.close() > print 'file recieved' > > user = 'user' > passwd = 'password' > s = paramiko.SSHClient() > s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) > > # iterate through the list and do the below for each > for ip in ipList: > s.connect(ip,22,user,passwd,timeout=4) > since I have all the hostnames in the dic, and I am removing > the if/elif statments, do I need the below command 'echo $HOSTNAME'? > stdin, stdout, stderr = s.exec_command('echo $HOSTNAME') > out = stdout.read() > get_png_file(?) > As hinted in my previous post I don't think you need a dict here. Once you have the hostname you can build the filename from it with hostname + ".png". Here's what I had in mind, unfortunately totally untested: #!/usr/bin/python import os import paramiko ip_list = [ 'ip-1', 'ip-2', 'ip-3', 'ip-4', 'ip-5', 'ip-6', 'ip-7', 'ip-8', 'ip-9'] def connect(ip): user = 'user' passwd = 'password' s = paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(ip, 22, user, passwd, timeout=4) stdin, stdout, stderr = s.exec_command('echo $HOSTNAME') hostname = stdout.read().strip() filename = hostname + ".png" png_source = os.path.join("/Downloads/Hourly", filename) png_dest = os.path.join("/Downloads/Hourly", hostname, filename) s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh') sftp = s.open_sftp() sftp.get(png_dest, png_source) sftp.close() print "file", png_dest, "received" for ip in ip_list: connect(ip) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is there a convenient table of Python 3.4 exceptions?
On 25/10/14 03:14, boB Stepp wrote: In the programs I have been dabbling in at work, I am often "surprised" by the situations my users stumble into that I did not have sufficient imagination to consider up front. That's always a problem. And just when you think you've seen everything the users will astound you by thinking up another way to screw your code. That's one reason for having specialist system test teams completely divorced from the developers. Developers know how its supposed to work so its very difficult for a developer to break their own code. System testers exist to break code, they will not be happy until they have found a way to make it crash or freeze. That's a good thing. As a user of software I have often wished that the error messages generated were understandable and help me to avoid the condition which triggered that message in the future. Some people like to raise Python error messages to user level but that can be dangerous. I've had users turn off their PC and call the help desk and wait several hours for help because of a fairly innocuous exception message that spooked them. That cost the business a lot of down time. So translating any uncaught errors into logged messages and displaying something more human friendly on the screen is a good thing IMHO. But that doesn't mean you need to predict every possible individual error or handle it, that's just not possible, especially in a dynamic language like Python. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] solution for for loop?
description_string=code_string='' description = code = 'a' for (description_position, code_position) in (description, code): print(description_position,code_position) I have tried variations on this for statement, and it doesn't work:<))) Both description and code have the same size array. I was hoping that some derivative of this for would bring in a new description_position value, and code_position value. Amongst various trials I have tried dp in d && cp in c; dp, cp in d,c. etc. This is the error report: Traceback (most recent call last): File "C:/Users/Dad/python/stock tracker/raw yahoo scraper codes.py", line 80, in for (description_position, code_position) in (description, code): ValueError: too many values to unpack (expected 2) Is there something like what I want? Thanks, CLayton ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] solution for for loop?
Clayton Kirkwood wrote: > description_string=code_string='' > > description = code = 'a' > > for (description_position, code_position) in (description, code): > > print(description_position,code_position) > I have tried variations on this for statement, and it doesn't work:<))) > Both description and code have the same size array. I was hoping that some > derivative of this for would bring in a new description_position value, > and code_position value. You want zip(): >>> colors = [ "red", "green", "yellow"] >>> fruit_list = ["cherry", "apple", "banana"] >>> for color, fruit in zip(colors, fruit_list): ... print(color, fruit) ... red cherry green apple yellow banana ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] solution for for loop?
On 25/10/14 04:17, Clayton Kirkwood wrote: description_string=code_string='' description = code = ‘a’ for (description_position, code_position) in (description, code): print(description_position,code_position) I have tried variations on this for statement, and it doesn’t work:<))) No surprise there, it doesn't make sense. Both description and code have the same size array. There are no arrays in sight. Both names refer to the same object: the letter 'a'. You then put the names in a tuple which is what I assume you mean? . But tuples are very different to arrays. > I was hoping that some derivative of this for would bring in a new description_position value, and code_position value. From where? You set them up to point to a fixed string. Where do you suppose Python would find any other values? Amongst various trials I have tried dp in d && cp in c; dp, cp in d,c. etc. This is the error report: Traceback (most recent call last): File "C:/Users/Dad/python/stock tracker/raw yahoo scraper codes.py", line 80, in for (description_position, code_position) in (description, code): ValueError: too many values to unpack (expected 2) Read the error description then look at your for line closely. for (description_position, code_position) in (description, code): That reads: for aTupleOfNames in aTupleOfValues: It's trying to iterate over aTupleofValues and then unpack the first value into the the two names. You need a collection as the for loop target to give the for something to iterate over. It only needs to contain your tuple (a single element collection) but it needs to have something to iterate over. An example might help: Using shorter names and literals for brevity: This is your code: >>> for x,y in (1,2): print('ok') which gives an error because it picks out the first element of the (1,2) tuple which is 1, and tries to unpack that into x,y - which it can't. That's what is happening in your case too. Now look at: >>> for x,y in [(1,2)]: print('ok') This time the loop picks out the (1,2) tuple as the first (and only) element of the target list and unpacks to x and y. So we get ok printed in this case. Is there something like what I want? I don't know because I don't really know what you were trying to do. Your code as it stands could just have been written as description_position, code_position = description, code But I don't think that really is what you were tying to do. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] Help with running an API
Hi Folks - new to python and trying to run an API. Running version 2.7.3. on Windows 7 machine. Here is the scenario for the given API (FRED API in this case): easy_install Fred from C:\ - this installs to C:\site packages then I fire up the python shell and run file created for fred api: from fredapi import Fred fred = Fred(api_key='my api key') data = fred.get_series('SP500') keep getting below error message: ImportError: cannot import name Fred thanks for any suggestions! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with running an API
On 25/10/14 17:08, Mark Meanwell wrote: Hi Folks - new to python and trying to run an API. Running version 2.7.3. on Windows 7 machine. Here is the scenario for the given API (FRED API in this case): easy_install Fred from C:\ - this installs to C:\site packages then I fire up the python shell and run file created for fred api: I'm not sure what you mean by this bit? Do you mean you run the file using python somefile.py Or do you start the interpreter to get the >>> prompt and then somehow execute the file? (If so how do you run it?) Or are you using some kind of IDE such as IDLE? In which case how do you run the file? from fredapi import Fred fred = Fred(api_key='my api key') data = fred.get_series('SP500') keep getting below error message: ImportError: cannot import name Fred Taking it back to basics start the Python interpreter and get a >>> prompt. Then type >>> import fredapi Does that work without errors? Also check that your Python version is compatible with your module version. Third party APIs are often version specific. Finally, since Fred is not part of the standard library, you might be better asking on a Fred specific forum, if one exists. This list is mainly for the language and standard libraries. Anything beyond that will have less chance of a good answer. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] Help with running an API
On Sat, Oct 25, 2014 at 12:08 PM, Mark Meanwell wrote: > Hi Folks - new to python and trying to run an API. Running version 2.7.3. on > Windows 7 machine. > > Here is the scenario for the given API (FRED API in this case): > > easy_install Fred from C:\ - this installs to C:\site packages > > then I fire up the python shell and run file created for fred api: > > from fredapi import Fred > fred = Fred(api_key='my api key') > data = fred.get_series('SP500') > > keep getting below error message: > ImportError: cannot import name Fred > > thanks for any suggestions! I'm not familiar with FRED, but a quick look on stackoverflow used the lowercase name to import > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Code critique
Thank you Peter for your example. I have the code working now and will post soon for eveyones benefit. Thank you all who took the time to help. Bo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] solution for for loop?
Ding, ding, ding. Winner, winner, winner. Peter wins the prize, suggesting the use of zip(). This allowed me to pull two different strings from two different lists at one time in a for loop. for description_position, code_position in zip(description, code): Yes!! Thank you Peter Clayton !-Original Message- !From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On !Behalf Of Peter Otten !Sent: Saturday, October 25, 2014 4:41 AM !To: tutor@python.org !Subject: Re: [Tutor] solution for for loop? ! !Clayton Kirkwood wrote: ! !> description_string=code_string='' !> !> description = code = 'a' !> !> for (description_position, code_position) in (description, code): !> !> print(description_position,code_position) ! !> I have tried variations on this for statement, and it doesn't !> work:<))) Both description and code have the same size array. I was !> hoping that some derivative of this for would bring in a new !> description_position value, and code_position value. ! !You want zip(): ! !>>> colors = [ "red", "green", "yellow"] fruit_list = ["cherry", !>>> "apple", "banana"] for color, fruit in zip(colors, fruit_list): !... print(color, fruit) !... !red cherry !green apple !yellow banana ! ! !___ !Tutor maillist - Tutor@python.org !To unsubscribe or change subscription options: !https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor