Re: [Tutor] Inherit from int?
"John Fouhy" <[EMAIL PROTECTED]> wrote > Subclassing int and other types is a bit special. Check out this > page; it may help you: > http://www.python.org/download/releases/2.2.3/descrintro/#__new__ > In case others have the same problem... John's link didn't work for me but I eventually found this relevant snippet here: http://www.python.org/download/releases/2.2/descrintro/#subclassing Recall that you create class instances by calling the class. When the class is a new-style class, the following happens when it is called. First, the class's __new__ method is called, passing the class itself as first argument, followed by any (positional as well as keyword) arguments received by the original call. This returns a new instance. Then that instance's __init__ method is called to further initialize it. (This is all controlled by the __call__ method of the metaclass, by the way.) Here is an example of a subclass that overrides __new__ - this is how you would normally use it. >>> class inch(float): ... "Convert from inch to meter" ... def __new__(cls, arg=0.0): ... return float.__new__(cls, arg*0.0254) ... >>> print inch(12) 0.3048 >>> This class isn't very useful (it's not even the right way to go about unit conversions) but it shows how to extend the constructor of an immutable type. If instead of __new__ we had tried to override __init__, it wouldn't have worked: >>> class inch(float): ... "THIS DOESN'T WORK!!!" ... def __init__(self, arg=0.0): ... float.__init__(self, arg*0.0254) ... >>> print inch(12) 12.0 >>> The version overriding __init__ doesn't work because the float type's __init__ is a no-op: it returns immediately, ignoring its arguments. All this is done so that immutable types can preserve their immutability while allowing subclassing. If the value of a float object were initialized by its __init__ method, you could change the value of an existing float object! For example, this would work: >>> # THIS DOESN'T WORK!!! >>> import math >>> math.pi.__init__(3.0) >>> print math.pi 3.0 >>> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Inherit from int?
> I'm stumped. This silly bit of code doesn't work. I expect the > output to be 8, not 18. What am I missing? > > class Under10(int): > def __init__(self, number): > number %= 10 > int.__init__(self, number) marilyn, i agree with most of the earlier replies... you need to use __new__() instead of __init__() in order to "tweak" the original value before the instance of the immutable object is created. once it's "frozen," you're stuck. note that __new__() is a class method, so you'll need a variable for the class (instead of self for the instance). also recall that __init__() is (the 1st method) called *after* an instance has been created, which for you, would be too late. in practice, i don't think __init__() is ever used for deriving from immutable types. does anyone have a counterexample? (since i know you've been reading Core Python, you can take a look at my example of subclassing an immutable type on p.552.) :-) cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 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] Inherit from int?
Thank you everyone. I, indeed, found it in your book, Wes, after I knew it was something extra special. The explanations here are great! Thank you everyone. Marilyn On Sun, 13 May 2007, wesley chun wrote: > > I'm stumped. This silly bit of code doesn't work. I expect the > > output to be 8, not 18. What am I missing? > > > > class Under10(int): > > def __init__(self, number): > > number %= 10 > > int.__init__(self, number) > > marilyn, > > i agree with most of the earlier replies... you need to use __new__() > instead of __init__() in order to "tweak" the original value before > the instance of the immutable object is created. once it's "frozen," > you're stuck. note that __new__() is a class method, so you'll need a > variable for the class (instead of self for the instance). > > also recall that __init__() is (the 1st method) called *after* an > instance has been created, which for you, would be too late. in > practice, i don't think __init__() is ever used for deriving from > immutable types. does anyone have a counterexample? > > (since i know you've been reading Core Python, you can take a look at > my example of subclassing an immutable type on p.552.) :-) > > cheers, > -- wesley > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > "Core Python Programming", Prentice Hall, (c)2007,2001 > 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
[Tutor] Getting value from web page
Hi all, i have a page which when i download from web by python and put tu variable have something like: 119/1157/43/40 neer end ( actualy this is only thing on that page + ) What i need actualy is to get this values 119/1157/43/40 to variables, they are changing allthe time, but they stay numbers and "/" is always between numbers, they are always 4 I tried using re to search for it but without luck :( could somebody more experienced with re than me how to do it ? something like match = re.match('+/',htmltxt) (in htmltxt is the source code downloaded from web) <-- example not working ;) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Parsing text file
I'm looking for a more elegant way to parse sections of text files that are bordered by BEGIN/END delimiting phrases, like this: some text some more text BEGIN_INTERESTING_BIT someline1 someline2 someline3 END_INTERESTING_BIT more text more text What I have been doing is clumsy, involving converting to a string and slicing out the required section using split('DELIMITER'): import sys infile = open(sys.argv[1], 'r') #join list elements with @ character into a string fileStr = '@'.join(infile.readlines()) #Slice out the interesting section with split, then split again into lines using @ resultLine = fileStr.split('BEGIN_INTERESTING_BIT')[1].split('END_INTERESTING_BIT')[0].split('@') for line in resultLine: do things Can anyone point me at a better way to do this? Thanks -- -- Alan Wardroper [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parsing text file
On 14/05/07, Alan <[EMAIL PROTECTED]> wrote: > I'm looking for a more elegant way to parse sections of text files that > are bordered by BEGIN/END delimiting phrases, like this: > > some text > some more text > BEGIN_INTERESTING_BIT > someline1 > someline2 > someline3 > END_INTERESTING_BIT > more text > more text If the structure is pretty simple, you could use a state machine approach. eg: import sys infile = open(sys.argv[1], 'r') INTERESTING, BORING = 'interesting', 'boring' interestingLines = [] for line in infile: if line == 'BEGIN_INTERESTING_BIT': state = INTERESTING elif line == 'END_INTERESTING_BIT': state = BORING elif state == INTERESTING: interestingLines.append(line) return interestingLines If you want to put each group of interesting lines into its own section, you could do a bit of extra work (append a new empty list to interestingLines on 'BEGIN', then append to the list at position -1 on state==INTERESTING). HTH! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parsing text file
"Alan" <[EMAIL PROTECTED]> wrote > I'm looking for a more elegant way to parse sections of text files > that > are bordered by BEGIN/END delimiting phrases, like this: > > some text > BEGIN_INTERESTING_BIT > someline1 > someline3 > END_INTERESTING_BIT > more text > > What I have been doing is clumsy, involving converting to a string > and > slicing out the required section using split('DELIMITER'): The method I usually use is only slightly less clunky - or maybe just as clunky! I iterate over the lines setting a flag at the start and unsetting it at the end. Pseudo code: amInterested = False for line in textfile: if amInterested and not isEndPattern(line): storeLine(line) amInterested = not isEndPattern(line) if line.find(begin_pattern): amInterested = True Whether thats any better than joining/splitting is debateable. (Obviously you need to write the isEndPattern helper function too) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] web spider
hello i am a beginner programmer and i am planning a project that will take a keyword enter that in google and start searching through links ans saving web pages if anyone can help or might know of any tutorials about: writing to websites searching links creating files and web spiders in general that would be great thanks for any help. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor