Re: [Tutor] Need to be able to accept Page Down or CTRL-E
-Original Message- From: Tutor [mailto:tutor-bounces+d.wilder=f5@python.org] On Behalf Of Dave Angel Sent: Friday, February 01, 2013 7:38 AM To: tutor@python.org Subject: Re: [Tutor] Need to be able to accept Page Down or CTRL-E On 02/01/2013 03:34 AM, Alan Gauld wrote: >> >> >> >> OTOH if you mean you want to send the page up/down/arrows as the >> "something" then its a different game and you can do it. In that case >> you need todo what you did with ESC. The character set your external >> app uses will determine the codes you send. Assuming its the same set >> as your local setup you can use the following program(untested!) to >> read the codes from your keyboard (assuming you are on Windows): >> >> import msvcrt >> >> print "Hit space to end..." >> print >> >> while True: >> ky = msvcrt.getch() > The OP is running Python 2.73 on Linux. Okay, thanks Alan and Dave. I will look into this a little more and if need be, email the list again but taking Dave's advice and start over w/ a clearer problem statement. Dave -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need to be able to accept Page Down or CTRL-E
>> On 01/31/2013 09:43 PM, Dave Wilder wrote: >> Hello, >> >> In a script I am writing, I would like to be able to accept the PAGE DOWN >> key as an input as well as the arrow keys. >> >> Is that possible w/ Python (I am using python 2.7.3 and Linux OS)? If so, >> could someone point me to documentation on how to do that? >> >> I have done things like this before in python. For example, I used the >> following to send the character. >> send (chr(27)) >> >> However, I have not been able to find the ascii code equivalent to use for >> PAGE DOWN and the up/down arrow keys. >> >> I'm not even sure this is even a python question. >> > So how are you taking this input in? If you're writing a terminal > application, using raw_input(), it would be very difficult, as you'd probably > have to modify the readline function. > Perhaps you're using some GUI. Please tell us which one. >- DaveA I am using a terminal application and an application called pexpect (instead of raw_input) where I send something to the console and then look for a result, such as or UP/DOWN arrows. I'm not sure if pexpect is standard Python application. I thought I recalled someone on this list saying it wasn't. Dave ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Need to be able to accept Page Down or CTRL-E
Hello, In a script I am writing, I would like to be able to accept the PAGE DOWN key as an input as well as the arrow keys. Is that possible w/ Python (I am using python 2.7.3 and Linux OS)? If so, could someone point me to documentation on how to do that? I have done things like this before in python. For example, I used the following to send the character. send (chr(27)) However, I have not been able to find the ascii code equivalent to use for PAGE DOWN and the up/down arrow keys. I'm not even sure this is even a python question. Thanks, Dave ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question on re.findall usage
On 28 January 2013 2:44, : Oscar Benjamin [mailto:oscar.j.benja...@gmail.com wrote: Please post in plain text (not html) as otherwise the code gets screwed up. ... Some people like to use regexes for everything. I prefer to try string methods first as I find them easier to understand. Here's my attempt: >>> junk_list = 'tmsh list net interface 1.3 media-ca \rpabilities\r\nnet >>> interface 1.3 {\r\nmedia-capabilities {\r\nnone\r\n >>> auto\r\n 4SR4-FD\r\n 10T-HD\r\n100TX-FD\r\n >>> 100TX-HD\r\n1000T-FD\r\n4LR4-FD\r\n 1000T-HD\r\n >>> }\r\n}\r\n' >>> junk_list = [s.strip() for s in junk_list.splitlines()] junk_list = >>> [s for s in junk_list if s == 'auto' or s[:2] in ('10', '40')] >>> junk_list ['auto', '4SR4-FD', '10T-HD', '100TX-FD', '100TX-HD', '1000T-FD', '4LR4-FD', '1000T-HD'] Does that do what you want? Oscar * Got it Oscar. Thank you for your respectful corrections and your solution. I used "Rich Text" which is what I thought was recommended by the list gurus at one point. Plain Text it is then. Your response definitely does the trick and I can use that as a base for the future. As per Joel's comment that it is a variation of questions I asked in the past, right you are. I had to put this away for a while and am picking it up again now. I will get string manipulation / RegEx educated. Thank You, Dave ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Question on re.findall usage
Hello, I am trying using re.findall to parse the string below and then create a list from the results. junk_list = 'tmsh list net interface 1.3 media-ca \rpabilities\r\nnet interface 1.3 {\r\nmedia-capabilities {\r\nnone\r\nauto\r\n 4SR4-FD\r\n 10T-HD\r\n100TX-FD\r\n100TX-HD\r\n 1000T-FD\r\n4LR4-FD\r\n 1000T-HD\r\n}\r\n}\r\n' What I am doing now is obviously quite ugly, but I have not yet able to manipulate it to work how I want but in a much more efficient and modular way. I did some research on re.findall but am still confused as to how to do character repetition searches, which I guess is what I need to do here. >> junk_list = >> re.findall(r'(auto|[1|4]0+[A-Z]-[HF]D|[1|4]0+[A-Z][A-Z]-[HF]D|[1|4]0+[A-Z][A-Z][0-9])', >> junk_list) >> junk_list ['auto', '4SR4', '10T-HD', '100TX-FD', '100TX-HD', '4LR4', '1000T-FD', '1000T-HD'] >>> Basically, all I need to search on is: - auto - anything that starts w/ '1' or '4' and then any number of subsequent zeroes e.g. 10T-HD, 4LR4-FD, 100TX-FD My environment: [root@f5ite ~/tests]$ uname -a Linux VM-QA-ITE-03 2.6.32-220.17.1.el6.i686 #1 SMP Tue May 15 22:09:39 BST 2012 i686 i686 i386 GNU/Linux [root@f5ite ~/tests]$ [root@f5ite ~/tests]$ /usr/bin/python Python 2.7 (r27:82500, Jul 6 2010, 02:54:50) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Type "help", "copyright", "credits" or "license" for more information. Any ideas? Thanks, Dave ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] manipulating a string in python
Hello, I believe there is a simple answer to this, but I am confused on what direction I should go that would perform what I wish to do most efficiently. ** My Environment *** [root@f5ite ~/tests]$ python Python 2.7 (r27:82500, Jul 6 2010, 02:54:50) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Type "help", "copyright", "credits" or "license" for more information. [root@f5ite ~/tests]$ uname -a Linux VM-QA-ITE-03 2.6.32-220.17.1.el6.i686 #1 SMP Tue May 15 22:09:39 BST 2012 i686 i686 i386 GNU/Linux [root@f5ite ~/tests]$ more /etc/redhat-release CentOS release 5.8 (Final) [root@f5ite ~/tests]$ I need to strip down a string to make a list of "auto" and anything starting w/ "10" and nothing else. Below is the string I am currently working with. >> results 'tmsh list net interface 1.1 media-capa \rbilities\nnet interface 1.1 {\n media-capabilities {\nnone\nauto\n10T-FD\n 10T-HD\n100TX-FD\n100TX-HD\n1000T-FD\n 1000T-HD\n}\n}\n' >> I want to chop it up, so I have a list of all media speeds (auto or "10X" that I can then loop through, so would like to end up with something like below: >> results 'none', 'auto', '10T-FD'...'1000T-HD' I could do this using a series of "split" commands. However, is there a better way? For example, can I just eliminate "media-capabilities", "{" and "}" right off the bat and then do a split on "\n"? Should I use "re" matching for this? I could also just a single split('\n') and then inside the loop ignore everything that is not "auto" or starts with "10", but that seems inefficient. Any advice on the best way to do this? Thanks, Dave ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Obtaining result from a sendline
Sorry for the poor info on my part Steven et al. I did not do my homework. I thought that sshclient was an imported module, however it is pxssh that I am importing as sshClient. import pxssh as sshClient There is no particular reason I need to debug this using the debugger to troubleshoot this. I was just using it to step through the program at the time. Thank you eryksun for the help…especially the quick response and great explanation. Both of your suggestions will work for me, particularly the 2nd one. I am not actually using “str” for the reasons you described. I just renamed it when I emailed my question, which was probably not a good idea. Thanks, Dave -Original Message- From: eryksun [mailto:eryk...@gmail.com] Sent: Wednesday, October 31, 2012 1:44 AM To: Dave Wilder Cc: tutor@python.org Subject: Re: [Tutor] Obtaining result from a sendline On Tue, Oct 30, 2012 at 9:47 PM, Dave Wilder mailto:d.wil...@f5.com>> wrote: > > However, all I get back is a numerical value. I am looking to get the > actual output from the “ls /var/tmp” That would be the number of bytes sent. > ssh = sshClient.pxssh() > ssh.login(server=ip_addr, username=user, password=pswd) str = > ssh.sendline('ls /var/tmp') It's not a good idea to shadow the name of the built-in type str. The pxssh object should have the methods read, read_nonblocking, readline, and readlines. But you probably want the output from between prompts. The prompt() method calls expect() to match the custom prompt, and returns False if there's a timeout (the default is 20 seconds). This sets the instance attribute "before" with the text that came before the match: ssh.sendline('ls /var/tmp') if ssh.prompt(): output = ssh.before Alternatively, if you've setup a public key on the host via ssh-keygen, you could use subprocess.check_output() instead: import subprocess cmd = ['ssh', '-l', login, hostname, 'ls /var/tmp'] output = subprocess.check_output(cmd) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Obtaining result from a sendline
Okay, file this under RTFM, but even after research, I cannot figure out the answer to this simple question. When I do an ssh in python (version 2.7.3) to a device, I am trying to capture the output of the command. However, all I get back is a numerical value. I am looking to get the actual output from the "ls /var/tmp" command below. This is probably readily available info, but I am just not searching on the python sites correctly. e.g. (Pdb) ssh = sshClient.pxssh() (Pdb) ssh.login(server=ip_addr, username=user, password=pswd) True (Pdb) str = ssh.sendline('ls /var/tmp') (Pdb) str 12 (Pdb) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Filename match on file extensions
Awesome! Thank you for the great info gentlemen. It should be an easy fix from here. -Original Message- From: Tutor [mailto:tutor-bounces+d.wilder=f5@python.org] On Behalf Of Steven D'Aprano Sent: Wednesday, October 03, 2012 1:49 AM To: tutor@python.org Subject: Re: [Tutor] Filename match on file extensions On Wed, Oct 03, 2012 at 03:14:16AM +0000, Dave Wilder wrote: > > Hello, > > Below is a snippet of a search I want to do for any file that contains the > string "quarantine" in the filename. > It works for picking up any file name containing "quarantine" except when > "quarantine" is used as an extension. Incorrect. It only picks up filenames that *begin* with "quarantine". The critical code is: i.lower().find("quarantine".lower()) == 0 which matches: quarantineblahblahblah.txt but not blahblahquarantineblah.txt The correct test for matching anywhere in the string is: "quarantine" in i.lower() By the way, it is misleading to call your loop variable "i". By convention, i is used for integer loop variables. Since your loop variable in this case is a file name, I would name it "filename": for filename in files: if "quarantine" in filename.lower(): ... Also, you might consider using the "glob" module, which is good for finding filenames matching so-called globs: "quarantine*.txt" matches file names starting with quarantine, ending with .txt, and anything at all in between. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Filename match on file extensions
>> Hello, >> Below is a snippet of a search I want to do for any file that contains the >> string "quarantine" in the filename. >> It works for picking up any file name containing "quarantine" except when >> "quarantine" is used as an extension. >> For example, the search would find quarantine.txt but fails to find >> txt.quarantine. I have done some investigating, but have >> Not been able to determine how to make it include extensions in the search. >> I believe I should be using something like "os.path.splitext". Does that >> make sense? Thanks. >> target = '/home' >> matches = 0 >> for (currdir,subdirs,files) in os.walk(target): >> for i in files: >> if i.lower().find("quarantine".lower()) == 0: ## Case-insensitive search >> for string "quarantine" in filename >> matches += 1 # increment number of matches >>print "Quarantine file",i,"found in directory",currdir >>if matches > 0: >>result = tc.FAIL >>else: >>result = 'FAIL' > Would this be a time when regex is necessary? Maybe: > \b[^.]*quarantine[^.]*\.[a-zA-Z]*\b I had originally used regular expression, but thought there might be a simpler solution w/o the need for regular expressions. If that is what is needed, then so be it though. Thank you for your quick response. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Filename match on file extensions
Hello, Below is a snippet of a search I want to do for any file that contains the string "quarantine" in the filename. It works for picking up any file name containing "quarantine" except when "quarantine" is used as an extension. For example, the search would find quarantine.txt but fails to find txt.quarantine. I have done some investigating, but have Not been able to determine how to make it include extensions in the search. I believe I should be using something like "os.path.splitext". Does that make sense? Thanks. target = '/home' matches = 0 for (currdir,subdirs,files) in os.walk(target): for i in files: if i.lower().find("quarantine".lower()) == 0: ## Case-insensitive search for string "quarantine" in filename matches += 1 # increment number of matches print "Quarantine file",i,"found in directory",currdir if matches > 0: result = tc.FAIL else: result = 'FAIL' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and boot sequences
The switch is in a state where user process can be run once the boot sequence has been halted (the aforementioned <9>). However the time between when the reboot is kicked off and when the asterisks appear (and the boot must be stopped), there is no shell Environment. I may be trying to hammer a nail in with a wrench. So I'm going to do a little more digging into the switch code and get to a point where a shell script can be run as Alan suggested. Then I'll be back. Thanks all for the replies. Dave -Original Message- From: tutor-bounces+d.wilder=f5@python.org [mailto:tutor-bounces+d.wilder=f5@python.org] On Behalf Of Alan Gauld Sent: Wednesday, July 04, 2012 1:07 PM To: tutor@python.org Subject: Re: [Tutor] Python and boot sequences On 28/06/12 13:54, Dave Wilder wrote: > Can a Python script be written that has the ability to stop a Linux > device in the middle of a boot when a certain sequence occurs and then > perform an action? It depends... > For example, when I boot my switch (w/ Linux OS 2.7.3), I want to stop > the boot when I see a series of asterisks. When I see this, I need to > hit the <9> Could you do it with a bash script? If so you could do it with Python. But I've no idea whether your switch will be in a state to run user processes at that point in its boot sequence. If you don't have a shell environment its going to be a whole lot harder. > doing a PXE boot, ??? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python and boot sequences
Hello, This is a general Python question, so I don't require specific answers on how to do it, but... Can a Python script be written that has the ability to stop a Linux device in the middle of a boot when a certain sequence occurs and then perform an action? For example, when I boot my switch (w/ Linux OS 2.7.3), I want to stop the boot when I see a series of asterisks. When I see this, I need to hit the <9> sequence. Then, I need to select a menu option (e.g. 1 to run a test or Q to quit and continue w/ the boot). Another example would be when doing a PXE boot, selecting the image to load when prompted during the reboot. I don't know enough about Python yet to know if that is something that Python can be used for or I am barking up the wrong tree. Thank You, Dave ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor