Re: [Tutor] "Print" behaviour inside a loop?
On Sat, 11 Jun 2005 19:42:27 -0400, Simon Gerber wrote (in article <[EMAIL PROTECTED]>): > Hullo, > > Firstly, thanks to everyone who helped me find my own IP address. That > was a big help. That bit is working now, and working very nicely. I am > now stuck on something purely aesthetic - printing a few dots across the > screen to provide a bit of feedback while the VPN tunnel is being > established. > > def vpn_connect(choice): > import time > ip_addr = "" > tries = 0 > retries = 10 > print "Connecting to %s" % (choice), > os.system("pon %s" % (choice)) > while ip_addr == "" and tries < retries: > print "...", # This is the line causing problems > time.sleep(0.5) > ip_addr = get_addr() > if ip_addr != '': > #create the route! > pass > else: > tries += 1 > sys.exit() > > It works. The problem is, nothing is displayed on screen until after the > connection occurs - at which point we see: > > "Connecting to Prodigi ... ... ... ... ... ... ... " > > If I remove the comma at the end of the marked line, the ellipses print > every .5 seconds as desired, except they print down the screen of course! > > After googling around a little, I found a post that seemed to say > Python won't draw the results of 'print' statements until it hits a > newline. I tried using sys.stout.write('...'), but had the same problem > there, too. I've also found a few progress bar classes around the web, > but I not exactly want a progress bar. I just want to print '...' every > few seconds until connected. > > Any hints? I looked up the python reference manual, but couldn't find > any way to force print statements to draw. Should I be looking into > threads, perhaps? > > Regards, > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Just use sys.stdout.writelines(' ... ') Example: >>> import sys >>> sys.stdout.writelines ('... '); sys.stdout.writelines ('... ') ... ... >>> That what your looking for? Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using code objects?
Using code objects? === As an OO exercise I have a factory pattern that returns class objects that each have an "action" method. ClassObj.action() in turn returns a code object in my recursive process loop. I create the code objects as a one time step outside my factory pattern and potential class definitions, then reference them in my potential classes which seems to work as expected. When I create the code objects though, it seems a couple different ways work and I'm wondering which is better and why (or is there a more correct technique in this situation)? The two different ways are illustrated below: Python 2.4.1 (#2, Mar 31 2005, 00:05:10) [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] Type "help", "copyright", "credits" or "license" for more information. >>> def foo(st): ... print st ... >>> exp1 = 'foo("#expersion 1#")' >>> exp2 = 'foo("#expersion 2#")' >>> obj1 = compile(exp1, 'whatever', 'single') >>> exec obj1 #expersion 1# >>> obj2 = compile(exp2, 'whatever', 'exec') >>> exec obj2 #expersion 2# >>> Thank you, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. Static Methods
On Tue, 21 Jun 2005 16:52:09 -0400, Chuck Allison wrote (in article <[EMAIL PROTECTED]>): > Sorry for the elementary question: I was wondering if someone could > explain the difference to me between class and static methods. Coming > from other languages, I'm used to static methods, but not "class > methods". Thanks. > > Does this help (from the QR)? Static methods : Use staticmethod(f) to make method f(x) static (unbound). Class methods: like a static but takes the Class as 1st argument => Use f = classmethod(f) to make method f(theClass, x) a class method. The decorators @staticmethod and @classmethod replace more elegantly the equivalent declarations f = staticmethod(f) and f = classmethod(f). Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. Static Methods
On Tue, 21 Jun 2005 17:58:09 -0400, Chuck Allison wrote (in article <[EMAIL PROTECTED]>): > Hello Chinook, > > So is the main motivation for class methods so that you can have the > class object available? It seems you can have that anyway in a static > method by just asking. I'm sure there's a good reason for this, but I > haven't yet gotten to the point of mastery where I can see a need for > class methods (even after reading Martelli's Nutshell). I understand > the syntax issues - I just don't see the need yet. > > Tuesday, June 21, 2005, 3:28:48 PM, you wrote: > > C> On Tue, 21 Jun 2005 16:52:09 -0400, Chuck Allison wrote > C> (in article <[EMAIL PROTECTED]>): > >>> Sorry for the elementary question: I was wondering if someone could >>> explain the difference to me between class and static methods. Coming >>> from other languages, I'm used to static methods, but not "class >>> methods". Thanks. >>> >>> > > C> Does this help (from the QR)? > > C> Static methods : Use staticmethod(f) to make method f(x) static (unbound). > C> Class methods: like a static but takes the Class as 1st argument => Use f > = > C> classmethod(f) to make method f(theClass, x) a class method. > > C> The decorators @staticmethod and @classmethod replace more elegantly the > C> equivalent declarations f = staticmethod(f) and f = classmethod(f). > > C> Lee C > > > C> ___ > C> Tutor maillist - Tutor@python.org > C> http://mail.python.org/mailman/listinfo/tutor > > > > Chuck, Being fairly new to Python myself, I know how to create them and how to reference them. I also know to use a static method in a factory pattern because it's name is then local to the scope it is defined in and it is called without an instance argument. Mostly they work like simple functions that happen to be coded inside a class. Static methods are usually used with class attributes to manage information that spans all instances generated from a class (a book's example is keeping track of the number of instances generated from a class). Class methods are similar, but Python automatically passes the class (not an instance) into the method's leftmost argument. I have also read that such was introduced to clear up a gotcha before Python 2.3 Yea, yea, your saying, but you never answered my question :~) All I can relate to at this point in my learning cycle is that it's all a name space issue (re: instance, static and class). Maybe one of the gurus can enlighten both of us further. Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] OO refactoring trial ??
OO refactoring trial Following is a simple trial structure of a refactoring (top-down to OO) learning exercise I'm doing. Whether you call it a Factory pattern, COR pattern, or some hinze 57, I don't know what class to use till run time and I'm trying to avoid a lengthy "if" sequence, the test sequence is important, and to avoid code duplication I'll be using code objects in the "doit" methods. You've already given me many good ideas in previous threads and this is where it got you :~) This works, but would you please tell me: 1) What you don't like about the approach 2) The implications of using this in a recursive approach (referenced from but outside the recursive function) and if the decorators help with such. 3) Any other comments you might offer Thank you, Lee C === ootest.py class MF(object): @staticmethod def findit(t): for it in MF.__subclasses__(): if it.testit(t): return it().doit class A(MF): @staticmethod def testit(tv): if tv == 'relates to A': return True else: return False def doit(self): print '# did A #' class B(MF): @staticmethod def testit(tv): if tv == 'relates to B': return True else: return False def doit(self): print '# did B #' mydoit = MF.findit('relates to B') mydoit() mydoit = MF.findit('relates to A') mydoit() Test run == Python 2.4.1 (#2, Mar 31 2005, 00:05:10) [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] Type "help", "copyright", "credits" or "license" for more information. >>> import ootest # did B # # did A # >>> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OO refactoring trial ??
[[ This message was both posted and mailed: see the 'To' and 'Newsgroups' headers for details. ]] Clarifications: 1) Truth test simplified after a %) by Peter Otten - thanks. In reality the "testit" methods will all be quite different as you might imagine (as will the "doit" methods). 2) A final subclass will always return True, so there will always be a valid factory (?) result. Following is a simple trial structure of a refactoring (top-down to OO) learning exercise I'm doing. Whether you call it a Factory pattern, COR pattern, or some hinze 57, I don't know what class to use till run time and I'm trying to avoid a lengthy "if" sequence, the test sequence is important, and to avoid code duplication I'll be using code objects in the "doit" methods. You've already given me many good ideas in previous threads and this is where it got you :~) This works, but would you please tell me: 1) What you don't like about the approach and/or how I might improve it 2) The implications of using this in a recursive approach (referenced from but outside the recursive function). 3) Any other comments you might offer Thank you, Lee C === ootest.py class MF(object): @staticmethod def findit(t): for it in MF.__subclasses__(): if it.testit(t): return it().doit class A(MF): @staticmethod def testit(tv): return (tv == 'relates to A') def doit(self): print '# did A #' class B(MF): @staticmethod def testit(tv): return (tv == 'relates to B') def doit(self): print '# did B #' mydoit = MF.findit('relates to B') mydoit() mydoit = MF.findit('relates to A') mydoit() Test run == Python 2.4.1 (#2, Mar 31 2005, 00:05:10) [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] Type "help", "copyright", "credits" or "license" for more information. >>> import ootest # did B # # did A # >>> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OO refactoring trial ??
Clarifications: 1) Truth test simplified after a %) by Peter Otten - thanks. In reality the "testit" methods will all be quite different as you might imagine (as will the "doit" methods). 2) A final subclass will always return True, so there will always be a valid result. Following is a simple trial structure of a refactoring (top-down to OO) learning exercise I'm doing. Whether you call it a Factory pattern, COR pattern, or some hinze 57, I don't know what class to use till run time and I'm trying to avoid a lengthy "if" sequence, the test sequence is important, and to avoid code duplication I'll be using code objects in the "doit" methods. You've already given me many good ideas in previous threads and this is where it got you :~) This works, but would you please tell me: 1) What you don't like about the approach and/or how it might be improved 2) The implications of using this in a recursive approach (referenced from but outside the recursive function) 3) Any other comments you might offer Thank you, Lee C === ootest.py class MF(object): @staticmethod def findit(t): for it in MF.__subclasses__(): if it.testit(t): return it().doit class A(MF): @staticmethod def testit(tv): return (tv == 'relates to A') def doit(self): print '# did A #' class B(MF): @staticmethod def testit(tv): return (tv == 'relates to B') def doit(self): print '# did B #' mydoit = MF.findit('relates to B') mydoit() mydoit = MF.findit('relates to A') mydoit() Test run == Python 2.4.1 (#2, Mar 31 2005, 00:05:10) [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] Type "help", "copyright", "credits" or "license" for more information. >>> import ootest # did B # # did A # >>> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OO refactoring trial ??
On Tue, 28 Jun 2005 07:31:43 -0400, Chinook wrote (in article <[EMAIL PROTECTED]>): > [[ This message was both posted and mailed: see >the 'To' and 'Newsgroups' headers for details. ]] > Sorry for the duplication. I'm trying Hogwasher on OS X and it seems I better look around some more. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] String to List and back?
I'm missing something simple again. The simplified issue is: Python 2.4.1 (#2, Mar 31 2005, 00:05:10) >>> mystr = 'abc' # I can create a list of the string characters # with list comprehension >>> [c for c in mystr] ['a', 'b', 'c'] # Or just a simple builtin conversion function >>> list(mystr) ['a', 'b', 'c'] # But a conversion back to a string simply makes the # the list a string (I know there would have to be # special handling, but I noted it for illustration) >>> str(['a', 'b', 'c']) "['a', 'b', 'c']" # I know I could get the original string back by # rebuilding it with "for" loop >>> bts = '' >>> for c in strlist: ... bts += c ... >>> bts 'abc' # or even a function or class method which implemented # the "for" loop # I could also build a new string directly from the # original with a "for" loop Is there a one-liner like a builtin conversion function or list comprehension technique for the reverse (i.e. a list of strings back to a single string)? Thank you, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String to List and back?
Just the thing Light and Chuck - I thought there might be something simple (and I've used the join method before duh). Thanks, Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sudoku puzzle implementation
Liam, I couldn't get "into" your approach at the moment, but I was wondering if you have looked at the following: First, the Reverse Puzzle section of: http://cs.gettysburg.edu/~tneller/resources/ai-search/uninformed-java/ and secondly more generally: http://www.gamedev.net/reference/articles/article1374.asp http://www.gamedev.net/reference/articles/article1433.asp http://www.gamedev.net/reference/articles/article2041.asp Just some references I've used for puzzle problems, Lee C Liam Clarke wrote: > Hi all, > > If you aren't aware of Sudoku, this is it - http://www.sudoku.com/ > > Big craze at my office, and I've just been wracking my brains to try and > think of how to model it in Python (in order to solve.) > > What I'm not sure of is how to implement it. > > Basic rundown of a Sudoku puzzle is - > > > Generally composed of 9 x 9 squares, with the box divided further into > nine 3 x 3 mini-squares. Certain numbers are given at the start. > > Sudoku puzzles follow these rules - > > Each column, row and mini-square must contain the complete set of > numbers 1 - 9. > The numbers do not have to be sequential. > Each column, row, and mini-square can only contain each number once > > So that's it. So, to implement... > > I was thinking, a list of lists of lists > [grid[0], grid[1], grid[2], > grid[3], grid[4], grid[5], > grid[6], grid[7], grid[8] ] > > [ > [ > [a,b,c], > [d,e,f,], > [g,h,i] > ], > [ > [j,k,l], > [m,n,o], > [p,q,r] > > So forth. This approach gets a little confusing in terms of array > arithmetic but it's livable. > > Now each variable can be [1-9] > > Variable a (grid[0][0][0]) can be 1 unless > > grid - any member of grid[0] is 1 > row - any member of grid[1][0] or grid[2][0] is 1 > column - grid[3][x][0] or grid[6][x][0] is 1 (where x is an index of 0 to 2) > > My current approach is to start with each variable as a list of numbers > [1-9] and to pop numbers based on the above rules, > but I got thinking about sets and what not, and wondered if they would > work better, unions and intersections and whatnot. > > Problem is, I don't know the first thing about set mathematics. > > Any pointers in the right direction would be gratefully appreciated. > > Regards, > > > Liam Clarke > -- > '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] Single Underscore
Kevin Reeder wrote: >What's the significance of naming a variable with a single >underscore as its first character? For example, I'm looking at >find.py in the standard library and it has variables named _debug >and _prune. > >Kevin >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > Kevin, If it is naming conventions then possibly they mean a private property or method. http://jaynes.colorado.edu/PythonGuidelines.html Lee C ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor