Re: [Tutor] clearing a text document
max baseman wrote: > right it's for a quick math "game" the rules are simple you start > with any number to get the next number you, a. if it's odd multiply > by 3 than add 1 or b. if it's even divide by two, the point of this > is to see how long it takes to get to one are it starts to repeat > 4,2,1,4,2,1... > now im looking for a a number with a very high amount of numbers > till you get to 1 so every time it's finds a new one i would like it > to write that to a file and clear the last entry, Just open the file for writing and it will erase all previous contents and create the file if it doesn't already exist. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] clearing a text document
right it's for a quick math "game" the rules are simple you start with any number to get the next number you, a. if it's odd multiply by 3 than add 1 or b. if it's even divide by two, the point of this is to see how long it takes to get to one are it starts to repeat 4,2,1,4,2,1... now im looking for a a number with a very high amount of numbers till you get to 1 so every time it's finds a new one i would like it to write that to a file and clear the last entry, On Aug 29, 2007, at 5:45 PM, Alan Gauld wrote: > > "max baseman" <[EMAIL PROTECTED]> wrote > >> quick question how would i clear a text document? > > A bit of context? If its a file you just open the file for writing. > > If its a list of strings in memory > > text = [] > > will clear it... > > If its a GUI text widget it will depend on which framework you are > using. > > Alan G > > > ___ > 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 script not parsing out text
Tim Finley wrote: > I get the following when running a script. > > TypeError: argument 1 must be string or read-only character buffer, > not _sre.SRE_Pattern First, please post the entire error report when asking for help. In this case I can tell you what the problem is, but in others the context of the error may not be so apparent. > > Here is the script I am trying to run. I am trying to verify that my > search is returning what I am trying to search for, but due to the > error I can verify it. > > import re > > log = open('audit.log') # Opens audit log > log2 = open('timaudit.log','w') > for line in log: > line =re.compile(r""" I think you want to use a different variable name than "line". You're using it for the current line, then setting it to the compiled regular expression. > \w #match any alphanumeric character > \Audit report for user+ > \User reported as inactive+ > """, re.VERBOSE) > line.search('Audit report for user () User reported as inactive') > log2.write(line) Hence the error. Write expects a line of text, but it doesn't point to the line of text you read any more. Hope that helps, e. > > log.close() > log2.close() > > Thank you, > > Tim Finley > Novell IT Services Engineer > Novell Technical Services > Novell > > > ___ > 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 script not parsing out text
Tim Finley wrote: > I get the following when running a script. > > TypeError: argument 1 must be string or read-only character buffer, not > _sre.SRE_Pattern > > Here is the script I am trying to run. I am trying to verify that my > search is returning what I am trying to search for, but due to the error > I can verify it. > > import re > > log = open('audit.log') # Opens audit log > log2 = open('timaudit.log','w') > for line in log: > line =re.compile(r""" Here you are replacing 'line' the data from one line of the log with 'line' a compiled regular expression. 'line' the data is no longer available. > \w #match any alphanumeric character > \Audit report for user+ > \User reported as inactive+ > """, re.VERBOSE) > line.search('Audit report for user () User reported as inactive') Now you use 'line' the regex to search some fixed text. > log2.write(line) This writes the regex to the file, which is the cause of the error. > > log.close() > log2.close() I'm not really sure what you are trying to do. I think you want to write every line from log that matches the regex to log2. Code to do that would look like this: log = open('audit.log') # Opens audit log log2 = open('timaudit.log','w') audit_re =re.compile(r""" \w #match any alphanumeric character \Audit report for user+ \User reported as inactive+ """, re.VERBOSE) for line in log: if audit_re.search(line): log2.write(line) log.close() log2.close() Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Code reading for learning Python
Alessandro Dantas wrote: > Hello Everyone, > > I'm learning Python and my preferred method for learning a new language > is to read code written by experts. I guess it makes even more sense in > Python since I've been hearing so much about how your code should be > pythonic to benefit from all the language can offer. Can anyone suggest > some good pieces of (pythonic???) code? I found the printed Python Cookbook very helpful after I had learned the basics. Django seems to be well written and fairly easy to read. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Trouble with script not parsing out text
I get the following when running a script. TypeError: argument 1 must be string or read-only character buffer, not _sre.SRE_Pattern Here is the script I am trying to run. I am trying to verify that my search is returning what I am trying to search for, but due to the error I can verify it. import re log = open('audit.log') # Opens audit loglog2 = open('timaudit.log','w')for line in log: line =re.compile(r""" \w #match any alphanumeric character \Audit report for user+ \User reported as inactive+ """, re.VERBOSE) line.search('Audit report for user () User reported as inactive') log2.write(line) log.close() log2.close() Thank you, Tim FinleyNovell IT Services EngineerNovell Technical ServicesNovell timaudit.log Description: Binary data ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] clearing a text document
"max baseman" <[EMAIL PROTECTED]> wrote > quick question how would i clear a text document? A bit of context? If its a file you just open the file for writing. If its a list of strings in memory text = [] will clear it... If its a GUI text widget it will depend on which framework you are using. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Code reading for learning Python
"Alessandro Dantas" <[EMAIL PROTECTED]> wrote > Can anyone suggest some good pieces of (pythonic???) code? The samples that come with python? The standard library modules? The IDLE IDE? Most of the sourceforge Python projects... Should be enough reading there... :-) Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Code reading for learning Python
Hello Everyone, I'm learning Python and my preferred method for learning a new language is to read code written by experts. I guess it makes even more sense in Python since I've been hearing so much about how your code should be pythonic to benefit from all the language can offer. Can anyone suggest some good pieces of (pythonic???) code? Alessandro Dantas Flickr agora em português. Você clica, todo mundo vê. http://www.flickr.com.br/___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tagging pieces of information
Have a look at Tasty last time I checked it was postgres centric, it might have changed now. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] clearing a text document
quick question how would i clear a text document? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tagging pieces of information
> > Hi, I am curious about ways in Python to approach the idea of "tagging" > > pieces of information much in the way that one can tag favorite websites > > like on the site Del.icio.us. I'm not sure if tagging is the best term >for > > this (due to confusion with HTML tags), but the idea would be a way to > > assign one or more words to stored data such that later one might search >by > > those words in order to retrieve the data. That data might be a chunk >of > > text, a graph, image, whatever...the point would be to be able to search > > later by tags name. I know the prorgram GyrFalcon uses tags and is >written > > in Python. And of course Flickr and many other things. > >A simple way to do this in-memory would be to use a dict: keys are >tags and values are sets (or lists) of objects. You might need to >maintain an inverse structure too, mapping object to list/set of tags. > >You could use a database (sqlite comes with python 2.5). I'm not sure >what the "best practice" strucutre would be, but maybe you could have >a table with two columns: "object ID" and "tag". "object ID" would be >some kind of identifier for your tagged objects. You could then: > >Find tags for an object: > select tag from tagTable where objectID = ? > >Find objects matching a tag: > select objectID from tagTable where tag = ? > >-- >John. Thanks, John, I've fooled around with the database way to do it and it fits in nicely with other things I'm trying to do and should work well for me. Thanks to the others for their suggestions as well. -Che _ Puzzles, trivia teasers, word scrambles and more. Play for your chance to win! http://club.live.com/home.aspx?icid=CLUB_hotmailtextlink ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A replacement for a "for" loop
On Wed, 29 Aug 2007, Scott Oertel wrote: > John Fouhy wrote: > > On 29/08/07, Trey Keown <[EMAIL PROTECTED]> wrote: > > > >> attrs={u'title': u'example window title', u'name': u'SELF', u'icon': > >> u'e.ico'} > >> keys = ['name','title','icon'] > >> for (tag, val) in attrs.iteritems(): > >> for key in keys: > >> print val > >> > >> the first "for" tag causes the dictionary (attrs) to have its keys called > >> "tag" and its value called "val". The second "for" loop causes the > >> dictionary keys to be read in a certain order. How could I take away the > >> first "for" loop and replace it with something else to do the same general > >> function? > >> > > > > for key in keys: > > print 'Attribute %s has value %s' % (key, attrs[key]) > > > > > Why even have the keys variable at all.. > > for key in attrs: > print 'Attribute %s has value %s' % (key, attrs[key]) In a prior email thread, the OP indicated that he needed to process the keys in that particular order; and it's not really amenable to any sort. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A replacement for a "for" loop
John Fouhy wrote: > On 29/08/07, Trey Keown <[EMAIL PROTECTED]> wrote: > >> attrs={u'title': u'example window title', u'name': u'SELF', u'icon': >> u'e.ico'} >> keys = ['name','title','icon'] >> for (tag, val) in attrs.iteritems(): >> for key in keys: >> print val >> >> the first "for" tag causes the dictionary (attrs) to have its keys called >> "tag" and its value called "val". The second "for" loop causes the >> dictionary keys to be read in a certain order. How could I take away the >> first "for" loop and replace it with something else to do the same general >> function? >> > > for key in keys: > print 'Attribute %s has value %s' % (key, attrs[key]) > > Why even have the keys variable at all.. for key in attrs: print 'Attribute %s has value %s' % (key, attrs[key]) -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie
On 8/28/07, Toby Holland <[EMAIL PROTECTED]> wrote: > HI all, > I was wondering if any of you had any advice as to where I should start in > regards to learning using and programing with Python. I have wanted to > learn a program language for some time and just felt that now was good and I > have heard some great things about Python so any suggestions would be great. > Thank you all in advance! > Toby Greetings, The Python Books I have are the ones that are freely available for download from the Internet. Here is the list: Learning to Program (by Alan Gauld - a Tutor on this list.) http://www.freenetpages.co.uk/hp/alan.gauld/index.htm This book is also available for purchase in dead-tree form. How To Think Like a Computer Scientist: Learning with Python http://ibiblio.org/obp/thinkCS/python/english2e/html/index.html Dive Into Python http://www.diveintopython.org/ A Byte of Python http://swaroopch.info/text/Byte_of_Python:Main_Page Python Documentation http://docs.python.org/index.html Thinking in Python http://mindview.net/Books/TIPython Text Processing in Python http://gnosis.cx/TPiP/ Your best bet may be the "Learning to Program" book by Alan Gauld. Also there are a ton of tutorials on the Internet, many of which will get you up to speed with the basic stuff in a hurry. Your best bet is to find a book that has a writing style that "clicks" with you, and work through it, sitting in front of your computer, with Python installed and working. It is important to read and DO if you want to learn how to program a computer. Happy Programming! -- bhaaluu at gmail dot com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor