Re: [Tutor] List of regular expressions
Hi Shidan! on Wed, 22 Jun 2005 00:28:44 -0400 Shidan <[EMAIL PROTECTED]> wrote : - Shidan > Hi I have a list of regular expression patterns like such: Shidan > Shidan > thelist = ['^594694.*','^689.*','^241.*','^241(0[3-9]|1[0145]|2[0-9]|3[0-9]|41|5[1-37]|6[138]|75|8[014579]).*'] Shidan > Shidan > Shidan > Now I want to iterate thru each of these like: Shidan > Shidan > for pattern in thelist: Shidan > regex=re.compile(pattern) Shidan > if regex.match('24110'): Shidan > the_pattern = pattern Shidan > . Shidan > . Shidan > sys.exit(0) Shidan > Shidan > but in this case it will pick thelist[2] and not the list[3] as I wanted to, Shidan > how can I have it pick the pattern that describes it better from the list. Perhaps you can reorder "thelist" in such a way, that the more extact pattern come's first thelist = ['^594694.*','^689.*','^241(0[3-9]|1[0145]|2[0-9]|3[0-9]|41|5[1-37]|6[138]|75|8[014579]).*','^241.*'] So you cann exit if '^241(0[3-9]|1[0145]|2[0-9]|3[0-9]|41|5[1-37]|6[138]|75|8[014579]).*' is found, because here you want 241 followed by the number-combination which is an exacter description as '241.*'. --- end -- HTH Ewald ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. Static Methods
- Original Message - From: "Kent Johnson" <[EMAIL PROTECTED]> > No, a classmethod is passed the class that it is called on. > If you have an inheritance tree you don't know this with a staticmethod. Aha! Like the OP I was aware of the class/no class distinction but couldn't see how this helped since a static method implicitly knows hich class it is in. But this example shows the big difference, the static method knows its in T1 but is not aware of inheritance so will always respond as a T1. The classmethod is aware of inheritance and will respond as whatever class is being accessed. So If I have a heirarchy of shapes and want a class method that only operates on the shape class itself, not on all the subclasses then I have to use staticmethod whereas if I want the class method to act on shape and each of its sub classes I must use a classmethod. The canonical example being counting instances. staticmethod would only allow me to count shapes but class method would allow me to count all the sub classes separately. Mind you this would require reprogramming the class method for each new shape which is probably a bad idea - overriding would be a better approach IMHO... Neat, I understand, I think... Thanks Kent. Alan G. > >>> class Test(object): > ... @staticmethod > ... def static(): # no args > ... print 'I have no clue how I was called' > ... @classmethod > ... def cls(cls): > ... print 'I was called on class', cls > ... > >>> class T2(Test): > ... pass > ... > >>> t2=T2() > >>> t2.static() > I have no clue how I was called > >>> t2.cls() > I was called on class > >>> T2.cls() > I was called on class ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of regular expressions
> for pattern in thelist: >regex=re.compile(pattern) >if regex.match('24110'): >the_pattern = pattern >. >. >sys.exit(0) > > but in this case it will pick thelist[2] and not the list[3] as I wanted to, > how can I have it pick the pattern that describes it better from the list. Define 'better'. Both regex describe it equally well, how is Python supposed to know which one is 'better'? You have to tell it by ordering your list according to your idea of better, usually meaning most specific test first. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how do i pause a script ?
>Hey all, >how do i pause a script. like >print 'something' >pause a half second >print 'something else' > > Hi, I think you're looking for 'sleep' in the time module. >>> import time >>> print "something" >>> time.sleep(1) >>> print "something else" That should do what you're describing. Incidentally, 'sleep' takes a float, not an int. So you can use time.sleep(1.5) to pause for one and a half seconds, and so forth. Hope that helps! If anyone knows a better way, feel free to correct me. I've only just started to learn Python myself. -- "Come back to the workshop and dance cosmological models with me?" - Peer, "Permutation City", by Greg Egan. Simon Gerber [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. Static Methods
Alan G wrote: > So If I have a heirarchy of shapes and want a class method that > only operates on the shape class itself, not on all the > subclasses then I have to use staticmethod whereas if I want > the class method to act on shape and each of its sub classes > I must use a classmethod. The canonical example being counting > instances. staticmethod would only allow me to count shapes > but class method would allow me to count all the sub classes > separately. Sounds good so far. Mind you this would require reprogramming the > class method for each new shape which is probably a bad > idea - overriding would be a better approach IMHO... Not sure why you think you have to write a new classmethod for each shape. Suppose you want to maintain creation counts for each class. Here is one way to do it using classmethods: class Shape(object): _count = 0# Default for classes with no instances (cls.count() never called) @classmethod def count(cls): try: cls._count += 1 except AttributeError: cls._count = 1 @classmethod def showCount(cls): print 'Class %s has count = %s' % (cls.__name__, cls._count) def __init__(self): self.count() class Point(Shape): pass class Line(Shape): pass p, p2, p = Point(), Point(), Point() Point.showCount() Line.showCount() l = Line() Line.showCount() ### prints Class Point has count = 3 Class Line has count = 0 Class Line has count = 1 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Changing what you've already printed
Is it possible (and easy) to change something you've already printed rather than print again? For example, if I'm making a little noughts and crosses game and I print the board: | | | | ___|___|___ | | | | ___|___|___ | | | | | | Then the computer has it's go, and rather than print the board out again and have the previous empty board appear higher up, I want to just add a X to the board I've already created. Eg. | | | | ___|___|___ | | | X | ___|___|___ | | | | | | I've programs do this on the command line in Linux, so I assume it must be possible. (I don't mind making my programs Linux only so that's not a problem). Any clues or pointers to documentation gratefully recieved. Thanks Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] loading an image into a Postgre database
Thanks Danny. That did the trick. I think I had thought about putting the variables in the execute statement, but I didn't run across any examples. I'll need to read the DB API 2.0 docs some more. Mike Danny Yoo wrote: >>Thankfully, you don't have to change much to fix the formatting bug. The >>only thing you'll need to do is let the SQL cursor do the value formatting >>for you: >> >>## >>sqlStatement = """INSERT INTO images (image) >> VALUES (%s); >>cur.execute(sqlStatement, (data_obj)) >>## > > > > Hi Mike, > > > Gaaa. I was a little sloppy there, wasn't I? Let me fix that: > > ## > sqlStatement = """INSERT INTO images (image) > VALUES (%s);""" > cur.execute(sqlStatement, (data_obj,)) > ## > > > My apologies! > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Changing what you've already printed
Well, it is certainly possible. First, the hard way: you use the ANSI termainal control commands (you can find the reference on the web, or if you need it I can send you an HTML file containing them). Second, the better way: you can use the ncurse library (via the curses Python module). There you'll get a whole toolkit for TUI (Terminal User Interface ;) ). BTW, if you happen to need this while drawing only a single line, the "\r" char gets you at the beginning of the current line ! So flushing stdout and then sending the "\r" char will allow you to overwrite the current line. Pierre Ed Singleton a écrit : > Is it possible (and easy) to change something you've already printed > rather than print again? > > For example, if I'm making a little noughts and crosses game and I > print the board: > >| | >| | > ___|___|___ >| | >| | > ___|___|___ >| | >| | >| | > > Then the computer has it's go, and rather than print the board out > again and have the previous empty board appear higher up, I want to > just add a X to the board I've already created. > > Eg. > > >| | >| | > ___|___|___ >| | >| X | > ___|___|___ >| | >| | >| | > > I've programs do this on the command line in Linux, so I assume it > must be possible. (I don't mind making my programs Linux only so > that's not a problem). > > Any clues or pointers to documentation gratefully recieved. > > Thanks > > Ed > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77fax : (33) 4 67 61 56 68 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Changing what you've already printed
Curses might help you: http://docs.python.org/lib/module-curses.html Take a look at the Demo-directory which is included in the Python source-package. There is a curses subdir in it which might get you started. HTH, Wolfram Ed Singleton wrote: > Is it possible (and easy) to change something you've already printed > rather than print again? > > For example, if I'm making a little noughts and crosses game and I > print the board: > >| | >| | > ___|___|___ >| | >| | > ___|___|___ >| | >| | >| | > > Then the computer has it's go, and rather than print the board out > again and have the previous empty board appear higher up, I want to > just add a X to the board I've already created. > > Eg. > > >| | >| | > ___|___|___ >| | >| X | > ___|___|___ >| | >| | >| | > > I've programs do this on the command line in Linux, so I assume it > must be possible. (I don't mind making my programs Linux only so > that's not a problem). > > Any clues or pointers to documentation gratefully recieved. > > Thanks > > Ed > ___ > 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] Changing what you've already printed
Yeah I think curses looks like it will do what I want. It doesn't look particularly easy to use (the [y,x] format is going to trip me up a lot) but I think I could write myself a simple interface to it that would make it simpler for me to use. Pierre's '/r' char thing isn't enough for what I want to do, but it's well worth knowing. Thanks All Ed On 6/22/05, Wolfram Kraus <[EMAIL PROTECTED]> wrote: > Curses might help you: > http://docs.python.org/lib/module-curses.html > > Take a look at the Demo-directory which is included in the Python > source-package. There is a curses subdir in it which might get you started. > > HTH, > Wolfram > > Ed Singleton wrote: > > Is it possible (and easy) to change something you've already printed > > rather than print again? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] CGIXMLRPCRequestHandler doesn't actually work, does it?
I believe I've tried every setting known to man, in every script in every little scrap of documentation available. XMLRPC requests using SimpleXMLRPCRequestHandler -- no problem. But try to run them as a CGI script, and I get system lock ups and that's all. No error codes; no response whatsoever. I am using Python 2.3, Windows XP. I have run other CGI scripts in the same directory, so I know that works. Has anyone used this successfully? Can you share demo server and client scripts -- just an echo function or something? Ron ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MySQL Connection Function
On Tue, 21 Jun 2005 19:13:43 -0400 Python <[EMAIL PROTECTED]> wrote: Alright, I've had time to play with this and wanted to be sure I understand this well. It works, so I understand enough to make it work. However, I see a difference between your code and mine that makes me think I've missed something other than the relationship between the cursor & the connection > >>> > #! /usr/bin/python > # sqlconnect.py > import MySQLdb as SQLdb > I didn't see anything in the tutorial about the import as. Is this simply assigning the module a variable name? > > def connect( parm_name): > parms = db_parms[parm_name] > return SQLdb.connect( **parms) > O.k., I was attempting to return "Con", not sql.connect() in my function. I think that's where part of my problem was. > > Use a dictionary to save the connect parameters. Different users and > circumstances can require different parameters. To use this code > > import sqlconnect as sql > conn = sql.connect('regular') > > # then get a cursor so that you can do something to the database > curs = conn.Cursor() > curs.execute(Some_SQL_Command) > results = curs.fetchall() > curs.close() > As is, I can copy/paste the 4 lines above into each function I've defined, and it works like a charm. What I would like now, is to use this like so: ### function for the cursor ### def get_Curs(): curs = conn.Cursor() curs.execute(sqlCmd) results = curs.fetchall() curs.close() Would I use curs.execute as an argument to get_Curs()? And would I then feed sqlCmd into mbr_Phone(sqlCmd)? ### Run get_Curs() for this query and give the user the results.### def mbr_Phone(): # how to get the cursor? get_Curs()# # Make SQL string and execute it. sqlCmd = "SELECT fst_name, lst_name, hphn FROM person\ order by lst_name" print 'Phone List' for record in Results: print record[0] , '', record[1], '\t', record[2] def a_Query(sqlCmd): get_Curs() code for a_Query def other_Query(sqlCmd): get_Curs() code for other_Query Kind of like this, anyway. Thanks for your patience. :) Don -- evangelinuxGNU Evangelist http://matheteuo.org/ http://chaddb.sourceforge.net/ "Free software is like God's love - you can share it with anyone anytime anywhere." ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] question about programmers
What's the average age of a python user? This is my only question about programmers themselves. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about programmers
If that's your only question about programmers then you aint been around them much. I'm 45. From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Wednesday, June 22, 2005 14:38 To: tutor@python.org Subject: [Tutor] question about programmers What's the average age of a python user? This is my only question about programmers themselves. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. Static Methods
> class Shape(object): > _count = 0 > > @classmethod > def count(cls): > try: > cls._count += 1 > except AttributeError: > cls._count = 1 Ah, clever. This is where I thought I'd need an if/elif chain, adding a new clause for each subclass. i never thought of using a try/except to add an attribute to subclasses based on cls. I like it. Thanks again Kent. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about programmers
On Wed, 22 Jun 2005 [EMAIL PROTECTED] wrote: > What's the average age of a python user? This is my only question about > programmers themselves. Hi Catdude, We actually had a small thread about this a few months back. The subject line "O.T." from that thread didn't make it particularly easy to find. *grin* But here it is: http://mail.python.org/pipermail/tutor/2004-December/034373.html The message threading doesn't work perfectly, but just search for the subject "O.T." from the December 2004 archives, and you should be able to find the right messages. Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Changing what you've already printed
> Is it possible (and easy) to change something you've already printed > rather than print again? Its possible. How easy depends on where you are printing. Using curses in a character based terminal its easy, just define the window within the screen that you want to change. Similarly any GUI toolkit will likewise be easy. But if you are uising a teletype interface you are reduced to using backspace characters and it gets messy, even with a cursor addressable screen (such as a 3270 or ANSI terminal) it still gets very hard to keep track of exactly what to erase and when. > For example, if I'm making a little noughts and crosses > game and I print the board: I'd use curses for this. Or better still a GUI. > I've programs do this on the command line in Linux, so > I assume it must be possible. Curses comes as standard on linux... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about programmers
> What's the average age of a python user? This is my only question about > programmers themselves. > There was a thread on this on comp.lang.python recently. Try searching google groups and you should find literally hundreds of replies! >From memory average was around 30 but with a distribution from about 10 to over 70... Be really keen and write a python program to collect the answers parse out the ages and do the sums... :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] age of python programmers
On Wed, 22 Jun 2005, Danny Yoo wrote: > > > On Wed, 22 Jun 2005 [EMAIL PROTECTED] wrote: > > > What's the average age of a python user? This is my only question about > > programmers themselves. > > The message threading doesn't work perfectly, but just search for the > subject "O.T." from the December 2004 archives, and you should be able to > find the right messages. Ah, from that thread, Roel Schroeven mentioned that he has collected statistics from comp.lang.python, and nicely presented them on his web site: http://mail.python.org/pipermail/tutor/2004-December/034378.html http://www.roelschroeven.net/pythonages/ So there you go. *grin* I'm sure it's not completely representative, but it appears to be a good rough sample. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Changing what you've already printed
> BTW, if you happen to need this while drawing only a single line, the > "\r" char gets you at the beginning of the current line ! And '\h' should delete back one char. Between the two techniques you can control a single line, but not, sadly, a noughts and crosses board! Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List of regular expressions
On Wed, 22 Jun 2005, Shidan wrote: > Hi I have a list of regular expression patterns like such: > > thelist = ['^594694.*','^689.*','^241.*', >'^241(0[3-9]|1[0145]|2[0-9]|3[0-9]|41|5[1-37]|6[138]|75|8[014579]).*'] > > > Now I want to iterate thru each of these like: > > for pattern in thelist: > regex=re.compile(pattern) > if regex.match('24110'): > the_pattern = pattern > . > . > sys.exit(0) > > but in this case it will pick thelist[2] and not the list[3] as I wanted > to, how can I have it pick the pattern that describes it better from the > list. Hi Shidan, Regular expressions don't have a concept of "better match": a regular expression either matches a pattern or it doesn't. It's binary: there's no concept of the "specificity" of a regular expression match unless you can define one yourself. Intuitively, it sounds like you're considering anything that uses a wildcard to be less match-worthy than something that uses simpler things like a character set. Does that sound right to you? If so, then perhaps we can write a function that calculates the "specificity" of a regular expression, so that 'theList[2]' scores less highly than 'theList[3]'. You can then see which regular expressions match your string, and then rank them in terms of specificity. But it's important to realize that what you've asked is actually a subjective measure of "best match", and so we have to define specifically what "best" means to us. (Other people might consider short regular expressions to be better because they're shorter and easier to read!) Tell us more about the problem, and we'll do what we can to help. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. Static Methods
This is a neat trick. But can't this also be done with a static method that accesses a static data attribute the same way? Alan G wrote: >>class Shape(object): >> _count = 0 >> >> @classmethod >> def count(cls): >>try: >> cls._count += 1 >>except AttributeError: >> cls._count = 1 >> >> > >Ah, clever. This is where I thought I'd need an if/elif >chain, adding a new clause for each subclass. i never thought of >using a try/except to add an attribute to subclasses based on cls. > >I like it. > >Thanks again Kent. > >Alan G. > >___ >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] loading an image into a Postgre database
On Wed, 22 Jun 2005, Mike Hansen wrote: > Thanks Danny. That did the trick. I think I had thought about putting > the variables in the execute statement, but I didn't run across any > examples. I'll need to read the DB API 2.0 docs some more. Hi Mike, No problem; it's actually a really common mistake in many introductory language/db binding tutorials too. AMK's tutorial doesn't mention it: http://www.amk.ca/python/writing/DB-API.html so you'd have to either read the API really closely, read a tutorial that does mention it, or get someone to point the problem out. *grin* By the way, if you're going to do a lot of DB stuff, you may find the DB-SIG a useful resource too: http://mail.python.org/mailman/listinfo/db-sig Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] samples
Hello, everyone! I hope this isnt the wrong place to post this, but the internet has not been very helpful to me on this point. I am looking for several (open-source, obviously) clones of Bejeweled (the pop cap game) or something like it. There is one listed at pygame (and that same one is referenced at sourceforge and other places), and I have downloaded it and am trying to go through the code, but it's made up of 46 different .py files, none of which seem to be the "main" game (lots of little modules, like an input box, a high score list, etc). It's a lot harder for someone new to programming to read. (It doesn't actually play on my computer either, but that's another matter). Also, the reason that I would like to see several examples is that I would like to see how different people approach things like keeping track of the columns' being filled or not, how they approach timing questions, whether or not they give hints, that kind of thing. I did this when first beginning python, with simple arcade games: it helps me a great deal to see how different people try to do the same (or similar) things in their code, especially when that code is actually documented. As I've said in here a hundred times, I am very new to this, and I know that my experience with space invaders - type games really helped solidify my understanding of what I was doing in my own game. I have donwloaded tetris clones as well, and have made one myself (getting toward the falling objects idea, anyway), but they havent been on a static board which is filled at all times with objects. Nor have I ever, actually, done anything with mouse clicks (!!). In any case, I am certainly not asking for howto's on all these complex subjects. For one, I have stacks of howto's and other books at my disposal, and what's more, I want to figure it out on my own. What I *am* looking for, if you have it or know of anyone who does, is *simple* source code files (preferrably the entire game's code is in one .py file), with lots of documentation, to familiarize myself further with examples of code, and particularly, to see how others attack some of the problems I am looking at now. Does anyone have any little "gamelets" like these, or know of well-documented comparable examples? I'd particularly love some bejeweled examples, but really, the more "little examples" I can look through and play with the better off I am. (And yes, I have checked and regularly do check pygame, sourceforge, google, and the like - I'm just looking for others that a). might not get posted online, b). someone just was playing around with, or c). that you have or know of that are particularly easy to 'read'). Thanks for any suggestions! ~Denise ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Changing what you've already printed
On Wed, 22 Jun 2005, Ed Singleton wrote: > Yeah I think curses looks like it will do what I want. It doesn't > look particularly easy to use (the [y,x] format is going to trip me up > a lot) but I think I could write myself a simple interface to it that > would make it simpler for me to use. Hi Ed, You might also find: http://hkn.eecs.berkeley.edu/~dyoo/python/circularwriting.py helpful as an example of what curses can do. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Changing what you've already printed
On Jun 23, 2005, at 01:04, Alan G wrote: Curses comes as standard on linux... They also do on Windows. Just listen to any user when the bloody thing crashes. (sorry, couldn't resist :p ) More seriously, I seem to recall that on the contrary, the Windows Python distribution does not include the curses module (you have to use msvcrt[?] instead). I wonder why, because I'm pretty sure I saw (C) curses-based applications running on Windows (NetHack is one, AFAIK). -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger Téléchargez cette version sur http://fr.messenger.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class vs. Static Methods
Hello Kent, This is the killer example I've been looking for. Now I understand. Sorry I've been so dense. This is way cool. Thanks. Wednesday, June 22, 2005, 4:39:38 AM, you wrote: KJ> Not sure why you think you have to write a new classmethod for KJ> each shape. Suppose you want to maintain creation counts for each KJ> class. Here is one way to do it using classmethods: KJ> class Shape(object): KJ> _count = 0# Default for classes with no instances (cls.count() never called) KJ> @classmethod KJ> def count(cls): KJ> try: KJ> cls._count += 1 KJ> except AttributeError: KJ> cls._count = 1 KJ> @classmethod KJ> def showCount(cls): KJ> print 'Class %s has count = %s' % (cls.__name__, cls._count) KJ> def __init__(self): KJ> self.count() KJ> class Point(Shape): pass KJ> class Line(Shape): pass KJ> p, p2, p = Point(), Point(), Point() KJ> Point.showCount() KJ> Line.showCount() KJ> l = Line() KJ> Line.showCount() KJ> ### prints KJ> Class Point has count = 3 KJ> Class Line has count = 0 KJ> Class Line has count = 1 KJ> Kent -- Best regards, Chuck ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] database app
Alan G wrote: >>>going on at a time I'd consider moving the database >>>or using snapshot technology or similar, Access locks >>>by pages (default 2K?) which can mean a lot of data >>>rows being locked by a single update. >>> >>> >>> >>ok, another question about this, if i use a snapshot, copy that >> >> >snapshot to > > >>the shared folder, i should be ok, to not lock it up for that main >> >> >app > > >>that uses this database right? the database has a .mdb extension, >> >> >ODBC > > >>provides for that right? i have been looking but not finding out if >> >> >i > > >>can copy the .mdb access database file to a windows XP shared >> >> >folder, > > >>copy that into my debian linux box and manipulate the database. >> >> > >I'm no expert but I don't think the file on its own will be enough. >You need the Access engine for the ODBC driver to work - so it needs >to be on windows. > >When I said snapshot I expected you to copy the data to a new Access >database, run the web app and alongside have a synchronisation job >running looking at the audit trails on both sides and synchronising >every 15 minutes or so. Of course that has its own locking issues >since Access doesn't really support that either, but maybe you could >copy the snapshot onto MySQL or somesuch, then at least the locking >problems are only in one direction! > >It might be worth checking MSDN to see if they recommend a solution. >I know we gave up on Access for web apps but presumably others >have grappled and found working solutions? > >Alan G. > > > > ok, i have looked and looked, and i cant find a way to use an access mdb file. yes from windows, no from linux. no way around it, i gotta be able to export access to a string of text somewhere. i think that access can export to and from excel - perhaps with the cv module i can do something with the log file. but right now, unless my research is bugged out, access is a no-go solution for linux. thanks, shawn ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] samples
D. Hartley wrote: > Does anyone have any little "gamelets" like these, or know of > well-documented comparable examples? I'd particularly love some > bejeweled examples, but really, the more "little examples" I can look > through and play with the better off I am. (And yes, I have checked > and regularly do check pygame, sourceforge, google, and the like - I'm > just looking for others that a). might not get posted online, b). > someone just was playing around with, or c). that you have or know of > that are particularly easy to 'read'). You might be interested in Livewires. It might be too simple for you, but it's worth looking at. http://www.livewires.org.uk/python/ Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Refreshing the interpreter environment
Javier Ruere wrote: > lawrence wang wrote: > >>How do I refresh the interpreter environment without restarting it, if >>possible? For example, I'm using the interpreter to test a class I'm >>writing; importing and instantiating it reveals a typo; I go and fix >>the typo. Now, is there any way to reload the class afresh? Simply >>importing again doesn't seem to do it. Thanks in advance for your >>help. > > > Yes: > > (echo "a=1" > a.py) > import a a.a > > 1 > (echo "a=2" > a.py) > a = reload(a) a.a > > 2 but be careful, if you try from a import a then the value of a will not change when a.py changes; you have created a new variable in your namespace that is bound to 1. Also if there is a class defined in a.py and you create instances of the class, when you reload(a) the instances will still refer to the old class. I'm sure there are many other ways this method can fail. You should have a clear understanding of namespaces to use it reliably; otherwise you may be surprised. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] strip an email
Does anyone know how to strip everything off of an email? i have a little app that i am working on to read an email message and write the body of a message to a log file. each email this address gets is only about three to five lines long. but i cannot seem to get just the body filtered through. i get all the headers, the path, what spam-wall it went through, etc... any suggestions would be greatly appreciated . thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor