Re: [Tutor] how to reference a function itself when accessing its private functions?
Le Mon, 04 May 2009 16:08:38 -0700, Emile van Sebille s'exprima ainsi: > On 5/4/2009 3:37 PM Tim Michelsen said... > > Dear Tutors and fellow pythonistas, > > I would like to get access to the private methods of my function. > > > > For instance: > > Who can I reference the docstring of a function within the function > > itself? > > > > > def show2(str): > > """prints str""" > > print str > > d = self.__doc__ > > print d > > >>> def show2(str): > ... """prints str""" > ... print str > ... print globals()['show2'].__doc__ > ... > >>> show2('hello') > hello > prints str > >>> Hello Emile, Why don't you use the func name directly? print show2.__doc__ I mean it's a constant in the sense that it is know at design time, right? And you need it anyway, as shown by the fact that it becomes a string literal in the your version. It's not like if you would _not_ know it ;-) Fortunately, funcs (and methods and classes and modules -- but not other object AFAIK) know their own (birth)name: def docString(obj): name = obj.__name__ return globals()[name].__doc__ Should work for the above listed object types. Anyway, other types have no docstring... > This is the easy way -- ie, you know where to look and what name to use. > You can discover the name using the inspect module, but it can get > ugly. If you're interested start with... > > from inspect import getframeinfo, currentframe -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Le Tue, 5 May 2009 00:52:11 +0100, "Alan Gauld" s'exprima ainsi: > > Is there a way in Python to say this is a string of HEX characters like > > Perl's pack? Right now I have to take the string and add a \x to every > > two > > values i.e. \x41\x42... > > Assuming you actually want to send the hex values rather than > a hex string representation then the way I'd send that would be > to convert that to a number using int() then transmit it using > struct() I can hardly imagine why you want an hex string representation for further process instead of the values, neither. Would be interested in the reason for this. Assuming that an interface require hex string, wouldn't it be easier to change this so that it directly gets values? Id est avoid double format conversion. Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
"Emile van Sebille" wrote "414243440d0a" Is there a way in Python to say this is a string of HEX characters like Perl's pack? Right now I have to take the string and add a \x to every two values i.e. \x41\x42... import binascii binascii.a2b_hex('41424344') I hadn't come across binascii before, but it doesn't do what I expected: import binascii as b b.a2b_hex('414243440d0a') 'ABCD\r\n' This appears to be converting it to a binary value then displaying that binary value as an ascii string. I'm not sure what the value of that is over struct or int? Can anyone enlighten me about why I'd ever want to use this? -- 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] Replacing fields in lines of various lengths
"Dan Liang" wrote And I put together the code below based on your suggestions, with minor changes and it does work. Good, now your question is? -Begin code #!usr/bin/python tags = { 'noun-prop': 'noun_prop null null'.split(), 'case_def_gen': 'case_def gen null'.split(), 'dem_pron_f': 'dem_pron f null'.split(), 'case_def_acc': 'case_def acc null'.split(), } TAB = '\t' def newlyTaggedWord(line): line = line.rstrip() # I strip line ending (word,tag) = line.split(TAB)# separate parts of line, keeping data only new_tags = tags[tag] # read in dict tagging = TAB.join(new_tags)# join with TABs return word + TAB + tagging # formatted result def replaceTagging(source_name, target_name): target_file = open(target_name, "w") # replacement loop for line in open(source_name, "r"): new_line = newlyTaggedWord(line) + '\n' target_file.write(new_line) source_name.close() target_file.close() AG> These two lines should be inside the function, after the loop. if __name__ == "__main__": source_name = sys.argv[1] target_name = sys.argv[2] replaceTagging(source_name, target_name) -End code Now since I have to workon different data format as follows: -Begin data w1\t case_def_acc \t yes w2\t noun_prop \t no w3\t case_def_gen \t w4\t dem_pron_f \t no w3\t case_def_gen \t w4\t dem_pron_f \t no w1\t case_def_acc \t yes w3\t case_def_gen \t w3\t case_def_gen \t -End data Notices that some lines have nothing in yes-no filed, and hence end in a tab. My question is how to replace data in the filed of composite tags by sub-tags like those in the dictionary values above and still be able to print the whole line only with this change (i.e, composite tags replace by sub-tags). Earlier, we read words and tags from line directly into the dictionary since we were sure each line had 2 fields after separating by tabs. Here, lines have various field lengths and sometimes have yes and no finally, and sometimes not. I tried to make changes to the code above by changing the function where we read the dictionary, but it did not work. While it is ugly, I include it as a proof that I have worked on the problem. I am sure you will have various nice ideas. -End code def newlyTaggedWord(line): tagging = "" line = line.split(TAB)# separate parts of line, keeping data only if len(line)==3: word = line[-3] tag = line[-2] new_tags = tags[tag] decision = line[-1] # in decision I wanted to store #either yes or no if one of #these existed elif len(line)==2: word = line[-2] tag = line[-1] decision = TAB # I thought if it is a must to put sth in decision while decision #is really absent in line, I would put a tab. But I really want to #avoid putting anything there. new_tags = tags[tag] # read in dict tagging = TAB.join(new_tags)# join with TABs return word + TAB + tagging + TAB + decision -End code I appreciate your support! --dan ___ 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] Tutor Digest, Vol 63, Issue 8
Please use a sensible subject and don;t reporst the whole digest. "Dan Liang" wrote in message Thank you for your help. I have been working on the file, but I still have a problem doing what I wanted. As a reminder, Can you tell us what exactly the problem is? It's easier than us trying to guess. #!usr/bin/python tags = { 'noun-prop': 'noun_prop null null'.split(), 'case_def_gen': 'case_def gen null'.split(), 'dem_pron_f': 'dem_pron f null'.split(), 'case_def_acc': 'case_def acc null'.split(), } TAB = '\t' def newlyTaggedWord(line): (word,tag) = line.split(TAB)# separate parts of line, keeping new_tags = tags[tag] # read in dict tagging = TAB.join(new_tags)# join with TABs return word + TAB + tagging # formatted result def replaceTagging(source_name, target_name): target_file = open(target_name, "w") # replacement loop for line in open(source_name, "r"): new_line = newlyTaggedWord(line) + '\n' target_file.write(new_line) target_file.close() if __name__ == "__main__": source_name = sys.argv[1] target_name = sys.argv[2] replaceTagging(source_name, target_name) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Replacing fields in lines of various lengths
My turn to apologise for premature sending of email... For some reason I didn't notice the section of mail after the code snippet! Alan G "Dan Liang" wrote in message Now since I have to work on different data format as follows: -Begin data w1\t case_def_acc \t yes w2\t noun_prop \t no w3\t case_def_gen \t w4\t dem_pron_f \t no -End data Notices that some lines have nothing in yes-no filed, and hence end in a tab. My question is how to replace data in the filed of composite tags by sub-tags like those in the dictionary values above and still be able to print the whole line only with this change (i.e, composite tags replace by sub-tags). ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Replacing fields in lines of various lengths
Le Tue, 5 May 2009 00:22:45 -0400, Dan Liang s'exprima ainsi: > -Begin data > > w1\t case_def_acc \t yes > w2\t noun_prop \t no > w3\t case_def_gen \t > w4\t dem_pron_f \t no > w3\t case_def_gen \t > w4\t dem_pron_f \t no > w1\t case_def_acc \t yes > w3\t case_def_gen \t > w3\t case_def_gen \t > > -End data > I tried to make changes to the code above by changing the function where we > read the dictionary, but it did not work. While it is ugly, I include it as > a proof that I have worked on the problem. I am sure you will have various > nice ideas. > > > -End code > def newlyTaggedWord(line): >tagging = "" >line = line.split(TAB)# separate parts of line, keeping data only >if len(line)==3: >word = line[-3] >tag = line[-2] >new_tags = tags[tag] >decision = line[-1] > > # in decision I wanted to store #either yes or no if one of #these existed > >elif len(line)==2: >word = line[-2] >tag = line[-1] >decision = TAB > > # I thought if it is a must to put sth in decision while decision #is really > absent in line, I would put a tab. But I really want to #avoid putting > anything there. > >new_tags = tags[tag] # read in dict >tagging = TAB.join(new_tags)# join with TABs >return word + TAB + tagging + TAB + decision > -End code > For simplicity, it would be cool if file would have some placeholder in place of absent yes/no 'decisions' so that you know there are always 3 fields. That's what would be cool with most languages. But python is rather flexible and clever for such border cases. Watch the example below: s1, s2 = "1\t2\t3", "1\t2\t" items1, items2 = s1.split('\t'), s2.split('\t') print items1, items2 ==> ['1', '2', '3'] ['1', '2', ''] So that you always have 3 items, the 3rd one maybe the empty string. Right? This means: * You can safely write "(word,tag,decision) = line.split(TAB)" [Beware of misleading naming like "line = line.split(TAB)", for after this the name 'line' actually refers to field values.] * You can have a single process. * The elif branch in you code above will never run, i guess ;-) [place a print instruction inside to check that] Denis Ps: I noticed that in your final version for the case of files with 2 fields only, you misplaced the file closings. They fit better in the func. -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Conversion question
Thanks everyone for your replies. This reason for the long string is sometimes I have to pass a 32 byte key (MD5 hash value) to the socket. The data I am sending is the hex values i.e. 41=A. I believe the binascii will work. Mike On Mon, May 4, 2009 at 7:52 PM, Alan Gauld wrote: > > "Tom Green" wrote > > Here is my question. I work with a lot of sockets and most of them >> require >> hex data. I am usually given a string of data to send to the socket. >> Example: >> >> "414243440d0a" >> >> Is there a way in Python to say this is a string of HEX characters like >> Perl's pack? Right now I have to take the string and add a \x to every >> two >> values i.e. \x41\x42... >> > > Assuming you actually want to send the hex values rather than > a hex string representation then the way I'd send that would be > to convert that to a number using int() then transmit it using > struct() > > Sometimes my string values are 99+ bytes in length. I did write a parsing >> program that would basically loop thru the string and insert the \x, but I >> was wondering if there was another or better way. >> > > OK, Maybe you do want to send the hex representation rather than > the actual data (I can't think why unless you have a very strange > parser at the other end). In that case I think you do need to insert > the \x characters. > > > -- > 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] Encode problem
On Tue, May 5, 2009 at 1:14 AM, Mark Tolonen wrote: > The below works. ConfigParser isn't written to support Unicode correctly. I > was able to get Unicode sections to write out, but it was just luck. Unicode > keys and values break as the OP discovered. So treat everything as byte > strings: Thanks for the complete example. > files = glob.glob('*.txt') > c.add_section('files') > > for i,fn in enumerate(files): > fn = fn.decode(sys.getfilesystemencoding()) I think if you give a Unicode string to glob.glob(), e.g. glob.glob(u'*.txt'), then the strings returned will also be unicode and this decode step will not be needed. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r')
Le Tue, 5 May 2009 00:41:39 +0100, "Alan Gauld" s'exprima ainsi: > > Backwards compatibility. The file type was introduced in python 2.2, > > before which there was open. > > And file has been removed again in Python v3 > In fact open is now an alias for io.open and no longer simply returns > a file object - in fact the file type itself is gone too! > > A pity, there are cases where I found file() more intuitive than > open and vice versa so liked having both available. The fact that it > looked like creating an instance of a class seemed to fit well > in OO code. Same for me. It makes files an exception in the python OO system. Conversely, I have always considered open(), rather than file(), a flaw. (But there are numerous things on which I have a different point of view than the majority of pythonistas ;-) Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r')
On Tue, May 5, 2009 at 5:41 AM, spir wrote: > Le Tue, 5 May 2009 00:41:39 +0100, > "Alan Gauld" s'exprima ainsi: > > > > Backwards compatibility. The file type was introduced in python 2.2, > > > before which there was open. > > > > And file has been removed again in Python v3 > > In fact open is now an alias for io.open and no longer simply returns > > a file object - in fact the file type itself is gone too! > > > > A pity, there are cases where I found file() more intuitive than > > open and vice versa so liked having both available. The fact that it > > looked like creating an instance of a class seemed to fit well > > in OO code. > > Same for me. It makes files an exception in the python OO system. > Conversely, I have always considered open(), rather than file(), a flaw. > (But there are numerous things on which I have a different point of view > than the majority of pythonistas ;-) > Well, when you consider that it's really, underneath everything(way down in the assembly code), a pointer to a location on the hard disk/memory, open does make a little more sense. Of course, conversely there are also (like Alan mentioned) cases where file just makes more sense when you're looking at/writing the code, even if the concept may not technically be correct. Still, I think that's part of OO programming - obscuring some of the underlying ideas in favor of clarity of purpose. For those of us who may still find a need/desire to use file(), I'm sure file = open still works in python3 ( of course that's not making one preferably obvious right way of doing things ) -Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advanced String Search using operators AND, OR etc..
Alex Feddor wrote: Hi I am looking for method enables advanced text string search. Method string.find() or re module seems no supporting what I am looking for. The idea is as follows: Text ="FDA meeting was successful. New drug is approved for whole sale distribution!" I would like to scan the text using AND and OR operators and gets -1 or other value if the searching elements haven't found in the text. Example 01: search criteria: "FDA" AND ( "approve*" OR "supported") The catch is that in Text variable FDA and approve words are not one after another (other words are in between). Bring on your hardest searches... class Pattern(object): pass class Logical(Pattern): def __init__(self, pat1, pat2): self.pat1 = pat1 self.pat2 = pat2 def __call__(self, text): a, b = self.pat1(text), self.pat2(text) if self.op(a != len(text), b != len(text)): return min((a, b)) return len(text) def __str__(self): return '(%s %s %s)' % (self.pat1, self.op_name, self.pat2) class P(Pattern): def __init__(self, pat): self.pat = pat def __call__(self, text): ret = text.find(self.pat) return ret if ret != -1 else len(text) def __str__(self): return '"%s"' % self.pat class NOT(Pattern): def __init__(self, pat): self.op_name = 'NOT' self.pat = pat def __call__(self, text): ret = self.pat(text) return ret - 1 if ret == len(text) else len(text) def __str__(self): return '%s (%s)' % (self.op_name, self.pat) class XOR(Logical): def __init__(self, pat1, pat2): self.op_name = 'XOR' self.op = lambda a, b: not(a and b) and (a or b) super().__init__(pat1, pat2) class OR(Logical): def __init__(self, pat1, pat2): self.op_name = 'OR' self.op = lambda a, b: a or b super().__init__(pat1, pat2) class AND(Logical): def __init__(self, pat1, pat2): self.op_name = 'AND' self.op = lambda a, b: a and b super().__init__(pat1, pat2) class Suite(object): def __init__(self, pat): self.pat = pat def __call__(self, text): ret = self.pat(text) return ret if ret != len(text) else -1 def __str__(self): return '[%s]' % self.pat pat1 = P('FDA') pat2 = P('approve*') pat3 = P('supported') p = Suite(AND(pat1, OR(pat2, pat3))) print(p('')) print(p('FDA')) print(p('FDA supported')) print(p('supported FDA')) print(p('blah FDA bloh supported blih')) print(p('blah FDA bleh supported bloh supported blih ')) p = Suite(AND(OR(pat1, pat2), XOR(pat2, NOT(pat3 print(p) print(p('')) print(p('FDA')) print(p('FDA supported')) print(p('supported sdc FDA sd')) print(p('blah blih FDA bluh')) print(p('blah blif supported blog')) # I guess I went a bit overboard here (had too much time on hand), the working is based on function composition, so instead of evaluation, you composes a function (or more accurately, a callable class) that will evaluate the logical value and return the index of the first item that matches the logical expression. It currently uses str's builtin find, but I guess it wouldn't be very hard to adapt it to use the re myfind() below (only P class will need to change) The Suite class is only there to turn the NotFound sentinel from len(text) to -1 (used len(text) since it simplifies the code a lot...) Caveat: The NOT class cannot reliably convert a False to True because I don't know what index number to use. Code written for efficient vertical space, not the most readable in the world. No guarantee no bug. Idea: Overrides the operator on Pattern class so we could write it like: P("Hello") & P("World") instead of AND(P("Hello"), P("World")) Example 02: search criteria: "Ben" The catch is that code sould find only exact Ben words not also words which that has firts three letters Ben such as Benquick, Benseek etc.. Only Ben is the right word we are looking for. The second one was easier... import re def myfind(pattern, text): pattern = r'(.*?)\b(%s)\b(.*)' % pattern m = re.match(pattern, text) if m: return len(m.group(1)) textfound = 'This is a Ben test string' texttrick = 'This is a Benquick Benseek McBen QuickBenSeek string' textnotfound = 'He is away' textmulti = 'Our Ben found another Ben which is quite odd' pat = 'Ben' print(myfind(pat, textfound))# 10 print(myfind(pat, texttrick))# None print(myfind(pat, textnotfound)) # None print(myfind(pat, textmulti))# 4 if you only want to test for existence, simply: pattern = 'Ben' if re.match(r'(.*?)\b(%s)\b(.*)' % pattern, text): pass I would really appreciated your advice - code sample / links how above can be achieved! if possible I would appreciated solution achieved with free of charge module. Standard library is free of charge, no? ___ Tutor maillist - Tutor@python.
Re: [Tutor] Advanced String Search using operators AND, OR etc..
On Tue, May 5, 2009 at 8:11 AM, Lie Ryan wrote: > Bring on your hardest searches... Nice! > The Suite class is only there to turn the NotFound sentinel from len(text) > to -1 (used len(text) since it simplifies the code a lot...) How does this simplify the code? Why not use the 'in' operator and return True or False from the terms? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advanced String Search using operators AND, OR etc..
Le Tue, 05 May 2009 22:11:22 +1000, Lie Ryan s'exprima ainsi: > Idea: > Overrides the operator on Pattern class so we could write it like: > P("Hello") & P("World") instead of AND(P("Hello"), P("World")) You could also override directly all python logical ops to allow direct expression of logical searches in python (~ like in pyparsing). (P("Hello") or P("Salut")) and (P("World") and not P("Universe")) (Then rewriting from user expression may even be easier.) ;-) Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advanced String Search using operators AND, OR etc..
Kent Johnson wrote: On Tue, May 5, 2009 at 8:11 AM, Lie Ryan wrote: Bring on your hardest searches... Nice! The Suite class is only there to turn the NotFound sentinel from len(text) to -1 (used len(text) since it simplifies the code a lot...) How does this simplify the code? Why not use the 'in' operator and return True or False from the terms? Using len(text) as NotFound sentinel simplifies the code because, as it was searching for the lowest index that matches the expression, I used min((a, b)) to do the comparison, and if NotFound is -1, min((a, b)) will return -1. Since len(text) is always higher (or equal) to the other expression and thus will not affect the comparison in codespeak: text = 'ABCD' pat1 = 'C' # P('C') determined 2 pat2 = 'Z' # P('Z') determined 4/NotFound expr = OR(P('C'), P('Z')) # 2 or NotFound is True # min(2, 4) is 2 pat3 = 'A' # P('A') determined 0 expr = OR(P('C'), P('A')) # 3 or 0 is True # min((3, 0)) is 0 pat4 = 'Y' # P('Y') determined 4/NotFound expr = OR(P('Z'), P('Y')) # NotFound or NotFound is False $ min(4, 4) is 4, i.e. NotFound Also, the 'in' operator cannot be used to handle nested expressions. On nested expressions, I simply need to evaluate two indexes and find their lower index (if applicable). Other alternative sentinel I was thinking about was 0, which simplify the if expressions, but it 1) also suffers the same problem with -1 and 2) 0 is already occupied by the first character. I decided that having pat1 != len(text) sprinkled is simpler than rewriting min() to special case -1 AND still had to have pat != -1 sprinkled. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Encode problem
"Kent Johnson" wrote in message news:1c2a2c590905050337j1afc177ene64f800dcc3a7...@mail.gmail.com... On Tue, May 5, 2009 at 1:14 AM, Mark Tolonen wrote: > The below works. ConfigParser isn't written to support Unicode > correctly. I > was able to get Unicode sections to write out, but it was just luck. > Unicode > keys and values break as the OP discovered. So treat everything as byte > strings: Thanks for the complete example. > files = glob.glob('*.txt') > c.add_section('files') > > for i,fn in enumerate(files): > fn = fn.decode(sys.getfilesystemencoding()) I think if you give a Unicode string to glob.glob(), e.g. glob.glob(u'*.txt'), then the strings returned will also be unicode and this decode step will not be needed. You're right, that's why I had the comment above it :^) # The following could be glob.glob(u'.') to get a filename in # Unicode, but this is for illustration that the encoding of the # source file has no bearing on the encoding strings other than # ones hard-coded in the source file. The OP had wondered why his source file encoding "doesn't use the encoding defined for the application (# -*- coding: utf-8 -*-)." and I thought this would illustrate that byte strings could be in other encodings. It also shows the reason spir could said "... you shouldn't even need explicit encoding; they should pass through silently because they fit in an 8 bit latin charset.". If I'd left out the Chinese, I could've use a latin-1 encoding for everthing and not decode or encode at all (assuming the file system was latin-1). -Mark ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r')
"W W" wrote in message news:333efb450905050408m48246dd8wc90b94880898e...@mail.gmail.com... On Tue, May 5, 2009 at 5:41 AM, spir wrote: Le Tue, 5 May 2009 00:41:39 +0100, "Alan Gauld" s'exprima ainsi: > > Backwards compatibility. The file type was introduced in python 2.2, > > before which there was open. > > And file has been removed again in Python v3 > In fact open is now an alias for io.open and no longer simply returns > a file object - in fact the file type itself is gone too! I, for one, am glad it is gone again. It took awhile to train myself not to write: file = open('filename.txt') and incorrectly shadow the built-in. It's just the most natural name for the variable :^) -Mark ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using read() before closing a file
Hello, I found interesting today, as I was playing with this code in the shell: >>> arq = open('c:/myscripts/simple2.ttt', 'w') >>> len(a) 9 >>> arq.write('The length is %d' % len(a)) >>> res = arq.read() >>> print res The print doesn't print the text I had written, but some thing else I can't copy and paste to this message. But if I had closed the file and then did this: res = open('c:/myscripts/simple2.ttt').read(), it would have worked. Just thought of sharing this. Eduardo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using read() before closing a file
"Eduardo Vieira" wrote arq = open('c:/myscripts/simple2.ttt', 'w') arq.write('The length is %d' % len(a)) res = arq.read() print res The print doesn't print the text I had written, That's because of two things: a) You opened the file for write only not reading b) You had moved the file cursor beyond the text you wrote On (b): the file is conceptually a sequential data stream so think of the cursor in a text editor line. After you have written something you cannot select what you have written unless you cursor back to the beginning. Similarly with a file. Even if you opened it with rw access you would still need to position the cursor (with the seek() call) to the point before what you wrote to be able to read it. After reading it the cursor would once again be at the end of the file and you could write more data. But if I had closed the file and then did this: res = open('c:/myscripts/simple2.ttt').read(), it would have worked. Thats right because you now open the file for reading, therefore the cursor is repositioned at he start of the file and the mode is set correctly. 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] quick question to open(filename, 'r') vs. file(filename, 'r')
> It seems that open(filename, 'r') and file(filename, 'r') are > used interchangeably, and I wonder what this is all about. as alan and others have pointed out, file() was added in 2.2. this was part of the unification of types and classes. every object now has a factory function, i.e., list() creates a list, and same goes for int, float, dict, etc., and of course, file() creates a file. open(), on the other hand, has been around for ages and will remain so while file() is removed in 3.0 -- yes, easy come, easy go. the original intention was *not* to have file() for opening files with, but rather for type checking. in other words: OPEN A FILE with open(), i.e., f = open('foo.txt', 'r') TEST FOR A FILE with file(), i.e., if isinstance(f, file): ... does anyone have the idiom for the above isinstance() check for 3.x? hope the rest of this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to reference a function itself when accessing its private functions?
Thanks a lot! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r')
It may not be a good reason, but I like that "open" is preferred, because in Java a "File" object is not a stream for the content in the file, just its directory entry, and simply creating a File object does not open the file at all. Larry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] returning the entire line when regex matches
for root, dirs, files in os.walk('./'): for f in files: if f.endswith('.txt'): allfiles.append(os.path.join(root, f)) This returns a list of the *.txt files. Of course now I am stuck on how to apply the delimiter and search function to the items in the list..? Any clues/examples would be great. ty On Mon, May 4, 2009 at 7:32 PM, Alan Gauld wrote: >> Mr. Gauld is referring to!! I searched python.org and alan-g.me.uk. >> Does anyone have a link? > > I posted a link to the Python howto and my tutorial is at alan-g.me.uk > You will find it on the contents frame under Regular Expressions... > Its in the Advanced Topics section. > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] returning the entire line when regex matches
forgot to add this at the top, allfiles = [] On Tue, May 5, 2009 at 6:15 PM, Nick Burgess wrote: > for root, dirs, files in os.walk('./'): > for f in files: > if f.endswith('.txt'): > allfiles.append(os.path.join(root, f)) > > This returns a list of the *.txt files. Of course now I am stuck on > how to apply the delimiter and search function to the items in the > list..? Any clues/examples would be great. ty > > On Mon, May 4, 2009 at 7:32 PM, Alan Gauld wrote: >>> Mr. Gauld is referring to!! I searched python.org and alan-g.me.uk. >>> Does anyone have a link? >> >> I posted a link to the Python howto and my tutorial is at alan-g.me.uk >> You will find it on the contents frame under Regular Expressions... >> Its in the Advanced Topics section. >> >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> ___ >> Tutor maillist - tu...@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] returning the entire line when regex matches
"Nick Burgess" wrote for root, dirs, files in os.walk('./'): for f in files: if f.endswith('.txt'): allfiles.append(os.path.join(root, f)) myFunction( os.path.join(root,f) ) This returns a list of the *.txt files. Of course now I am stuck on how to apply the delimiter and search function to the items in the list..? Either call your function on each file as you find it -- no need for a list, or... for f in allfiles: myFunction(f) Provided your function can take the filename as a parameter either method will work. -- 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] quick question to open(filename, 'r') vs. file(filename, 'r')
"wesley chun" wrote does anyone have the idiom for the above isinstance() check for 3.x? Test for io.stream (or a derivative) - which is what open now returns. files are no more... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] quick question to open(filename, 'r') vs. file(filename, 'r')
"Larry Riedel" wrote It may not be a good reason, but I like that "open" is preferred, because in Java a "File" object is not a stream for the content in the file, just its directory entry, and simply creating a File object does not open the file at all. I'm not sure citing anything Java does is a good rationale in Python! Hopefully we can do rather better than the horrors of Java :-) -- 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] quick question to open(filename, 'r') vs. file(filename, 'r')
> I'm not sure citing anything Java does is a good rationale in Python! I find the languages/platforms complementary, so I appreciate when they are not arbitrarily inconsistent with each other. Larry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Reset while loop
Hi, not even sure if I can ask this very smartly but we shall try: I was playing around with trying to create a python program like tail -f as discussed on the list. i came up with this; def tail_it(bufsize=8192, linesep=__import__('os').linesep): f = None if f == None: f = open(tail_fname, 'rb') f.seek(0, 2) pos, tailsize = divmod(f.tell(), bufsize) if tailsize == 0: pos = max(0, pos-1) pos *= bufsize f.seek(pos) lines = f.read().split(linesep) x = len(lines) x = x-2 print lines[x:] f.close() while True: new_time = os.stat(tail_fname).st_mtime if new_time > old_time: f = open(tail_fname, 'rb') f.seek(0, 2) pos, tailsize = divmod(f.tell(), bufsize) if tailsize == 0: pos = max(0, pos-1) pos *= bufsize f.seek(pos) lines = f.read().split(linesep) x = len(lines) x = x-2 print lines[x:] time.sleep(sec_to_wait) f.close() [output] ['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed for user root', ''] ['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed for user root', ''] ['May 5 22:32:26 opteron su[5589]: pam_unix(su:session): session closed for user root', ''] ['May 5 22:40:01 opteron cron[22996]: (root) CMD (test -x /usr/sbin/run-crons && /usr/sbin/run-crons )', ''] ['May 5 22:40:01 opteron cron[22996]: (root) CMD (test -x /usr/sbin/run-crons && /usr/sbin/run-crons )', ''] ['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change 4001', ''] ['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change 4001', ''] ['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change 4001', ''] ['May 5 22:41:26 opteron ntpd[3571]: kernel time sync status change 4001', ''] [/output] That was with sec_to_wait set at 30 The problem I am having is it will print something whatever sec_to_wait is set to even if the file's st_mtime is the same. I want it to only prine when the st_mtime has changed. I think I need to update the while loop and make old_time = new-time and start over but I need some direction to get started, thanks -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor