Re: [Tutor] *nix tail -f multiple log files with Thread
On Sun, Feb 15, 2009, bob gailer wrote: > David wrote: >> Hi everyone, >> >> I copied a program from C to track multiple log files. I would like to >> be able to print a label when a log file is updated. > > Did the C program accomplish that? If so, it might help to see that > code. If not why mention it? > The ``swatch'' program, written in perl, does this by using the gnu-tail program via a pipe using. gtail --follow=name --lines=1 file1 file2 ... Use the source Luke. Bill -- INTERNET: b...@celestial.com Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax:(206) 232-9186 The day-to-day travails of the IBM programmer are so amusing to most of us who are fortunate enough never to have been one -- like watching Charlie Chaplin trying to cook a shoe. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] *nix tail -f multiple log files with Thread
David wrote: bob gailer wrote: Did the C program accomplish that? If so, it might help to see that code. If not why mention it? It is here if you want to check it out; http://dwabbott.com/downloads/logtailer.c.txt Your example output falls short of your stated goal as I understand it! My guess is that you want what you showed: Is this accurate? Yes, thanks for the reply, It was good practice but come to find out that tail will do what I want on its own. The only thing I would like to do is add color to the labels but that is for another list :) I am new to python and never programed before and would like to thank everyone on the list. It has made learning python fun. -david Resent because I forgot to reply all :( -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] *nix tail -f multiple log files with Thread
David wrote: Hi everyone, I copied a program from C to track multiple log files. I would like to be able to print a label when a log file is updated. Did the C program accomplish that? If so, it might help to see that code. If not why mention it? Your example output falls short of your stated goal as I understand it! My guess is that you want what you showed: [snip] 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET /theme/custom_corners/favicon.ico HTTP/1.1" 200 894 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET /lib/speller/spellChecker.js HTTP/1.1" 200 15980 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET / HTTP/1.1" 200 13718 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET /theme/custom_corners/styles.php HTTP/1.1" 200 30709 followed by, for example: [Fri Feb 13 10:08:01 2009] [error] [client 127.0.0.1] File does not exist: /var/www/localhost/htdocs/moodle/favicon.ico When tail discovers more records in the error log. Is this accurate? If so I think solutions might lie in either redirecting tail output to files that would be post-processed later or monitoring the logs using python rather than the tail command. How shall we proceed? Here is the program; #!/usr/bin/python from threading import Thread import subprocess from Queue import Queue num_threads = 3 queue = Queue() logfiles = ["/var/log/messages", "/var/log/apache2/access_log", "/var/log/apache2/error_log"] def logtailer(i, q,): while True: lfile = q.get() sudo = "sudo" tail = "tail" arg = "-f" ret = subprocess.call([sudo, tail, arg, lfile]) q.task_done() for i in range(num_threads): worker = Thread(target=logtailer, args=(i, queue)) worker.setDaemon(True) worker.start() for lfile in logfiles: queue.put(lfile) queue.join() And here is a sample of the output; [Fri Feb 13 08:58:48 2009] [error] [client 127.0.0.1] File does not exist: /var/www/localhost/htdocs/moodle/favicon.ico [Fri Feb 13 08:58:51 2009] [error] [client 127.0.0.1] File does not exist: /var/www/localhost/htdocs/moodle/favicon.ico Feb 13 09:02:26 opteron sudo:david : TTY=pts/4 ; PWD=/home/david ; USER=root ; COMMAND=/bin/tail -f /var/log/apache2/error_log Feb 13 09:02:26 opteron sudo: pam_unix(sudo:session): session opened for user root by david(uid=0) Feb 13 09:02:26 opteron sudo: pam_unix(sudo:session): session closed for user root Feb 13 09:10:01 opteron cron[10633]: (root) CMD (test -x /usr/sbin/run-crons && /usr/sbin/run-crons ) Feb 13 09:18:33 opteron su[10678]: Successful su for root by david Feb 13 09:18:33 opteron su[10678]: + pts/6 david:root Feb 13 09:18:33 opteron su[10678]: pam_unix(su:session): session opened for user root by david(uid=1000) 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET /theme/custom_corners/favicon.ico HTTP/1.1" 200 894 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET /lib/speller/spellChecker.js HTTP/1.1" 200 15980 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET / HTTP/1.1" 200 13718 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET /theme/custom_corners/styles.php HTTP/1.1" 200 30709 I would like to be able to add a label like; [Fri Feb 13 08:58:48 2009] [error] [client 127.0.0.1] File does not exist: /var/www/localhost/htdocs/moodle/favicon.ico [Fri Feb 13 08:58:51 2009] [error] [client 127.0.0.1] File does not exist: /var/www/localhost/htdocs/moodle/favicon.ico Feb 13 09:02:26 opteron sudo:david : TTY=pts/4 ; PWD=/home/david ; USER=root ; COMMAND=/bin/tail -f /var/log/apache2/error_log Feb 13 09:02:26 opteron sudo: pam_unix(sudo:session): session opened for user root by david(uid=0) Feb 13 09:02:26 opteron sudo: pam_unix(sudo:session): session closed for user root Feb 13 09:10:01 opteron cron[10633]: (root) CMD (test -x /usr/sbin/run-crons && /usr/sbin/run-crons ) Feb 13 09:18:33 opteron su[10678]: Successful su for root by david Feb 13 09:18:33 opteron su[10678]: + pts/6 david:root Feb 13 09:18:33 opteron su[10678]: pam_unix(su:session): session opened for user root by david(uid=1000) 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET /theme/custom_corners/favicon.ico HTTP/1.1" 200 894 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET /lib/speller/spellChecker.js HTTP/1.1" 200 15980 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET / HTTP/1.1" 200 13718 127.0.0.1 - - [13/Feb/2009:09:18:47 -0500] "GET /theme/custom_corners/styles.php HTTP/1.1" 200 30709 I can create the label like this; def label() print "<%s\n>" % lfile But I have no idea how to get it to work when a thread is updated. thanks -david -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
Title: Signature.html Thanks. Got it. It finally dawned on me in the midst of responding to Kent. If you ever get a chance to try the Moz experiment above, I'd be interested in your reaction. I see it as my 11:47 post. Alan Gauld wrote: "John Fouhy" wrote for index, item in [9,8,7,6]: print index, item 0 9 1 8 2 7 3 6 You mean: for index, item in enumerate([9,8,7,6]): print index, item Oops, yes, thanks for catching that. It was fairly fundamental to the discussion! Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) S, quiet. I'm thinking about filling this space. Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ConfigParser re-read fails
It looks way too simplified. I have no idea where the problem is. Would you mind showing the script? gist.github.com is good for posting code. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
"John Fouhy" wrote for index, item in [9,8,7,6]: print index, item 0 9 1 8 2 7 3 6 You mean: for index, item in enumerate([9,8,7,6]): print index, item Oops, yes, thanks for catching that. It was fairly fundamental to the discussion! Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
2009/2/16 Alan Gauld : > for index, item in [9,8,7,6]: > print index, item > > > 0 9 > 1 8 > 2 7 > 3 6 You mean: for index, item in enumerate([9,8,7,6]): print index, item :-) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
"Wayne Watson" wrote I'm still looking for an explanation of " for (line_cnt, each_line) in enumerate(input_file)". Why the tuple? Apparently, line_count gets a line number, and each_line gets the string OK, Back up from the specific problem to the more general case. enumerate does not return the line number it returns the current index of the collection: for index, item in [9,8,7,6]: print index, item 0 9 1 8 2 7 3 6 If you are familiar with list comprehensions (or generator expressions) you could do the same yourself with: lst = [9,8,7,6] for i,n in ((i,lst[i]) for i in range(len(lst))): ... print i, n ... 0 9 1 8 2 7 3 6 But enumerate is easier on the eye. And if its the i,n bit that confuses that is just tuple unpacking. We can do this: a,b = (1,2) print a, b 1 2 Now, coming back to your specific case. The index of a file "collection" is the line number (starting at zero of course) and the item is the line of text. HTH, -- Alan G 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] General Feedback, Script Structure
Kent and Alan - thanks! I moved things around a bit and I think it "looks" better: http://python.pastebin.com/m64e4565d On Sun, Feb 15, 2009 at 2:25 PM, Kent Johnson wrote: > Exactly. dict.get() does a key lookup with a default for missing keys, > then the result is used as a string format. See > http://docs.python.org/library/stdtypes.html#dict.get > > BTW I strongly recommend becoming familiar with the Built-in Functions > and Built-in Types sections of the standard library docs: > http://docs.python.org/library/ Great - thanks. That's exactly what I needed! My issue is that I don't use Python enough (or computers, for that matter) to remember everything I've learned previously ... after I saw that, I remember I had read it before ... I just do it for fun, so one of the challenges is finding the right word/answer to what I need to do ... hopefully, if I stick with Python long enough it will become more second nature ... This list is a great resource because I learn a lot from other people's issues ... and is easy to search, too, for my own! Thanks again, everyone. > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
Title: Signature.html I don't think so. Not as a Python concept, but it looks sensible in your example. However, why would enumerate produce a line number? How would one know that it does? Ah, I see. enumerate produces a tuple which has the index and a list. It appears the only place this can be used is in a for? And, of course (devoid of an example): >>> help(enumerate) class enumerate(object) | enumerate(iterable) -> iterator for index, value of iterable | | Return an enumerate object. iterable must be an other object that supports | iteration. The enumerate object yields pairs containing a count (from | zero) and a value yielded by the iterable argument. enumerate is useful | for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ... ... Kent Johnson wrote: On Sun, Feb 15, 2009 at 2:47 PM, Wayne Watson wrote: I'm still looking for an explanation of "for (line_cnt, each_line) in enumerate(input_file)". Why the tuple? Apparently, line_count gets a line number, and each_line gets the string of text. Do you know about sequence unpacking? In an assignment statement, when the right side is a sequence, the left side can be a list of variables of the same length as the sequence. Then each sequence element is assigned to one variable. For example, In [24]: item = (0, 'Spring') In [25]: i, season = item In [26]: i Out[26]: 0 In [28]: season Out[28]: 'Spring' This can be used in a for loop, too, if the items are sequences. That is what is commonly done with enumerate(). Kent -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) S, quiet. I'm thinking about filling this space. Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
On Sun, Feb 15, 2009 at 2:47 PM, Wayne Watson wrote: > I'm still looking for an explanation of "for (line_cnt, each_line) in > enumerate(input_file)". Why the tuple? Apparently, line_count gets a line > number, and each_line gets the string of text. Do you know about sequence unpacking? In an assignment statement, when the right side is a sequence, the left side can be a list of variables of the same length as the sequence. Then each sequence element is assigned to one variable. For example, In [24]: item = (0, 'Spring') In [25]: i, season = item In [26]: i Out[26]: 0 In [28]: season Out[28]: 'Spring' This can be used in a for loop, too, if the items are sequences. That is what is commonly done with enumerate(). Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
On Sun, Feb 15, 2009 at 1:54 PM, Alan Gauld wrote: > Have you tried GoogleChrome yet? It might suit your style better. > It looks at addresses you type and searches both your history and > bookmarks for previous sites visited that might match. Firefox 3 also does that. Very handy! Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
Title: Signature.html I'm still looking for an explanation of "for (line_cnt, each_line) in enumerate(input_file)". Why the tuple? Apparently, line_count gets a line number, and each_line gets the string of text. Chrome. I've heard of it. Does it require an install, or is it one of those odd ball things like arXchiv that I believe opens to an archive of technical papers (entered in Google search field?)? It sounds intriguing. I've never used it, but may soon. If Chrome imports my Moz bmkrs that would be a good sign. Regarding Moz-seamonkey, here's the problem with mgr. Create a folder called MyPython in bmrks. Fire up mgr, and enter python in the search. That should display every bmrk with python in it. Highlight them. From Edit, note the Move bookmark entry. Don't use it now. If you try to move them to MyPython it will do it, but you will now have duplicates. Ones in their original place and the ones that were "moved". It's a copy and not a move. Try it sometime by creating some test bkmrks and a folder. I've had repeated problems with Alan Gauld wrote: "Wayne Watson" wrote Thanks to your use of the word pillow it's not likely I will be able to find the link to the Library Reference more easily, strange as that may seem. You are right it seems very strange!? My browser's bookmark capabilities have put me in the corner of disorganization, Mozilla Seamonkey. I used Mozilla briefly before moving to Firefox, but I don't recall it having anything odd about its bookmarking capabilities! In fact I still use the directory structure I created for my bookmarks on Mozilla in Firefox to this day. (And on IE via importing), I easily have 1000 bookmarks and probably 40 or more on python. Me too, but I just have a programming folder with a Python one inside that. I then name the bookmark clearly - like Library Reference say... and it's been no problem. chance at finding my python bkmrks, I've put python in the title to make sure I have a fighting chance with their search to find them I just file them in the python folder when I bookmark them! This may change. I'm about to install Firefox. It might help because the new version of Firefox has tagged bookmarks, but personally I find my folder structure faster and more useful and saves me adding tags. Have you tried GoogleChrome yet? It might suit your style better. It looks at addresses you type and searches both your history and bookmarks for previous sites visited that might match. It sweems to work pretty well. I tend to primarily use Firefox and Chrome for all my browsing now except for a couple of sites that I know are IE only (including my bank sadly!) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) S, quiet. I'm thinking about filling this space. Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] General Feedback, Script Structure
On Sun, Feb 15, 2009 at 1:01 PM, Damon Timm wrote: > On Sun, Feb 15, 2009 at 12:20 PM, Kent Johnson wrote: >> - put the main code in a main() function rather than splitting it >> across the file. > > That's a good idea - I will do that. Is it proper to create a def > main() or just under: if __name__ == "__main__" Either one works; I usually don't put more than a few lines of code in the if block so I would make a main() function. >> then you can build the message as >> body = status.get(event, nomatch) % vars() >> message = header + body + footer > > > That last part I am not so clear on ... how does: body = > status.get(event, nomatch) % vars() work ? Does it say, first look > for "event" as a key and then, if it doesn't find a match with event,, > use the "nomatch" key ? Exactly. dict.get() does a key lookup with a default for missing keys, then the result is used as a string format. See http://docs.python.org/library/stdtypes.html#dict.get BTW I strongly recommend becoming familiar with the Built-in Functions and Built-in Types sections of the standard library docs: http://docs.python.org/library/ Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] General Feedback, Script Structure
"Damon Timm" wrote It works fine -- seems to do what I need -- but, to be very frank, I don't think it is very *pretty* ! But, I am not sure what the http://python.pastebin.com/m4e1694d5 Would be interested in any feedback (obviously, I should add some doc strings!). It doesn't look too bad to me, I'd probably do the dictionary assignments as a single statement rather than all the separate assignment lines and I'd also move it out of the function as a module level variable. Other than that it's not too bad IMHO Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
"Wayne Watson" wrote Thanks to your use of the word pillow it's not likely I will be able to find the link to the Library Reference more easily, strange as that may seem. You are right it seems very strange!? My browser's bookmark capabilities have put me in the corner of disorganization, Mozilla Seamonkey. I used Mozilla briefly before moving to Firefox, but I don't recall it having anything odd about its bookmarking capabilities! In fact I still use the directory structure I created for my bookmarks on Mozilla in Firefox to this day. (And on IE via importing), I easily have 1000 bookmarks and probably 40 or more on python. Me too, but I just have a programming folder with a Python one inside that. I then name the bookmark clearly - like Library Reference say... and it's been no problem. chance at finding my python bkmrks, I've put python in the title to make sure I have a fighting chance with their search to find them I just file them in the python folder when I bookmark them! This may change. I'm about to install Firefox. It might help because the new version of Firefox has tagged bookmarks, but personally I find my folder structure faster and more useful and saves me adding tags. Have you tried GoogleChrome yet? It might suit your style better. It looks at addresses you type and searches both your history and bookmarks for previous sites visited that might match. It sweems to work pretty well. I tend to primarily use Firefox and Chrome for all my browsing now except for a couple of sites that I know are IE only (including my bank sadly!) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
Title: Signature.html Yes, true enough about simplicity, but see my response to Alan. Kent Johnson wrote: On Sun, Feb 15, 2009 at 9:16 AM, Wayne Watson wrote: 3. Where can I find out more about enumerate, as used here: input_file=open('Initial.sen','r') for (line_cnt, each_line) in enumerate(input_file): print each_line input_file.close() I used this small program for other purposes in the distant past, but what's going here with enumerate? Documentation for enumerate seems scarce. The official docs are here: http://docs.python.org/library/functions.html#enumerate It's pretty simple, perhaps that is why not a lot is written about it. When you iterate a sequence with a for loop, you get just the elements in the sequence: In [18]: seasons = ['Spring', 'Summer', 'Fall', 'Winter'] In [19]: for season in seasons: : print season Spring Summer Fall Winter Sometimes it is useful to also get the index of the element, not just the element. That is when you use enumerate: In [20]: for i, season in enumerate(seasons): : print i, season 0 Spring 1 Summer 2 Fall 3 Winter Technically, the result of calling enumerate() is a sequence whose elements are pairs (tuples) of (index, item). Using tuple assignment you can easily assign these to two variables, as above. This is the most common use. You can also use enumerate() without unpacking the tuples, maybe this makes it a little clearer what is happening: In [22]: for item in enumerate(seasons): print item (0, 'Spring') (1, 'Summer') (2, 'Fall') (3, 'Winter') Kent -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) S, quiet. I'm thinking about filling this space. Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] General Feedback, Script Structure
Hi Kent - thanks for taking a look! Follow-up below: On Sun, Feb 15, 2009 at 12:20 PM, Kent Johnson wrote: > - put the main code in a main() function rather than splitting it > across the file. That's a good idea - I will do that. Is it proper to create a def main() or just under: if __name__ == "__main__" > - the use of tmpfile is awkward, can you make the gmailme script take > its input in strings? that gmail script needs an actual file to attach ... or rather, the location of a file to attach ... would have to change something so it could take text that it could save as a file. but that would probably be better. > - I would use plain positional parameters to getMessage(), rather than > supplying defaults. > - the status dict could be built once, containing just the format strings, > e.g. > status = dict( > TestMessage = "This was a crazy test message! Woo hoo!", > RebuildStarted = "The rebuilding of %(mddevice)s has begun!", > # etc > } > > then you can build the message as > body = status.get(event, nomatch) % vars() > message = header + body + footer That last part I am not so clear on ... how does: body = status.get(event, nomatch) % vars() work ? Does it say, first look for "event" as a key and then, if it doesn't find a match with event,, use the "nomatch" key ? I was trying to do something like that but couldn't figure out how to make it work ... Thanks! > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] General Feedback, Script Structure
On Sun, Feb 15, 2009 at 11:00 AM, Damon Timm wrote: > Hi - > > Am still new to python -- was writing a script that is used by mdadm > (linux software raid utility) when there was a raid "event" ... the > script then sends an email (using another python script caled > "gmailme") to me with the information from the event and attaches the > details of the raid device that triggered the problem. > > It works fine -- seems to do what I need -- but, to be very frank, I > don't think it is very *pretty* ! But, I am not sure what the > "python-way" would be to re-structure this so it continues to do what > it needs to do but is more readable and future-friendly. > > Could you take a look ? > > http://python.pastebin.com/m4e1694d5 > > Would be interested in any feedback (obviously, I should add some doc > strings!). Some ideas: - put the main code in a main() function rather than splitting it across the file. - the use of tmpfile is awkward, can you make the gmailme script take its input in strings? - I would use plain positional parameters to getMessage(), rather than supplying defaults. - the status dict could be built once, containing just the format strings, e.g. status = dict( TestMessage = "This was a crazy test message! Woo hoo!", RebuildStarted = "The rebuilding of %(mddevice)s has begun!", # etc } then you can build the message as body = status.get(event, nomatch) % vars() message = header + body + footer Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
On Sun, Feb 15, 2009 at 9:16 AM, Wayne Watson wrote: > 3. Where can I find out more about enumerate, as used here: > > input_file=open('Initial.sen','r') > for (line_cnt, each_line) in enumerate(input_file): > print each_line > input_file.close() > > I used this small program for other purposes in the distant past, but what's > going here with enumerate? Documentation for enumerate seems scarce. The official docs are here: http://docs.python.org/library/functions.html#enumerate It's pretty simple, perhaps that is why not a lot is written about it. When you iterate a sequence with a for loop, you get just the elements in the sequence: In [18]: seasons = ['Spring', 'Summer', 'Fall', 'Winter'] In [19]: for season in seasons: : print season Spring Summer Fall Winter Sometimes it is useful to also get the index of the element, not just the element. That is when you use enumerate: In [20]: for i, season in enumerate(seasons): : print i, season 0 Spring 1 Summer 2 Fall 3 Winter Technically, the result of calling enumerate() is a sequence whose elements are pairs (tuples) of (index, item). Using tuple assignment you can easily assign these to two variables, as above. This is the most common use. You can also use enumerate() without unpacking the tuples, maybe this makes it a little clearer what is happening: In [22]: for item in enumerate(seasons): print item (0, 'Spring') (1, 'Summer') (2, 'Fall') (3, 'Winter') Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] url parsing
use this if you want to take a hammer to crack a nut :D import os url = 'http://this/is/my/url/to/parse' os.path.basename(url) #gives "parse" os.path.dirname(url) #gives 'http://this/is/my/url/to' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
Title: Signature.html Thanks. A file name! That's funny. Bitten by my early acquaintance a few weeks ago with TkFileDialog, when I innocently stuck the method in the function to come back to. Wrong method! I don't think I'll forget next time. :-) Yes, I agree with wanting to open and close the file oneself. The fact that there's a ton of such documentation on file is the problem. Using Google to find the "right" one is like playing a slot machine. Similarly enumerate. The link you gave to enumerate doesn't quite do it for my example. for (line_cnt, each_line) ... I no longer have any idea what that means. Ah, by Googling with "for (line_cnt, each_line)", I've found some direct help, and the source of help on it--Python Tutor, Feb 13-14,2008. Someone responded to a question I posted on it, but there was no explanation. I just used it, and it worked. I may be the only one in the history of computing to ask about it. :-) Thanks to your use of the word pillow it's not likely I will be able to find the link to the Library Reference more easily, strange as that may seem. It is now bookmarked, as it has been, but with the word pillow in the title. My browser's bookmark capabilities have put me in the corner of disorganization, Mozilla Seamonkey. I easily have 1000 bookmarks and probably 40 or more on python. It an its predecessors have what I consider poor mgmt tools for bmrks. To make sure I even have a chance at finding my python bkmrks, I've put python in the title to make sure I have a fighting chance with their search to find them. One cannot even do a search on "python" and then do a "Move To" to put them in a single folder. I might as well roll dice. This may change. I'm about to install Firefox. Alan Gauld wrote: "Wayne Watson" wrote Three questions about askopenfilename and enumerate. For askopenfilename: 1. Do I have to close the file before exiting from it? 2. How do I read each line of text in the file and quit when an eof is reached? This has nothing to do with askopenfilename! 3. Where can I find out more about enumerate, as used here: input_file=open('Initial.sen','r') for (line_cnt, each_line) in enumerate(input_file): In the python documentation - try the search box on the python site or just google for python enumerate. The latter took me on the third link to: http://python.active-venture.com/whatsnew/section-enumerate.html Which is probably the simplest and shortest explanayin you will get! Documentation for enumerate seems scarce. BTW, is there a thorough document on the use of files in Python out there somewhere? There is a ton of it and every tutorial (including mine) will have a section on handling files. The official tutorial has a fair bit on it and the reference manual has a whole section. A good place to start might be the Library Reference (the one they suggest keeping under your pillow!) http://docs.python.org/library/stdtypes.html#file-objects HTH, -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) S, quiet. I'm thinking about filling this space. Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] General Feedback, Script Structure
Hi - Am still new to python -- was writing a script that is used by mdadm (linux software raid utility) when there was a raid "event" ... the script then sends an email (using another python script caled "gmailme") to me with the information from the event and attaches the details of the raid device that triggered the problem. It works fine -- seems to do what I need -- but, to be very frank, I don't think it is very *pretty* ! But, I am not sure what the "python-way" would be to re-structure this so it continues to do what it needs to do but is more readable and future-friendly. Could you take a look ? http://python.pastebin.com/m4e1694d5 Would be interested in any feedback (obviously, I should add some doc strings!). Thanks, Damon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
"Wayne Watson" wrote Three questions about askopenfilename and enumerate. For askopenfilename: 1. Do I have to close the file before exiting from it? 2. How do I read each line of text in the file and quit when an eof is reached? This has nothing to do with askopenfilename! Askopenfilename is the GUI equivalent of doing fname = raw_input("What f8ile d9o you want to open?") It simply returns a string which is a file name. It does NOT open the file, process the file or do anything else. It just returns the name as a string. So for your questions: 1. Do I have to close the file before exiting from it? Yes, it is good practice, but you need to open it first. 2. How do I read each line of text in the file and quit when an eof is reached? for line in open(fname): # process line This certainly doesn't do it. def OpenConfigFile(self): config_file = askopenfilename( title="Open Configuration File", filetypes=CEN_FILE_TYPES ) print "cfile---: ",config_file, type(config_file) for it in config_file: No, this will asign each letter in the filename to 'it' You need to open(config-file) Just as you would in any other non GUI program. If you had used askopenfile then it would be different because it does return an open file object, then your code would do what you expect. But personally I prefer to open the file myself since that keeps my code more independant of the GUI. I get a "'str' object has no attribute 'readline'" msg Because you are processing the file name not the file. 3. Where can I find out more about enumerate, as used here: input_file=open('Initial.sen','r') for (line_cnt, each_line) in enumerate(input_file): In the python documentation - try the search box on the python site or just google for python enumerate. The latter took me on the third link to: http://python.active-venture.com/whatsnew/section-enumerate.html Which is probably the simplest and shortest explanayin you will get! Documentation for enumerate seems scarce. BTW, is there a thorough document on the use of files in Python out there somewhere? There is a ton of it and every tutorial (including mine) will have a section on handling files. The official tutorial has a fair bit on it and the reference manual has a whole section. A good place to start might be the Library Reference (the one they suggest keeping under your pillow!) http://docs.python.org/library/stdtypes.html#file-objects HTH, -- Alan G 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] url parsing
On Sun, Feb 15, 2009 at 5:09 AM, Jay Jesus Amorin wrote: > Hi, > > Can you please help my how to parse. > > url = http://this/is/my/url/to/parse > > how do i parse url to print "http://this/is/my/url/to"; > > i want to remove the last part of my url with is "parse". I would like to > remove the last string of my url. > > i have try split, but i think it wont work as i dont know how long my url > would be in my real application. If you just want to remove everything after the last / then rsplit() with an argument telling it how many times to split will do it: In [13]: url = 'http://this/is/my/url/to/parse' In [14]: url.rsplit('/', 1) Out[14]: ['http://this/is/my/url/to', 'parse'] In [15]: url.rsplit('/', 1)[0] Out[15]: 'http://this/is/my/url/to' Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] advise on using re
On Sun, Feb 15, 2009 at 5:53 AM, Norman Khine wrote: MXlocate = re.compile('^.*preference = (\d*).*exchanger = (.*)$', > On the shell, the command > > $ nslookup -querytype=MX abakuc.com > Server: 193.252.19.3 > Address:193.252.19.3#53 > > Non-authoritative answer: > abakuc.com mail exchanger = 30 ASPMX3.GOOGLEMAIL.com. > abakuc.com mail exchanger = 30 ASPMX4.GOOGLEMAIL.com. > abakuc.com mail exchanger = 30 ASPMX5.GOOGLEMAIL.com. > abakuc.com mail exchanger = 60 mail.abakuc.com. > abakuc.com mail exchanger = 10 ASPMX.L.GOOGLE.com. > abakuc.com mail exchanger = 20 ALT1.ASPMX.L.GOOGLE.com. > abakuc.com mail exchanger = 20 ALT2.ASPMX.L.GOOGLE.com. > abakuc.com mail exchanger = 30 ASPMX2.GOOGLEMAIL.com. > > > I would like to extract the MX servers in to a list. None of your output lines contain the text 'preference = ' so your re does not match any of them. A standalone re tester can help with figuring out re's, for example Tools/scripts/redemo.py which comes with Python. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Reading a Text File with tkFileDialog, askopenfilename+enumerate
Title: Signature.html A three questions about askopenfilename and enumerate. For askopenfilename: 1. Do I have to close the file before exiting from it? 2. How do I read each line of text in the file and quit when an eof is reached? This certainly doesn't do it. def OpenConfigFile(self): config_file = askopenfilename( title="Open Configuration File", filetypes=CEN_FILE_TYPES ) print "cfile---: ",config_file, type(config_file) for it in config_file: config_file.readline(aline) print aline pass print "finished the file" I get a "'str' object has no attribute 'readline'" msg trying to read a text file, Initial.sen, which is a text file and of type 'str'. 3. Where can I find out more about enumerate, as used here: input_file=open('Initial.sen','r') for (line_cnt, each_line) in enumerate(input_file): print each_line input_file.close() I used this small program for other purposes in the distant past, but what's going here with enumerate? Documentation for enumerate seems scarce. BTW, is there a thorough document on the use of files in Python out there somewhere? -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) S, quiet. I'm thinking about filling this space. Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] url parsing
Thanks this is much more simple and is what i need. On Sun, Feb 15, 2009 at 9:29 PM, Lie Ryan wrote: > On Sun, 15 Feb 2009 14:22:09 +0100, Andre Engels wrote: > > What is 'new' in your solution? Apart from that, the following looks > > simpler: > > > url = "http://this/is/my/url/to/parse"; parts = url.split('/') > sol = '/'.join(parts[:-1]) > sol > > 'http://this/is/my/url/to' > > do you want something even more simpler? > > >>> url ="http://this/is/my/url/to/parse"; > >>> url.rsplit('/', 1)[0] > 'http://this/is/my/url/to' > > ___ > 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] url parsing
On Sun, 15 Feb 2009 14:22:09 +0100, Andre Engels wrote: > What is 'new' in your solution? Apart from that, the following looks > simpler: > url = "http://this/is/my/url/to/parse"; parts = url.split('/') sol = '/'.join(parts[:-1]) sol > 'http://this/is/my/url/to' do you want something even more simpler? >>> url ="http://this/is/my/url/to/parse"; >>> url.rsplit('/', 1)[0] 'http://this/is/my/url/to' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] url parsing
On Sun, Feb 15, 2009 at 1:37 PM, Emad Nawfal (عماد نوفل) wrote: > I'm not sure this is the best strategy, but it seems to work: > url ="http://this/is/my/url/to/parse"; m = url.replace("//", '/').split("/") n = m[0]+"//"+"/".join(new[1:]) n > 'http://this/is/my/url/to' What is 'new' in your solution? Apart from that, the following looks simpler: >>> url = "http://this/is/my/url/to/parse"; >>> parts = url.split('/') >>> sol = '/'.join(parts[:-1]) >>> sol 'http://this/is/my/url/to' -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] url parsing
On Sun, Feb 15, 2009 at 7:37 AM, Emad Nawfal (عماد نوفل) < emadnaw...@gmail.com> wrote: > > > On Sun, Feb 15, 2009 at 5:09 AM, Jay Jesus Amorin wrote: > >> Hi, >> >> Can you please help my how to parse. >> >> url = http://this/is/my/url/to/parse >> >> how do i parse url to print "http://this/is/my/url/to"; >> >> i want to remove the last part of my url with is "parse". I would like to >> remove the last string of my url. >> >> i have try split, but i think it wont work as i dont know how long my url >> would be in my real application. >> >> Thanks, >> >> >> Newbie, >> >> Jay >> >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> I'm not sure this is the best strategy, but it seems to work: > > >>> url ="http://this/is/my/url/to/parse"; > >>> m = url.replace("//", '/').split("/") > >>> n = m[0]+"//"+"/".join(m[1:]) > >>> n > 'http://this/is/my/url/to' > >>> Sorry there was something wrong in the first reply > > > > > > -- > لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد > الغزالي > "No victim has ever been more repressed and alienated than the truth" > > Emad Soliman Nawfal > Indiana University, Bloomington > http://emnawfal.googlepages.com > > -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] url parsing
On Sun, Feb 15, 2009 at 5:09 AM, Jay Jesus Amorin wrote: > Hi, > > Can you please help my how to parse. > > url = http://this/is/my/url/to/parse > > how do i parse url to print "http://this/is/my/url/to"; > > i want to remove the last part of my url with is "parse". I would like to > remove the last string of my url. > > i have try split, but i think it wont work as i dont know how long my url > would be in my real application. > > Thanks, > > > Newbie, > > Jay > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > I'm not sure this is the best strategy, but it seems to work: >>> url ="http://this/is/my/url/to/parse"; >>> m = url.replace("//", '/').split("/") >>> n = m[0]+"//"+"/".join(new[1:]) >>> n 'http://this/is/my/url/to' >>> -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] advise on using re
Hello, What am I doing wrong here. >>> import smtplib, socket, re, os, operator, sys >>> domain = 'abakuc.com' >>> validMXservers = [] >>> MXlocate = re.compile('^.*preference = (\d*).*exchanger = (.*)$', re.IGNORECASE) >>> MXservers = os.popen('nslookup -querytype=MX %s' %domain, 'r') >>> for line in MXservers: ... if 'mail exchanger' in line: ... MXcheck = MXlocate.match(line) ... if MXcheck: ... MXserver = MXcheck.group(1), MXcheck.group(2) ... validMXservers.append(MXserver) ... >>> validMXservers [] >>> print MXservers >>> print line >>> print MXcheck None >>> On the shell, the command $ nslookup -querytype=MX abakuc.com Server: 193.252.19.3 Address:193.252.19.3#53 Non-authoritative answer: abakuc.com mail exchanger = 30 ASPMX3.GOOGLEMAIL.com. abakuc.com mail exchanger = 30 ASPMX4.GOOGLEMAIL.com. abakuc.com mail exchanger = 30 ASPMX5.GOOGLEMAIL.com. abakuc.com mail exchanger = 60 mail.abakuc.com. abakuc.com mail exchanger = 10 ASPMX.L.GOOGLE.com. abakuc.com mail exchanger = 20 ALT1.ASPMX.L.GOOGLE.com. abakuc.com mail exchanger = 20 ALT2.ASPMX.L.GOOGLE.com. abakuc.com mail exchanger = 30 ASPMX2.GOOGLEMAIL.com. I would like to extract the MX servers in to a list. Cheers Norman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] url parsing
Hi, Can you please help my how to parse. url = http://this/is/my/url/to/parse how do i parse url to print "http://this/is/my/url/to"; i want to remove the last part of my url with is "parse". I would like to remove the last string of my url. i have try split, but i think it wont work as i dont know how long my url would be in my real application. Thanks, Newbie, Jay ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor