Re: [Tutor] Newsreader list name?
Thanks for the reply Karl. In the last couple days I have carefully read through the Gmane site several times. I'm on OS X 10.4.1 and downloaded Hogwasher to try. Using my ISPs news server I found the two lists I mentioned, but I can't get anything nntp://news.gmane.org alone or in combination with a group (e.g. + gmane.comp.python.tutor), or alone (server) with the group in the subscription window to work. Are you using Gmane successfully? Thanks Lee C On Jun 9, 2005, at 4:12 PM, Karl Pflästerer wrote: > On 9 Jun 2005, [EMAIL PROTECTED] wrote: > > >> I'm switching over from email digests to a newsreader and I can't >> find a reference for this tutor list. >> >> For example, I have the lists comp.lang.python and >> comp.lang.python.announce setup, but can't find something like >> comp.lang.python.tutor? >> >> There must be others on this list using a newsreader and I would >> appreciate the list name used. >> > > The server is: news.gmane.org > and the group name is: gmane.comp.python.tutor > > In the WWW you can find the site of Gmane where all is explained. > Gmane > is great, since it hosts a lot of mailing lists. > > >Karl > -- > Please do *not* send copies of replies to me. > I read the list > ___ > 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] Newsreader list name?
I'm switching over from email digests to a newsreader and I can't find a reference for this tutor list. For example, I have the lists comp.lang.python and comp.lang.python.announce setup, but can't find something like comp.lang.python.tutor? There must be others on this list using a newsreader and I would appreciate the list name used. Thanks, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OO re-factoring (was Pythonese/Efficiency/Generalesecritique)
Javier, Allen, Kent, Liam Hmm, I think I understand what you all are saying. Basically, my familiar take on "code reuse" (the function model) as used in the utility is essentially not rewriting any more of a block than what is different, as opposed to "do all" blocks with convoluted switching. Whereas what it seems to me you are saying is levels of functional abstraction more akin to the Unix model. My only prior experience with OOP was with Lingo several years ago in a variable record file use, which was simple to grasp. Obviously my take is flawed, so I will study the material noted, reread my Learning Python Part VI, and take another stab at it. No doubt this is "rank amateurish" to you all - somewhat like I felt back in the 80's when a "senior analyst" asked me how to use more than one output file in a COBOL program ;') Anyway, starting with assembler in the 60s, and doing my last substantial technical work in the 80s with C and Pascal, I have a bit of catching up to do :~) Thank you all for pointing me in the right direction, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OO re-factoring (was Pythonese/Efficiency/Generalese critique)
Thanks again for your thoughts Javier. You've certainly given me a mouthful to chew on :~) I was thinking more in terms of "OOP is about code reuse" and trying to equate the function approach to such with the class methods approach in a similar way. Obviously I have got my mind wrapped around the wrong tree. I'm not actually looking for the best approach here - rather just trying to map a concept I'm familiar with to a new (to me) concept. The idea being than one learns from doing :') My initial thought that it would be a simple reusable code structure to re-map is shaping up otherwise. Thanks, Lee C On Jun 8, 2005, at 1:10 AM, Javier Ruere wrote: > Lee Cullens wrote: > > >> I was thinking of extending the learning exercise by re-factoring it >> as an OO approach, since it would contain a minimum altered method. >> Being new to OOP also though, I'm confusing myself with how that >> would be best accomplished. My thinking is that class/subclass >> method instances would replace the recursive functions approach, but >> have as yet not formed the coding in my mind. >> >> > >I can't imagine this either and, though I'm no OO guru, have never > considered that that an inheritance relationship could replace a > recursive function call. > > > >> I've read a lot on OOP, but seem to have a problem with the practical >> use of such in this utility. I'm undoubtedly creating a mountain of >> an ant hill in my mind, so any thoughts would be appreciated. >> >> > >I don't see any benefit from using OO in this case. Module level > functions (like you have now) are ideal in this case IMHO. >Maybe in a situation where you can benefit from OO it would be > crearer how to use it. Having state is usually a good sign. Working > with > several "things" which have a behaviour would be another. >Just for fun lets do it anyway. :) Directories and files could be > considered things, files could tell if they are files, links or > whatever > and directories could tell which subdirectories and files they have. > Also this objects could render themselfs to a proper string > representation we can directly output as a row. >So, in pseudocode, this would be something like: > > class File: >"""File(name:str, parent:Dir) > >parent: The directory which contains this File. >name: The name of this File. >""" >def __init__(self, name, parent): > self._parent = parent > self._name = name > > assert exists(join(repr(parent), name)) > >def isLink(self): > return islink(self.path) > >[Rest of the is* functions.] > >def __str__(self): > return [...] > > class Dir: >"""Dir(name:str, [parent:Dir]) > >parent: The directory which contains this File. >If omited, an absolute path is assumed. >name: The name of this File. >""" >def __init__(self, name, parent=None) > self._parent = parent > self._name = name > > assert isdir(join(repr(parent), name)) > >def getDirs(self): > return [...] > >def getFiles(self): > return [...] > >Now, both classes could inherit from a FSElement (or something) > but I > see no benefit in doing that. >The output functionality could also be in a different class: > > class CSVOutput: >def __init__(self, filename): > [validate for overwritting] > self._out = file(filename, 'w') > >def process(self, root): > for file in root.getFiles(): >self._out.write(str(file)) > > for dir in root.getDirs(): >self.process(dir) > >And the main: > > CVSOuput(argv[2]).process(Dir(mkAbsolutePath(argv[1]))) > >I'm very sleepy so this could all just be gibberish but I hope it > will help you get the idea. Ideally, functions should be very small, a > couple of lines, and object should be reusable. So moving the > rendering > to CVSOutput would be better. :) > > Javier > > ___ > 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] OO re-factoring (was Pythonese/Efficiency/Generalese critique)
In my original post I noted my recent exposure to Python and put up a little utility to my iDisk asking for a Pythonese/Efficiency/ Generalese critique. Javier, Kent and Liam were gracious enough to offer comments, which were greatly appreciated. To follow through with the learning exercise, I incorporated their comments (at least the ones there were no conflicting opinions on :~) and put up the new version as: http://homepage.mac.com/lee_cullens/DirTreeToCSV.zip I was thinking of extending the learning exercise by re-factoring it as an OO approach, since it would contain a minimum altered method. Being new to OOP also though, I'm confusing myself with how that would be best accomplished. My thinking is that class/subclass method instances would replace the recursive functions approach, but have as yet not formed the coding in my mind. I've read a lot on OOP, but seem to have a problem with the practical use of such in this utility. I'm undoubtedly creating a mountain of an ant hill in my mind, so any thoughts would be appreciated. Thanks, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pythonese/Efficiency/Generalese critique please
OK Kent - got it:My little formating function could be written as (tested) def cellpos(pname, alvl, blvl, clvl): # breakout path components into list pl = pname.split('/') # insert empty cells for repeated names and # add new path components (cells) to csvline csvline = '"",'*(alvl - blvl) + '"' + '","'.join(pl[alvl:clvl]) + '",' return csvline"So, it is good to know about join(), but write the code the way that is clearest to you."One of my thoughts in posting this exercise is to first understand alternatives before I decide what I like best :~) Your points, together with Javier's points on presentation and thoroughness, and Liam's point on making the logic more obvious are appreciated. Thank you all for taking the time to offer your comments,Lee COn Jun 5, 2005, at 5:47 AM, Kent Johnson wrote:Lee Cullens wrote: OK, let's try to get this right.Given a list of strings and a divider string, join() returns the strings from the list 'joined' by the divider string. For example, pl = ['a', 'b', 'c']'","'.join(pl) 'a","b","c'The syntax for this takes a little getting used to; join() is actually a string method; you call it on the divider string and pass it the list of strings to be joined.To make a list of quoted, comma separated values, you also need initial and final quotes: '"' + '","'.join(pl) + '"' '"a","b","c"'In your example you need '","'.join(pl[0:2]) Saving one line of code in this module doesn't mean much, but if it were a efficiency in processing it would? Conventional wisdom is that it is more efficient to use the join method. More enlightened wisdom says, don't optimize until you know you have a problem, and the only way to know what is fastest in your program with your data is to test. And of course working code always beats broken code :-)I did some experiments with these alternatives and concluded that if the length of the resulting string is less than about 500-800, the version using += is faster than the one using join():http://www.pycs.net/users/323/weblog/2004/08/27.htmlSo, it is good to know about join(), but write the code the way that is clearest to you.Kent___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pythonese/Efficiency/Generalese critique please
Such would serve to pull the test and the reason for it together - more obvious. Which is a good point in writing code that others may read. Thanks Liam On Jun 5, 2005, at 9:29 AM, Liam Clarke wrote: > > > > There is no need for the if(dlst); if the list is empty the > iteration will do nothing. You can write this as > > for dlf in os.listdir(pname): > >Though it is quite distant, there is an else statement which makes > the if construct a requierement. > > Javier > > > You could just do - > > if not len(dlst): > #Your else code block here > > for dlf in os.listdir(pname): > > and so forth. > > > > -- > 'There is only one basic human right, and that is to do as you damn > well please. > And with it comes the only basic human duty, to take the > consequences.' > ___ > 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] Pythonese/Efficiency/Generalese critique please
Well, I've tried both and can't produce what I was doing with the for construct. With a listpl = ['a', 'b', 'c', 'd'] of the path components I'm trying to add justified non repeated path elements, say pl[2] and pl [3] to csvline so that csvline would end up '"",'*x plus '"c","d",' >>> pl = ['a', 'b', 'c'] >>> csvl = '"",' >>> csvl += '"'.join(pl[0:2]) + '",' >>> csvl '"",a"b",' which is intended to be '"","a","b",' Saving one line of code in this module doesn't mean much, but if it were a efficiency in processing it would? Lee C On Jun 4, 2005, at 6:55 PM, Kent Johnson wrote: > Javier Ruere wrote: > >> for i in range(alvl, clvl): >> csvline = csvline + '"' + pl[i] + '",' >> >> should be replaced by something like the following: >> >> cvsline += '"' + pl[alvl:clvl].join('",') >> > > or maybe more like this: > cvsline += '"' + '",'.join(pl[alvl:clvl]) + '",' > > though if alvl == clvl this will output an empty cell. > > Kent > > ___ > 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] Pythonese/Efficiency/Generalese critique please
On Jun 4, 2005, at 9:32 PM, Javier Ruere wrote: > Lee Cullens wrote: > >> The initial os.walk() approach I tried is simple in concept: >> >> pgo = os.walk(base_directory) >> for xnode in pgo: >> >> >> but I ended up with much more processing in having to sort the rows >> and then blank repeated cells, than the recursive listdir approach >> which gave me the desired ordering in one pass. >> > >You have certainly analysed this issue far more than me so you know > better but I just can't see the extra processing. I guess I should > just > give it a try. :) > Or that I just was not smart enough to pull it into one pass :~) If this were a serious project I would look into the csv module Kent mentioned and possibly combine the two for a much more succinct module. > Javier > > ___ > 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] Pythonese/Efficiency/Generalese critique please
Thanks for the critique Kent On Jun 4, 2005, at 6:54 PM, Kent Johnson wrote: > Lee Cullens wrote: > > >> Pythonese/Efficiency/Generalese critique please >> >> I'm new to Python and as an exercise created a little utility module. >> I'm learning from the O'Reilly books and updating that understanding >> from the 2.4 documentation. >> >> I would appreciate any comments you see fit to offer. >> >> > > I find the nested functions confusing myself; is there a reason > they are nested? If they can stand alone I would make them separate > top-level functions with names starting with _ to indicate that > they are private. > > I did so first for a visual indication of use and relation (eyes of the beholder thing) and second to simplify scope. The latter though is a mute point since (I believe) the scope is the same either way, and my structure does add a little baggage to the recursion which is a negative. Your point is well taken. > dlst = os.listdir(pname) > if len(dlst): > for dlf in dlst: > > There is no need for the if(dlst); if the list is empty the > iteration will do nothing. You can write this as > for dlf in os.listdir(pname): > > Something I wrestled with - but as I needed to recognize an empty directory I needed something to facilitate such ( the for else was no help) so that is what I came up with and still don't see a way around. > Some blank lines would aid readability. For example before each > comment in cellpos() and before each elif in the main conditional > block. > > Good point. I've been letting all the bright colors in WingIDE lull me into forgetting others may see just b&w. > Do you know there is a csv module that helps to read and write csv > files? It will take care of escaping " in your filenames, if such a > thing is possible... > > No, I didn't know. If I do something like this seriously I'll check it out Thanks again, Lee C > ___ > 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] Pythonese/Efficiency/Generalese critique please
Sorry about the double post. I sent with the wrong from address so I resent with the correct address and before I could cancel the first the moderator had let it through :~) Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pythonese/Efficiency/Generalese critique please
Thank you for the critique Javier. You made some good points that I will play with before moving on. Beyond that, I intentionally neglected error checking in this exercise as you may have surmised. Checking input arguments and handling access restrictions gracefully would indeed be important components. The initial os.walk() approach I tried is simple in concept: pgo = os.walk(base_directory) for xnode in pgo: but I ended up with much more processing in having to sort the rows and then blank repeated cells, than the recursive listdir approach which gave me the desired ordering in one pass. Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pythonese/Efficiency/Generalese critique please
Thank you for the critique Javier. You made some good points that I will play with before moving on. Beyond that, I intentionally neglected error checking in this exercise as you may have surmised. Checking input arguments and handling access restrictions gracefully would indeed be important components. The initial os.walk() approach I tried is simple in concept: pgo = os.walk(base_directory) for xnode in pgo: but I ended up with much more processing in having to sort the rows and then blank repeated cells, than the recursive listdir approach which gave me the desired ordering in one pass. Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Pythonese/Efficiency/Generalese critique please
Pythonese/Efficiency/Generalese critique pleaseI'm new to Python and as an exercise created a little utility module. I'm learning from the O'Reilly books and updating that understanding from the 2.4 documentation. The utility produces a csv file of a specified directory tree for import to a spreadsheet application. My first attempt employed os.walk(), but I found converting the results of such more troublesome than the recursive approach of this version. Including the module code in this email puts it a little over the 40K limit (lots of comments) so I uploaded it to my dotMac iDisk:http://homepage.mac.com/lee_cullens/dirtss.py.zipOn a Mac (at least) the .py is recognized as an executable and warns of such so Ctrl click (right click) and open with your programming text editor. It would fail to run anyway without arguments. I would appreciate any comments you see fit to offer.Thank you,Lee CDual 2.5 Power Macintosh G5 (OS 10.4.1) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] increment operator
I find the following invaluable - maybe you will also. http://rgruet.free.fr/PQR24/PQR2.4.html Lee C On May 27, 2005, at 11:25 PM, Servando Garcia wrote: > Hello > Is there a increment operator in python similar to c++ > like so "SomeVariable++" > > ___ > 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] __init__.py
Joseph, I'm relatively new to Python, but I might be able to help with your question. First, think of the convention "self" (or sometimes "me" in other languages) as a reference to a specific instance derived from a class. When you reference an inherited class method of an instance, the specific instance address is automatically passed as the first argument. There is nothing special about the label "self" by itself (you could use instance_ref for example), but it is a convention that makes your code more easily understood. If I've used any inappropriate Python terminology, maybe someone will correct such :~) Now your real question. In my limited Python experience so far the module (file) __init__.py is used with module packages (multiple directories involved). Let's say you want to use "import dir1.dir2.mod" because you have structured you package something like: dir0/ dir1/ __init__.py dir2/ __init__.py mod.py another_mod.py dir3/ __init__.py yet_another_mod.py Two things here: 1) The container directory (dir0) needs to be added to your module search path, unless it's the directory of the importing module. 2) Each directory named in the package import statement must contain a file called __init__.py These may be empty files (used by Python in the package initialization phase) and are not meant to be executed directly. However you could use such to create a data file, connect to a database, etc. Package imports can be used to simplify your PYTHONPATH/.pth search path settings. If you have cross-directory imports, you might make such relative to a common root directly. We'll let the experts take it from there. Lee C On May 22, 2005, at 7:54 PM, Joseph Quigley wrote: > I've seen many (python) "plugins" (some located in site-packages > [windows > here]) with the name __init__.py. > What's their use? > class foo: > def __init__: > print "this starts first" > def foo1(): > print "this comes later. Init initializes the chain of > functions in this > class > > Now, i've never used a program (i can't seem to grasp all the self, > and > other things in OOP yet) with __init__ but I know what __init__ > does in a > class, not as a file name. > I'm asking this out of curiosity, not for help. > JQ > > ___ > 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] Python debugger under Tiger?
Mike, You may not be looking for a commercial IDE, but I am very happy with WingIDE and using it with Tiger. Lee C On May 18, 2005, at 6:54 PM, Mike Hall wrote: > I should of specified that I'm looking for an IDE with full > debugging. Basically something like Xcode, but with Python support. > > > On May 18, 2005, at 3:10 PM, [EMAIL PROTECTED] wrote: > > >> Quoting Mike Hall <[EMAIL PROTECTED]>: >> >> >> >>> Does anyone know of a Python debugger that will run under OSX 10.4? >>> The Eric debugger was looked at, but it's highly unstable under >>> Tiger. Thanks. >>> >>> >> >> pdb should work :-) >> >> -- >> John. >> ___ >> 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] Python Interest Group Query
Python Interest Group Query I'm aware of the Boston PIG, a smaller one in Amherst and have been told that there is possibly a PIG in Manchester, NH. Basically I'm trying to find out if there are any, or any interest in (or even any other Python users at all :~)) in a PIG in the northern NE corridor (Vermont, New Hampshire, Maine). I'm a recent Mac and Python convert working at some multimedia software development, and retired from a software engineering career. I live in western NH and back in the late 80's commuted to Boston and NY, so I'm not very keen on such anymore :~) Thank you, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] import problem
I assume you mean PythonIDE for Python 2.3 (I usually use 2.4 and WingIDE). Here it is (indents screwed up with var font): HTH, Lee C Python 2.3 (#1, Sep 13 2003, 00:49:11) [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] Type "copyright", "credits" or "license" for more information. MacPython IDE 1.0.1 >>> import timeit >>> def y1(): print 'y1 executed' ... >>> def y2(): print 'y2 executed' ... >>> for f in [y1,y2]: name = f.__name__ print name; f() t=timeit.Timer('%s()' % name, 'from __main__ import %s' % name) print t.timeit(1) ... y1 y1 executed y1 executed 0.00186991691589 y2 y2 executed y2 executed 0.00309705734253 >>> import sys >>> sys.version '2.3 (#1, Sep 13 2003, 00:49:11) \n[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)]' >>> On Apr 19, 2005, at 2:16 AM, Chris Smith wrote: I sent the following to the mac-sig without reply (except for an autoresponder telling me that a person was out of the office :-)). Is there anyone with a mac that could test this code in the IDE to see if you have the same problems? (I don't have problems running it through the 2.4 version of python in the terminal.) = I was recently trying to use the timeit module and although I was able to do so without problem on a Windows machine, I get a "cannot import y1" ImportError from the following script. ### import timeit def y1(): print 'y1 executed' def y2(): print 'y2 executed' for f in [y1,y2]: name = f.__name__ print name; f() t=timeit.Timer('%s()' % name, 'from __main__ import %s' % name) print t.timeit(1) ### --the output-- y1 y1 executed ** ImportError I am using the IDE for 2.3.3 under OS X (10.2.8). I am able to successfully run timeit in other ways, but I like the above way to loop through the codes that I want to test. /c ___ 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] for loop
Well I was reading too fast (as usual) - you wanted to print 'yes' only if 5 is not in a sub list but you want to look in all the sub lists and yet print 'yes' only once??? So in long hand lets reverse the logic and make sure we print 'yes' only once >>> yes = 0 >>> for num in x: ... if 5 not in num: ... if not yes: ... print 'yes' ... yes = 1 ... yes On Apr 19, 2005, at 12:31 AM, Lee Cullens wrote: As you probably have already found out the expression >>> print [ e for e in x if 5 in e] will produce [[8, 4, 5, 6]] The problem with your original code is that you are printing 'yes' for each sub list until you encounter a sub list with 5 whereupon you break without printing yes. If you change your test to check for 5 in a sub list and, print yes and break at that point you will get the results you are looking for. >>> for num in x: ... if 5 in num: ...print "yes" ...break ... yes >>> Lee C On Apr 18, 2005, at 11:51 PM, Ching-Yi Chan wrote: *Ron A* /Wed Jan 7 18:41:15 EST 2004/ I'm experimenting and would like 'yes' to be printed only if 5 is not in the list, but I want to look in each list. This prints out two yeses. How do I get it to print just one 'yes'? x = [[1,2,3],[2,4,6],[8,4,5,6],[9,8,7]] for num in x: if 5 in num: break else: print 'yes' -- Hi, I read the code and consider for a while, you can try it : x = [[1,2,3],[2,4,6],[8,4,5,6],[9,8,7]] print [ e for e in x if 5 in e] ___ 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] Newbie question
Thank you Brian for making the point that I did such a poor job of conveying. My post did indeed fail to clearly and concisely answer the question. Lee C On Apr 18, 2005, at 11:34 PM, Brian van den Broek wrote: Lee Cullens said unto the world upon 2005-04-18 21:07: That just gives you a spacer line after your output. To see such as a separator change it to print '*'*10 On Apr 18, 2005, at 8:55 PM, Hoffmann wrote: Hi All: I am a newbie, and I am enjoying to study Python a lot. I have a question about an example I got from one of my books. The program is: def square(y): return y * y for x in range(1, 11): print square(x), print Well, I understood the code above. My question is: Is it really necessary I have the last "print" statment (last line) in the code? Thanks a lot in advance. Hoffmann Hi all, in the particular case, it is more than just a spacer, I think. It serves to break the "print on the same line" feature of the print some_thing, statement in the loop. Without it, the next thing printed will be on the same line: >>> for i in range(1): # contrived to keep it all in block construct ... for i in range(5): ... print 'in', ... print 'out' ... in in in in in out (Unless, of course, that is what Lee meant. In which case--nevermind :-) Best, Brian vdB ___ 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] for loop
As you probably have already found out the expression >>> print [ e for e in x if 5 in e] will produce [[8, 4, 5, 6]] The problem with your original code is that you are printing 'yes' for each sub list until you encounter a sub list with 5 whereupon you break without printing yes. If you change your test to check for 5 in a sub list and, print yes and break at that point you will get the results you are looking for. >>> for num in x: ... if 5 in num: ...print "yes" ...break ... yes >>> Lee C On Apr 18, 2005, at 11:51 PM, Ching-Yi Chan wrote: *Ron A* /Wed Jan 7 18:41:15 EST 2004/ I'm experimenting and would like 'yes' to be printed only if 5 is not in the list, but I want to look in each list. This prints out two yeses. How do I get it to print just one 'yes'? x = [[1,2,3],[2,4,6],[8,4,5,6],[9,8,7]] for num in x: if 5 in num: break else: print 'yes' --- --- Hi, I read the code and consider for a while, you can try it : x = [[1,2,3],[2,4,6],[8,4,5,6],[9,8,7]] print [ e for e in x if 5 in e] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie question
That just gives you a spacer line after your output. To see such as a separator change it to print '*'*10 On Apr 18, 2005, at 8:55 PM, Hoffmann wrote: Hi All: I am a newbie, and I am enjoying to study Python a lot. I have a question about an example I got from one of my books. The program is: def square(y): return y * y for x in range(1, 11): print square(x), print Well, I understood the code above. My question is: Is it really necessary I have the last "print" statment (last line) in the code? Thanks a lot in advance. Hoffmann ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] More Function Questions (Joseph Q.)
I think your looking at it too literally Joseph. Rather than any one example, think of a function as a block of code that is used in more than one place in your program. The x + y block of code is just a simplistic representation, intended to illustrate without the distracting complications of a more involved algorithm. Functions are also useful (even necessary) in various approaches to problem solving. If you need an example one might be the recursive matrix solution approach I posted a link to in another post to this list. Lee C On Apr 17, 2005, at 12:29 PM, Joseph Quigley wrote: Hi all, Another function question. def bar(x, y): return x + y bar(4, 5) So I can put anything I want in there. What good is a function like that? Of course I know about. def foo(): print "Hello all you who subscribe to the Python Tutor mailing list!" So what do you use the def bar(x, y): return x + y bar(4, 5) functions for? (I just need a simple example) Thanks, Joe ___ 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] Re: Recursion....what are the best situations to use it?
It is really more of a question of the type of problem and the solution approach in general. For example, one can find the intersection of two lines with a simple equation, but that equation depends on the dimension the lines are in (2D, 3D, ...). If one were working in 2D space only, then the simple equation is the simplest solution. However if one wanted a function that would work in any space, then setting up the problem as a matrix and solving it recursively might be a better approach. One more concrete consideration is that if you find yourself tackling a problem with nested looping and the algorithm at each level is mostly the same, then perhaps recursion would be better. Some time ago I wrote a recursion article for an audience using Lingo scripting (in Macromedia Director). The actual code at the end is Lingo (not all that different) but the thrust of the article is constructing a recursive solution for matrix type problems (more than one unknown in an equation). I put the pdf in my .Mac account if you wish to view it. http://homepage.mac.com/lee_cullens/ A_Simple_Recursive_Solution_(article_v2).pdf Of course, with Python we have math packages to do the grunt work, but it helps to understand the problem. Lee C On Apr 14, 2005, at 21:06, [EMAIL PROTECTED] wrote: I've seen a couple of nice tutorials on recursion, and a lot of awful ones. The latter always trot out the fibonacci and factorial examples for some reason. And that's about it! The good ones showed me how to trace through recursive calls and gave me practical examples(tictactoe, magic squares, Tower of Hanoi, 4-Square, etc) What I want to know is this: what are other specific situations where a recursive algorithm would be better or easier to program than an iterative one? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Re: GUI module selection?
Thank you, Liam and Steve, for the informative feedback. Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Re: Interesting anomaly with the Eight Queens problem
So there is no misunderstanding, the trial positioning would be applicable to all levels, not just the first. In other words, solving for each of the remaining Queens in turn is the same as for the first Queen, except for the eighth Queen where no lower level positioning need be considered, On Apr 13, 2005, at 1:04 PM, Lee Cullens wrote: John The type of problem you mention and the extent of positioning you go to could result in an incomplete solution. In very general terms one would need to place the first Queen then find an appropriate position for the second, and each of the remaining Queens in turn until either there are no appropriate positions to be found or all eight Queens have been positioned. If all eight Queens can not be positioned, then the first Queen would be moved and the remaining Queen positioning worked through again. This is a "made for recursion" type of problem, but could be done with nested loops. The "Trying everything" approach can , of course, be a very lengthy process. I seem to remember a more sophisticated model for this type of problem (I think somehow related to linear algebra) but can't recall the specifics at the moment. Maybe one of the more academic (an younger) members of this list can point you in such a direction. Lee C On Apr 13, 2005, at 12:15 PM, [EMAIL PROTECTED] wrote: I read through Magnus Hetland's book and noticed the Eight Queens problem, which I had solved some time ago using Visual Basic. This time, I wanted to use a non-recursive solution. I randomly place each queen on board coordinates running from 0,0(top left hand corner of board) to 7,7(lower right hand corner of board) Here's the interesting part: I can only get eight queens safely on the board perhaps every one in 20 runs. At least 12 or 13 of those times, I can place seven queens, while five or six times, I can only place six queens or even five! Once a queen is safely placed, I establish her "territory". Once this is done, when I try to place a subsequent queen, I check for all placed queen's territories. Once I place six or seven queens, I have to give the program a lot more tries to try to find a safe square. And it can't always be done. Does this sound right? Does trying to place each queen randomly cause this anomaly? I can post my code(beginner code!) if anyone is interested in seeing it. Best, John = John Soares, Webmaster Family Safe Surfinghttp://www.family-safe-surfing.net [EMAIL PROTECTED] [EMAIL PROTECTED] Tel: (810) 343-0571 Fa x: (866) 895-3082 "Your best bet for online family-friendly resources" = ___ 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] Re: Interesting anomaly with the Eight Queens problem
John The type of problem you mention and the extent of positioning you go to could result in an incomplete solution. In very general terms one would need to place the first Queen then find an appropriate position for the second, and each of the remaining Queens in turn until either there are no appropriate positions to be found or all eight Queens have been positioned. If all eight Queens can not be positioned, then the first Queen would be moved and the remaining Queen positioning worked through again. This is a "made for recursion" type of problem, but could be done with nested loops. The "Trying everything" approach can , of course, be a very lengthy process. I seem to remember a more sophisticated model for this type of problem (I think somehow related to linear algebra) but can't recall the specifics at the moment. Maybe one of the more academic (an younger) members of this list can point you in such a direction. Lee C On Apr 13, 2005, at 12:15 PM, [EMAIL PROTECTED] wrote: I read through Magnus Hetland's book and noticed the Eight Queens problem, which I had solved some time ago using Visual Basic. This time, I wanted to use a non-recursive solution. I randomly place each queen on board coordinates running from 0,0(top left hand corner of board) to 7,7(lower right hand corner of board) Here's the interesting part: I can only get eight queens safely on the board perhaps every one in 20 runs. At least 12 or 13 of those times, I can place seven queens, while five or six times, I can only place six queens or even five! Once a queen is safely placed, I establish her "territory". Once this is done, when I try to place a subsequent queen, I check for all placed queen's territories. Once I place six or seven queens, I have to give the program a lot more tries to try to find a safe square. And it can't always be done. Does this sound right? Does trying to place each queen randomly cause this anomaly? I can post my code(beginner code!) if anyone is interested in seeing it. Best, John = John Soares, Webmaster Family Safe Surfinghttp://www.family-safe-surfing.net [EMAIL PROTECTED] [EMAIL PROTECTED] Tel: (810) 343-0571 Fa x: (866) 895-3082 "Your best bet for online family-friendly resources" = ___ 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] Python backwards program (fwd)
for the slicing see: http://docs.python.org/lib/typesseq.html for a longhand loop approach see: http://docs.python.org/tut/node6.html#SECTION00620 On Apr 13, 2005, at 12:46 AM, Danny Yoo wrote: -- Forwarded message -- Date: Tue, 12 Apr 2005 22:37:44 -0500 From: Jim and Laura Ahl <[EMAIL PROTECTED]> To: Danny Yoo <[EMAIL PROTECTED]> Subject: Re: [Tutor] Python backwards program I have read about loops, strings, tuples. I am taking this class on distance education and I am lost with this assignment. I have read what Tony has wrote and that does me no good. I do not understand what he is talking about. I understand how slicing works and what the numbers mean. I know what the -1 and such like that mean. I know what the len function does. I am wasting you guys time. Thanks Jim - Original Message - From: Danny Yoo To: Tony Meyer Cc: 'Jim and Laura Ahl' ; tutor@python.org Sent: Tuesday, April 12, 2005 9:09 PM Subject: RE: [Tutor] Python backwards program On Wed, 13 Apr 2005, Tony Meyer wrote: I am very new to programming and I have an assignment to have a raw_input string that is inputted by the user and then is printed backwards. Can anyone help me? I can get it to print regular but backwards in not working. I guess that I shouldn't give code if this is for an assignment, but if you are using Python 2.4, then try seeing what the "reversed()" built-in function does. If you're not using Python 2.4, then read up about "slices" - which is when you get subsections of a string (e.g. "tony"[2:4] is "ny"). You can pass a 'direction' in a slice as well. It might also help to have a better feeling for what Jim already knows. If this is a school assignment, I doubt that reversed() is allowed, nor reverse slices, since that would make the problem too easy. *cough* Jim, can you give a brief description of what things you've learned already? Do you already know about loops? ___ 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] Re: GUI module selection?
I'm trying to narrow down the detailed trials I need to go through in selecting basic GUI module approaches (just getting started in this Python "playground" - dev platform Mac OS X). Maybe these old eyes are just missing it, but I can't seem to satisfy my curiosity on the following: 1) Use of GTK+, PyGTK and Gazpacho with Python 2.4 in general 2) GTK+, PyGTK and Gazpacho binaries for Mac OS X in general and more specifically for MacPython 2.4.1 3) wxPython vs GTK+ (vs Tkinter?) as a starting point for developing multimedia apps (for building sophisticated multimedia presentations) potentially for cross platform (and pyObjC for Mac only) - opinions/pointers anyone or maybe *another* better course? Thanks, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is the best book to start?
I can only comment on what I know and I have the O'Reilly Python series which begins with the "Learning Python" book you have (if you have the 2nd ed.). Just getting started with Python myself. The first book has gone quite fast, but then I'm a retired software engineer. Even so, I think it would be very good for someone with limited programming experience. Now get your nose back in that book :~) Lee C On Mar 30, 2005, at 11:08 PM, Hoffmann wrote: Hi All, I am starting to studying Python. I have some previous experience with C (beginner level). Could, anyone, please, suggest a good Python book? I have both "Learning Python" by Lutz & Ascher, and "Python How to Program" by Deitel and others. Are those good books? Thanks. Hoffmann ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] RE: Using IDLE on Mac OS X?
This is not a great way to start on this list, but I am having trouble with an annoyance that I have not been able to solve and have not found an answer to elsewhere. (Dual 2.5 Mac G5; 10.3.8; Python 2.3; retired software engineer getting started with Python) I don't like using Python via the Bash terminal and PythonIDE works well but as you know is somewhat lacking, and I'm not ready yet to shell out for something like WingIDE. So I started using IDLE but have found an annoyance with it on (OS X) 10.3.8 - reportedly not a problem on 10.2. In the Help menu, the About and IDLE Help work fine, but the Python Docs selection is DOA as is defining an alternate help source with the correct Python site URL. Yes I know I can always click on a link in Safari to bring such up, but would like to use the help viewer from within IDLE. Just getting started, I make a lot of trips to the Python Docs :~) Does anyone on this list have any experience with the problem or know of any appropriate links? Thanks, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor