Re: [Tutor] Writing a csv from a dictionary
On Tue, Jun 23, 2009 at 1:08 AM, Mark Tolonen wrote: > import csv > > dyc = { > 'a50' : ['textfield', 50, 40], > 'k77' : ['othertext', 60, 10] > } > > myfile = open('csv-test.csv', 'w') > mywriter = csv.writer(myfile, dialect='excel') > > for k,[a,b,c] in dyc.items(): > mywriter.writerow([k,a,b,c]) I think I would write this as for k, v in dyc.items(): # or iteritems() mywriter.writerow([k] + v) That way it doesn't depend on the actual size of the value list. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] filtering NaN values
On Tue, Jun 23, 2009 at 1:53 AM, Alan Gauld wrote: > Interesting! How is a NaN stored in Python? > ie. How do you get to the point of having one in the first place? Google 'python nan' for lots of interesting discussion... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing a csv from a dictionary
"Kent Johnson" wrote in message news:1c2a2c590906230415q351c7c74kebc591907ce0e...@mail.gmail.com... On Tue, Jun 23, 2009 at 1:08 AM, Mark Tolonen wrote: import csv dyc = { 'a50' : ['textfield', 50, 40], 'k77' : ['othertext', 60, 10] } myfile = open('csv-test.csv', 'w') mywriter = csv.writer(myfile, dialect='excel') for k,[a,b,c] in dyc.items(): mywriter.writerow([k,a,b,c]) I think I would write this as for k, v in dyc.items(): # or iteritems() mywriter.writerow([k] + v) That way it doesn't depend on the actual size of the value list. I knew there was a more straightforward way, it just wouldn't come to me at 1am. ;^) -Mark ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing a csv from a dictionary
Mark Tolonen wrote: > It's a good idea to cut-and-paste actual code and actual output. Your > above code doesn't work. I'd just like to clarify, in case someone misunderstood, it isn't a really good idea to simply cut-and-paste actual code and actual output for 2 reasons: 1) cut-and-paste means you lost your own copy of the code :) 2) you should simplify your own code before copy-and-paste to a mailing list. The simplified code should: 1) run when copy-and-pasted to the interpreter[1], 2) still generates the error/unexpected result[2], 3) demonstrate the intent of the code. In many cases, trying to simplify the code would reveal the error to yourself, and in other cases it makes it easier for others to spot the errors. [1] except when the question is why a code works in an interpreter but not the other or vice versa; or when the code doesn't even run at all [2] it is easier to debug a wrong code than debug a correct code ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] extracting lines in large file
Bryan Fodness wrote: > tried both again, they both return the same 9 lines, when i expect > 492. it dies on a blank line, but the if i_line takes care of the > previous ones. > Can you give a sample input that should, but not passed by the code? Unrelated Tips: You can rely on python's short-circuiting logical operator and write the `if i_line` like this: if i_line and i_line[0] == "intrinsic": ... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Trouble with passing commands / variables to os.system()
Hi, I'm very very green when it comes to python. I know bash better than python, so I figured a good way to learn things was covert my bash stuff to python. So here goes... Here's a quick example of the code I have that is broken. import os username = 'charlie' private_key = '/path/to/key' ssh = '/usr/bin/ssh' command = 'hostname && df -h && exit' servers = ['172.16.1.1', '172.16.12.2', '172.16.1.3'] for host in servers: print(os.system(ssh -l username -i private_key host command)) What I'm trying to do is is, is use ssh with a private key. That way I can quickly run some remote commands on a few hundred servers in a quick way to do stuff (disk usage, top, etc). When I run this, I get errors like this for every host in my list. Warning: Identity file private_key not accessible: No such file or directory. ssh: Could not resolve hostname i: nodename nor servname provided, or not known 65280 My first thoughts are, it's not passing my variables to the function the way I'd expect. So my questions are... 1.) Is it nessacary to put my IP's in quotes? 2.) When I call a variable in a function (like os.system() or print()) I don't use $'s right? 3.) Am I putting variables in my functions properly? Can I put variables like this in there? Thanks for any help. Charlie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with passing commands / variables to os.system()
os.system is not the best way to handle this you may want to look into the subprocess module however: import os username = 'charlie' private_key = '/path/to/key' ssh = '/usr/bin/ssh' command = 'hostname && df -h && exit' servers = ['172.16.1.1', '172.16.12.2', '172.16.1.3'] for host in servers: os.system("ssh %...@%s -i %s %s" %(username, host, private_key, command) On Tue, Jun 23, 2009 at 2:01 PM, Charlie Reddington < charlie.redding...@gmail.com> wrote: > Hi, > > I'm very very green when it comes to python. I know bash better than > python, so I figured a good way to learn things was covert my bash stuff to > python. So here goes... > > Here's a quick example of the code I have that is broken. > > import os > > username = 'charlie' > private_key = '/path/to/key' > ssh = '/usr/bin/ssh' > command = 'hostname && df -h && exit' > > servers = ['172.16.1.1', '172.16.12.2', '172.16.1.3'] > > for host in servers: >print(os.system(ssh -l username -i private_key host command)) > > What I'm trying to do is is, is use ssh with a private key. That way I can > quickly run some remote commands on a few hundred servers in a quick way to > do stuff (disk usage, top, etc). > > When I run this, I get errors like this for every host in my list. > > Warning: Identity file private_key not accessible: No such file or > directory. > ssh: Could not resolve hostname i: nodename nor servname provided, or not > known > 65280 > > My first thoughts are, it's not passing my variables to the function the > way I'd expect. > > So my questions are... > > 1.) Is it nessacary to put my IP's in quotes? > 2.) When I call a variable in a function (like os.system() or print()) I > don't use $'s right? > 3.) Am I putting variables in my functions properly? Can I put variables > like this in there? > > Thanks for any help. > > Charlie > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with passing commands / variables to os.system()
Thanks, Your code works as expected! Can you tell me what your code is doing different than mine? Charlie On Jun 23, 2009, at 3:06 PM, vince spicer wrote: os.system is not the best way to handle this you may want to look into the subprocess module however: import os username = 'charlie' private_key = '/path/to/key' ssh = '/usr/bin/ssh' command = 'hostname && df -h && exit' servers = ['172.16.1.1', '172.16.12.2', '172.16.1.3'] for host in servers: os.system("ssh %...@%s -i %s %s" %(username, host, private_key, command) On Tue, Jun 23, 2009 at 2:01 PM, Charlie Reddington > wrote: Hi, I'm very very green when it comes to python. I know bash better than python, so I figured a good way to learn things was covert my bash stuff to python. So here goes... Here's a quick example of the code I have that is broken. import os username = 'charlie' private_key = '/path/to/key' ssh = '/usr/bin/ssh' command = 'hostname && df -h && exit' servers = ['172.16.1.1', '172.16.12.2', '172.16.1.3'] for host in servers: print(os.system(ssh -l username -i private_key host command)) What I'm trying to do is is, is use ssh with a private key. That way I can quickly run some remote commands on a few hundred servers in a quick way to do stuff (disk usage, top, etc). When I run this, I get errors like this for every host in my list. Warning: Identity file private_key not accessible: No such file or directory. ssh: Could not resolve hostname i: nodename nor servname provided, or not known 65280 My first thoughts are, it's not passing my variables to the function the way I'd expect. So my questions are... 1.) Is it nessacary to put my IP's in quotes? 2.) When I call a variable in a function (like os.system() or print()) I don't use $'s right? 3.) Am I putting variables in my functions properly? Can I put variables like this in there? Thanks for any help. Charlie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with passing commands / variables to os.system()
"Charlie Reddington" wrote Your code works as expected! Can you tell me what your code is doing different than mine? os.system needs the command to be a string so you have to build up the string by passing in your variables using the string format operator(%) or building it bit by bit outside the call to system. Beware that the return value from system is just the exit code which is not very useful, hencehthe recommendation to oook at subprocess... for host in servers: os.system("ssh %...@%s -i %s %s" %(username, host, private_key, command) Also while this might seem a good approach to start you will likely find that most of the things you are doing via sysyem will be possible directly from Python which will be more efficient in resources. Take a look at the Using the OS topic in my tutorioal for just a few examples of the kinds of things you can do directly - as well as how to use subprocess. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with passing commands / variables to os.system()
On Jun 23, 2009, at 4:50 PM, Alan Gauld wrote: "Charlie Reddington" wrote Your code works as expected! Can you tell me what your code is doing different than mine? os.system needs the command to be a string so you have to build up the string by passing in your variables using the string format operator(%) or building it bit by bit outside the call to system. Beware that the return value from system is just the exit code which is not very useful, hencehthe recommendation to oook at subprocess... for host in servers: os.system("ssh %...@%s -i %s %s" %(username, host, private_key, command) Also while this might seem a good approach to start you will likely find that most of the things you are doing via sysyem will be possible directly from Python which will be more efficient in resources. Take a look at the Using the OS topic in my tutorioal for just a few examples of the kinds of things you can do directly - as well as how to use subprocess. HTH, Thanks for all the replies, I'll definitely look into it all. Charlie -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with passing commands / variables to os.system()
Charlie Reddington wrote: Thanks for all the replies, I'll definitely look into it all. Charlie Something else for your consideration; http://commandline.org.uk/python/sftp-python-really-simple-ssh/ -- Powered by Gentoo GNU/Linux http://linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with passing commands / variables to os.system()
David wrote: Charlie Reddington wrote: Thanks for all the replies, I'll definitely look into it all. Charlie Something else for your consideration; http://commandline.org.uk/python/sftp-python-really-simple-ssh/ almost forgot pexpect #!/usr/bin/python import pexpect child = pexpect.spawn ('ssh r...@localhost') child.expect ('Password:') child.sendline ('mypassword') child.expect ('') child.sendline ('ls -a') print child.before child.interact() -- Powered by Gentoo GNU/Linux http://linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor