Re: [Tutor] Just bought Python in a Nutshell
Lamonte Harris wrote: http://www.powells.com/biblio/63-9780596001889-7 Used, has anyone read this book. Any additional information that you like,dislike about this book? [I like having real books and stead of ebooks because its better on the eyes.] Should be her 2morrow Afternoon :), few hours before I get home great deal :D. You have just purchased the most comprehensive language reference and instructional manual currently available, written by an acknowledged expert whose pedantry ensures an excruciating level of correctness in the text. It's a well-written book, and contains enough information that almost every Python programmer will find it a useful addition to his or her bookshelf. You will enjoy it whether you choose to read from the beginning or just dip in. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden --- Asciimercial -- Get on the web: Blog, lens and tag the Internet Many services currently offer free registration --- Thank You for Reading - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How can I extend my vocabulary that fits well with python.
Lamonte Harris [EMAIL PROTECTED] wrote See theres a lot of words that I know and some that I don't know, how can I extend and improve my python vocabulary so I can interpret information in a faster manor. Makes things easier to understand if you actually understand the things the people are saying in tutorials,etc.. It might help if you give examples of words you don't understand. In fact just posting to this list asking about any such word will usually get you an answer. Or try Wikipedia. However if you are referring to the jargon of programming, things like source code, compiling, etc then you may find the introductory section of my tutorial useful. I try to explain all jargon the first time I use it... And one of my aims was to ensure the student was introduced to the kind of language they would encounter on the 'net. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] list iteration question for writing to a file on disk
Hi can someone help with this please? i got to this point with help from the list. from BeautifulSoup import BeautifulSoupdoc = ['htmlheadtitlePage title/title/head', 'bodyp id=firstpara align=centerThis is paragraph bone/b.', 'p id=secondpara align=blahThis is paragraph btwo/b.', 'a href=http://www.google.co.uk;/a', 'a href=http://www.bbc.co.uk;/a', 'a href=http://www.amazon.co.uk;/a', 'a href=http://www.redhat.co.uk;/a', '/html']soup = BeautifulSoup(''.join(doc))alist = soup.findAll('a') import urlparsefor a in alist:href = a['href']print urlparse.urlparse(href)[1] so BeautifulSoup used to find a tags; use urlparse to extract to fully qualified domain name use print to print a nice list of hosts 1 per line. here www.google.co.ukwww.bbc.co.ukwww.amazon.co.ukwww.redhat.co.uk nice, so i think write them out to a file; change program to this to write to disk and read them back to see what's been done. from BeautifulSoup import BeautifulSoupdoc = ['htmlheadtitlePage title/title/head', 'bodyp id=firstpara align=centerThis is paragraph bone/b.', 'p id=secondpara align=blahThis is paragraph btwo/b.', 'a href=http://www.google.co.uk;/a', 'a href=http://www.bbc.co.uk;/a', 'a href=http://www.amazon.co.uk;/a', 'a href=http://www.redhat.co.uk;/a', '/html']soup = BeautifulSoup(''.join(doc))alist = soup.findAll('a') import urlparseoutput = open(fqdns.txt,w) for a in alist:href = a['href']output.write(urlparse.urlparse(href)[1]) output.close() this writes out www.google.co.ukwww.bbc.co.ukwww.amazon.co.ukwww.redhat.co.uk so I look in Alan's tutor pdf for issue and read page 120 where it suggests doing this; outp.write(line + '\n') # \n is a newline so i change my line from this output.write(urlparse.urlparse(href)[1]) to this output.write(urlparse.urlparse(href)[1] + \n) I look at the output file and I get this www.google.co.ukwww.bbc.co.ukwww.amazon.co.ukwww.redhat.co.uk hooray I think, so then I open the file in the program to read each line to do something with it. i pop this after the last output.close() input = open(fqdns.txt,r)for j in input:print j input.close() but his prints out www.google.co.uk www.bbc.co.uk www.amazon.co.uk www.redhat.co.uk Why do i get each record with an extra new line ? Am I writing out the records incorrectly or am I handling them incorrectly when I open the file and print do I have to take out newlines as I process? any help would be great s _ Feel like a local wherever you go. http://www.backofmyhand.com___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Killing an instance
Ara Kooser wrote: I have two instances called and running. They interact with each other and I would like one of the instances to cease to exist in the second round based on a given condition. How do you kill an instance? What you really need is for your main code to stop using the instance. You could write it like this: if one.isAlive(): one.lys_re(lys) if two.isAlive(): two.ade_re(ade) etc. But your two classes are so similar, I would try to merge them into one class and have the behaviour (which aa they consume or release) determined by configuration. Then you can keep the instances in a list and treat them uniformly. Something like yeasts = [Yeast(Red,alpha, lys, ade), Yeast(Yellow,alpha, ade, lys)] #Game logic while count rounds: for yeast in yeasts: yeast.release() print print Chemicals in the environment,chemicals print time.sleep(1) for yeast in yeasts: yeast.consume() # This step removes dead yeasts from the list yeasts = [yeast for yeast in yeasts if yeast.isAlive()] def ade_need(self,stuffb): if stuffb != chemicals: I'm not sure what you think you are testing, but this will always be true; stuffb is a string and chemicals is a list, they will never be equal. I think you want if stuffb in chemicals: Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting one line in multiple files
On 9/13/07, wormwood_3 [EMAIL PROTECTED] wrote: I think the problem is that the original script you borrowed looks at the file passed to input, and iterates over the lines in that file, removing them if they match your pattern. What you actually want to be doing is iterating over the lines of your list file, and for each line (which represents a file), you want to open *that* file, do the check for your pattern, and delete appropriately. Hope I am not completely off:-) This is exactly what I'd like to do. =) After manually opening about 25 files individually and deleting the line that I wanted to delete, and seeing about 175 files left to finish, I thought to myself, 'I'm learning Python! Python is supposed to be really good at this kind of stuff.' So, since there isn't a rush deadline for this project, I figured I could play around and see what kinds of solutions I could find. The 'fileinput' snippet is one solution, but I'd rather be able to pass it a list of filenames to work on, rather than have to manually change the filename in the snippet each time. The files are numbered, in order, from 0001 to 0175, (filename0001.html to filename0175.html). One thought was to be able to change the filename number incrementally, assign it to a variable, and run it through a for loop? Isn't it amazing how a Newbie approaches a problem? =) I'm also looking into 'sed' for doing this. I've used 'sed' in the past for deleting a specific line from files, as well as doing simple search and replace in a file. I just figured that if it can be done in 'sed,' it can be done in Python much easier and maybe even more elegantly (although at this point in my Python career, elegance isn't a top priority). Happy Programming! -- bhaaluu at gmail dot com If I am right so far, you want to do something like: import fileinput for file in fileinput.input(filelist.list, inplace=1): curfile = file.open() for line in curfile: line = line.strip() if not 'script type'in line: print line BUT, fileinput was made (if I understand the documentation) to avoid having to do this. This is where the sys.argv[1:] values come in. The example on this page (look under Processing Each Line of One or More Files: The fileinput Module) helped clarify it to me: http://www.oreilly.com/catalog/lpython/chapter/ch09.html. If you do: % python myscript.py script type `ls` This should pass in all the items in the folder you run this in (be sure it only contains the files you want to edit!), looking for script type. Continuing with the O'Reilly example: import fileinput, sys, string # take the first argument out of sys.argv and assign it to searchterm searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:] for line in fileinput.input(): num_matches = string.count(line, searchterm) if num_matches: # a nonzero count means there was a match print found '%s' %d times in %s on line %d. % (searchterm, num_matches, fileinput.filename(), fileinput.filelineno()) To test this, I put the above code block in mygrep.py, then made a file test.txt in the same folder, with some trash lines, and 1 line with the string you said you want to match on. Then I did: [EMAIL PROTECTED]:~$ python mygrep.py script type test.txt found 'script type' 1 times in test.txt on line 3. So you could use the above block, and edit the print line to also edit the file as you want, maybe leaving the print to confirm it did what you expect. Hope this helps! -Sam _ I have a directory of files, and I've created a file list of the files I want to work on: $ ls file.list Each file in file.list needs to have a line removed, leaving the rest of the file intact. I found this snippet on the Net, and it works fine for one file: # the lines with 'script type' are deleted. import fileinput for line in fileinput.input(file0001.html, inplace=1): line = line.strip() if not 'script type'in line: print line The docs say: This iterates over the lines of all files listed in sys.argv[1:]... I'm not sure how to implement the argv stuff. However, the documentation also states: To specify an alternative list of filenames, pass it as the first argument to input(). A single file name is also allowed. So, when I replace file0001.html with file.list (the alternative list of filenames, nothing happens. # the lines with 'script type' are deleted. import fileinput for line in fileinput.input(file.list, inplace=1): line = line.strip() if not 'script type'in line: print line file.list has one filename on each line, ending with a newline. file0001.html file0002.html ::: ::: file0175.html Have I interpreted the documentation wrong? The goal is to delete the line that has 'script type' in it. I can supply more information if needed. TIA. -- bhaaluu at gmail dot com
Re: [Tutor] deleting one line in multiple files
bhaaluu wrote: On 9/13/07, wormwood_3 [EMAIL PROTECTED] wrote: I think the problem is that the original script you borrowed looks at the file passed to input, and iterates over the lines in that file, removing them if they match your pattern. What you actually want to be doing is iterating over the lines of your list file, and for each line (which represents a file), you want to open *that* file, do the check for your pattern, and delete appropriately. This is exactly what I'd like to do. =) After manually opening about 25 files individually and deleting the line that I wanted to delete, and seeing about 175 files left to finish, I thought to myself, 'I'm learning Python! Python is supposed to be really good at this kind of stuff.' Good idea! Python is excellent for this kind of stuff. The 'fileinput' snippet is one solution, but I'd rather be able to pass it a list of filenames to work on, rather than have to manually change the filename in the snippet each time. The files are numbered, in order, from 0001 to 0175, (filename0001.html to filename0175.html). Sam has already given you one solution - to pass the list of filenames on the command line. The first argument to fileinput.input() can be a list of filenames, so you could do filenames = open(filelist.list).read().splitlines() for line in fileinput.input(filenames, inplace=1): # etc One thought was to be able to change the filename number incrementally, assign it to a variable, and run it through a for loop? Isn't it amazing how a Newbie approaches a problem? =) That is arguably a better approach than reading the file, since it avoids the effort of creating the file. for i in range(1, 176): filename = 'filename%04d.html' % i for line in fileinput.input(filename, inplace=1): # etc Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting one line in multiple files
Greetings, Many thanks to wormwood_3 and Kent for their help. Summary: The working script looks like this: # the lines with 'script type' are deleted. import fileinput for i in range(1, 176): filename = 'filename%04d.html' % i for line in fileinput.input(filename, inplace=1): #for line in fileinput.input(file0001.html, inplace=1): line = line.strip() if not 'script type'in line: print line It was implemented as: $ python delete.py ...and it took less than a second to delete the line from all the files! I'm working on the files in a temporary directory, so I'm going to try the other solution as well. Happy Programming! -- bhaaluu at gmail dot com On 9/14/07, Kent Johnson [EMAIL PROTECTED] wrote: bhaaluu wrote: On 9/13/07, wormwood_3 [EMAIL PROTECTED] wrote: I think the problem is that the original script you borrowed looks at the file passed to input, and iterates over the lines in that file, removing them if they match your pattern. What you actually want to be doing is iterating over the lines of your list file, and for each line (which represents a file), you want to open *that* file, do the check for your pattern, and delete appropriately. This is exactly what I'd like to do. =) After manually opening about 25 files individually and deleting the line that I wanted to delete, and seeing about 175 files left to finish, I thought to myself, 'I'm learning Python! Python is supposed to be really good at this kind of stuff.' Good idea! Python is excellent for this kind of stuff. The 'fileinput' snippet is one solution, but I'd rather be able to pass it a list of filenames to work on, rather than have to manually change the filename in the snippet each time. The files are numbered, in order, from 0001 to 0175, (filename0001.html to filename0175.html). Sam has already given you one solution - to pass the list of filenames on the command line. The first argument to fileinput.input() can be a list of filenames, so you could do filenames = open(filelist.list).read().splitlines() for line in fileinput.input(filenames, inplace=1): # etc One thought was to be able to change the filename number incrementally, assign it to a variable, and run it through a for loop? Isn't it amazing how a Newbie approaches a problem? =) That is arguably a better approach than reading the file, since it avoids the effort of creating the file. for i in range(1, 176): filename = 'filename%04d.html' % i for line in fileinput.input(filename, inplace=1): # etc Kent -- bhaaluu at gmail dot com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting one line in multiple files
Sed is a little easier than python for this project, but python is more flexible. Sed would be sed -e s/^.*\script line.*$//g file000.lst That would leave a blank line where your script line was. To run this command multiple times, you'd use xargs: ls filename* | xargs -I fn -n 1 sed -e s/^.*\script line.*$//g fn fn.modified This will take each file that starts with filename, strip out the line that has script line in it, then rename it to fn.modified. A second application of xargs will easily rename all the .modified files back to the original name ls filename* | xargs -I fn -n 1 mv fn.modified fn So your total sed solution is: ls filename* | xargs -I fn -n 1 sed -e s/^.*\script line.*$//g fn fn.modified ls filename* | xargs -I fn -n 1 mv fn.modified fn As far as python goes, use of import sys.argv and xargs will get you to the same place, but a more flexible solution. --Michael As for the python one, you want to import sys.args -- Michael Langford Phone: 404-386-0495 Consulting: http://www.TierOneDesign.com/ Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com On 9/14/07, bhaaluu [EMAIL PROTECTED] wrote: On 9/13/07, wormwood_3 [EMAIL PROTECTED] wrote: I think the problem is that the original script you borrowed looks at the file passed to input, and iterates over the lines in that file, removing them if they match your pattern. What you actually want to be doing is iterating over the lines of your list file, and for each line (which represents a file), you want to open *that* file, do the check for your pattern, and delete appropriately. Hope I am not completely off:-) This is exactly what I'd like to do. =) After manually opening about 25 files individually and deleting the line that I wanted to delete, and seeing about 175 files left to finish, I thought to myself, 'I'm learning Python! Python is supposed to be really good at this kind of stuff.' So, since there isn't a rush deadline for this project, I figured I could play around and see what kinds of solutions I could find. The 'fileinput' snippet is one solution, but I'd rather be able to pass it a list of filenames to work on, rather than have to manually change the filename in the snippet each time. The files are numbered, in order, from 0001 to 0175, (filename0001.html to filename0175.html). One thought was to be able to change the filename number incrementally, assign it to a variable, and run it through a for loop? Isn't it amazing how a Newbie approaches a problem? =) I'm also looking into 'sed' for doing this. I've used 'sed' in the past for deleting a specific line from files, as well as doing simple search and replace in a file. I just figured that if it can be done in 'sed,' it can be done in Python much easier and maybe even more elegantly (although at this point in my Python career, elegance isn't a top priority). Happy Programming! -- bhaaluu at gmail dot com If I am right so far, you want to do something like: import fileinput for file in fileinput.input(filelist.list, inplace=1): curfile = file.open() for line in curfile: line = line.strip() if not 'script type'in line: print line BUT, fileinput was made (if I understand the documentation) to avoid having to do this. This is where the sys.argv[1:] values come in. The example on this page (look under Processing Each Line of One or More Files: The fileinput Module) helped clarify it to me: http://www.oreilly.com/catalog/lpython/chapter/ch09.html. If you do: % python myscript.py script type `ls` This should pass in all the items in the folder you run this in (be sure it only contains the files you want to edit!), looking for script type. Continuing with the O'Reilly example: import fileinput, sys, string # take the first argument out of sys.argv and assign it to searchterm searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:] for line in fileinput.input(): num_matches = string.count(line, searchterm) if num_matches: # a nonzero count means there was a match print found '%s' %d times in %s on line %d. % (searchterm, num_matches, fileinput.filename(), fileinput.filelineno()) To test this, I put the above code block in mygrep.py, then made a file test.txt in the same folder, with some trash lines, and 1 line with the string you said you want to match on. Then I did: [EMAIL PROTECTED]:~$ python mygrep.py script type test.txt found 'script type' 1 times in test.txt on line 3. So you could use the above block, and edit the print line to also edit the file as you want, maybe leaving the print to confirm it did what you expect. Hope this helps! -Sam _ I have a directory of files, and I've created a file list of the files I want to work on: $ ls file.list Each file in file.list needs to have a line
[Tutor] remove blank list items
Hi i was expanding my program to write urls parsed from a html page and write them to a file so i chose www.icq.com to extract the urls from. when i wrote these out to a file and then read the file back I noticed a list of urls then some blank lines then some more urls then some blank lines, does this mean that one of the functions called has for some reason added some whitespace into some of the list items so that i wrote them out to disk? I also noticed that there are duplicate hosts/urls that have been written to the file. So my two questions are; 1. how and where do I tackle removing the whitespace from being written out to disk? 2. how do i tackle checking for duplicate entries in a list before writing them out to disk? My code is below from BeautifulSoup import BeautifulSoupimport urllib2import urlparse file = urllib2.urlopen(http://www.icq.com;) soup = BeautifulSoup(''.join(file))alist = soup.findAll('a') output = open(fqdns.txt,w) for a in alist:href = a['href']output.write(urlparse.urlparse(href)[1] + \n) output.close() input = open(fqdns.txt,r) for j in input:print j, input.close() the chopped output is here chat.icq.comchat.icq.comchat.icq.comchat.icq.comchat.icq.com labs.icq.comdownload.icq.comgreetings.icq.comgreetings.icq.comgreetings.icq.comgames.icq.comgames.icq.com _ Celeb spotting – Play CelebMashup and win cool prizes https://www.celebmashup.com___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] remove blank list items
First off: What you want is a set, not a list. Lucky for you, a python dict uses a set for its keys, so cheat and use that. change: for j in input: print j, to mySet={} for j in input: mySet[j]=j for item in mySet.keys(): print item Secondly, to not print blank lines, strip items and only add them if they aren't == change: for j in input: print j, to mySet={} for j in input: cleaned = j.strip() if j!=: mySet[cleaned]=cleaned for item in mySet.keys(): print item On 9/14/07, sacha rook [EMAIL PROTECTED] wrote: Hi i was expanding my program to write urls parsed from a html page and write them to a file so i chose www.icq.com to extract the urls from. when i wrote these out to a file and then read the file back I noticed a list of urls then some blank lines then some more urls then some blank lines, does this mean that one of the functions called has for some reason added some whitespace into some of the list items so that i wrote them out to disk? I also noticed that there are duplicate hosts/urls that have been written to the file. So my two questions are; 1. how and where do I tackle removing the whitespace from being written out to disk? 2. how do i tackle checking for duplicate entries in a list before writing them out to disk? My code is below from BeautifulSoup import BeautifulSoup import urllib2 import urlparse file = urllib2.urlopen(http://www.icq.com;) soup = BeautifulSoup(''.join(file)) alist = soup.findAll('a') output = open(fqdns.txt,w) for a in alist: href = a['href'] output.write(urlparse.urlparse(href)[1] + \n) output.close() input = open(fqdns.txt,r) for j in input: print j, input.close() the chopped output is here chat.icq.com chat.icq.com chat.icq.com chat.icq.com chat.icq.com labs.icq.com download.icq.com greetings.icq.com greetings.icq.com greetings.icq.com games.icq.com games.icq.com -- Get free emoticon packs and customisation from Windows Live. Pimp My Live!http://www.pimpmylive.co.uk ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Michael Langford Phone: 404-386-0495 Consulting: http://www.TierOneDesign.com/ Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] remove blank list items
Michael Langford wrote: First off: What you want is a set, not a list. Lucky for you, a python dict uses a set for its keys, so cheat and use that. Python has had a set type in the stdlib since 2.3 and built-in since 2.4 so there is no need to cheat any more. change: for j in input: print j, to mySet={} for j in input: mySet[j]=j mySet = set() for j in input: mySet.add(j) or just mySet = set(input) for item in mySet.keys(): print item for item in mySet: print item (actually the above works for dict or set, iterating a dict gives its keys) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Inheritance
You're still using the wrong terminology... A subclass is derived (or subclassed) from its parent class (or super class) A function that is part of the class definition (def func1(self): pass) is a method A variable that is part of the class definition or added after instantiation is an attribute When you create an object (a = A()) you are instantiating the class and the resultant object is an instance Lamonte Harris wrote: So ok, when I inherit a class, all the classes methods. Classes have methods (functions) and attributes (variables). in the parent class will overwrite override. all the functions methods. in the class I inherited the parent class into? No, exactly the opposite. If they have the same class names that is? # declare a class class A(object): this = is a class attribute def method1(self): self.thisthing = is an attribute print class A: method1 def method2(self): print class A: method2 # subclass it class B(A): def method2(self): print class B: method2 def method3(self): print class B: method3 # create an instance of each class a = A() b = B() a.method1() OUTPUT classA: method1 a.method2() OUTPUT classA: method2 b.method1() OUTPUT classA: method1 b.method2() OUTPUT classB: method2 super(B,b).method2() OUTPUT classA: method2 # super() only works because B is a subclass of object by virtue or A being derived from object b.method3() OUTPUT classB: method3 a.method3() Traceback (most recent call last): File stdin, line 1, in module AttributeError: A instance has no attribute 'method3' When you derive a subclass from another class all methods of the parent class are available Is that clear? On 9/13/07, *Eric Brunson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Lamonte Harris wrote: Okay class A: def __init__(self,x,y): self.x = x self.y = y def save(self,fn): f = open(fn,w) f.write(str(self.x)+ '\n') # convert to a string and add newline f.write(str(self.y)+'\n') return f # for child objects to use def restore(self, fn): f = open(fn) self.x = int(f.readline()) # convert back to original type self.y = int(f.readline()) return f class B(A): def __init__(self,x,y,z): A.__init__(self,x,y) self.z = z def save(self,fn): f = A.save(self,fn) # call parent save f.write(str(self.z)+'\n') return f # in case further children exist def restore(self, fn): f = A.restore(self,fn) self.z = int(f.readline()) return f In the class B, I'm not understanding the A.__init(self,x,y) part. So its initializing the class A, and basically you can use the A class like normal? Essentially, yes, but the way you've worded it is imprecise. Any instance of B is a subclass of A, essentially an A: Every boy is a male, but not all males are boys. When you override a method in a subclass you have essentially turned off all behavior in the parent method, including magic methods like the constructor. You have to call the parent constructor explicitly to get its benefits. A method of an instance can be called like this: instance.method() and python makes sure that a pointer to the instance is passed in as the first argument, generally self. But a method of a class can be called without instantiating the class, but you have to supply self on you own. The method you are using is valid, yet depricated. Using new style classes (which aren't actually that new) you'd write your code like this: class A(object): def __init__(self, a): self.aye = a class B(A): def __init__(self, a, b): self.bee = b super( B, self ).__init__( a ) Notice that you don't have to supply self. You can use any method of A in an instance of B that you haven't overridden in B without having to use super(). Part im confused about is the self.z, does that belong to the class A or class B? z is an attribute B only. Hope that helps, e. Else I think I'm understanding it correctly. ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to add arguments to a command line program
lo there all, i want to write a program that will be called from another program. I need to pass it one variable. i suppose i could also do this with a module, and create a new instance of whatever i want to pass it to, but all the same, how would i go about this. like if i had a program that i wanted to pass a number into. i am sure i do it with sys args, but don't know how. like python run_my_script.py somevar does this make sense ? The other way to do this is create a class i suppose that could be called. Should probably do it that way, but still, i would like to know. thanks. shawn ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to add arguments to a command line program
See optparse http://docs.python.org/lib/module-optparse.html or sys.argv.http://effbot.org/librarybook/sys.htm On 9/14/07, shawn bright [EMAIL PROTECTED] wrote: lo there all, i want to write a program that will be called from another program. I need to pass it one variable. i suppose i could also do this with a module, and create a new instance of whatever i want to pass it to, but all the same, how would i go about this. like if i had a program that i wanted to pass a number into. i am sure i do it with sys args, but don't know how. like python run_my_script.py somevar does this make sense ? The other way to do this is create a class i suppose that could be called. Should probably do it that way, but still, i would like to know. thanks. shawn ___ 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] Problem with assigning list elements
[EMAIL PROTECTED] wrote: I'm having a problem assigning numbers to a 2-D list. for the 2d list z for example, when I type in z[0][0]=67 every zeroth element of every sublist will be set to 67, instead of just element [0,0] being set to 67. http://effbot.org/pyfaq/how-do-i-create-a-multidimensional-list.htm Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with assigning list elements
[EMAIL PROTECTED] wrote: I'm having a problem assigning numbers to a 2-D list. for the 2d list z for example, when I type in z[0][0]=67 every zeroth element of every sublist will be set to 67, instead of just element [0,0] being set to 67. If z = [[1,2],[3,4]] then z[0][0]=100 results in z= [[100,2],[100,4]] When I run that I see: z = [[1,2],[3,4]] z[0][0]=100 z [[100, 2], [3, 4]] Which is what I'd expect. Is this normal for python? And is there a way to assign to a specific element of a 2-dimensional list? Thanks ___ 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] Problem with assigning list elements
I'm having a problem assigning numbers to a 2-D list. for the 2d list z for example, when I type in z[0][0]=67 every zeroth element of every sublist will be set to 67, instead of just element [0,0] being set to 67. If z = [[1,2],[3,4]] then z[0][0]=100 results in z= [[100,2],[100,4]] Is this normal for python? And is there a way to assign to a specific element of a 2-dimensional list? Thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to add arguments to a command line program
shawn bright [EMAIL PROTECTED] wrote i am sure i do it with sys args, but don't know how. like python run_my_script.py somevar does this make sense ? Check my Talking to the User topic in my tutorial. The second half talks about using command line arguments. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting one line in multiple files
bhaaluu [EMAIL PROTECTED] wrote I'm also looking into 'sed' for doing this. I've used 'sed' in the past for deleting a specific line from files, as well as doing simple search and replace in a file. I just figured that if it can be done in 'sed,' it can be done in Python much easier and maybe even more elegantly Probably not because this is exactly the kind of task sed was designed for. And using the right tool fotr the job is usually more elegant and efficient than using a general purpose configured for the job... If the editing was more complex and involved multiple files (copying from file a to file b etc) then sed begins to creak and Python becomes much better suited. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with assigning list elements
That is not the expected behavior and not the behavior I see: Python 2.5.1 (r251:54863, May 1 2007, 13:27:01) [C] on sunos5 z = [[1,2],[3,4]] z[0][0]=100 print z [[100, 2], [3, 4]] [EMAIL PROTECTED] wrote: I'm having a problem assigning numbers to a 2-D list. for the 2d list z for example, when I type in z[0][0]=67 every zeroth element of every sublist will be set to 67, instead of just element [0,0] being set to 67. If z = [[1,2],[3,4]] then z[0][0]=100 results in z= [[100,2],[100,4]] Is this normal for python? And is there a way to assign to a specific element of a 2-dimensional list? Thanks ___ 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] how to add arguments to a command line program
Just what i was looking for, thanks shawn On 9/14/07, Alan Gauld [EMAIL PROTECTED] wrote: shawn bright [EMAIL PROTECTED] wrote i am sure i do it with sys args, but don't know how. like python run_my_script.py somevar does this make sense ? Check my Talking to the User topic in my tutorial. The second half talks about using command line arguments. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ 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] Just bought Python in a Nutshell
Right, I like reading books it comes handier then reading ebooks, less programs and its right there in your hands. Main reason I'm going to use it for is to find questions without asking them on the python list or tutor list for a quicker referrence. On 9/14/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I respectfully disagree with Shawn, in this case. Don't skim Nutshell, unless you know very little Python, and even then it is really the wrong book. It is rather dry reading and provides very little of the usual user-friendly introductions to language features by solving simple problems. Doesn't sound like that much of an endorsement, does it? Well, in fact, it is pretty much my most used Python book (out of 7 or 8 others). If you read Alex's posts in this newsgroup, you'll see that he is one of the most pragmatic and rigorous posters who usually contributes code that elegantly and simply solves the issue at hand with the minimum amount of clutter. What Python in a Nutshell is really good at is showing you exactly what Python is capable of doing, feature by feature, in a thoroughly Pythonic way for the feature. With code and exact implication. For example, I know Python well but I am kinda lacking in metaclass comprehension. If I were to write some non-trivial metaclasses I would surely have his 3 or 4 pages open on my desk as I write code and skim through other internet postings. Those 3-4 pages have kinda made my brain shrivel every time I've looked at them, but they are the clearest overview I've seen of what is probably one of the hardest Python features to understand. For normal, easy-to-understand Python, Nutshell really dissects the languages with new insight. The information is dense, because each word has its place and there very little filler. That's why skimming it does not work for me, I just don't have the requisite sustained attention span. So, although I read almost all other computer books like Shawn does, I don't think it applies in this particular case. When you have a particular aspect of Python in mind, use Nutshell. Read up on 'look before you leap' in it if you really want a sample of how it is written. Cheers -- http://mail.python.org/mailman/listinfo/python-list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] evaluating AND
On Fri, 14 Sep 2007, Rikard Bosnjakovic wrote: For me, if x would be enough. If you think it's a bad thing when x is of the wrong data, then you really should check that it contains *correct* data as well. That's an okay approach, but, but it's also non-Pythoninc; more of the look-before-you-leap approach rather than ask-forgiveness-not-permission. Using the the two function of yours, setting x to an integer: x = 2 print test01(x) Traceback (most recent call last): File stdin, line 1, in ? File /usr/tmp/python-3716vZq, line 3, in test01 TypeError: unsubscriptable object print test02(x) Traceback (most recent call last): File stdin, line 1, in ? File /usr/tmp/python-3716vZq, line 8, in test02 TypeError: unsubscriptable object which is exactly what I would want it to do: raise an exception on bad data. Rewriting your test01-function into this: def test01(x): if (type(x) == type((0,)) and (x is not None) and (length(x) == 2)): if x[0]0: return x[1]/x[0] and testing again: x = 2 print test01(x) None This silently produces an incorrect result, which is a Bad Thing; and it took a lot more code to do it, too. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How do I add uvs to each vert line if a texture/bump map is used?
C3DModel 4 2 0,50 Cube 1 1 8,1,12,0 v1 1.00,0.500,0.500,0,1,NULL.TIF null.bump.tif -7.028856,3.349522,4.785803,0.23,0.46, -0.46,0.00,0.00 -7.028856,1.349522,4.785803,0.816492,-0.408246, -0.408246,0.00,0.00 -9.028855,1.349522,4.785803,-0.577349,-0.577349, -0.577349,0.00,0.00 -9.028855,3.349522,4.785803,-0.577349,0.577349, -0.577349,0.00,0.00 -7.028855,3.349521,6.785803,0.46,0.23,0.46,0.00,0.00 -7.028856,1.349521,6.785803,0.408246, -0.816492,0.408246,0.00,0.00 -9.028855,1.349522,6.785803,-0.408246, -0.408246,0.816492,0.00,0.00 -9.028855,3.349522,6.785803, -0.46,0.46,0.23,0.00,0.00 4, 0, 7 0, 3, 7 2, 6, 7 2, 7, 3 1, 5, 2 5, 6, 2 0, 4, 1 4, 5, 1 4, 7, 6 4, 6, 5 0, 1, 2 0, 2, 3 Plane 1 1 4,1,2,0 v1 1.00,0.500,0.500,0,1,NULL.TIF null.bump.tif 0.991230,-2.242427,0.00,0.00,0.00,1.00,0.00,0.00 0.991230,-4.242427,0.00,0.00,0.00,1.00,0.00,0.00 -1.008770, -4.242427,0.00,0.00,0.00,1.00,0.00,0.00 -1.008770, -2.242426,0.00,0.00,0.00,1.00,0.00,0.00 0, 3, 2 0, 2, 1 NOTE: each vert line contains x,y,z,nx,ny,nz,u,v Location of smf_export.py file on savefile: http://www.savefile.com/files/1054095 Thanks, Paul ( upretirementman ) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] summing arrays, along a dimension
d array([[0, 0, 1], [1, 2, 3], [2, 2, 4], [3, 6, 8]]) e=reshape((d[:,-2]+d[:,-1]),(4,1)) e array([[ 1], [ 5], [ 6], [14]]) is there a better way to accomplish this? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] summing arrays, along a dimension
John wrote: d array([[0, 0, 1], [1, 2, 3], [2, 2, 4], [3, 6, 8]]) e=reshape((d[:,-2]+d[:,-1]),(4,1)) e array([[ 1], [ 5], [ 6], [14]]) is there a better way to accomplish this? d [[0, 0, 1], [1, 2, 3], [2, 2, 4], [3, 6, 8]] e = [sum(i) for i in d] e [1, 6, 8, 17] f = [sum([L[i] for L in d]) for i in xrange(len(d[0]))] f [6, 10, 16] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Just bought Python in a Nutshell
On Thu, 13 Sep 2007, Lamonte Harris wrote: http://www.powells.com/biblio/63-9780596001889-7 Used, has anyone read this book. Any additional information that you like,dislike about this book? I love it, and use it more than any other Python book. Much as I hate to be the bearer of bad news here, though, the URL you give above is for the first edition, published four years ago. Alex put out a second edition just last year. The edition you bought covers through Python 2.2 or 2.3. The current edition covers through 2.5. That being said... it's still very useful, and while I've thought about buying a second edition, I still find the first edition to be extremely useful. I'd suggest reviewing the Python release notes for 2.4 and 2.5 to see what's changed since then. I've bought a *lot* of slightly-out-of-date computer books used and cheap, and you can still learn an awful lot from them. [I like having real books and stead of ebooks because its better on the eyes.] I'm with you. I like to do my reading laying on my bed before going to sleep; and even when using a book as a reference while coding, I like to look from my editor and leaf back and forth in the book; much more effective, to me, than screen swapping. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] remove blank list items
On Fri, 14 Sep 2007, Michael Langford wrote: What you want is a set, not a list. Lucky for you, a python dict uses a set for its keys, so cheat and use that. Is that true? d={1:a, 2:b} k=d.keys() type(k) type 'list' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] summing arrays, along a dimension
John [EMAIL PROTECTED] wrote d array([[0, 0, 1], [1, 2, 3], [2, 2, 4], [3, 6, 8]]) e=reshape((d[:,-2]+d[:,-1]),(4,1)) e array([[ 1], [ 5], [ 6], [14]]) is there a better way to accomplish this? Better? Maybe. More readable I think is: e1 = [ [row[-2]+row[-1]] for row in d ] You can convert the 2D list to an array object if you want later Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Is there some sort of Python Error log.
Command prompt is a pain and it would be pretty nice to have this feature. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] writing the correct map names
# Test for maps #if faceUV = 0: texture_map = NULL.TIF if me.hasVertexUV(): print must be a texture map! texture_map = current_obj.name texture_map = texture_map + .TIF smf_file.write(str(texture_map + '\n')) #if not map: bump_map = 'null.bump.tif' if me.hasVertexUV(): printmust be a bump map! bump_map = current_obj.name bump_map = bump_map + _bump.TIF smf_file.write(str(bump_map + '\n')) 1.00,0.500,0.500,0,1,NULL.TIF null.bump.tif -7.028856,3.349522,4.785803,0.23,0.46, -0.46,0.00,0.00 When I do have a mapped object, the script is not putting in the map name nor the bump map name. What am I missing? The python script is for the 3D graphics program Blender, which is free.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] summing arrays, along a dimension
John wrote: d array([[0, 0, 1], [1, 2, 3], [2, 2, 4], [3, 6, 8]]) e=reshape((d[:,-2]+d[:,-1]),(4,1)) e array([[ 1], [ 5], [ 6], [14]]) is there a better way to accomplish this? Which module are you using? In APL we'd write +/d to reduce along the rows using +. Does the array object have a similar reduce method? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] vi and python
Greetings, I use vim (vi improved): http://www.ibiblio.org/obp/pyBiblio/tips/elkner/vim4python.php -- bhaaluu at gmail dot com On 9/14/07, Danyelle Gragsone [EMAIL PROTECTED] wrote: Good Evening, I am running gentoo. I want to use vi to program in python. I wondered are there any other gentooovians out there who know if python support is already installed. Thanks, LadyNikon ___ 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] vi and python
Hi Bhaaluu, Sorry for the misunderstanding. Since vim points to vi automatically I have gotten used to calling it vi. But I did mean vim. LadyNikon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] vi and python
Hi Bhaaluu and LadyNikon, I'm not exactly sure what you mean by Python support, but since I do most of my coding in plain text editors anyway I have had no trouble using vi/vim to edit/code. It's extremely useful for when my laptop is on battery mode - I have a terminal profile setup to be mostly dark and fullscreen to conserve power. Best of luck! Dave M. On 9/14/07, Danyelle Gragsone [EMAIL PROTECTED] wrote: Hi Bhaaluu, Sorry for the misunderstanding. Since vim points to vi automatically I have gotten used to calling it vi. But I did mean vim. LadyNikon ___ 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] vi and python
Hello, I go back and forth between SPE and VIM for Python myself. As for Python support in VIM, you can use most editions of VIM as a plain text editor, and it is fine for Python. But, if you install vim-python (http://ddtp.debian.net/ddt.cgi?desc_id=20183), you get some nice added features such as syntax highlighting, code completion, etc. Sort of like a basic IDE all in VIM! Not sure how you have vim installed. On Ubuntu for example, I just do sudo apt-get install vim-full, and I get all the variants, including vim-python. Other handy things: * pydiction: http://www.vim.org/scripts/script.php?script_id=850 * python.vim: http://vim.sourceforge.net/scripts/script.php?script_id=30 Best of luck! -Sam __ - Original Message From: Danyelle Gragsone [EMAIL PROTECTED] To: python lista [EMAIL PROTECTED]; tutor@python.org Sent: Friday, September 14, 2007 8:35:21 PM Subject: [Tutor] vi and python Good Evening, I am running gentoo. I want to use vi to program in python. I wondered are there any other gentooovians out there who know if python support is already installed. Thanks, LadyNikon ___ 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] Is there some sort of Python Error log.
On 15/09/2007, Lamonte Harris [EMAIL PROTECTED] wrote: Command prompt is a pain and it would be pretty nice to have this feature. If you are using a Unixish system, do python myscript.py 2 error.log. -- - Rikard - http://bos.hack.org/cv/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [pygame] Re: Just bought Python in a Nutshell
Lamonte Harris wrote: Wow I just got it, and its nice doesn't even look used god damn. :D. It's generally considered rude to curse in technical forums such as this. Also, please use more punctuation. You're hard to understand sometimes. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] remove blank list items
Terry Carroll wrote: On Fri, 14 Sep 2007, Michael Langford wrote: What you want is a set, not a list. Lucky for you, a python dict uses a set for its keys, so cheat and use that. Is that true? d={1:a, 2:b} k=d.keys() type(k) type 'list' He worded it incorrectly since there is actually a 'set' type, but what he meant was that there wouldn't be multiples of each key that you add, so you would effectively eliminate multiples and thus have a set when you get a list of dict keys. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is there some sort of Python Error log.
Lamonte Harris wrote: Command prompt is a pain and it would be pretty nice to have this feature. If you're on windows, try using an IDE for your code editing. Then the errors will show up in the interactive shell that the IDE runs, and you won't have to deal with starting a DOS command prompt to catch your errors. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Just bought Python in a Nutshell
awesome! I should see it in about 2 wks.. im poor. So I choose super snail mail. LN ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Just bought Python in a Nutshell
Luckily that site still had one left .. so i brought it :D. I can always use another good and CHEAP book. Danyelle ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is there some sort of Python Error log.
Lamonte Harris wrote: Command prompt is a pain and it would be pretty nice to have this feature. I'm not sure what this feature is but look at the logging module. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do I add uvs to each vert line if a texture/bump map is used?
Does this have something to do with Python? What are uvs? vert line? texture/bump map? Kent Paul Coones wrote: C3DModel 4 2 0,50 Cube 1 1 8,1,12,0 v1 1.00,0.500,0.500,0,1,NULL.TIF null.bump.tif -7.028856,3.349522,4.785803,0.23,0.46, -0.46,0.00,0.00 -7.028856,1.349522,4.785803,0.816492,-0.408246, -0.408246,0.00,0.00 -9.028855,1.349522,4.785803,-0.577349,-0.577349, -0.577349,0.00,0.00 -9.028855,3.349522,4.785803,-0.577349,0.577349, -0.577349,0.00,0.00 -7.028855,3.349521,6.785803,0.46,0.23,0.46,0.00,0.00 -7.028856,1.349521,6.785803,0.408246, -0.816492,0.408246,0.00,0.00 -9.028855,1.349522,6.785803,-0.408246, -0.408246,0.816492,0.00,0.00 -9.028855,3.349522,6.785803, -0.46,0.46,0.23,0.00,0.00 4, 0, 7 0, 3, 7 2, 6, 7 2, 7, 3 1, 5, 2 5, 6, 2 0, 4, 1 4, 5, 1 4, 7, 6 4, 6, 5 0, 1, 2 0, 2, 3 Plane 1 1 4,1,2,0 v1 1.00,0.500,0.500,0,1,NULL.TIF null.bump.tif 0.991230,-2.242427,0.00,0.00,0.00,1.00,0.00,0.00 0.991230,-4.242427,0.00,0.00,0.00,1.00,0.00,0.00 -1.008770, -4.242427,0.00,0.00,0.00,1.00,0.00,0.00 -1.008770, -2.242426,0.00,0.00,0.00,1.00,0.00,0.00 0, 3, 2 0, 2, 1 NOTE: each vert line contains x,y,z,nx,ny,nz,u,v Location of smf_export.py file on savefile: http://www.savefile.com/files/1054095 Thanks, Paul ( upretirementman ) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor