OSCON: Volunteer for Python?
I'm trying to organize a group of people to run a Python booth at OSCON so that people will see an alternative to Perl. ;-) I'd like at least ten or twelve volunteers so that nobody has to pull a long shift. If you're interested, please subscribe to the OSCON mailing list: http://mail.python.org/mailman/listinfo/oscon -- Aahz ([EMAIL PROTECTED]) * http://www.pythoncraft.com/ f u cn rd ths, u cn gt a gd jb n nx prgrmmng. -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
Re: Dealing with marketing types...
Paul Rubin replied to me: If you're running a web site with 100k users (about 1/3 of the size of Slashdot) that begins to be the range where I'd say LAMP starts running out of gas. Let me elaborate a bit. That claim of 100K from me is the entire population of people who would use bioinformatics or chemical informatics. It's the extreme upper bound of the capacity I ever expect. It's much more likely I'll only need to handle a few thousand users. I believe LiveJournal (which has something more like a million users) uses methods like that, as does ezboard. There was a thread about it here a year or so ago. I know little about it, though I read at http://goathack.livejournal.org/docs.html ] LiveJournal source is lots of Perl mixed up with lots of MySQL I found more details at http://jeremy.zawodny.com/blog/archives/001866.html It's a bunch of things - Perl, C, MySQL-InnoDB, MyISAM, Akamai, memcached. The linked slides say lots of MySQL usage. 60 servers. I don't see that example as validating your statement that LAMP doesn't scale for mega-numbers of hits any better than whatever you might call printing press systems. As a simple example, that article's advice of putting all fine grained session state into the database (so that every single browser hit sets off SQL queries) is crazy. To be fair, it does say database plus cache though the author suggests the place for the cache is at the HTTP level and not at the DB level. I would have considered something like memcached perhaps backed by an asychronous write to a db if you want the user state saved even after the cache is cleared/reset. How permanent though does the history need to be? Your approach wipes history when the user clears the cookie and it might not be obvious that doing so should clear the history. In any case, the implementation cost for this is likely higher than what you did. I mention it to suggest an alternative. As for big, hmm, I'd say as production web sites go, 100k users is medium sized, Slashdot is largish, Ebay is big, Google is huge. I'ld say that few sites have 100k users, much less daily users with personalized information. As a totally made-up number, only few dozens of sites (maybe a couple hundred?) would need to worry about those issues. If that's indeed the case then I'll also argue that each of them is going to have app-specific choke points which are best hand-optimized and not framework optimized. Is there enough real-world experience to design a EnterpriseWeb-o-Rama (your printing press) which can handle those examples you gave any better than starting off with a LAMP system and hand-caching the parts that need it? Andrew [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: What language to manipulate text files
On Saturday 11 June 2005 11:37 pm, ross wrote: I want to do some tricky text file manipulation on many files, but have only a little programming knowledge. [...] Would Python be best, or would a macro-scripting thing like AutoHotKey work? I thought about Perl, but think I would learn bad habits and have hard to read code. Both Perl and Python are *extremely* good at this kind of work. This is pretty much what inspired Perl, and Python implements most of the same toolset. You will solve many of these kinds of problems using regular expressions (built-in first-class object in Perl, created from strings in Python using the re module). No surprise of course that I would choose Python. Mainly because of what it provides beyond regular expressions. Many simple cases can be handled with string methods in Python (check the Sequence types information in the built-ins section of the Library Reference -- also look at the string module, though it's usually easier to use the string methods approach). You will probably end up with more readable code using Python and take less time to develop sufficient proficiency to do the job with it. -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Dealing with marketing types...
Andrew Dalke [EMAIL PROTECTED] writes: I know little about it, though I read at http://goathack.livejournal.org/docs.html ] LiveJournal source is lots of Perl mixed up with lots of MySQL I found more details at http://jeremy.zawodny.com/blog/archives/001866.html It's a bunch of things - Perl, C, MySQL-InnoDB, MyISAM, Akamai, memcached. The linked slides say lots of MySQL usage. 60 servers. LM uses MySQL extensively but what I don't know is whether it serves up individual pages by the obvious bunch of queries like a smaller BBS might. I have the impression that it's more carefully tuned than that. I don't see that example as validating your statement that LAMP doesn't scale for mega-numbers of hits any better than whatever you might call printing press systems. What example? Slashdot? It uses way more hardware than it needs to, at least ten servers and I think a lot more. If LJ is using 6x as many servers and taking 20x (?) as much traffic as Slashdot, then LJ is doing something more efficiently than Slashdot. How permanent though does the history need to be? Your approach wipes history when the user clears the cookie and it might not be obvious that doing so should clear the history. The cookie is set at user login and it only has to persist through the login session. It's not as if the info only exists in the cookie and nowhere else. As for big, hmm, I'd say as production web sites go, 100k users is medium sized, Slashdot is largish, Ebay is big, Google is huge. I'ld say that few sites have 100k users, much less daily users with personalized information. As a totally made-up number, only few dozens of sites (maybe a couple hundred?) would need to worry about those issues. Yes, but for those of us interested in how big sites are put together, those are the types of sites we have to think about ;-). I'd say there's more than a few hundred of them, but it's not like there's millions. And some of them really can't afford to waste so much hardware--look at the constant Wikipedia fundraising pitches for more server iron because the Wikimedia software (PHP/MySQL, natch) can't handle the load. If that's indeed the case then I'll also argue that each of them is going to have app-specific choke points which are best hand-optimized and not framework optimized. Is there enough real-world experience to design a EnterpriseWeb-o-Rama (your printing press) which can handle those examples you gave any better than starting off with a LAMP system and hand-caching the parts that need it? Yes, of course there is. Look at the mainframe transaction systems of the 60's-70's-80's, for example. Look at Google. Then there's the tons of experience we all have with LAMP systems. By putting some effort into seeing where the resources in those things go, I believe we can do a much better job. In particular, those sites like Slashdot are really not update intensive in the normal database sense. They can be handled almost entirely with some serial log files plus some ram caching. At that point almost all the SQL overhead and a lot of the context switching can go away. -- http://mail.python.org/mailman/listinfo/python-list
Re: What language to manipulate text files
Why do people keep asking what language to use for certain things in the Python newsgroup? Obviously the answer is going to biased. Not that it's a bad thing because I love Python, but it doesn't make sense if you honestly want an objective opinion. R ross wrote: I want to do some tricky text file manipulation on many files, but have only a little programming knowledge. What are the ideal languages for the following examples? 1. Starting from a certain folder, look in the subfolders for all filenames matching *FOOD*.txt Any files matching in each folder should be copied to a new subfolder within the current folder called EATING with a new name of *FOOD*COPY.txt 2. Process each file as follows: Here is a simplified example of what I want as input and output. - input . 'several unknown lines of text file Get apples from apples shop Get oranges from oranges shop Get plums from plums shop Get pears from pears shop Eat from apples, oranges, plums, pears'whitespace at start of line is unimportant . 'more unknown lines of text file Chapter 1 Several lines of text about apples in here Chapter 2 Several lines of text about oranges in here Chapter 3 Several lines of text about plums in here Chapter 4 Several lines of text about pears in here - output . 'several unknown lines of text file Get apples from apples shop Get oranges from oranges shop Get plums from plums shop Get pears from pears shop Get bagels from bagels shop 'the Get lines... Get donuts from donuts shop 'can be in any order Eat from apples, bagels, oranges, plums, donuts, pears'whitespace at start of line is unimportant . 'more unknown lines of text file Chapter 1 Several lines of text about apples in here Chapter 2 Several lines of text about bagels in here Chapter 3 Several lines of text about oranges in here Chapter 4 Several lines of text about plums in here Chapter 5 Several lines of text about donuts in here Chapter 6 Several lines of text about pears in here Summary: I have added two new items to Get; I have put them into the comma-delimited list after searching for a particular fruit to put each one after; The Chapters are renumbered to match their position in the comma-delimited list. The several lines of text about each new item can be pulled from a new_foods.txt file (or a bagels.txt and a donuts.txt file). My first objective is to process the files as described. My second objective is to learn the best language for this sort of text manipulation. The language should run on Windows 98, XP and Linux. Would Python be best, or would a macro-scripting thing like AutoHotKey work? I thought about Perl, but think I would learn bad habits and have hard to read code. Thanks, Ross -- http://mail.python.org/mailman/listinfo/python-list
Re: Dealing with marketing types...
Drazen Gemic wrote: With Java I depend very little on customers IT staff, sysadmins, etc. If I need additional functionality in form library, extension, whatever, all I need is to drop another JAR in, and that does it. Maybe this is for you? http://peak.telecommunity.com/DevCenter/PythonEggs Kay -- http://mail.python.org/mailman/listinfo/python-list
Re: What language to manipulate text files
ross wrote: I want to do some tricky text file manipulation on many files, but have only a little programming knowledge. What are the ideal languages for the following examples? 1. Starting from a certain folder, look in the subfolders for all filenames matching *FOOD*.txt Any files matching in each folder should be copied to a new subfolder within the current folder called EATING with a new name of *FOOD*COPY.txt This should get you started: import errno from path import path # http://www.jorendorff.com/articles/python/path/ dst_dirpath = path(EATING) # create dst_dirpath try: dst_dirpath.makedirs() # make destination directory and its parents except OSError, err: # error! if err.errno = errno.EEXIST: # might just be that it already exists if not dst_dirpath.isdir(): # and it's a directory raise # if not, raise an exception for filepath in path(.).walkfiles(*FOOD*.txt): infile = file(filepath) outfile = file(dst_dirpath.joinpath(filepath.namebase+_COPY.txt)) ...do processing here... My first objective is to process the files as described. My second objective is to learn the best language for this sort of text manipulation. The language should run on Windows 98, XP and Linux. Would Python be best, or would a macro-scripting thing like AutoHotKey work? Personally, I'd use Python, but what do you expect when you ask here? -- Michael Hoffman -- http://mail.python.org/mailman/listinfo/python-list
Streaming Data Error in .read() (HTTP/ICY) Possible Bug?
I playing around with streaming shoutcast mp3s. Here is some sample code: --- import httplib # Put together the headers headers = {Icy-MetaData:1} con = httplib.HTTPConnection('64.142.8.154', 8000) con.request(GET, /) stream = con.getresponse() print stream.status,stream.reason print stream.read() --- For which i get error: bash-2.05b$ ./woxy_saver.py 200 Traceback (most recent call last): File ./woxy_saver.py, line 21, in ? print stream.read() File C:\python24\lib\httplib.py, line 463, in read s = self._safe_read(self.length) File C:\python24\lib\httplib.py, line 545, in _safe_read chunk = self.fp.read(amt) File C:\python24\lib\httplib.py, line 1273, in read return s + self._file.read(amt - len(s)) TypeError: unsupported operand type(s) for -: 'str' and 'int' bash-2.05b$ It seems somehow amt is turning into a str. Is this a bug in httplib.py or there something wrong with my code? Cheers - Haz -- http://mail.python.org/mailman/listinfo/python-list
Re: What is different with Python ?
John Machin [EMAIL PROTECTED] writes: Roy Smith wrote: Philippe C. Martin [EMAIL PROTECTED] wrote: Yet, many issues that a future software engineer should know are mostly hidden by Python (ex: memory management) and that could be detrimental. I know I'm going out on a limb by asking this, but why do you think future software engineers should know about memory management? Perhaps we have a terminology problem here i.e. different meanings of software engineer. Philippe started talking about CS courses, whereas you may be referring to people who have done an IT course or achieved a certification in the use of app development tool X. While I agree with John - software engineers should know something about memory managment - I sort of agree with Roy as well, in that, like Peter, I think memory management is something that doesn't need to be taught immediately. A modern programming environment should take care of the details, but a software engineer will be cognizant of the details, and know enough to know when they have to worry about it and when they can safely ignore it. I used to worry about register allocation. Today, I don't even know how many registers any machine I work on has. I used to worry about word size, and byte order. I used to worry about whether stacks grew up or down and addressing modes and floating point formats. Sure, somebody's got to worry about those things, but most people who write software can be blissfully ignorant (or, at best, dimly aware) of these issues because somebody else (compiler writer, hardware designer, operating system writer, etc) has already done the worrying. You would hope they'd done more than worry about it. However sometimes one's fondest hopes are dashed. You must have noticed the anguish in the timbot's posts that mention Windows 95 memory management. I think most of those things are indeed things that your average software engineer can ignore 90+% of the time. What makes someone a software engineer is that they know about those details, and know how they will affect the code they are writing - and hence when they have to worry about those details. Oddly enough, I find similar comments apply to a lot of the data structures I learned in school. I recently applied for a job that had a series of essay questions in the application. They had a series of problems with requests for solutions, and my immediate reaction to each was to reach for off-the-shelf software to solve the problem. While they wanted - and I provided - a discussion of data structures and big-O running time for various operations, all the things they wanted to do were essentially solved problems, and there was debugged and tuned code available to deal with things - and it's much faster to not write software if you can to solve the problem. For instance, one problem was You have two files that have lists of 1 billion names in them. Print out a list of the names that only occur in one of the files. That's a one-line shell script: comm -12 (sort file_one) (sort file_two) I gave them that answer. I also gave them a pseudo-code solution, but frankly, in real life, I'd install the shell script and get on with things. If I were hiring someone, I'd hire the person who gave me the shell script. Rather than spending hours/days debugging a program to solve the problem, I get a solution in minutes. If it runs into problems, *then* it's time to start hand coding the solution. There used to be a time when you had to worry about how many tracks to allocate when you created a disk file. When's the last time you worried about that? Seeing you asked: early 1970s, on an IBM 1800. But much more recently it certainly helped if one were slightly more than dimly aware of the difference between a FAT filesystem and an NTFS filesystem :-) For me it was the late 1970s, on an IBM 3081. But I was worried about disk sector sizes well into the 1990s. Since then I've worked on systems that didn't have a file system as such; it had a database of databases, and you queried the former to find the latter. mike -- Mike Meyer [EMAIL PROTECTED] http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Developers Handbook - Mistake done and corrected.
I am not mistaken at all and the use of hyperbole (2 billion) doesn't make your point . A post that piques the interest of even 50 people in an NG is a valid one. No doubt some people would be annoyed about a job posting of a Python vacancy... others would be interested in it... The original complainer expanded his complaint to say that I had not included sufficient information. I addressed that and apologised. For many that would have been the end of the matter. Since you want to talk about Usenet at large 1. I have made similar posts regarding books in my library to other NG's. 2. This is the only NG where anybody has complained (apart from your postage/starting price is too high). 3. This book has achieved the highest number of hits (179) than any other book I have promoted in this way. More even than a football book on the history of the Arsenal v Spurs fixture that I posted to both the Arsenal and Spurs NG (only managed 160). So the only NG that complained is the one that showed the most interest. What is far more off-topic for this NG are the posts pontificating about netiquette and usenet when really they are just whinges. -- http://mail.python.org/mailman/listinfo/python-list
Re: Streaming Data Error in .read() (HTTP/ICY) Possible Bug?
I should purhaps mention that i am basically trying to translate this. nc ~= telnet #!/bin/sh nc sc1.liquidviewer.com 9012 EOF GET / HTTP/1.0 Icy-MetaData:1 EOF -- http://mail.python.org/mailman/listinfo/python-list
Re: Dealing with marketing types...
Andrew Dalke [EMAIL PROTECTED] writes: Paul Rubin replied to me: As for big, hmm, I'd say as production web sites go, 100k users is medium sized, Slashdot is largish, Ebay is big, Google is huge. I'ld say that few sites have 100k users, much less daily users with personalized information. As a totally made-up number, only few dozens of sites (maybe a couple hundred?) would need to worry about those issues. I'd say quite a *lot* of sites have 100k users. A small client of mine was a (now defunct .com) that was focused on community building. They had a user base of a couple of million people, and you've probably never heard of The Park. They ran six servers, thousands of simultaneous users, and it was all built on LAMP. If you go looking for sites that offer the same kinds of things they did - free web hosting, free web-based email, web-based chat, calendering services, etc., you'll find a lot of such sites, and they all probably have more than 100K users. Of course, when you're talking about millions of web sites, a few sites could be a a fairly large number of them. An article I read recently made the point that I think you're trying to make. The author argued that for most sites, scalability just wasn't that big an issue. Web sites are cheap enough that they are affordable to relatively small communities, and in many cases a service that would bomb if they tried to go global with it would be a big success in a small community. As such, he expects the web to be dominated by sites that are really only of interest to a small community. For those sites, LAMP will work just fine. mike -- Mike Meyer [EMAIL PROTECTED] http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list
Re: case/switch statement?
On Sat, 11 Jun 2005 19:47:58 -0500, Skip Montanaro wrote: If the case values are constants known to the compiler, it can generate O(1) code to take the correct branch. (In fact, that could be done by the compiler for if statements such as in your example today. It just isn't.) It is precisely this behavior that is desired in many situations. See PEP 275 for details: Agreed. I certainly don't object to sensible optimizations! But the number of if...elif statements which can be optimised are a vanishingly small subset of the number of possible if...elif statements. And you can bet your last dollar that many people will use case statements even when doing so gives no advantage, in a mistaken opinion that it will be somehow magically faster. In fact, some languages even encourage this: I recall the old 4GL (back in the early 1990s when developers actually used the term Fourth Generation Language unselfconsciously) used to develop the spreadsheet Wingz. It included two different forms for case. By memory: case EXPR of: VALUE: SUITE VALUE: SUITE otherwise: SUITE (which could potentially be optimised) and a second form: case: EXPR of: SUITE EXPR of: SUITE otherwise: SUITE which was almost certainly nothing more than syntactic sugar for: if EXPR then: SUITE else if EXPR then: SUITE else: SUITE In any case, even when case statements can be optimized (and we all know the lesson about premature optimization), the original poster's complaint appeared to be purely a stylistic issue: he didn't relish using long if..elif statements. Who does? Not I. My comment, I hope, will remind folks that long pieces of branching code are ugly regardless of which syntax you use. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
subprocess module and blocking
I'm using a polling loop in a thread that looks approximately like this while 1: p = find_a_process() rc = p.poll() if rc is not None: out, err = p.communicate() #deal with output etc sleep(1) the process p is opened using p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=cwd) stdin is actually never written to. I notice that under both win32 and freebsd that things are fine provided that the subprocess doesn't write too much to stdout/stderr. However, the subprocess seems to lock often if too much is written (under freebsd I see a process state of POLL). I assume that the subprocess is filling up the pipe and then failing to wake up again. I had expected that subprocess would take care of this for me, but possibly I'm being utterly clueless and stupid. What should I do to avoid blocking in the subprocess? -- Robin Becker -- http://mail.python.org/mailman/listinfo/python-list
Re: case/switch statement?
Philippe C. Martin wrote: Leif K-Brooks wrote: Joe Stevenson wrote: I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the appropriate document, or post an example? I don't relish the idea especially long if-else statements. If you really want one, you could use http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410692. I _love_ Python! Well, if you loved that, here's something even more evil. This was Bengt Richter's original idea that Cliff Wells and I improved upon. First define some functions and classes: . _cache = {} . . class _BaseCaseException(Exception): . pass . . def switch(v): . if v not in _cache: . class _CaseException(_BaseCaseException): . pass . _cache[v] = _CaseException . raise _cache[v] . . def case(v): . if v not in _cache: . class _CaseException(_BaseCaseException): . pass . _cache[v] = _CaseException . return _cache[v] . . default = _BaseCaseException Then you can define a switch statement like this: . x = 2 . . try: switch(x) . . except case(1): . print Case 1 . . except case(2): . print Case 2 . . except case(3): . print Case 3 . . except default: . print Default . . except NameError: . print You can still catch regular exceptions like NameError I-love-Python-too-but-this-isn't-why-ly yr's, -- CARL BANKS -- http://mail.python.org/mailman/listinfo/python-list
Re: Best Web dev language
John Roth [EMAIL PROTECTED] writes: Mike Meyer [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Jon Slaughter [EMAIL PROTECTED] writes: Someone mentioned that you might require JavaScript on the client side. I recommend against that - people and organizations disable JavaScript for security reasons, and browsers on portable devices may not have JavaScript at all. Why limit your audience? If you understand HTML, it's possible to write a web page that uses JavaScript (or any other such technology) for flashy effects, but still functions properly if the user has disabled JavaScript, or doesn't have it available. But that's a long discussion - see URL: http://www.mired.org/home/mwm/papers.green.html for more information. I would have said that at one time, but then the world changed with AJAX, expecially with Google using very script heavy applications for all of their new work. It leads to very responsive web applications. Actually, AJAX just makes the case for wanting JavaScript turned on stronger - it doesn't change the fundamental facts of what's going on. People/organization will still turn off JavaScript because it represents a security risk. Low-end network devices will still have browsers that can't do JavaScript. You can still either code your pages to alienate such users, or you can provide them with the same basic functionality as they'd get if they had JavaScript, except it won't be as responsive/flashy as it would be if they did. Try Googles new work with JavaScript turned off. You'll find that a lot of the new stuff works fine without it, thought it may not be as spiffy. For those that don't, they warn the user that it won't work, which means they are doing better than 90% of the sites that require JavaScript on the web. mike -- Mike Meyer [EMAIL PROTECTED] http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list
Re: What language to manipulate text files
Roose wrote: Why do people keep asking what language to use for certain things in the Python newsgroup? Obviously the answer is going to biased. Not that it's a bad thing because I love Python, but it doesn't make sense if you honestly want an objective opinion. R What usenet group is it best to ask in then? Is there one where people have good knowledge of many scripting languages? Ross -- http://mail.python.org/mailman/listinfo/python-list
How to get/set class attributes in Python
I'm coming to Python from other programming languages. I like to hide all attributes of a class and to only provide access to them via methods. Some of these languages allows me to write something similar to this int age( ) { return theAge } void age( x : int ) { theAge = x } (I usually do more than this in the methods). I would like to do something similar in Python, and I've come up with two ways to do it: The first one uses the ability to use a variable number of arguments ... not very nice. The other is better and uses __setattr__ and __getattr__ in this way: class SuperClass: def __setattr__( self, attrname, value ): if attrname == 'somevalue': self.__dict__['something'] = value else: raise AttributeError, attrname def __str__( self ): return str(self.something) class Child( SuperClass ): def __setattr__( self, attrname, value ): if attrname == 'funky': self.__dict__['fun'] = value else: SuperClass.__setattr__( self, attrname, value ) def __str__( self ): return SuperClass.__str__( self ) + ', ' + str(self.fun) Is this the Pythonic way of doing it or should I do it in a different way or do I have to use setX/getX (shudder) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
On Sun, 12 Jun 2005 11:54:52 +0200, Kalle Anke [EMAIL PROTECTED] wrote: I'm coming to Python from other programming languages. I like to hide all attributes of a class and to only provide access to them via methods. Some of these languages allows me to write something similar to this int age( ) { return theAge } void age( x : int ) { theAge = x } (I usually do more than this in the methods). I would like to do something similar in Python, and I've come up with two ways to do it: The first one uses the ability to use a variable number of arguments ... not very nice. The other is better and uses __setattr__ and __getattr__ in this way: class SuperClass: def __setattr__( self, attrname, value ): if attrname == 'somevalue': self.__dict__['something'] = value else: raise AttributeError, attrname def __str__( self ): return str(self.something) class Child( SuperClass ): def __setattr__( self, attrname, value ): if attrname == 'funky': self.__dict__['fun'] = value else: SuperClass.__setattr__( self, attrname, value ) def __str__( self ): return SuperClass.__str__( self ) + ', ' + str(self.fun) Is this the Pythonic way of doing it or should I do it in a different way or do I have to use setX/getX (shudder) I'm totally new to Python myself, but my understanding is that -- http://mail.python.org/mailman/listinfo/python-list
unittest: collecting tests from many modules?
I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good old lines at the end: if __name__ == __main__: unittest.main() This is great, because each of the modules are runnable, and I can select classes or tests to run on the commandline if I want to. However, running all the tests from e.g. a Makefile is not that fun; I don't get a single pass/fail summary across the modules. What's the best way of creating a test.py which - aggregates the tests from all the test_*.py modules? - doesn't require me to enumerate all the test classes in test.py (forcing each module to define test_foo.theSuite or someting would be OK though) - retains the ability to select tests and verbosity (-q, -v) from the command line? Something like: import unittest import test_foo import test_bar if __name__ == __main__: unittest.main(modules = ['test_foo', 'test_bar']) Seems to me this should be possible, since all the logic for doing it /is/ there for the local module; I'd assume there would be a way to make unittest search additional modules for test classes. But my head starts spinning when I read the source code ... /Jorgen -- // Jorgen Grahn jgrahn@ Ph'nglui mglw'nafh Cthulhu \X/algonet.se R'lyeh wgah'nagl fhtagn! -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Kalle Anke wrote: I'm coming to Python from other programming languages. I like to hide all attributes of a class and to only provide access to them via methods. Some of these languages allows me to write something similar to this int age( ) { return theAge } void age( x : int ) { theAge = x } (I usually do more than this in the methods). You can 'hide' you getsetters using a property attribute[1]: class person(object): ... def __init__(self): ... self.age = 0 ... def set_age(self, age): ... print 'set %d' % age ... self.__age = age ... def get_age(self): ... print 'get' ... return self.__age ... age = property(get_age, set_age) ... joe = person() set 0 joe.age = 20 set 20 print joe.age get 20 [1]http://docs.python.org/lib/built-in-funcs.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Dealing with marketing types...
On Sat, 11 Jun 2005 11:51:02 -0500, tom [EMAIL PROTECTED] wrote: ... Let me add an Item #3 - If you have some entrepeneurial savvy and can keep your emotions out of it tou can simply tell them you have decided strike out on your own and tell them that you will be available. They will be happy to hear you are leaving and happier still to hear you can be available for backup. Their goals and fears are addressed at the same time. AND there is a very high possibility that they will *need* you at a later date for which you can charge them dearly. That last item #3 has actually worked for me with (2) prior employers. I did have to eat my indignation and keep it friendly but it did pay off in the end. Thomas Bartkus I have to say that, although I have yet to write a line of Python code for money, I've found that this concept basically works. When you realize that your employer is cutting you out to save the cost of paying you, funny enough, they'll be willing to -really- pay you as a consultant later when they get stuck, and one or more paying customers are impacted. They also win't mind figuring out how to have you come in after hours so it won't conflict with your new day job if you have one. In my case, the work was in VB/VBA, but the same principle should apply to any technology. Do make sure that your contract with any new employer allows you to do at least small amounts of moonlighting - they probably won't object. They will insist that any moonlighting shall not compete with their line of business, and you should agree to that stipulation. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
On Sun, 12 Jun 2005 03:15:27 -0700, Steve Jorgensen [EMAIL PROTECTED] wrote: ... Is this the Pythonic way of doing it or should I do it in a different way or do I have to use setX/getX (shudder) I'm totally new to Python myself, but my understanding is that ... Oops - I thought I cancelled that post when I relized I was saying nothing, but somehow, it got posted. -- http://mail.python.org/mailman/listinfo/python-list
Re: What is different with Python ?
I guess because I have mostly worked with embedded systems and that, although I have always tried to put abstraction layers between my applications and the hardware, some constraints still remain at the application level: (memory, determinism, re-entrance,...). You will notice that 99% of the embedded systems with realtime constaints use assembly, C/C++, or ADA. I agree with you and Peter though that these issues need not be treated on a first course. Yet, and depending on the ultimate goal (John spoke of IT versus CS) some of the newly trained folks should know about it. We could not enjoy Python if no one were here to implement its VM, I have not looked at the code, but I gather it is fairly complex and does require an amount of low level skills. Regards, Philippe Roy Smith wrote: Philippe C. Martin [EMAIL PROTECTED] wrote: Yet, many issues that a future software engineer should know are mostly hidden by Python (ex: memory management) and that could be detrimental. I know I'm going out on a limb by asking this, but why do you think future software engineers should know about memory management? I used to worry about register allocation. Today, I don't even know how many registers any machine I work on has. I used to worry about word size, and byte order. I used to worry about whether stacks grew up or down and addressing modes and floating point formats. Sure, somebody's got to worry about those things, but most people who write software can be blissfully ignorant (or, at best, dimly aware) of these issues because somebody else (compiler writer, hardware designer, operating system writer, etc) has already done the worrying. There used to be a time when you had to worry about how many tracks to allocate when you created a disk file. When's the last time you worried about that? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Kalle Anke wrote: I'm coming to Python from other programming languages. I like to hide all attributes of a class and to only provide access to them via methods. I'm pretty fond of this format for setting up class properties: class Klass(object): def propname(): def fget: pass def fset: pass def fdel: pass def doc: pass return locals() propname = property(**propname()) (Replacing 'pass' in the getters setters et.al with the actual functionality you want, of course...) This method minimises the leftover bindings, effectively leaving the getter/setter methods bound only to the property, ensuring they're only called when the property is acted upon. Incidentally, kudos thanks to whomever I originally stole it from :) -alex23 -- http://mail.python.org/mailman/listinfo/python-list
Re: Sending mail from 'current user' in Python
Mike Meyer wrote: BTW, an alternative for the username is the USER environment variable. I don't know whether or not it exists on Windows. Or LOGNAME. Don't about windows, though. I've also tried opening a pipe to sendmail, and feeding the message to that instead. This too works great (and does give an appropriate default 'From'), but that also turns my problem into the problem of finding the location of the sendmail program, which doesn't seem like much of an improvement, portability-wise. Well, you could provide a list of places to look for it. But you're right, this doesn't help much with portability. No, but at least it can be expected to do the right thing w.r.t. sending the mail. Finally, if at all possible I'd also like to get this working on Windows, so I'd rather stick with the standard smtplib if I can. smtplib needs an SMTP server to connect to. For unix systems, this is typically localhost. What do you use for Windows systems? Or are you connecting to your machine to deliver the mail? I'd be very surprised if the typical SMTP server is localhost on unix-like computers. Rather, sendmail is configured to transport the message to company/university mailserver(s). If that happens to fail, the mail is put on the queue at localhost, and transported later (e.g. via a cronjob) to the server. At no point is there a server on localhost involved. Of course, not everybody's computer is on such a network and a sendmail server may indeed be running on localhost, but that's not a very informed guess. Let the sendmail program take care of those details. The Unix Programming Frequently Asked Questions Q5.2 What's the best way to send mail from a program? is worth reading. I'd try some simple autodetection (Mike's suggestion sounded great) and prompt the user to correct the information, although sendmail itself ought to give good defaults, so this might not be necessary. Then try /usr/sbin/sendmail, /usr/libexec/sendmail and /usr/lib/sendmail. You could try using exitcode 127 to detect program could not be found or executed but I don't know how portable that is. I can't comment on the Windows side of things. Regards, Marcus -- http://mail.python.org/mailman/listinfo/python-list
Re: What language to manipulate text files
ross wrote: Roose wrote: Why do people keep asking what language to use for certain things in the Python newsgroup? Obviously the answer is going to biased. Not that it's a bad thing because I love Python, but it doesn't make sense if you honestly want an objective opinion. R What usenet group is it best to ask in then? Is there one where people have good knowledge of many scripting languages? What programming language is best for x questions can be asked in comp.programming and/or comp.lang.misc , and possibly in a domain-specific newsgroup if it exists, for example sci.math.num-analysis if x = scientific computing. The resulting debates contain both heat and light :). -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Kalle Anke wrote: I'm coming to Python from other programming languages. I like to hide all attributes of a class and to only provide access to them via methods. (...) Is this the Pythonic way of doing it or should I do it in a different way or do I have to use setX/getX (shudder) the pythonic way is to use property (as others have already explained) only when is *stricly necessary*. this may clarify things up: Python Is Not Java http://dirtsimple.org/2004/12/python-is-not-java.html HTH. -- deelan http://www.deelan.com -- http://mail.python.org/mailman/listinfo/python-list
Re: What is different with Python ?
John Machin [EMAIL PROTECTED] wrote: I know I'm going out on a limb by asking this, but why do you think future software engineers should know about memory management? Perhaps we have a terminology problem here i.e. different meanings of software engineer. Philippe started talking about CS courses, whereas you may be referring to people who have done an IT course or achieved a certification in the use of app development tool X. No, you've missed the point entirely. No, the problem is that I'm out on the limb, and you're still comfortably standing on the ground leaning up against the trunk. Climb up and come out on the limb with me. Now, stop hugging the trunk and take a few steps out here with me. Don't worry about how it's swaying, and whatever you do, don't look down. The point I was trying to make was that as computer science progresses, stuff that was really important to know a lot about becomes more and more taken for granted. This is how we make progress. I used to worry about memory busses at the milivolt and microsecond level. I knew about termination impedances and wired-OR logic, and power budgets and all that good stuff. Today all I know about memory is you go to www.crucial.com, type in your Visa card number, and the nice UPS guy shows up with some SIMMs in a few days. I expect that's all most current CS students know as well. Is that bad? Is their education somehow lacking because they don't understand why memory bus and transmission line belong in the same sentence? Not at all. All that's happened is that very important stuff has become so standardized that they don't have to worry about it any more and can spend their time and energy thinking about other problems that need to be solved today. There are lots of really important, hard, theoretical problems that today's CS majors need to be solving. User interfaces for the most part still suck. Self-configuring and self-healing high speed networks on a global scale. AI hasn't really progressed in 30 years. Computer vision and speech. Robotics. Cryptography and security. And what about flying cars? Just like you can't even begin to think about building today's GUI-driven desktop applications if you're still worrying about individual logic gates, you can't begin to think about solving some of these really hard problems (and others we haven't even imagined) if you're still worrying about memory buffer reference counting and garbage collection. Yesterday's research projects are today's utilities and tomorrow's historical footnotes. -- http://mail.python.org/mailman/listinfo/python-list
Re: Controlling a generator the pythonic way
Thomas Lotze wrote: Does anybody here have a third way of dealing with this? Sleeping a night sometimes is an insightful exercise *g* I realized that there is a reason why fiddling with the pointer from outside the generator defeats much of the purpose of using one. The implementation using a simple method call instead of a generator needs to store some internal state variables on an object to save them for the next call, among them the pointer and a tokenization mode. I could make the thing a generator by turning the single return statement into a yield statement and adding a loop, leaving all the importing and exporting of the pointer intact - after all, someone might reset the pointer between next() calls. This is, however, hardly using all the possibilities a generator allows. I'd rather like to get rid of the mode switches by doing special things where I detect the need for them, yielding the result, and proceeding as before. But as soon as I move information from explicit (state variables that can be reset along with the pointer) to implicit (the point where the generator is suspended after yielding a token), resetting the pointer will lead to inconsistencies. So, it seems to me that if I do want to use generators for any practical reason instead of just because generators are way cool, they need to be instantiated anew each time the pointer is reset, for simple consistency reasons. Now a very simple idea struck me: If one is worried about throwing away a generator as a side-effect of resetting the tokenization pointer, why not define the whole tokenizer as not being resettable? Then the thing needs to be re-instantiated very explicitly every time it is pointed somewhere. While still feeling slightly awkward, it has lost the threat of doing unexpected things. Does this sound reasonable? -- Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: Controlling a generator the pythonic way
Thomas Lotze wrote: A related problem is skipping whitespace. Sometimes you don't care about whitespace tokens, sometimes you do. Using generators, you can either set a state variable, say on the object the generator is an attribute of, before each call that requires a deviation from the default, or you can have a second generator for filtering the output of the first. Last night's sleep was really productive - I've also found another way to tackle this problem, and it's really simple IMO. One could pass the parameter at generator instantiation time and simply create two generators behaving differently. They work on the same data and use the same source code, only with a different parametrization. All one has to care about is that they never get out of sync. If the data pointer is an object attribute, it's clear how to do it. Otherwise, both could acquire their data from a common generator that yields the PDF content (or a buffer representing part of it) character by character. This is even faster than keeping a pointer and using it as an index on the data. -- Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: What is different with Python ?
On Sun, 12 Jun 2005, Mike Meyer wrote: For instance, one problem was You have two files that have lists of 1 billion names in them. Print out a list of the names that only occur in one of the files. That's a one-line shell script: comm -12 (sort file_one) (sort file_two) Incidentally, how long does sorting two billion lines of text take? The complementary question, of course, is how long does it take to come up with an algorithm for solving this problem that doesn't involve sorting the files?! the best thing i can come up with off the top of my head is making a pass over one file to build a Bloom filter [1] describing its contents, then going over the second file, checking if each name is in the filter, and if it is, putting it in a hashtable, then making a second pass over the first file, checking if each name is in the hashtable. this would work without the filter, but would require storing a billion names in the hashtable; the idea is that using the filter allows you to cut this down to a tractable level. that said, i'm not sure if it would work in practice - if you have a billion names, even if you have a filter a gigabyte in size, you still have a 2% false positive rate [2], which is 20 million names. tom [1] http://en.wikipedia.org/wiki/Bloom_filter [2] http://www.cc.gatech.edu/fac/Pete.Manolios/bloom-filters/calculator.html -- Think logical, act incremental -- http://mail.python.org/mailman/listinfo/python-list
Re: Controlling a generator the pythonic way
Thomas Lotze wrote: Mike Meyer wrote: What worries me about the approach of changing state before making a next() call instead of doing it at the same time by passing a parameter is that the state change is meant to affect only a single call. The picture might fit better (IMO) if it didn't look so much like working around the fact that the next() call can't take parameters for some technical reason. I suggest you make the tokenizer class itself into an iterator. Then you can define additional next() methods with additional parameters. You could wrap an actual generator for the convenience of having multiple yield statements. For example (borrowing Peter's PdfTokenizer): class PdfTokenizer: def __init__(self, ...): # set up initial state self._tokenizer = _getTokens() def __iter__(self): return self def next(self, options=None): # set self state according to options, if any n = self._tokenizer.next() # restore default state return n def nextIgnoringSpace(self): # alterate way of specifying variations # ... def _getTokens(self): while whatever: yield token def seek(self, newPosition): # change state here Kent -- http://mail.python.org/mailman/listinfo/python-list
Re: case/switch statement?
On Sun, 12 Jun 2005 10:35:42 +1000, Steven D'Aprano [EMAIL PROTECTED] wrote: I don't relish the idea of especially long case statements. I've never understood why something like: if x = 5: do_this elif x = 6: do_that else: do_something_else is supposed to be bad, but case of: x = 5: do_this x = 6: do_that otherwise: do_something_else is supposed to be good. In the truly general case, I agree. But twist your second example slightly into this: case x of: 5: do_this 6: do_that otherwise: do_something_else and the goodness is obvious: we're choosing functionality based on the value of x, so it's nice to see x in only one place. Arguably, a case statement *might* allow the compiler to optimize the code, maybe, sometimes. But in general, no such optimization is possible, so a case statement is merely equivalent to a series of if...elif... statements. I agree that in general, optimizing a series of if/elif statements is tricky, but your first example is very command and exactly the kind of code that a good optimizer *can* optimize (as long as x isn't pathological in the sense that evaluating it also changes its value or has other side effects). There is no case statement in Python. If you don't care about readability, one alternative is to use a dictionary: case = {5: do_this, 6: do_that} case.get(x, do_something_else)() I find this very readable. YMMV. I also find this easier to extend in the case (pardon the pun) that my program expands and x might now be 7 or 8 (or foo or 3j). The biggest drawback here, as others have noted in previous discussions, is that the do_* functions execute in a separate scope. Regards, Dan -- Dan Sommers http://www.tombstonezero.net/dan/ -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest: collecting tests from many modules?
Jorgen Grahn [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good old lines at the end: if __name__ == __main__: unittest.main() This is great, because each of the modules are runnable, and I can select classes or tests to run on the commandline if I want to. However, running all the tests from e.g. a Makefile is not that fun; I don't get a single pass/fail summary across the modules. What's the best way of creating a test.py which - aggregates the tests from all the test_*.py modules? - doesn't require me to enumerate all the test classes in test.py (forcing each module to define test_foo.theSuite or someting would be OK though) - retains the ability to select tests and verbosity (-q, -v) from the command line? I use your second point: I build a TestAll module. However, I'm going to look at building the test suite using the TestLoader class in the next version of PyFit. It sholdn't be all that difficult to find all the Test*.py modules in a directory, import them and use the TestLoader class to add them to the test suite. Or, for that matter, to use reflection to find all the classes that derive from TestCase and add them to the suite manually. That has the advantage that one could then select classes according to some parameter. John Roth Something like: import unittest import test_foo import test_bar if __name__ == __main__: unittest.main(modules = ['test_foo', 'test_bar']) Seems to me this should be possible, since all the logic for doing it /is/ there for the local module; I'd assume there would be a way to make unittest search additional modules for test classes. But my head starts spinning when I read the source code ... /Jorgen -- // Jorgen Grahn jgrahn@ Ph'nglui mglw'nafh Cthulhu \X/algonet.se R'lyeh wgah'nagl fhtagn! -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
On Sun, 12 Jun 2005 12:20:29 +0200, tiissa wrote (in article [EMAIL PROTECTED]): You can 'hide' you getsetters using a property attribute[1]: [1]http://docs.python.org/lib/built-in-funcs.html Thanks, this is exactly what I was looking for -- http://mail.python.org/mailman/listinfo/python-list
checking for when a file or folder exists, typing problems?
Hi I have a little program that is importing from os.path import exists, join, isdir, normpath, isfile at one point in my program I check if a file exists using if exists(c:\projects): and that works fine. If I change it to be if exists(thepath): where thepath is a commandline argument it does not work fine. Note that the commandline is c:\projects and when I print thepath to check what is going on it prints c:\projects The only thing I can assume is that there is some sort of typing problem going on here, but then it should go ahead and give an error then if it's getting something unexpected obviously. Any help on what this error is? Thanks -- Bryan Rasmussen -- http://mail.python.org/mailman/listinfo/python-list
Re: TKinter -- 'Destroy' event executing more than once?
For me, an 'is' test works to find out what widget the event is taking place on. # import Tkinter def display_event(e): print event received, e.widget, e.widget is t t = Tkinter.Tk() t.bind(Destroy, display_event) w = Tkinter.Entry(t) t.destroy() # This program prints: event received .-1209415348 False event received . True if that fails, you could compare str(e.widget) and t._w, though this can give a false positive if you have multiple Tk() instances---each Tk() instance is called .. Jeff pgpXtThASJKeK.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Scaling down (was Re: Dealing with marketing types...)
In article [EMAIL PROTECTED], Paul Rubin http://[EMAIL PROTECTED] wrote: What example? Slashdot? It uses way more hardware than it needs to, at least ten servers and I think a lot more. If LJ is using 6x as many servers and taking 20x (?) as much traffic as Slashdot, then LJ is doing something more efficiently than Slashdot. So what? I think you're missing the real point of the article: using LAMP scales *DOWN* in a way that enterprise systems don't. Getting your first prototype up and running is far more important than sheer scalability, and LAMP does have many mechanisms to obtain scalability when it's needed. -- Aahz ([EMAIL PROTECTED]) * http://www.pythoncraft.com/ f u cn rd ths, u cn gt a gd jb n nx prgrmmng. -- http://mail.python.org/mailman/listinfo/python-list
Slicing an IXMLDOMNodeList
Hi, I'm using MSXML to select elements from a XML document and want to slice off the last part of an IXMLDOMNodeList. import win32com.client xmldoc = win32com.client.Dispatch('msxml.DOMDocument') xmldoc.load('file.xml') True rgelem = xmldoc.selectNodes('/root/elem') if rgelem.length 10: ... rgelem = rgelem[-10:] ... Traceback (most recent call last): File interactive input, line 2, in ? File C:\Python24\Lib\site-packages\win32com\client\__init__.py, line 454, in __getattr__ raise AttributeError, '%s' object has no attribute '%s' % (repr(self), attr) AttributeError: 'win32com.gen_py.Microsoft XML, v4.0.IXMLDOMNodeList instance at 0x19500888' object has no attribute '__len__' The IXMLDOMNodeList obviously doesn't have a '__len__' member function (why not?). Is there a way to make the IXMLDOMNodeList support slicing? I've tried to give the rgelem object a __len__ member, but got an exception from __setattr__: File C:\Python24\Lib\site-packages\win32com\client\__init__.py, line 462, in __setattr__ raise AttributeError, '%s' object has no attribute '%s' % (repr(self), attr) AttributeError: 'win32com.gen_py.Microsoft XML, v4.0.IXMLDOMNodeList instance at 0x26395112' object has no attribute '__len__' Anybody know how to make this work? TIA, -- Marcel -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
On Sun, 12 Jun 2005 13:59:27 +0200, deelan wrote (in article [EMAIL PROTECTED]): the pythonic way is to use property (as others have already explained) only when is *stricly necessary*. this may clarify things up: Thanks for the link (although Java was only one of the languages I was thinking of). Anyway, I got another problem (read: being used to do it like this in other languages). I'm used to use statically typed languages and for me one of the advantages is that I can be sure that a parameter is of a certain type. So in Java I could write void doSomething( data : SomeClass ){ ... } and I would be sure at compile time that I would only get SomeClass objects as parameters into the method. In learning Python I've understood that I should write code in such a way that it can handle different data and this is fine with me. But what if I have a class where different attributes should only have values of a certain type and everything else is an error. For example, if I have an class that defines three attributes: first and last name plus email address. The only valid data types for the first two are strings and for the last an EmailAddress class. How should I handle this in Python? Should I use just don't care (but I'm going to send the data to a database so I need to ensure that the data is OK)? Should I use 'isinstance' and check manually? Or should I do something else? (I've been trying to figure out this and other things but I haven't found much in books or web sites) jem -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
On Sun, 12 Jun 2005 15:35:15 +0200, John Machin wrote (in article [EMAIL PROTECTED]): OTOH, I beseech you to consider an attitude transplant :-) ;-) I.e. put your effort into writing code that allows people to do useful things, rather than opaque guff full of __blahblah__ that stops them from doing dopey or evil things they're usually smart enough or righteous enough not to do anyway. I'm just trying to protect myself from myself :-) No, I'm playing around with different ways of doing things, trying to learn Python and how do things in a proper pythonic way. In this case I'm going to have a class with some properties that are going to be stored in a database, I don't want to read all the properties everytime I recreate the object from the database but I still want to give the impression that the attributes exists and is available. So my idea was to hide the actual database stuff ... BTW, what do you think of this: sys.maxint = -12345 I don't really understand what you're meaning. jem -- http://mail.python.org/mailman/listinfo/python-list
ElementTree Namespace Prefixes
Does anyone know how to make ElementTree preserve namespace prefixes in parsed xml files? The default behavior is to strip a document of all prefixes and then replace them autogenerated prefixes like ns0, ns1, etc. The correct behavior should be to write the file in the form that it was read, which it seems to do correctly for everything except namespace prefixes. The docs mention proper output can be achieved by using the Qname object, but they don't go into any detail. Any help is appreciated. Thanks, Chris Spencer -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
On Sun, 12 Jun 2005 15:35:46 +0200, Kalle Anke [EMAIL PROTECTED] wrote: In learning Python I've understood that I should write code in such a way that it can handle different data and this is fine with me. But what if I have a class where different attributes should only have values of a certain type and everything else is an error. For example, if I have an class that defines three attributes: first and last name plus email address. The only valid data types for the first two are strings and for the last an EmailAddress class. How should I handle this in Python? Should I use just don't care (but I'm going to send the data to a database so I need to ensure that the data is OK)? Should I use 'isinstance' and check manually? Or should I do something else? One key phrase here is duck typing: if it walks like a duck, and swims like a duck, and quacks like a duck, then it's a duck, or at least you can assume it's a duck. For example: def put_something( file_open_for_writing, something ): file_open_for_writing.write( str( something ) ) I don't care if file_open_for_writing is really a file, as long it has a write method that writes a string somewhere. The other key phrase is we're all adults here: if I import sin from the math module and pass it a unicode string, I get what I deserve. In lower-level methods/functions, I usually just assume that parameters are correct, and let the higher-level code catch any exceptions that occur becuase it (the higher-level code) messed up and passed the wrong kind of parameter. For example, UI code *has* to check things that users type, but once that happens, there's no need for my program to recheck every parameter on every function call all the way down. Either everything works, or that same UI code catches and logs a TypeError or ValueError or KeyError exception and asks the user what to do next. Regards, Dan -- Dan Sommers http://www.tombstonezero.net/dan/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
alex23 wrote: Kalle Anke wrote: I'm coming to Python from other programming languages. I like to hide all attributes of a class and to only provide access to them via methods. I'm pretty fond of this format for setting up class properties: class Klass(object): def propname(): def fget: pass def fset: pass def fdel: pass def doc: pass return locals() propname = property(**propname()) (Replacing 'pass' in the getters setters et.al with the actual functionality you want, of course...) This method minimises the leftover bindings, effectively leaving the getter/setter methods bound only to the property, ensuring they're only called when the property is acted upon. Incidentally, kudos thanks to whomever I originally stole it from :) -alex23 And a slight improvement in readability IMHO (for python 2.4+) is the following recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410698. Using the Property decorator, the property declaration above becomes: class Klass(object): @Property # --- the capitalized 'P' is not a typo def propname(): '''Documentation''' def fget: pass def fset: pass def fdel: pass The Property decorator peeks automagically the fget, fset, fdel and __doc__ from the property's locals(), instead of having the property return locals() explicitly. Also, it doesn't break when the property defines local variables other than [fget, fset, fdel, doc]. George -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Kalle Anke wrote: On Sun, 12 Jun 2005 13:59:27 +0200, deelan wrote (in article [EMAIL PROTECTED]): void doSomething( data : SomeClass ){ ... } and I would be sure at compile time that I would only get SomeClass objects as parameters into the method. Being an untyped language, Python does not require you to enforce types. However, for those that require such functionality, you can get away with using the assert statement. For example, if I wanted to make sure my function foo was only given instances of class Bar, I'd write something like: class Bar: pass def foo(bar): ... assert isinstance(bar, Bar), argument is not of type Bar ... print argument must be of type Bar ... bar = Bar() foo(bar) argument must be of type Bar foo(123) Traceback (most recent call last): File stdin, line 1, in ? File stdin, line 2, in foo AssertionError: argument is not of type Bar Chris -- http://mail.python.org/mailman/listinfo/python-list
Code documentation tool similar to what Ruby (on Rails?) uses
Hi, I wonder if there is a tool for generation Python API documentation that can include source code into HTML output. Example: http://api.rubyonrails.com/ I really like the possibility to click on show source link and read the source of the method! AFAIK it is not possible with Epydoc and Pydoc.. but maybe other tools? Thanks! -- Ksenia -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest: collecting tests from many modules?
Jorgen Grahn wrote: I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good old lines at the end: if __name__ == __main__: unittest.main() This is great, because each of the modules are runnable, and I can select classes or tests to run on the commandline if I want to. However, running all the tests from e.g. a Makefile is not that fun; I don't get a single pass/fail summary across the modules. What's the best way of creating a test.py which - aggregates the tests from all the test_*.py modules? - doesn't require me to enumerate all the test classes in test.py (forcing each module to define test_foo.theSuite or someting would be OK though) - retains the ability to select tests and verbosity (-q, -v) from the command line? Something like: import unittest import test_foo import test_bar if __name__ == __main__: unittest.main(modules = ['test_foo', 'test_bar']) Seems to me this should be possible, since all the logic for doing it /is/ there for the local module; I'd assume there would be a way to make unittest search additional modules for test classes. But my head starts spinning when I read the source code ... I had written a script to do something close to this; currently it doesn't do any kind of aggregation, but it should be easy to extend it as you like. What I don't like is the way it currently works: it replaces sys.modules['__main__'] for each unit test and then it execfile()s it, which seems like a hack. I didn't look into unittest's internals in case there is a more elegant way around this; if there isn't, a future version of unittest should address the automatic aggregation of tests, as py.test does already. The link to the script is http://rafb.net/paste/results/V0y16g97.html. Hope this helps, George -- http://mail.python.org/mailman/listinfo/python-list
Re: Code documentation tool similar to what Ruby (on Rails?) uses
What about doing it yourself? import inspect, os print pre%s/pre % inspect.getsource(os.makedirs) predef makedirs(name, mode=0777): makedirs(path [, mode=0777]) Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. This is recursive. head, tail = path.split(name) if not tail: head, tail = path.split(head) if head and tail and not path.exists(head): makedirs(head, mode) if tail == curdir: # xxx/newdir/. exists if xxx/newdir exists return mkdir(name, mode) /pre Michele Simionato -- http://mail.python.org/mailman/listinfo/python-list
Re: case/switch statement?
On Sun, 12 Jun 2005 08:33:32 -0400, Dan Sommers wrote: I've never understood why something like: if x = 5: do_this elif x = 6: do_that else: do_something_else is supposed to be bad, but case of: x = 5: do_this x = 6: do_that otherwise: do_something_else is supposed to be good. In the truly general case, I agree. But twist your second example slightly into this: case x of: 5: do_this 6: do_that otherwise: do_something_else and the goodness is obvious: we're choosing functionality based on the value of x, so it's nice to see x in only one place. Yes. But now change the references to do_this and do_that to ten lines of in-line code each, and add another dozen similar tests for 7, 8, etc, and by the time you get to the third page you've forgotten what the variable being tested is. Now, you or I will obviously never write such hard-to-maintain code wink, but some people will, and we'll have to maintain it. It isn't at all obvious that case statements are more readable than if...elif, nor are they necessarily faster at runtime, although they can be. Against that occasional optimization and sometimes increase in readability, you have disadvantages: more keywords, implicit tests instead of explicit, new syntax to learn. Arguably, a case statement *might* allow the compiler to optimize the code, maybe, sometimes. But in general, no such optimization is possible, so a case statement is merely equivalent to a series of if...elif... statements. I agree that in general, optimizing a series of if/elif statements is tricky, but your first example is very command and exactly the kind of code that a good optimizer *can* optimize (as long as x isn't pathological in the sense that evaluating it also changes its value or has other side effects). Yes. But that's also the sort of optimization that could be done for if...elif as well, without introducing new syntax and new reserved words. Case statements seem to be one of those things that Python newbies from other languages automatically ask for, but the case for introducing case (pun intended) doesn't appear to be strong enough to justify the amount of verbiage spent on it. And on that note, I will say no more on the subject. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
On Sun, 12 Jun 2005 14:40:26 +, Chris Spencer wrote: Being an untyped language, Python does not require you to enforce types. However, for those that require such functionality, you can get away with using the assert statement. Assuming that Python isn't executed with the optimize switch, which disables assert. $ python -O Python 2.3.3 (#1, May 7 2004, 10:31:40) [GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2 Type help, copyright, credits or license for more information. py py py def tester(x): ... assert type(x) == type(0) ... print %s is an integer. % x ... py tester(3) '3 is an integer' py tester(hello) 'hello is an integer' -- http://mail.python.org/mailman/listinfo/python-list
how to startup the default web browser to open a url?
I want to startup the default web browser(ie...) to open a url, execl can open a process, but when the process exit, the other process will exit too, has any why to deal this problem? -- http://mail.python.org/mailman/listinfo/python-list
Re: What language to manipulate text files
Hi Roose, Actually, it is a good thing because it allows those who know the Python language to be able to show the benefits and weaknesses of the language. Sure, the attitude here will be Yes, it's a great language. Yet, at the same time, it also enables the poster to be able to see potential benefits to Python that he or she may not of been aware of. If we don't let others know about the benefits of Python, who will? Brian --- Roose wrote: Why do people keep asking what language to use for certain things in the Python newsgroup? Obviously the answer is going to biased. Not that it's a bad thing because I love Python, but it doesn't make sense if you honestly want an objective opinion. R -- http://mail.python.org/mailman/listinfo/python-list
Re: checking for when a file or folder exists, typing problems?
Hi Bryan, Here's a potential idea. Try converting the variable to a string by using the following syntax: thePath = str(thePathArg) This will convert the current variable type to a string, which follows the data type syntax that you have specified at the beginning of your message. Hope this helps, Brian :-) --- Bryan Rasmussen wrote: Hi I have a little program that is importing from os.path import exists, join, isdir, normpath, isfile at one point in my program I check if a file exists using if exists(c:\projects): and that works fine. If I change it to be if exists(thepath): where thepath is a commandline argument it does not work fine. Note that the commandline is c:\projects and when I print thepath to check what is going on it prints c:\projects The only thing I can assume is that there is some sort of typing problem going on here, but then it should go ahead and give an error then if it's getting something unexpected obviously. Any help on what this error is? Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: how to startup the default web browser to open a url?
flyaflya wrote: I want to startup the default web browser(ie...) to open a url, execl can open a process, but when the process exit, the other process will exit too, has any why to deal this problem? You want the 'webbrowser' module. http://docs.python.org/lib/module-webbrowser.html Will McGugan -- http://www.willmcgugan.com .join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in jvyy*jvyyzpthtna^pbz) -- http://mail.python.org/mailman/listinfo/python-list
Re: python bytecode grammar
Terry Reedy [EMAIL PROTECTED] writes: M1st0 [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] where I can find the grammar of python bytecode ? ( better if is in BCF I believe the top-level production is something like BYTECODE := (OPCODE ARGS)* ROTFL :) -- http://mail.python.org/mailman/listinfo/python-list
Capture close window button in Tkinter
I am trying to make a simple data editor in Tkinter where each data element has a corresponding Entry widget. I have tried to use the FocusIn/FocusOut events to set a 'hasChanged' flag (if a record has not changed, the db doesnt need updating). This seems to work fine except that when the user finishes and clicks a done button or the close window button (in the root widget) no FocusOut event is triggered. I can trigger a FocusOut event if the done button opens another window (i.e. a messagebox) that takes focus. Enter and Leave follow the mouse, but dont trigger when the user tabs between fields. Is there a better way to monitor 'hasChanged'? Also, how do I capture the root window close button? Thanks, Bill -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Kalle Anke [EMAIL PROTECTED] writes: [snap] sys.maxint = -12345 I don't really understand what you're meaning. He meant None = 1 : -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree Namespace Prefixes
On Sun, 12 Jun 2005 15:06:18 +, Chris Spencer wrote: Does anyone know how to make ElementTree preserve namespace prefixes in parsed xml files? See the recent c.l.python thread titled ElemenTree and namespaces and started May 16 2:03pm. One archive is at http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/31b2e9f4a8f7338c/363f46513fb8de04?rnum=3hl=en Andrew [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest: collecting tests from many modules?
Jorgen Grahn wrote: I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good old lines at the end: if __name__ == __main__: unittest.main() What's the best way of creating a test.py which - aggregates the tests from all the test_*.py modules? - doesn't require me to enumerate all the test classes in test.py (forcing each module to define test_foo.theSuite or someting would be OK though) - retains the ability to select tests and verbosity (-q, -v) from the command line? Something like: import unittest import test_foo import test_bar if __name__ == __main__: unittest.main(modules = ['test_foo', 'test_bar']) Seems to me this should be possible, since all the logic for doing it /is/ there for the local module; I'd assume there would be a way to make unittest search additional modules for test classes. But my head starts spinning when I read the source code ... /Jorgen How about some variant of: import unittest import test_foo, test_bar, ... def set_globals(modules): glbl = globals() for module in modules: modprefix = module.__name__[5:] + '__' for element in dir(module): data = getattr(module, element) if isinstance(data, type) and issubclass(data, unittest.TestCase): glbl[modprefix + element] = data if __name__ == __main__: module = type(unittest) set_globals([mod for name, mod in globals().items() if name.lower().beginswith('test') and isinstance(mod, module)]) unittest.main() --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Peter Dembinski [EMAIL PROTECTED] schrieb im Newsbeitrag news:[EMAIL PROTECTED] | Kalle Anke [EMAIL PROTECTED] writes: | | [snap] | | sys.maxint = -12345 | | I don't really understand what you're meaning. | | He meant None = 1 : I'm sure you know that has become a no-no in Python 2.4+ ;) None = 1 SyntaxError: assignment to None -- Vincent Wehren -- http://mail.python.org/mailman/listinfo/python-list
Re: checking for when a file or folder exists, typing problems?
Bryan Rasmussen wrote: ... at one point in my program I check if a file exists using if exists(c:\projects): You should not be using a backslash in non-raw-string source to mean anything but escape the next character. The above should either be written as: if exists(rc:\projects): or: if exists(c:\\projects): I suspect you problem has to do with this difference, but perhaps not. Give exact short code that actually demonstrates the problem. --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: TKinter -- 'Destroy' event executing more than once?
Jeff Epler wrote: For me, an 'is' test works to find out what widget the event is taking place on. ... yes, I am apparently as stupid as I look. In my test code, I was trying if event is widget, and I just now saw that. Thanks! :) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
On Sun, 12 Jun 2005 15:35:46 +0200, Kalle Anke wrote: Anyway, I got another problem (read: being used to do it like this in other languages). I'm used to use statically typed languages and for me one of the advantages is that I can be sure that a parameter is of a certain type. So in Java I could write void doSomething( data : SomeClass ){ ... } and I would be sure at compile time that I would only get SomeClass objects as parameters into the method. In learning Python I've understood that I should write code in such a way that it can handle different data and this is fine with me. But what if I have a class where different attributes should only have values of a certain type and everything else is an error. For example, if I have an class that defines three attributes: first and last name plus email address. The only valid data types for the first two are strings and for the last an EmailAddress class. How should I handle this in Python? Should I use just don't care (but I'm going to send the data to a database so I need to ensure that the data is OK)? Should I use 'isinstance' and check manually? Or should I do something else? As you have worked out, Python doesn't do automatic type-checking for you. But if you really do need type-checking, you can do it yourself with isinstance() and/or type(). It isn't forbidden :-) Or, you can try just coercing the data you have to the type you want. eg instead of testing to see if obj is an integer, you might do: try: obj = int(obj) except: raise TypeError(can't convert to integer) insert_in_database(obj) This may be appropriate for your application. Another method that is sometimes useful: you are expecting an instance of Waterfowl class, but actually you are happy to use duck-typing: if it looks like a duck, swims like a duck, and quacks like a duck, it is close-enough to a duck: def process_waterfowl(duck): do things to an instance of Waterfowl, or equivalent try: look, swim, quack = duck.look, duck.swim, duck.quack except AttributeError: raise TypeError(object is not a waterfowl) # process any object that has look, swim and quack attributes # as if it were a waterfowl duck.look() duck.swim() duck.quack() -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
vincent wehren [EMAIL PROTECTED] writes: Peter Dembinski [EMAIL PROTECTED] schrieb im Newsbeitrag news:[EMAIL PROTECTED] | Kalle Anke [EMAIL PROTECTED] writes: | | [snap] | | sys.maxint = -12345 | | I don't really understand what you're meaning. | | He meant None = 1 : I'm sure you know that has become a no-no in Python 2.4+ ;) Yep. Still waiting for delegalisation of others : -- http://mail.python.org/mailman/listinfo/python-list
Re: Capture close window button in Tkinter
William Gill wrote: I am trying to make a simple data editor in Tkinter where each data element has a corresponding Entry widget. I have tried to use the FocusIn/FocusOut events to set a 'hasChanged' flag (if a record has not changed, the db doesn't need updating). This seems to work fine except that when the user finishes and clicks a 'done' button or the close window button (in the root widget) no FocusOut event is triggered. I can trigger a FocusOut event if the 'done' button opens another window (i.e. a messagebox) that takes focus. Enter and Leave follow the mouse, but don't trigger when the user tabs between fields. Is there a better way to monitor 'hasChanged'? I'd go with monitoring keypresses in the Entry widget. Also, how do I capture the root window close button? root = Tk() root.protocol(WM_DELETE_WINDOW, onexit) -- http://mail.python.org/mailman/listinfo/python-list
How to receve user events from IE on objects within frames
Resurrecting a month old thread.. (listed at http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/2f4e50e1e316eef4/5924203f822f7f4b?q=cal_2pacrnum=3#5924203f822f7f4b) Somehow - responses to that thread are not being brought up in chronological order. Thus the creation of another thread. The thread listed above proposed a solution to receive events from user events on the document in a webpage. However, it seems that this solution does not return events on objects within frames in webpages eg . if you go to www.andersondirect.com - the page is composed of three frames called as topFrame main and address. Now when I click on say 'Select a Vehicle' which is within main - I do not get any Onclick event. I also do not get an OnMousemove event if I move the mouse. However, I do get on Mousemove event on a tag called as frameset (which is part of the top page). How does one get events from the frames then? -- http://mail.python.org/mailman/listinfo/python-list
Re: Dealing with marketing types...
Paul Rubin http://phr.cx@NOSPAM.invalid wrote in message news:[EMAIL PROTECTED] Andrew Dalke [EMAIL PROTECTED] writes: If that's indeed the case then I'll also argue that each of them is going to have app-specific choke points which are best hand-optimized and not framework optimized. Is there enough real-world experience to design a EnterpriseWeb-o-Rama (your printing press) which can handle those examples you gave any better than starting off with a LAMP system and hand-caching the parts that need it? Yes, of course there is. Look at the mainframe transaction systems of the 60's-70's-80's, for example. Look at Google. Based on what I've read, if we could look at Google, we would see 150,000 to 200,000 servers (about half bought with IPO money). We would see a highly customized dynamic cluster computing infrastructure that can be utilized with high-level (Python-like) commands. The need to throw hundreds of machines at each web request strikes me as rather specialized, though definitely not limited to search. So while not LAMP, I don't see it as generic EWeboRama either. Terry J. Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Code documentation tool similar to what Ruby (on Rails?) uses
12 Jun 2005 08:12:14 -0700, Michele Simionato [EMAIL PROTECTED]: What about doing it yourself? import inspect, os print pre%s/pre % inspect.getsource(os.makedirs) That's easy, thanks! I guess I'll submit a patch for Epydoc with the functionality I've mentioned :) -- Ksenia -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Chris Spencer a écrit : Kalle Anke wrote: On Sun, 12 Jun 2005 13:59:27 +0200, deelan wrote (in article [EMAIL PROTECTED]): void doSomething( data : SomeClass ){ ... } and I would be sure at compile time that I would only get SomeClass objects as parameters into the method. Being an untyped language, Python does not require you to enforce types. Nope. Python *is* typed. But it doesnt confuse implementation with semantic. However, for those that require such functionality, you can get away with using the assert statement. For example, if I wanted to make sure my function foo was only given instances of class Bar, I'd write something like: class Bar: pass def foo(bar): ... assert isinstance(bar, Bar), argument is not of type Bar ... print argument must be of type Bar ... bar = Bar() foo(bar) argument must be of type Bar foo(123) Traceback (most recent call last): File stdin, line 1, in ? File stdin, line 2, in foo AssertionError: argument is not of type Bar And *this* is highly unpythonic. And un-OO too, since it makes foo() dependant on *class* Bar, when it should most probably be enough that it only depends on (probably part of) the *interface* of class Bar. Suppose I want to adapt my own class Baaz so it's compatible with Bar. Now I *must* make the adapter a subclass of Bar (and then depends on Bar's *implementation*) when it only needs to expose the same (probably subset of) interface. Except for exceptions (please pardon the pun), one most usually don't have to worry about the real class of an object. In 5 years programming Python, I only had to check for the type of an object a couple of times, and each time only for more genericity (I mostly needed to dispatch on some 'generic types' like string-like, sequence-like, map-like, numeric-like, callable, and others), and this was never used as a way to restrict the possible types (exceptions are good enough for this). -- http://mail.python.org/mailman/listinfo/python-list
Re: Custom type: PyObject_IS_GC access violation
Bue Krogh Vedel-Larsen wrote: But if I call SA_PyVector_Type.tp_new = PyType_GenericNew; PyType_Ready( SA_PyVector_Type ); then, when Py_Finalize is called, PyObject_IS_GC(op) in visit_decref() in gcmodule.c causes an access violation. If I don't call PyType_Ready, then the access violation doesn't occure, but then the type can't be used... So, the question is, does anyone have any idea about what could be causing this? Most likely some code that you haven't shown. Here is the expansion of PyObject_IS_GC(op) #define PyObject_IS_GC(o) (PyType_IS_GC((o)-ob_type) \ ((o)-ob_type-tp_is_gc == NULL || (o)-ob_type-tp_is_gc(o))) so it becomes PyType_IS_GC(op-type) (op-ob_type-tp_is_gc==NULL || op-ob_type-tp_is_gc(op)) Then, PyType_IS_GC(op-type) becomes PyType_HasFeature((op-type), Py_TPFLAGS_HAVE_GC) which in turn becomes (op-tp_flags Py_TPFLAGS_HAVE_GC) != 0 So typically, PyObject_IS_GC goes to the type of the object, which should never crash, and then looks into the flags of the type, which cannot crash - unless somebody messed with ob_type of the object, and unless this isn't a Python object in the first place. You did not say what kind of object op was in the crash - this is something you should investigate further. Does it point to a Python object? If so, what is the type of the Python object? Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: What is different with Python ?
On Sun, 12 Jun 2005 08:11:47 -0400, Roy Smith wrote: The point I was trying to make was that as computer science progresses, stuff that was really important to know a lot about becomes more and more taken for granted. This is how we make progress. I used to worry about memory busses at the milivolt and microsecond level. I knew about termination impedances and wired-OR logic, and power budgets and all that good stuff. Today all I know about memory is you go to www.crucial.com, type in your Visa card number, and the nice UPS guy shows up with some SIMMs in a few days. Yes. But (to a first approximation) memory either works or it doesn't. And we never need to worry about it scaling, because you don't get to assemble your own SIMMs -- you buy them pre-made. Software is nothing like that. [snip] Just like you can't even begin to think about building today's GUI-driven desktop applications if you're still worrying about individual logic gates, you can't begin to think about solving some of these really hard problems (and others we haven't even imagined) if you're still worrying about memory buffer reference counting and garbage collection. Yesterday's research projects are today's utilities and tomorrow's historical footnotes. Nice in theory, but frequently breaks down in practice. Let's take a nice, real, Python example: I write an text-handling application in Python. I've taken your advice, and don't worry about messy details about the language implementation, and concentrated on the application logic. Consequently, I've used the idiom: new_text = for word in text: new_text = new_text + process(word) I test it against text containing a few thousand words, and performance is good. Then my customers use my application in the real world, using texts of a few hundreds of millions of words, and performance slows to a painful crawl. Python does a good job of insulating the developer from the implementation details, but even in Python those details can sometimes turn around and bite you on the behind. And your users will discover those bum-biting situations long before your testing will. Joel of Joel On Software discusses this issue here: http://www.joelonsoftware.com/articles/fog000319.html Of course, we should not prematurely optimise. But we should also be aware of the characteristics of the libraries we call, so we can choose the right library. Fortunately, a high-level language like Python makes it comparatively easy to refactor a bunch of slow string concatenations into the list-append plus string-join idiom. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree Namespace Prefixes
Andrew Dalke wrote: On Sun, 12 Jun 2005 15:06:18 +, Chris Spencer wrote: Does anyone know how to make ElementTree preserve namespace prefixes in parsed xml files? See the recent c.l.python thread titled ElemenTree and namespaces and started May 16 2:03pm. One archive is at http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/31b2e9f4a8f7338c/363f46513fb8de04?rnum=3hl=en Thanks, although that thread didn't seem to resolve the issue. All the first few links talk about is how to hack your own parser to make sense of the Clark notation. The problem at hand is with how Elementtree outputs namespaces and represents the tag name in memory. Given xml with no namespaces, Elementtree works perfectly. However, if you give the root tag an xmlns attribute, Elementtree relabels all child nodes with it's own prefix, completely defeating the purpose of the default namespace. In my opinion, this is unacceptable behavior. If an XML parser reads in and then writes out a document without having altered it, then the new document should be the same as the original. With Elementtree this isn't so. Lundh apparently believes he knows better than you and I on how our namespaces should be represented. It's a shame the default ns behavior in Elementtree is in such a poort staten. I'm surprised no one's forked Elementtree solely to fix this issue. Anyways, Python's native minidom works as expected, so I'll probably use that instead, even if the api is slightly less intuitive. Chris -- http://mail.python.org/mailman/listinfo/python-list
RE: Dealing with marketing types...
just out of curiosity.. where'd you read the 150,000-200,000 servers... i've never seen guesses that high.. i've seen somewhere as high as possible 100K... but the author stated that he was purely guessing... -bruce -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Terry Reedy Sent: Sunday, June 12, 2005 9:48 AM To: python-list@python.org Subject: Re: Dealing with marketing types... Paul Rubin http://phr.cx@NOSPAM.invalid wrote in message news:[EMAIL PROTECTED] Andrew Dalke [EMAIL PROTECTED] writes: If that's indeed the case then I'll also argue that each of them is going to have app-specific choke points which are best hand-optimized and not framework optimized. Is there enough real-world experience to design a EnterpriseWeb-o-Rama (your printing press) which can handle those examples you gave any better than starting off with a LAMP system and hand-caching the parts that need it? Yes, of course there is. Look at the mainframe transaction systems of the 60's-70's-80's, for example. Look at Google. Based on what I've read, if we could look at Google, we would see 150,000 to 200,000 servers (about half bought with IPO money). We would see a highly customized dynamic cluster computing infrastructure that can be utilized with high-level (Python-like) commands. The need to throw hundreds of machines at each web request strikes me as rather specialized, though definitely not limited to search. So while not LAMP, I don't see it as generic EWeboRama either. Terry J. Reedy -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Controlling a generator the pythonic way
news:[EMAIL PROTECTED] Thomas Lotze [EMAIL PROTECTED] writes: A related problem is skipping whitespace. Sometimes you don't care about whitespace tokens, sometimes you do. Using generators, you can either set a state variable, say on the object the generator is an attribute of, before each call that requires a deviation from the default, or you can have a second generator for filtering the output of the first. Again, both solutions are ugly (the second more so than the first). Given an application that *only* wanted non-white tokens, or tokens meeting any other condition, filtering is, to me, exactly the right thing to do and not ugly at all. See itertools or roll your own. Given an application that intermittently wanted to skip over non-white tokens, I would use a *function*, not a second generator, that filtered the first when, and only when, that was wanted. Given next_tok, the next method of a token generator, this is simply def next_nonwhite(): ret = next_tok() while not iswhte(ret): ret = next_tok() return ret A generic method of sending data to a generator on the fly, without making it an attribute of a class, is to give the generator function a mutable parameter, a list, dict, or instance, which you mutate from outside as desired to change the operation of the generator. The pair of statements mutate generator mutable val = gen.next() can, of course, be wrapped in various possible gennext(args) functions at the cost of an additional function call. Terry J. Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Scaling down (was Re: Dealing with marketing types...)
[EMAIL PROTECTED] (Aahz) writes: So what? I think you're missing the real point of the article: using LAMP scales *DOWN* in a way that enterprise systems don't. Getting your first prototype up and running is far more important than sheer scalability, There comes a day when your first prototype can no longer handle the traffic. The question then is how do you deal with that. and LAMP does have many mechanisms to obtain scalability when it's needed. LAMP seems to have no solution other than scaling (i.e. blowing more money on hardware and colo space). One really gets the impression that a more thoughtful design could handle the traffic without needing to scale the hardware. -- http://mail.python.org/mailman/listinfo/python-list
Re: Capture close window button in Tkinter
Jonathan Ellis wrote: William Gill wrote: I am trying to make a simple data editor in Tkinter where each data element has a corresponding Entry widget. I have tried to use the FocusIn/FocusOut events to set a 'hasChanged' flag (if a record has not changed, the db doesn't need updating). This seems to work fine except that when the user finishes and clicks a 'done' button or the close window button (in the root widget) no FocusOut event is triggered. I can trigger a FocusOut event if the 'done' button opens another window (i.e. a messagebox) that takes focus. Enter and Leave follow the mouse, but don't trigger when the user tabs between fields. Is there a better way to monitor 'hasChanged'? I'd go with monitoring keypresses in the Entry widget. Well, It doesn't seem logical to do a before and after test of the Entry values after each keypress, but I suppose I could monitor keypresses, test event.key for Tab (in case the user tabbed out of the widget w/o making any changes. Is that along the lines of what you are suggesting? Also, how do I capture the root window close button? root = Tk() root.protocol(WM_DELETE_WINDOW, onexit) I had seen something like this somewhere, but couldn't remember where. Thanks to your reminder I found an example of Capturing destroy events. Since that will allow me to route the exit process through a tkMessageBox, it may be just as good to continue using FocusIn/FocusOut events. -- http://mail.python.org/mailman/listinfo/python-list
slicing a bsddb table, eg. for rec in bsddb[AArdvark:zebra]: print rec
#!/bin/env python import bsddb test=bsddb.btopen(test.tbl) for m in JFMATQPHSOND: test[m]=Profit for month +m+ $1B def subyear_report(record_selection): for data in record_selection.iteritems(): print data # I was expecting a slice of an index file to yield a # generator so not all the records need to be read from disk subyear_report(test[Apr:Sep]) I have tried a few ideas but is there a simple way to convert this [Apr:Sep] slice of a file into a generator of the requested records? (I tried UserDict and got a bit further...) Any hints? ThanX NevilleDNZ -- http://mail.python.org/mailman/listinfo/python-list
Re: checking for when a file or folder exists, typing
Hi Bryan, Here's a potential idea. Try converting the variable to a string by using the following syntax: thePath = str(thePathArg) Actually it was a stupid thing, what happened was I was looping through the commandline to build a file path in cases where there was more than one commandline argument, such as when one passes c:/Documents and Settings/ only I was adding the space in the wrong place so I never had a proper location. Thanks though, it got me back to the problem and solved it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Developers Handbook - Mistake done and corrected.
Arsenal v Spurs in baseball that translates to Red Sox v Yankees. -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree Namespace Prefixes
Chris Spencer napisa(a): Given xml with no namespaces, Elementtree works perfectly. However, if you give the root tag an xmlns attribute, Elementtree relabels all child nodes with it's own prefix, completely defeating the purpose of the default namespace. In my opinion, this is unacceptable behavior. There is no functional difference between default namespace and normal namespace. Replacing default with normal has no effect for document processing (namespace doesn't change, only prefix), although it looks differently for humans. Anyway, XML is for machines, not for humans. If an XML parser reads in and then writes out a document without having altered it, then the new document should be the same as the original. With Elementtree this isn't so. Lundh apparently believes he knows better than you and I on how our namespaces should be represented. No, this is perfectly valid behaviour. Go, see spec. It's a shame the default ns behavior in Elementtree is in such a poort staten. I'm surprised no one's forked Elementtree solely to fix this issue. There is at least one ElementTree API implementation that retains prefixes, lxml.ETree. Go google for it. -- Jarek Zgoda http://jpa.berlios.de/ | http://www.zgodowie.org/ -- http://mail.python.org/mailman/listinfo/python-list
searching for IDE
I need an IDE for python that has the ability to show the filds of a class when I write . Just the way it works in eclipse/JBuilder with java or visual studio with c++ For now I treid eric3 and IDLE they don't do this... -- http://mail.python.org/mailman/listinfo/python-list
Re: What is different with Python ?
Taking stuff for granted in unrelated to progress. I agree that the trade of software engineering evolves and that, thanks to hardware advances, we _usually_ can now object orient our software, add billions of abstraction layers, and consume memory without a second thought. But the trade evolves in the sense sub-trades are created, one person becomes a database experts while another will html all of his/her life (I personally find that sad). I'm being redundant here: The reason we can use Python and take many issues for granted is because some very skilled people handle the issues we find cumbersome. Roy Smith wrote: The point I was trying to make was that as computer science progresses, stuff that was really important to know a lot about becomes more and more taken for granted. This is how we make progress. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Bruno Desthuilliers [EMAIL PROTECTED] writes: [snap] Being an untyped language, Python does not require you to enforce types. Nope. Python *is* typed. But it doesnt confuse implementation with semantic. Python is typed. And its type system may look strange for anyone who did only Java or C++ programming before : -- http://mail.python.org/mailman/listinfo/python-list
Re: searching for IDE
Try ActiveState Komodo. It costs $30 for a personal license, and is well worth it. If you like visual studio you will like it. Like many other people, I have looked far and wide for a Python IDE and this is what I've found. The free solutions don't cut it, at least without spending many many hours of work, more than $30 worth. R alexrait1 [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I need an IDE for python that has the ability to show the filds of a class when I write . Just the way it works in eclipse/JBuilder with java or visual studio with c++ For now I treid eric3 and IDLE they don't do this... -- http://mail.python.org/mailman/listinfo/python-list
How to test if an object IS another object?
If two objects are of equal value you can compare them with ==. What I want to do is find out if two objects are actually just references to the same object, how can I do this in Python? Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
[EMAIL PROTECTED] a écrit : If two objects are of equal value you can compare them with ==. What I want to do is find out if two objects are actually just references to the same object, how can I do this in Python? The most obvious way (as usual ?): if obj1 is obj2: // your code here -- http://mail.python.org/mailman/listinfo/python-list
Re: What language to manipulate text files
ross [EMAIL PROTECTED] writes: What are the ideal languages for the following examples? 1. Starting from a certain folder, look in the subfolders for all filenames matching *FOOD*.txt Any files matching in each folder should be copied to a new subfolder within the current folder called EATING with a new name of *FOOD*COPY.txt Bash? for f in *FOOD*.txt; do cp ${f} EATING/${f}COPY.txt; done Or mmv, a linux utility: mmv '*FOOD*.txt' 'EATING/#1FOOD#2COPY.txt' For the rest, I personally for choose python. Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: What is different with Python ?
Steven D'Aprano [EMAIL PROTECTED] writes: [snap] new_text = for word in text: new_text = new_text + process(word) new_text = .join(map(process, text)) (I couldn't resist) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Chris Spencer a écrit : Bruno Desthuilliers wrote: And *this* is highly unpythonic. And un-OO too, since it makes foo() dependant on *class* Bar, when it should most probably be enough that it only depends on (probably part of) the *interface* of class Bar. I was providing the original poster with a simple way to ensure appropriate type. s/appropriate type/specific implementation/ Hint : the appropriate type for print XXX is whatever has a 'write(anything_that_can_be_coerced_to_a_string)' method. -- http://mail.python.org/mailman/listinfo/python-list
Re: python bytecode grammar
Peter Dembinski [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Terry Reedy [EMAIL PROTECTED] writes: I believe the top-level production is something like BYTECODE := (OPCODE ARGS)* ROTFL :) Glad to make your day ;-) I am aware that since ARGS depends on OPCODE, the above would lead to context-dependent productions for ARGS. Upon thoroughly perusing http://docs.python.org/lib/bytecodes.html 18.10.1 Python Byte Code Instructions I discovered that there is at most one (non-stack) argument in the byte stream (contrary to the possibly plural implication of All of the following opcodes expect arguments). So the above could be written context-freely as BYTECODE := (NO_ARG_CODE | ARG_CODE TWO_BYTE_ARG)* where the symbolic expansions of NO_ARG_CODE and ARG_CODE, with semantic explanations, constitute the contents of the doc above. Terry J. Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
Sorry about removing my message, I posted with the wrong google account, I don't really want my email where those irritating spam bots can find it. The most obvious way (as usual ?): if obj1 is obj2: // your code here I immediately thought of is, and tested it in the console, but it didn't work quite like I expected: foo = 3 bar = 3 zoo = foo foo is zoo True foo is bar True zoo is bar True clearly foo and bar have the same value but they are different objects aren't they? Yet applying the is operator yields True. Thanks, -Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
[EMAIL PROTECTED] writes: foo = 3 bar = 3 clearly foo and bar have the same value but they are different objects aren't they? No, they're the same object. Now try it with 300 instead of 3 ;-). -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
[EMAIL PROTECTED] a écrit : Sorry about removing my message, I posted with the wrong google account, I don't really want my email where those irritating spam bots can find it. The most obvious way (as usual ?): if obj1 is obj2: // your code here I immediately thought of is, and tested it in the console, but it didn't work quite like I expected: foo = 3 bar = 3 zoo = foo foo is zoo True foo is bar True zoo is bar True clearly foo and bar have the same value but they are different objects aren't they? Nope. They are two different names bound to the same integer object. You may have similar situation with strings: s1 = toto s2 = toto s1 is s2 True This is an application of the lightweight pattern. The Python interpreter reuse the same value object to avoid memory clutter. Since ints and strings are immutable, this is perfectly safe (but yet confusing when you're not aware of this). Yet applying the is operator yields True. Yes. But now you know why !-) And don't worry, this is quite unlikely that it will cause you any trouble in real code. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to overcome automatic cyrillic-to-/hex convert
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote: But when I enter some Bulgarian (actually cyrillic) text as a string, it seems that Python automatically converts it to '\x00..\x00 ' and once converted that way I can't get it back into its original look. The only way to get it right is using print : a = 'ÌÀÌÀ' # 'Mam' in Bulgarian print a 'ÌÀÌÀ' but a '\xcc\xe0\xec\xe0' Did you try the locale module? Florian -- begin signature_virus Hi! I'm a signature virus. Please copy me to your signature to help me spread. end -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if an object IS another object?
Fascinating. With small strings, it uses the same object, and with small numbers like 3. With 300 they were different objects (why, shouldn't they both be ints still?) Mutable objects functioned differently as you suggested: foo = [] bar = [] foo == bar True foo is bar False Tuples (which are immutable) also appear to be reused foo = () bar = () foo is bar True Thanks for your help, I know how to solve the problem now. -Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Bruno Desthuilliers wrote: Chris Spencer a écrit : I was providing the original poster with a simple way to ensure appropriate type. s/appropriate type/specific implementation/ Hint : the appropriate type for print XXX is whatever has a 'write(anything_that_can_be_coerced_to_a_string)' method. Certainly, although now I think you're splitting hairs over my terminology. Let's synchronize our wording with a point made in the Python docs. Section 2.1 of the Library Reference recommends that the builtin function isinstance() be used for testing the object type. Does this then mean that type refers to the class of an object? In which case, wouldn't interface refer to a collection of methods, that when present in a class create an implementation? Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get/set class attributes in Python
Peter Dembinski wrote: Bruno Desthuilliers [EMAIL PROTECTED] writes: Nope. Python *is* typed. But it doesnt confuse implementation with semantic. Python is typed. And its type system may look strange for anyone who did only Java or C++ programming before : Of course, in that Python is dynamically typed as opposed to the static typing of Java or C++. Please excuse my previous mis-wording :) Chris -- http://mail.python.org/mailman/listinfo/python-list
Learning more about The Python Way
Those who have read my posts today have probably understood that I'm not a true Python programmer ... but I want to learn more (I think that Python is rather fun). I've read Learning Python pretty thoroughly, I've looked at some of the tutorials, some of online documentation, etc. But I still miss a lot of pieces for writing good python code, idioms, advanced usage/features, etc. I've also seen a lot of references to v3, but I haven't found any real documentation of what's planned for that version. So, I'm looking for advice/information on: + How to write proper python code instead of Java/Perl/C/C++/Pascal/Modula-2/etc inspired code + The more advanced parts/uses of Python + Discussions about the ideas behind different Python constructs + What's going to happen with v3 I would really appriciate some pointers to where I can find info about this. Web sites (I've looked at python.org but haven't manage to find the stuff of what I'm looking for ... but perhaps I've missed all the interesting parts) ? Books (I've got 'Learning Python' and 'Programming Python')? Other things? jem -- http://mail.python.org/mailman/listinfo/python-list
Post-EuroPython 2005 PyPy Sprint 1st - 7th July 2005
Post-EuroPython 2005 PyPy Sprint 1st - 7th July 2005 == The next PyPy sprint is scheduled right after EuroPython 2005 in Gothenborg, Sweden. Its main focus is translation to lower level backends but there are also other possible topics. We'll give newcomer-friendly introductions. To learn more about the new PyPy Python-in-Python implementation look here: http://codespeak.net/pypy On a side note, there are a number of sub projects that may be interesting for participating in google's summer-of-code event (deadline June 14th!). The PyPy group is willing to mentor projects that have some link with PyPy, so if you are accepted in such a project, the sprint could also serve as a good meeting and kick-off point. Further down you'll find some examples, but there are certainly more and bigger ones :-) Goals and topics of the sprint -- The main, though not the only, focus of the sprint will be on the translation aspect of PyPy. The goal here is to progress towards a completely translated PyPy. How much will already have been done before EuroPython is unknown; as a guess, we will be left with: - completing the rtyper, the piece of code that assigns low-level C-like types to high-level RPython objects (lists, dicts, instances, etc.) and low-level control flow graphs to high-level ones; - polish off the GenC and GenLLVM back-ends, responsible for turning the low-level C-like flow graphs into real C or LLVM source code. See http://codespeak.net/pipermail/pypy-dev/2005q2/002136.html for more information (10th of June status). Non-translation-related topics are welcome too. Here are some suggestions from the issue tracker (https://codespeak.net/issue/pypy-dev/): - integrate the parser module, possibly making it RPython conformant; - rewrite in Python a C module you are familiar with (partial list of missing/incomplete modules: os, math, array, regular expressions, binascii...) - implement Python 2.3's import hook extensions (zip-imports etc.) - fix Windows-related issues, '%'-formatting rounding errors, add missing docstrings on app-level built-in types and functions, etc. - weakrefs (but this requires discussion and planning on pypy-dev before the sprint! feel free to start such a discussion, though.) Location Accomodation The sprint will be held in the former Math Center building near the crossing of Gibraltargatan and Eklandagatan. Entrance is on the middle of the side facing Gibraltargatan. The doors to the building are normally locked, so you need the phone number of somebody inside to get in. Instructions on whom to call will be posted on the door. The sprint will be co-located with several other sprints. See the `EuroPython Wiki`_, to find out what other sprints will be running. Nearest, and probably cheapest is to book accomodation at SGS Veckobostäder through the Europython website. This option will be available until about 20 June. .. _`EuroPython special accomodation`: http://www.europython.org/sections/accomodation/special_accomodation .. _`EuroPython Wiki`: http://www.europython.org/sections/sprints_and_wiki Exact times --- The public Pypy sprint is held Friday 1st July - Thursday 7 July 2005. Hours will be from 09:00 until people have had enough. It's a good idea to arrive a day before the sprint starts. (There is a sprint for people who are familiar with the Pypy codebase before Europython as well. This will be held at Jacob Laura's home on Götabergsgatan 22.) Network, Food, currency Sweden is not part of the Euro zone. One SEK (krona in singular, kronor in plural) is roughly 1/10th of a Euro (9.15 SEK to 1 Euro). There are some pizzerias, kebab places and the like close to the venue. Their food is edible and cheap, but not very good. For good food, you need to go downtown. You need a wireless network card to access the network. You will be issued a login to the Chalmers NOMAD network. This will allow you to use access points all over Chalmers. However, we can likely provide a wireless/ethernet bridge. Sweden uses the same kind of plugs as Germany. 230V AC. Registration etc.pp. Please subscribe to the `PyPy sprint mailing list`_, introduce yourself and post a note that you want to come. Feel free to ask any questions there! .. _`PyPy sprint mailing list`: http://codespeak.net/mailman/listinfo/pypy-sprint -- Armin Rigo the PyPy team -- http://mail.python.org/mailman/listinfo/python-list