Re: [Tutor] Remove a number from a string
>> s = ' '.join(s.split()[1:]) > > or just > s = s.split(None, 1)[1] Neat, I hadn't noticed the maxsplit parameter before. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError and appending to lists [Was: Re: Need Helpon Assignment]
Hi Tom, Glad you got it working with Danny's help. I'll throw in some style points too. > input = open('/home/tom/Python/Input/SPY3.txt', 'r') > N=0 > s = 'boo' > while s: >s = input.readline() >if s == '':break You might find it easier to use a for loop. you could for example use for s in input: to replace most of the above code and, in the process, eliminate the need for N completely, see below... >s = s[:-2] >T[N] = s.split(',') Since you are using append elsewhere why not for T too? And if you store the split result in a local value before putting it into T you can use that variable in all the following appends to save indexing T... >date.append(T[N][0]) >open.append(float(T[N][1])) >hi.append(float(T[N][2])) >lo.append(float(T[N][3])) >close.append(float(T[N][4])) >vol.append(float(T[N][5])) >N+=1 And with the for loop you don;t need to increment N either. > print N > for i in range(N): And you can use len(T) to replace N here. >T[i] This doesn't do anything! :-) >print T[i] >print date[i], open[i], hi[i], lo[i], close[i], vol[i] > print T[1][2], T[0][0] > z = (hi[2] +lo[2])/2.0 > print z HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Counting help
Kent Johnson wrote: Scott Oertel wrote: The next problem I have though is creating the dict, i have a loop, but i can't figure out how to compile the dict, it is returning this: ('Joey Gale', ('Scott Joe', 'This is lame' ))) listofnames = [] while (cnt < number[1][0]): if (date[2] == today[2]): test = regex.findall(M.fetch(int(number[1][0]) - cnt, '(BODY[HEADER.FIELDS (FROM)])')[1][0][1].rstrip()) cnt += 1 if (nameofsender != []): print nameofsender[0] listofnames = nameofsender[0], listofnames I think you want listofnames.append(nameofsender[0]) which will add nameofsender[0] to the list. What you have - listofnames = nameofsender[0], listofnames is making a tuple (a pair) out of the new name and the old list, and assigning it to listofnames. Kind of like CONS in LISP - but Python lists are more like arrays than like LISP lists. Kent else: no_name += 1 else: break ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Thank you everyone, this is exactly it, I'm going to take that link from byron and read up on dicts/lists. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Source PC MAC address
Hi List, I am doing some networking programming and would like to limit access to my socket server on the the source devices' MAC address. I know the IP from where the connection is coming, but how could I find out what the MAC of the source device is? Any quick answers / ideas? Is there a build-in function in socket that can do this? Thanks, Johan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Source PC MAC address
Socket is built up on IP, not on Ethernet: you have no way of finding a MAC address using sockets simply because it may not exist one ! (if you're not on an Ethernet network) You need to access lower levels of network and probably access directly the network packet as your network card is sending it to your OS ! Pierre Johan Geldenhuys a écrit : > Hi List, > I am doing some networking programming and would like to limit access to > my socket server on the the source devices' MAC address. > I know the IP from where the connection is coming, but how could I find > out what the MAC of the source device is? > Any quick answers / ideas? > Is there a build-in function in socket that can do this? > > Thanks, > > Johan > > > > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77fax : (33) 4 67 61 56 68 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Website Retrieval Program
I'm currently trying to write a script that will get all the files necessary for a webpage to display correctly, followed by all the intra-site pages and such forth, in order to try and retrieve one of the many sites I have got jumbled up on my webspace. After starting the writing, someone introduced me to wget, but I'm continuing this because it seems like fun (and that statement is the first step on a slippery slope :P). My script thus far reads: """ import re import urllib source = urllib.urlopen("http://www.aehof.org.uk/real_index2.html";) next = re.findall('src=".*html"',source.read()) print next """ This returns the following: "['src="nothing_left.html"', 'src="testindex.html"', 'src="nothing_right.html"']" This is a good start (and it took me long enough! :P), but, ideally, the re would strip out the 'src=' as well. Does anybody with more re-fu than me know how I could do that? Incidentally, feel free to use that page as an example. In addition, I am aware that this will need to be adjusted and expanded later on, but it's a start. Thanks in advance, Dan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Website Retrieval Program
Daniel Watkins wrote: >I'm currently trying to write a script that will get all the files >necessary for a webpage to display correctly, followed by all the >intra-site pages and such forth, in order to try and retrieve one of the >many sites I have got jumbled up on my webspace. After starting the >writing, someone introduced me to wget, but I'm continuing this because >it seems like fun (and that statement is the first step on a slippery >slope :P). > >My script thus far reads: >""" >import re >import urllib > >source = urllib.urlopen("http://www.aehof.org.uk/real_index2.html";) >next = re.findall('src=".*html"',source.read()) >print next >""" > >This returns the following: >"['src="nothing_left.html"', 'src="testindex.html"', >'src="nothing_right.html"']" > >This is a good start (and it took me long enough! :P), but, ideally, the >re would strip out the 'src=' as well. Does anybody with more re-fu than >me know how I could do that? > >Incidentally, feel free to use that page as an example. In addition, I >am aware that this will need to be adjusted and expanded later on, but >it's a start. > >Thanks in advance, >Dan > >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > Well, you don't necessarily need re-fu to do that: import re import urllib source = urllib.urlopen("http://www.aehof.org.uk/real_index2.html";) next = [i[4:] for i in re.findall('src=".*html"',source.read())] print next gives you ['"nothing_left.html"', '"testindex.html"', '"nothing_right.html"'] And if you wanted it to specifically take out "src=" only, I'm sure you could tailor it to do something with i[i.index(...):] instead. -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try except continue (fwd)
-- Forwarded message -- Date: Wed, 24 Aug 2005 11:24:44 -0700 From: [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: Danny Yoo <[EMAIL PROTECTED]> Subject: Re: [Tutor] try except continue hi Danny, I finally had a chance to review your explanation of continue you wrote a while back, and I think I understand it. You say the program will filter empty lines from standard input. I expected the behavior to be such that, upon running the program, and typing: hello Danny and pressing ctrl-d, I would get: hello Danny Instead, I got the former output, which surprised me. Why doesn't your program work as I expected ? I then wondered what your program would do if I slightly modified it. *grin* import sys for line in sys.stdin: print line This time, running the same former test, I got the following output: hello Danny As you can see, there are three empty lines now, not just one ! Where did the extra empty lines come from ? On 7/28/05, Danny Yoo <[EMAIL PROTECTED]> wrote: > > > Hi Tpc, > > I should have written an example of 'continue' usage to make things more > explicit. Here is a simple example: > > ### > """Filters empty lines out of standard input.""" > import sys > for line in sys.stdin: > if not line.strip(): > continue > print line > ## > > This is a small program that filters out empty lines from standard input. > The 'continue' statement causes us to immediately jump back to the > beginning of the loop and to keep going. > > > > > In Python, continue is intrinstically tied with Python's looping > operations (while/for). > > http://www.python.org/doc/ref/continue.html > > It has very little to do with exception handling, except where the > footnotes mentions certain artificial limitations in using it within an > exception-handling block. > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Website Retrieval Program
On 8/24/05, Daniel Watkins <[EMAIL PROTECTED]> wrote: > I'm currently trying to write a script that will get all the files > necessary for a webpage to display correctly, followed by all the > intra-site pages and such forth, in order to try and retrieve one of the > many sites I have got jumbled up on my webspace. After starting the > writing, someone introduced me to wget, but I'm continuing this because > it seems like fun (and that statement is the first step on a slippery > slope :P). > > My script thus far reads: > """ > import re > import urllib > > source = urllib.urlopen("http://www.aehof.org.uk/real_index2.html";) > next = re.findall('src=".*html"',source.read()) > print next > """ > > This returns the following: > "['src="nothing_left.html"', 'src="testindex.html"', > 'src="nothing_right.html"']" > > This is a good start (and it took me long enough! :P), but, ideally, the > re would strip out the 'src=' as well. Does anybody with more re-fu than > me know how I could do that? > > Incidentally, feel free to use that page as an example. In addition, I > am aware that this will need to be adjusted and expanded later on, but > it's a start. > > Thanks in advance, > Dan > You may wish to have a look at the Beautiful Soup module, http://www.crummy.com/software/BeautifulSoup/ Luis. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Website Retrieval Program
Daniel Watkins wrote: > I'm currently trying to write a script that will get all the files > necessary for a webpage to display correctly, followed by all the > intra-site pages and such forth, in order to try and retrieve one of the > many sites I have got jumbled up on my webspace. After starting the > writing, someone introduced me to wget, but I'm continuing this because > it seems like fun (and that statement is the first step on a slippery > slope :P). If you are not set on writing this yourself, you might be interested in Naja and HarvestMan, both of which seem intended for this purpose. http://www.keyphrene.com/ http://harvestman.freezope.org/ Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] try except continue (fwd)
The problem with the original solutions is that strings are immutable so if not line.strip(): continue doesn't actually remote the new line from the end so when you do print line you get two new lines: one from the original and one from the print command. You either need import sys for line in sys.stdin: if not line.strip(): continue print line, #<-- note the comma or my preference import sys for line in sys.stdin: line = line.strip() if not line: continue print line Jeff -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Wednesday, August 24, 2005 2:29 PM To: Tutor Subject: Re: [Tutor] try except continue (fwd) -- Forwarded message -- Date: Wed, 24 Aug 2005 11:24:44 -0700 From: [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: Danny Yoo <[EMAIL PROTECTED]> Subject: Re: [Tutor] try except continue hi Danny, I finally had a chance to review your explanation of continue you wrote a while back, and I think I understand it. You say the program will filter empty lines from standard input. I expected the behavior to be such that, upon running the program, and typing: hello Danny and pressing ctrl-d, I would get: hello Danny Instead, I got the former output, which surprised me. Why doesn't your program work as I expected ? I then wondered what your program would do if I slightly modified it. *grin* import sys for line in sys.stdin: print line This time, running the same former test, I got the following output: hello Danny As you can see, there are three empty lines now, not just one ! Where did the extra empty lines come from ? On 7/28/05, Danny Yoo <[EMAIL PROTECTED]> wrote: > > > Hi Tpc, > > I should have written an example of 'continue' usage to make things > more explicit. Here is a simple example: > > ### > """Filters empty lines out of standard input.""" > import sys > for line in sys.stdin: > if not line.strip(): > continue > print line > ## > > This is a small program that filters out empty lines from standard > input. The 'continue' statement causes us to immediately jump back to > the beginning of the loop and to keep going. > > > > > In Python, continue is intrinstically tied with Python's looping > operations (while/for). > > http://www.python.org/doc/ref/continue.html > > It has very little to do with exception handling, except where the > footnotes mentions certain artificial limitations in using it within > an exception-handling block. > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Recursive function calling
Hello, Sorry in advance for the long email. I have a two-part script. The first parts creates a structure made of nested lists. Some of these lists have only strings, other have other lists. Anyway the point is to ultimately write this structure as a XML file. Here is the script that builds the structure: for oCharacter in oCharacters: # -- # < bb_file > aFileTag = [] aFileTag.append( 'bb_file' ) aFileTag.append( [ ( 'type', '"bb_characterxml"' ), ( 'character_name', '""' ), ( 'character_version', '""' ), ( 'syntax_version', '"1.0"' ) ] ) # -- # < model > aModelTag = [] aModelTag.append( 'model' ) aModelTag.append( [] ) # No options aModelTag.append( [ str( oCharacter.fullname ) ] ) # -- # < objects > aObjectsTag = [] aObjectsTag.append( 'objects' ) aObjectsTag.append( [] ) # No options # -- # < object > # Get renderable character children oChildren = xsifactory.CreateObject( 'XSI.Collection' ) oChildren.additems( oCharacter.findchildren() ) oRenderable = xsiHandler.process( 20, oChildren, '01_02_01', (1,0), 23, bVerbose ) for oObject in oRenderable: aObjectTag = [] aObjectTag.append( 'object' ) aObjectTag.append( [ ( 'objectname', '"' + str( oObject.name ) + '"' ) ] ) # -- # < material > aMaterialTag = [] aMaterialTag.append( 'material' ) aMaterialTag.append( [ ( 'stylename', '"styleX"' ) ] ) aMaterialTag.append( [ str( oObject.material.name ) ] ) aObjectTag.append( aMaterialTag ) # -- # < clusters > if hasattr( oObject.activeprimitive.geometry, 'clusters' ) == False: pass else: aClustersTag = [] aClustersTag.append( 'clusters' ) aClustersTag.append( [] ) # No options # < cluster > aClusterTags = [] for oCluster in oObject.activeprimitive.geometry.clusters: # < cluster > aClusterTag = [] aClusterTag.append( 'cluster' ) aClusterTag.append( [ ( 'clustername', str( oCluster.name ) ), ( 'clustertype', str( oCluster.type ) ) ] ) # Check if cluster has material if oCluster.localproperties.count > 0: for oLocalProp in oCluster.localproperties: if oCluster.type == 'poly' and oLocalProp.type == c.siMaterialType: # < clustermaterial > aClusterMaterialTag = [] aClusterMaterialTag.append( 'clustermaterial' ) aClusterMaterialTag.append( [ ( 'stylename', '"styleX"' ) ] ) aClusterMaterialTag.append( [ str( oLocalProp.name ) ] ) aClusterTag.append( [ aClusterMaterialTag ] ) elif oCluster.type == 'sample' and oLocalProp.type == c.siClsUVSpaceTxtType: #
[Tutor] Working with files
How do I use the built in file objects to insert text into a file at a certain location? i.e. something, 2, chance, weee nothing, happened, crap, nice need to search for "something" and insert, "what," before it thanks for the feedback you guys are great :) -Scott Oertel -Py2.4 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
At 02:55 PM 8/24/2005, Scott Oertel wrote: >How do I use the built in file objects to insert text into a file at a >certain location? > >i.e. > >something, 2, chance, weee >nothing, happened, crap, nice > > need to search for "something" and insert, "what," before it Here's the algorithm. If you know enough Python you will be able to code it. So put together a program, give it a try and come back with questions. read the file into a string variable (assuming the file is not humungus) find the location of "something" in the string assemble a new string consisting of: the original string up to the location (index) of "something" "what" the rest of the original string write the new string to the file Bob Gailer 303 442 2625 home 720 938 2625 cell ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Hello
all, Hello... I just finished a class given by Mark Lutz and I love python. Now I need to find a project to hone my skills. I am sure I will be sending lots of questions to this list. I used to use perl but now Its gone. I am now a Python guy... Hail Guido __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello
In Python that's Guido.Hail('All') -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Eric Walker Sent: Wednesday, August 24, 2005 16:49 To: tutor@python.org Subject: [Tutor] Hello all, Hello... I just finished a class given by Mark Lutz and I love python. Now I need to find a project to hone my skills. I am sure I will be sending lots of questions to this list. I used to use perl but now Its gone. I am now a Python guy... Hail Guido __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ 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] Working with files
Bob Gailer wrote: > read the file into a string variable (assuming the file is not humungus) > find the location of "something" in the string > assemble a new string consisting of: >the original string up to the location (index) of "something" >"what" >the rest of the original string > write the new string to the file Hi Scott, Bob gave you the basic instructions that you need to complete the task that you are asking for. If you don't know how to do this, I would suggest that you start with the following Python tutorial: http://www.greenteapress.com They provide an excellent tutorial for learning the basics of Python -- and best of all, it's free and written for absolute beginners. Take care, Byron --- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Importing modules/classes
Hi Am trying to get my head around classes in python. I have a file dummy_class.py with a class definition in it, as foloows class dummy_class: def __init__(self): print "__init__" def run(self): print "run" Now, I have another file test_dummy.py, which only has the foll 2 lines import dummy_class d=dummy_class() When I run this file (via IDLE), I get the foll error: Traceback (most recent call last): File "H:/Docs/PyScripts/test_dummy_class.py", line 3, in -toplevel- d=dummy_class() TypeError: 'module' object is not callable However, if the class definition was part of the same file (test_dummy.py), instead of importing it, it runs as expected. Why does this happen? Cheers Hans ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
Byron wrote: Bob Gailer wrote: read the file into a string variable (assuming the file is not humungus) find the location of "something" in the string assemble a new string consisting of: the original string up to the location (index) of "something" "what" the rest of the original string write the new string to the file Hi Scott, Bob gave you the basic instructions that you need to complete the task that you are asking for. If you don't know how to do this, I would suggest that you start with the following Python tutorial: http://www.greenteapress.com They provide an excellent tutorial for learning the basics of Python -- and best of all, it's free and written for absolute beginners. Take care, Byron --- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Thanks again, I figured it out with the index function of the file objects, I didn't know that you could search for a word and find the exact pos of it, that was the tiny bit of information I was looking for. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello
You know, I got that backwards and I can't tell you how much it's bugging me! All.Hail('Guido') Thanks. John -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of John Purser Sent: Wednesday, August 24, 2005 16:52 To: 'Eric Walker'; tutor@python.org Subject: Re: [Tutor] Hello In Python that's Guido.Hail('All') -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Eric Walker Sent: Wednesday, August 24, 2005 16:49 To: tutor@python.org Subject: [Tutor] Hello all, Hello... I just finished a class given by Mark Lutz and I love python. Now I need to find a project to hone my skills. I am sure I will be sending lots of questions to this list. I used to use perl but now Its gone. I am now a Python guy... Hail Guido __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ 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] Importing modules/classes
> class dummy_class: > def __init__(self): > print "__init__" > > def run(self): > print "run" > > > Now, I have another file test_dummy.py, which only has the foll 2 lines > > import dummy_class > d=dummy_class() Hi Hans, In Python, modules are containers. They can contain possibly more than one thing, so you need to make sure to fully qualify the class: import dummy_class d = dummy_class.dummy_class() Does this make sense? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError and appending to lists [Was: Re: Need Helpon Assignment]
Alan, Thanks for your comments. I see how I can simplify my code, but I've run into a problem trying to do so. If I replace the "while" loop with a "for" loop as you suggest, the program won't enter the loop unless "s" is initialized so that it's in "input". How do I do that? Also, near the end of your remarks you say that the code at the bottom of my program doesn't do anything. It does for me. Since I'm trying to learn Python, I'm not too sure that what I'm doing is correct. Thus, I put in those print statements to verify that the contents of those variables are what I expected them to be. That's all that mess is for. Thanks for your help and Danny's too! Tom Alan G wrote: > Hi Tom, > > Glad you got it working with Danny's help. > > I'll throw in some style points too. > >> input = open('/home/tom/Python/Input/SPY3.txt', 'r') > > >> N=0 >> s = 'boo' >> while s: >>s = input.readline() >>if s == '':break > > > You might find it easier to use a for loop. > > you could for example use > > for s in input: > > > to replace most of the above code and, in the process, eliminate the > need for N completely, see below... > > >>s = s[:-2] >>T[N] = s.split(',') > > > Since you are using append elsewhere why not for T too? > And if you store the split result in a local value before putting it > into T you can use that variable in all the following appends to save > indexing T... > >>date.append(T[N][0]) >>open.append(float(T[N][1])) >>hi.append(float(T[N][2])) >>lo.append(float(T[N][3])) >>close.append(float(T[N][4])) >>vol.append(float(T[N][5])) > > >>N+=1 > > > And with the for loop you don;t need to increment N either. > >> print N >> for i in range(N): > > > And you can use len(T) to replace N here. > >>T[i] > > > This doesn't do anything! :-) > >>print T[i] >>print date[i], open[i], hi[i], lo[i], close[i], vol[i] >> print T[1][2], T[0][0] >> z = (hi[2] +lo[2])/2.0 >> print z > > > HTH, > > Alan G > Author of the Learn to Program web tutor > http://www.freenetpages.co.uk/hp/alan.gauld > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello
On Wed, 24 Aug 2005 16:49:17 -0700 (PDT) Eric Walker <[EMAIL PROTECTED]> wrote: > all, > Hello... I just finished a class given by Mark Lutz > and I love python. Now I need to find a project to > hone my skills. I am sure I will be sending lots of > questions to this list. I used to use perl but now > Its gone. I am now a Python guy... Hail Guido How was the class? I am taking a class that Mark Lutz is teaching in October. Is it worth it? Thanks -- JLands Arch Current(Laptop) Slackware 9.1(Server) Slackware Current(Desktop) Registered Linux User #290053 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing modules/classes
Me again :) Just to make sure that I understand it right, 1) the __init__ method in a class is invoked when a object is instantiated from a class 2) the run method is invoked when a class derived from "threading.Thread" is "start"ed Is that right? The snippet below, ><-- import threading class show_num(threading.Thread): def __init__(self, num): print "__init__: Num = ", num def run(self): print "run" show_num_thread = show_num(742) show_num_thread.start() ---><- Throws an error >>> __init__: Num = 742 Traceback (most recent call last): File "H:/Docs/PyScripts/test_thread_1.py", line 12, in -toplevel- show_num_thread.start() AssertionError: Thread.__init__() not called >>> Which __init__ method is it referring to? Thanks in advance for your help (and time). U guys are awesome :) -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Thursday, 25 August 2005 12:17 p.m. To: Hans Dushanthakumar Cc: Tutor Subject: Re: [Tutor] Importing modules/classes > class dummy_class: > def __init__(self): > print "__init__" > > def run(self): > print "run" > > > Now, I have another file test_dummy.py, which only has the foll 2 > lines > > import dummy_class > d=dummy_class() Hi Hans, In Python, modules are containers. They can contain possibly more than one thing, so you need to make sure to fully qualify the class: import dummy_class d = dummy_class.dummy_class() Does this make sense? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Differences in running a multithreaded script under IDLE and otherwise
Hi, While running the foll script by double-clicking it (under WinXP), it runs as expected. However, when I run it via IDLE, it hangs after a few secs (no runtime errors - just hangs). Why does this happen? Cheers Hans import threading class incr_num(threading.Thread): num = '' def __init__(self, local_num): global num threading.Thread.__init__(self) num = local_num print "__init__: ", num def run(self): global num for k in range (20): print "run: ", num num = num + 1 incr_num_thread = incr_num(501) incr_num_thread.start() print "Wait for thread to finish" incr_num_thread.join() print "Thread finished" raw_input("Press enter") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] checking substrings in strings
Hello, I would like to check if a certain word exists in a given string. since I learned to love lists, I used if myword in mystring: ...didnt work. I have now resorted to if mystring.find(myword) >1: is this really the canonical way to check if myword exists in mystring? it feels somewhat "unpythonic", thats why I'm asking thanks for any insight you might have -frank ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Handling binary file
I have opened a file in binary mode. The 9th, 10th and 11th bytes contain the time in seconds. In order to get this value in decimal I did the following: timeinsec = bytes[9] * 65536 + bytes[10] * 256 + bytes{11] Would someone please advise if there is a better way to do this? Thanks, Julie.___ 想即時收到新 email 通知? 下載 Yahoo! Messenger http://messenger.yahoo.com.hk ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IndexError and appending to lists [Was: Re: Need Helpon Assignment]
> "for" loop as you suggest, the program won't enter the loop unless > "s" is initialized so that it's in "input". How do I do that? for s in input: means that s takes on each value in input. input is your file. Thus s takes on the value of each line in the input file. You don't need to initialise s before entering the loop as you would with a while loop. Similarly you don't need to test for the end of the file, 'for' does all that too. Take a look at the 'Loops' topic and then the 'Handling Files' topic in my tutorial for more info on this. > Also, near the end of your remarks you say that the code at the > bottom of my program doesn't do anything. It does for me. Lets take a look: >>> print N >>> for i in range(N): >>>T[i]< This does nothing >>>print T[i] The line that simply has the value in it will not do anything. It will not print out the value, you need to call print for that to happen. > put in those print statements to verify that the contents of those > variables are what I expected them to be. That's all that mess is > for. The debug/test print statements are fine, I was only pointing out that one line did nothing, not the entire block. HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing modules/classes
> Am trying to get my head around classes in python. Looks like you have the classes bit figured out! :-) > import dummy_class > d=dummy_class() But you have a problem with namespaces. the class 'dummy_class' is inside the module 'dummy_class' So when you import the module that allows you to use the name 'dummy_class' to refer to the *contents* of that module. To access the class 'dummy_class' you need to prepend it with the name of the module: d = dummy_class.dummy_class() You can get more on this in the 'Whats in a Name?' topic of my tutor. And more on OOP in the OOP topic - naturally! :-) Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor