Re: ANN: equivalence 0.1
Interesting.. it took me a while to figure out why the second case is so much slower and you're right, it is indeed quadratic. I don't know how likely would such pathological cases be in practice, given that the preferred way to merge a batch of objects is eq.merge(*xrange(10001)), which is more than 3 times faster than the non-pathologic first case (and which I optimized even further to avoid attribute lookups within the loop so it's more like 5 times faster now). Also the batch version in this case remains linear even if you merge backwards, eq.merge(*xrange(1,-1,-1)), or in any order for that matter. The example just showed what could happen if the merges are done in pathological order, it is not about batch merging. I think that pathological cases like this indeed show up in real cases: many algorithms of near duplicate elimination and clustering reduce to finding connected components of a graph whose edges are given as a stream, so you can't control their order. With this implementation, every time a component sized N is given a second (or following) argument to merge, you pay Omega(N). I am familiar with it and I will certainly consider it for the next version; for now I was primarily interested in functionality (API) and correctness. Good :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Better performance
Franck Y [EMAIL PROTECTED] writes: Hello Folks, I am facing a problem where i need to parse around 200 files, i have a bit of knowledge in PHP/Perl/Python (the magic P :-P) Which one would you suggest me since i have to generate a web interface ? And each one has his area of 'work' Thanks for your help ! Python, of course. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's doc problems: sort
szr wrote: Jürgen Exner wrote: Andrew Koenig [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote in message [Subject: Python's doc problems: sort] I want to emphasize a point here, as i have done quite emphatically in the past. The Python documentation, is the world's worst technical And WTF does Python documentation have to do with Perl of Lisp? szr, do you still have any doubts about the nature of xahlee? I wasn't involved in this thread, but no, after that statement comparing Perl's and Python's docs, I no doubts. * should have been, , I have no doubts. -- szr -- http://mail.python.org/mailman/listinfo/python-list
platypus in page header
Is it possible to use platypus in page header and footer? I need to create a document with long paragraphs but also I need to put tables and images in page header and multi line info in page footer with alignment etc. Thanks, Laszlo -- http://mail.python.org/mailman/listinfo/python-list
Re: The Importance of Terminology's Quality
Arne Vajhøj wrote: szr wrote: Arne Vajhøj wrote: szr wrote: Peter Duniho wrote: On Fri, 30 May 2008 22:40:03 -0700, szr [EMAIL PROTECTED] wrote: Arne Vajhøj wrote: Stephan Bour wrote: Lew wrote: } John Thingstad wrote: } Perl is solidly based in the UNIX world on awk, sed, } bash and C. I don't like the style, but many do. } } Please exclude the Java newsgroups from this discussion. Did it ever occur to you that you don't speak for entire news groups? Did it occur to you that there are nothing about Java in the above ? Looking at the original post, it doesn't appear to be about any specific language. Indeed. That suggests it's probably off-topic in most, if not all, of the newsgroups to which it was posted, inasmuch as they exist for topics specific to a given programming language. Perhaps - comp.programming might of been a better place, but not all people who follow groups for specific languages follow a general group like that - but let me ask you something. What is it you really have against discussing topics with people of neighboring groups? Keep in mind you don't have to read anything you do not want to read. [1] I very much doubt that the original thread is relevant for the Java group. But the subthread Lew commente don was about Perl and Unix. That is clearly off topic. I agree with and understand what you are saying in general, but still, isn't it possible that were are people in the java group (and others) who might of been following the thread, only to discover (probably not right away) that someone decided to remove the group they were reading the thread from? I know I would not like that, even if it wasn't on topic at the branch. Personally, I find it very annoying to have to switch news groups in order to resume a thread and weed my way down the thread to where it left off before it was cut off from the previous group. I am relative tolerant towards threads that are a bit off topic, if the S/N ratio overall is good. Agreed. [...] If a thread, that is cross-posted, branches off on a tangent that has nothing to do with one or more groups what so ever, then yes, it makes sense to prune the 'newsgroup:' list / set follow ups, but in this case, someone made one mention or so of 'Perl', which was being used as an example, and someone (lew) moved to have the Java group removed. There was little reason to cut off the thread, when people very well may have been following it, over the utterance of one word, which was being used as an example. The bulk of the thread had to do with general programming, and suddenly writing the name of a language doesn't mean it's way off on a tangent. I hope this clears up some waters. Regards. -- szr -- http://mail.python.org/mailman/listinfo/python-list
Re: Better performance
Franck Y a écrit : Hello Folks, I am facing a problem where i need to parse around 200 files, i have a bit of knowledge in PHP/Perl/Python (the magic P :-P) Which one would you suggest me since i have to generate a web interface ? And each one has his area of 'work' And where's your performance problem ? You don't give enough details to seriously answer your question, but Python is a good tool for file parsing and web development anyway... -- http://mail.python.org/mailman/listinfo/python-list
printf in python
Hy, I've a problem with may python library generated with swig from C code. I works and I can access all function but a simèple function that print a string don't work's. The function is like this: int PrintTEST() { printf(TEST ); return 1; } If I call the function (myDLL.PrintTEST() ) the function print only the returned value, not the string TEST. Could anybody help me? Thanks Gianluca -- http://mail.python.org/mailman/listinfo/python-list
Re: php vs python
Joel Koltner [EMAIL PROTECTED] writes: There's potentially a large difference between a good speaker of English/German/etc. vs. eloquent. I'd tend to agree with Jerry that if you can write good code in one language, you can in pretty much any other as well... but that doesn't imply you're necessarily eloquent in any languages. :-) Eloquence is nice, but eradicating bad code in this world is about a million times more important than attempting to move people from good code to eloquent code. This is wrong, because if you know well one language only, you tend to think that the principles that underpin it are universal. So you will try to shoehorn these principles into any other language you use. It's only when you have become reasonably proficient in a number of conceptually different languages that you start to build a picture of what a programming language is. I understand that there are some sane practices that are useful to know when programming in any language, but it is wrong to say that the skill of programming can be reduced to that, the rest being syntax. There is (hopefully!) a design behind the syntax, you have to understand it to use the language well. You may be great at building Turing machines. That doesn't make you a master of crafting lambda-expressions. To be Pythonic here, eloquent code would perhaps often have clear, clean list comprehensions used when good code would use a for loop but still be easy to follow as well and perfectly acceptable in the vast majority of cases. I find that eloquent Python speakers often tend to write a for loop when mere good ones will try to stick a list comprehension in! Regards -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Formatting Output
Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 The matter is that i don't know in advance how many columns there will be. By the way, each column will be actually FLOATs, not INTs. How can i do this ? Any help welcome. Regards, Victor -- http://mail.python.org/mailman/listinfo/python-list
Re: php vs python
Arnaud Delobelle [EMAIL PROTECTED] wrote: I find that eloquent Python speakers often tend to write a for loop when mere good ones will try to stick a list comprehension in! +1 QOTW -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Writing HTML
I've searched the standard library docs, and, while there are a couple options for *reading* HTML from Python, I didn't notice any for *writing* it. Does anyone have any recommendations (particularly ones not listed on PyPI)? Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: printf in python
gianluca [EMAIL PROTECTED] writes: Hy, I've a problem with may python library generated with swig from C code. I works and I can access all function but a simèple function that print a string don't work's. The function is like this: int PrintTEST() { printf(TEST ); return 1; } Finish the printed string with a newline, or call fflush(stdout). By default stdout is line-buffered when connected to interactive displays. -- http://mail.python.org/mailman/listinfo/python-list
How solid is PyPy?
I've been looking at PyPy recently, and I see it's reached version 1.0 (and supports language version 2.4). Given that, I was wondering what level of backwards-compatibility one can expect from future versions, i.e. if I run code on, say, a translated stackless PyPy now, what is the probability that it will run unmodified on PyPy 1.x, 2.x, etc.? Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing HTML
On Mon, 02 Jun 2008 01:03:05 -0700, miller.paul.w wrote: I've searched the standard library docs, and, while there are a couple options for *reading* HTML from Python, I didn't notice any for *writing* it. I guess that's where the many templating engines are used. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting Output
On Mon, 02 Jun 2008 00:34:09 -0700, [EMAIL PROTECTED] wrote: i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 The matter is that i don't know in advance how many columns there will be. By the way, each column will be actually FLOATs, not INTs. How can i do this ? Any help welcome. Regards, Look at string methods, `join()` for example, and string formatting with the ``%`` operator. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting Output
On Jun 2, 9:34 am, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 The matter is that i don't know in advance how many columns there will be. By the way, each column will be actually FLOATs, not INTs. How can i do this ? Any help welcome. Regards, Victor import sys float_list = [1.0, 5.0, 6.0, 2.0, 2.0, 8.0, 2.0, 9.0, 5.0] num_columns = 3 for i,item in enumerate(float_list): sys.stdout.write('%.4f\t' % item) if not (i+1) % num_columns: sys.stdout.write('\n') Problem with this approach is it doesn't cater for instances where you exceed the standard 80 characters for a terminal window. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On 2008-05-24, Ben Finney [EMAIL PROTECTED] wrote: Sh4wn [EMAIL PROTECTED] writes: first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. From whom are you trying to hide your attributes? In Python, the philosophy we're all consenting adults here applies. You shouldn't pretend to know, at the time you write it, all the uses to which your code will be put. Barriers such as enforced private attributes will only cause resentment when people, despite your anticipations, *need* to access them and are then forced to hack their way around them. I don't find this argument very compelling. You can't anticipate all functionality people would like your function to have. Acces to information in a (private) attribute is just one of those possible functionallities. People will resent you if you don't provide functionality they think fits logically in your package. If you want the users of your code to know that an attribute should not be used as a public API for the code, use the convention of naming the attribute with a single leading underscore. This is a string signal that the attribute is part of the implementation, not the interface. The reader is then on notice that they should not rely on that attribute; but they are not *prohibited* from using it if necessary to their ends. But they will resent you just as much if you decide to rewrite your module in such a way that the attribute is no longer present or is used now in a slightly different way, so that it break code. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On 2008-05-24, Sh4wn [EMAIL PROTECTED] wrote: Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language, but why does it miss one of the basic principles of OOP? Will it ever be added to python? Thanks in advance, Lucas If you really need it, you can do data hiding in python. It just requires a bit more work. - Hide.py - class Rec(object): def __init__(__, **kwargs): for key,value in kwargs.items(): setattr(__, key, value) def __getitem__(self, key): return getattr(self, key) def __setitem__ (self, key, val): setattr(self, key, val) class Foo(object): def __init__(self): hidden = Rec(x=0, y=0) def SetX(val): hidden.x = val def SetY(val): hidden.y = val def GetX(): return hidden.x def GetY(): return hidden.y self.SetX = SetX self.SetY = SetY self.GetX = GetX self.GetY = GetY -- $ python Python 2.5.2 (r252:60911, Apr 17 2008, 13:15:05) [GCC 4.2.3 (Debian 4.2.3-3)] on linux2 Type help, copyright, credits or license for more information. From Hide import Foo var = Foo() var.GetX() 0 var.SetX(5) var.GetX() 5 var.x Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'Foo' object has no attribute 'x' var.hidden.x Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'Foo' object has no attribute 'hidden' -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
Antoon Pardon [EMAIL PROTECTED] wrote: If you really need it, you can do data hiding in python. It just requires a bit more work. snip --- $ python Python 2.5.2 (r252:60911, Apr 17 2008, 13:15:05) [GCC 4.2.3 (Debian 4.2.3-3)] on linux2 Type help, copyright, credits or license for more information. From Hide import Foo var = Foo() var.GetX() 0 var.SetX(5) var.GetX() 5 var.x Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'Foo' object has no attribute 'x' var.hidden.x Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'Foo' object has no attribute 'hidden' That sort of hiding isn't any more secure than the 'hiding' you get in C++. var.GetX.func_closure[0].cell_contents.x 5 All you've done is force the user who wants to bypass it to use a longer expression, and if that's your criterion for 'hiding' then just use two leading underscores. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 5:38 am, Antoon Pardon [EMAIL PROTECTED] wrote: If you really need it, you can do data hiding in python. It just requires a bit more work. - Hide.py - class Rec(object): def __init__(__, **kwargs): for key,value in kwargs.items(): setattr(__, key, value) def __getitem__(self, key): return getattr(self, key) def __setitem__ (self, key, val): setattr(self, key, val) class Foo(object): def __init__(self): hidden = Rec(x=0, y=0) def SetX(val): hidden.x = val def SetY(val): hidden.y = val def GetX(): return hidden.x def GetY(): return hidden.y self.SetX = SetX self.SetY = SetY self.GetX = GetX self.GetY = GetY Red Herring. 1. This doesn't hide the variables; it just changes their spelling. 2. This also hides the variables from its own class. In other words, it's a useless no-op. In fact, I'd say this is even worse than useless. Creating accessor functions is a sort of blessing for external use. Knowing that there are accessor functions is likely to cause a user to show even less restraint. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing HTML
[EMAIL PROTECTED] wrote: I've searched the standard library docs, and, while there are a couple options for *reading* HTML from Python, I didn't notice any for *writing* it. Does anyone have any recommendations (particularly ones not listed on PyPI)? Thanks My approach is usually to write the data-load in XML rather than directly in HTML, and then use XSLT to produce the (X)HTML. You can do the whole of this, or just the second part, using FourSuite. If the XML, later, needs to be changed, you can use FourSuite to drill down to somewhere using XPath and change that particular part. But none of this is really specific to Python, its the XML skill-set you really need. -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing HTML
On Jun 2, 6:11 am, Ken Starks [EMAIL PROTECTED] wrote: My approach is usually to write the data-load in XML rather than directly in HTML, and then use XSLT to produce the (X)HTML. That's a good idea I hadn't considered. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Shed my a light :)
Hi, I using eval for quite strange reason, as long as I don't know a different way to implement. An example: actions= ('print', 'sum', 'divide', 'myfunction') parameters=(5, 'nothing',5.63, object) for routines in actions: routines(parameters) I'd like to note that actions are string or string expressions of the program functions or python itself, so I've in my program something like: for nn in actions: eval('cp.%s' %nn) Where cp is an instance. So I'm asking here whether exist a way that these string become functions inside my program, without using eval() -- Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On 2008-06-02, Duncan Booth [EMAIL PROTECTED] wrote: Antoon Pardon [EMAIL PROTECTED] wrote: If you really need it, you can do data hiding in python. It just requires a bit more work. snip --- $ python Python 2.5.2 (r252:60911, Apr 17 2008, 13:15:05) [GCC 4.2.3 (Debian 4.2.3-3)] on linux2 Type help, copyright, credits or license for more information. From Hide import Foo var = Foo() var.GetX() 0 var.SetX(5) var.GetX() 5 var.x Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'Foo' object has no attribute 'x' var.hidden.x Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'Foo' object has no attribute 'hidden' That sort of hiding isn't any more secure than the 'hiding' you get in C++. So? var.GetX.func_closure[0].cell_contents.x 5 All you've done is force the user who wants to bypass it to use a longer expression, and if that's your criterion for 'hiding' then just use two leading underscores. That you can find a lock pick to get at an object doesn't contradict that the object is locked away. I think the intention of not having these variables accesable to the application programmer is much stronger expressed than with two leading underscores. Even if the current implementation of the language makes it relatively easy to get at the information if you really want to. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 5:38 am, Antoon Pardon [EMAIL PROTECTED] wrote: If you really need it, you can do data hiding in python. It just requires a bit more work. - Hide.py - class Rec(object): def __init__(__, **kwargs): for key,value in kwargs.items(): setattr(__, key, value) def __getitem__(self, key): return getattr(self, key) def __setitem__ (self, key, val): setattr(self, key, val) class Foo(object): def __init__(self): hidden = Rec(x=0, y=0) def SetX(val): hidden.x = val def SetY(val): hidden.y = val def GetX(): return hidden.x def GetY(): return hidden.y self.SetX = SetX self.SetY = SetY self.GetX = GetX self.GetY = GetY Red Herring. 1. This doesn't hide the variables; it just changes their spelling. 2. This also hides the variables from its own class. In other words, it's a useless no-op. In fact, I'd say this is even worse than useless. Creating accessor functions is a sort of blessing for external use. Knowing that there are accessor functions is likely to cause a user to show even less restraint. I think you completed missed the point. This is just a proof of concept thing. In a real example there would of course no Set en Get methods but just methods that in the course of their execution would access or update the hidden attributes -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: Shed my a light :)
On Jun 2, 12:35 pm, TheSaint [EMAIL PROTECTED] wrote: Hi, I using eval for quite strange reason, as long as I don't know a different way to implement. An example: actions= ('print', 'sum', 'divide', 'myfunction') parameters=(5, 'nothing',5.63, object) for routines in actions: routines(parameters) I'd like to note that actions are string or string expressions of the program functions or python itself, so I've in my program something like: for nn in actions: eval('cp.%s' %nn) Where cp is an instance. So I'm asking here whether exist a way that these string become functions inside my program, without using eval() -- Mailsweeper Home :http://it.geocities.com/call_me_not_now/index.html help(getattr) Help on built-in function getattr in module __builtin__: getattr(...) getattr(object, name[, default]) - value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case. for nn in actions: func = getattr(cp, nn) if callable(func): func(parameters) or alternatively for nn in actions: getattr(cp, nn)(parameters) Hope that helps. -- http://mail.python.org/mailman/listinfo/python-list
Re: Shed my a light :)
TheSaint [EMAIL PROTECTED] wrote: actions= ('print', 'sum', 'divide', 'myfunction') parameters=(5, 'nothing',5.63, object) for routines in actions: routines(parameters) I'd like to note that actions are string or string expressions of the program functions or python itself, so I've in my program something like: for nn in actions: eval('cp.%s' %nn) Where cp is an instance. So I'm asking here whether exist a way that these string become functions inside my program, without using eval() eval('cp.%s' %nn) is more cleanly done as: getattr(cp, nn) In the more general case, you could just use the functions/methods directly instead of using their names: actions = (cp.Print, cp.sum, cp.divide, cp.myfunction) for nn in actions: nn(parameters) -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
mocking a logging object
In my unittest I want to override the logger of a working module so that it puts all logging messages in /tmp/test.log instead so that in my unittest I can inspect that it logs things correctly. Hopefully this pseudo code will explain my problem:: import logging, os logging.basicConfig(filename='/tmp/real.log', level=logging.INFO) logger = logging.getLogger('Real') logger.info('Real stuff') os.path.isfile('/tmp/real.log') True # do the monkey patching like the unit test does logging.basicConfig(filename='/tmp/test.log', level=logging.INFO) logger = logging.getLogger('Test') logger.info('Test stuff') os.path.isfile('/tmp/test.log') False open('/tmp/real.log').read() 'INFO:Real:Real stuff\nINFO:Test:Test stuff\n' How can I change what file the logger should write to? -- http://mail.python.org/mailman/listinfo/python-list
Re: mocking a logging object
Peter Bengtsson wrote: In my unittest I want to override the logger of a working module so that it puts all logging messages in /tmp/test.log instead so that in my unittest I can inspect that it logs things correctly. Hopefully this pseudo code will explain my problem:: import logging, os logging.basicConfig(filename='/tmp/real.log', level=logging.INFO) logger = logging.getLogger('Real') logger.info('Real stuff') os.path.isfile('/tmp/real.log') True # do the monkey patching like the unit test does logging.basicConfig(filename='/tmp/test.log', level=logging.INFO) logger = logging.getLogger('Test') logger.info('Test stuff') os.path.isfile('/tmp/test.log') False open('/tmp/real.log').read() 'INFO:Real:Real stuff\nINFO:Test:Test stuff\n' How can I change what file the logger should write to? You should simply attach a new handler to the logger in question that logs the data into a stream/StringIO-object for later retrieval. Then remove that handler after the test. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 6:40 am, Antoon Pardon [EMAIL PROTECTED] wrote: On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 5:38 am, Antoon Pardon [EMAIL PROTECTED] wrote: If you really need it, you can do data hiding in python. It just requires a bit more work. - Hide.py - class Rec(object): def __init__(__, **kwargs): for key,value in kwargs.items(): setattr(__, key, value) def __getitem__(self, key): return getattr(self, key) def __setitem__ (self, key, val): setattr(self, key, val) class Foo(object): def __init__(self): hidden = Rec(x=0, y=0) def SetX(val): hidden.x = val def SetY(val): hidden.y = val def GetX(): return hidden.x def GetY(): return hidden.y self.SetX = SetX self.SetY = SetY self.GetX = GetX self.GetY = GetY Red Herring. 1. This doesn't hide the variables; it just changes their spelling. 2. This also hides the variables from its own class. In other words, it's a useless no-op. In fact, I'd say this is even worse than useless. Creating accessor functions is a sort of blessing for external use. Knowing that there are accessor functions is likely to cause a user to show even less restraint. I think you completed missed the point. I'm not sure I missed the point so much as I failed to read your mind. This is just a proof of concept thing. In a real example there would of course no Set en Get methods but just methods that in the course of their execution would access or update the hidden attributes Fair enough, but I don't see anything in your example that suggests a way to discriminate between access from within the class and access from outside the class, which is the crucial aspect of data hiding. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
GOZERBOT 0.8.1.0 released
0.8.1.0 is here and can be downloaded from http://gozerbot.org new features: * ssl connections are now supported * third party software included into gozerbot: o feedparser (used by RSS) .. makes atom feeds possible o simplejson (used by COLLECTIVE) o BeautifulSoup (used to parse webpages) * renewed RSS plugin * renewed collective plugin * renewed webserver plugin .. new webserver API * tcp.py notification plugin as well as a totcp.py client program the following plugins were removed from the main distribition: * autovoice * away * convert * country * event * grab * jcoll * probe if you are using one of these plugins use install-plug plugin to install them from the plugin server. any problems with the bot can be reported on #dunkbots on IRCnet or email [EMAIL PROTECTED] debian package and freebsd port will follow have fun ;] about gozerbot: Requirements * a shell * python 2.4 or higher * if you want to remotely install plugins: the gnupg module * if you want mysql support: the py-MySQLdb module * if you want jabber support: the xmpppy module Why gozerbot? * provide both IRC and Jabber support * user management by userhost .. bot will not respond if it doesn't know you * fleet .. use more than one bot in a program (list of bots) * use the bot through dcc chat * fetch rss feeds * remember items * relaying between bots * program your own plugins * run the builtin webserver * query other bots webserver via irc * serve as a udp - irc or jabber notification bot * mysql and sqlite support -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 8:14 am, Carl Banks [EMAIL PROTECTED] wrote: Fair enough, but I don't see anything in your example that suggests a way to discriminate between access from within the class and access from outside the class, which is the crucial aspect of data hiding. And, if you want an example of something that does that, how about this metaclass. It creates a class that checks the stack frame to see if the caller was defined in the same class. Issues: Classes are prevented from defining their own __setattr__ and __getattribute__. Classes and subclasses should not use the same names for their private variables. Private attribute access is pretty slow, but that's obvious. Pretty easy to thwart. #-- import sys import itertools class PrivateAccessError(Exception): pass class PrivateDataMetaclass(type): def __new__(metacls,name,bases,dct): function = type(lambda x:x) privates = set(dct.get('__private__',())) codes = set() for val in dct.itervalues(): if isinstance(val,function): codes.add(val.func_code) getframe = sys._getframe count = itertools.count def __getattribute__(self,attr): if attr in privates: for i in count(1): code = getframe(i).f_code if code in codes: break if code.co_name != '__getattribute__': raise PrivateAccessError( attribute '%s' is private % attr) return super(cls,self).__getattribute__(attr) def __setattr__(self,attr,val): if attr in privates: for i in count(1): code = getframe(i).f_code if code in codes: break if code.co_name != '__setattr__': raise PrivateAccessError( attribute '%s' is private % attr) return super(cls,self).__setattr__(attr,val) dct['__getattribute__'] = __getattribute__ dct['__setattr__'] = __setattr__ cls = type.__new__(metacls,name,bases,dct) return cls #-- import traceback class A(object): __metaclass__ = PrivateDataMetaclass __private__ = ['internal'] def __init__(self,n): self.internal = n def inc(self): self.internal += 1 def res(self): return self.internal class B(A): __private__ = ['internal2'] def __init__(self,n,m): super(B,self).__init__(n) self.internal2 = m def inc(self): super(B,self).inc() self.internal2 += 2 def res(self): return self.internal2 + super(B,self).res() def bad(self): return self.internal2 + self.internal a = A(1) a.inc() print Should print 2: print a.res() print print Should raise PrivateAccessError: try: print a.internal except PrivateAccessError: traceback.print_exc() print b = B(1,1) b.inc() print Should print 5: print b.res() print print Should raise PrivateAccessError: try: print b.internal2 except PrivateAccessError: traceback.print_exc() print print Should raise PrivateAccessError: try: print b.bad() except PrivateAccessError: traceback.print_exc() print #-- Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 6:40 am, Antoon Pardon [EMAIL PROTECTED] wrote: On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 5:38 am, Antoon Pardon [EMAIL PROTECTED] wrote: If you really need it, you can do data hiding in python. It just requires a bit more work. - Hide.py - class Rec(object): def __init__(__, **kwargs): for key,value in kwargs.items(): setattr(__, key, value) def __getitem__(self, key): return getattr(self, key) def __setitem__ (self, key, val): setattr(self, key, val) class Foo(object): def __init__(self): hidden = Rec(x=0, y=0) def SetX(val): hidden.x = val def SetY(val): hidden.y = val def GetX(): return hidden.x def GetY(): return hidden.y self.SetX = SetX self.SetY = SetY self.GetX = GetX self.GetY = GetY In other words, it's a useless no-op. In fact, I'd say this is even worse than useless. Creating accessor functions is a sort of blessing for external use. Knowing that there are accessor functions is likely to cause a user to show even less restraint. I think you completed missed the point. I'm not sure I missed the point so much as I failed to read your mind. Fine with me, it is just the other side of the coin. This is just a proof of concept thing. In a real example there would of course no Set en Get methods but just methods that in the course of their execution would access or update the hidden attributes Fair enough, but I don't see anything in your example that suggests a way to discriminate between access from within the class and access from outside the class, which is the crucial aspect of data hiding. The fact is that hidden and its attributes are not accessible from outside the instance. They are only accessible to the local functions of __init__. By binding those local functions as atributes to the instance, hidden can be modified by what for all practical purposes looks like a method call, but really is a closure call. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: a question about the #prefix of sys.argv
On Jun 2, 8:50 am, Aldarion [EMAIL PROTECTED] wrote: On 6月2日, 上午8时05分, Peter Otten [EMAIL PROTECTED] wrote: Aldarion wrote: for the little script #egg.py import sys for k,v in enumerate(sys.argv): print k,v it ignores the part after # on linux below is the running output on windows and linux. no clue here. This has nothing to do with python, it's the shell that treats the # and everything that follows as a comment. $ ./listargs.py alpha #beta 0 ./listargs.py 1 alpha But you can escape it: $ ./listargs.py alpha \#beta 0 ./listargs.py 1 alpha 2 #beta $ ./listargs.py alpha '#beta' 0 ./listargs.py 1 alpha 2 #beta Peter thanks everyone for the quickly reply, i see now. Thank you Aldarion for your post. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 9:07 am, Antoon Pardon [EMAIL PROTECTED] wrote: On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 6:40 am, Antoon Pardon [EMAIL PROTECTED] wrote: On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 5:38 am, Antoon Pardon [EMAIL PROTECTED] wrote: If you really need it, you can do data hiding in python. It just requires a bit more work. - Hide.py - class Rec(object): def __init__(__, **kwargs): for key,value in kwargs.items(): setattr(__, key, value) def __getitem__(self, key): return getattr(self, key) def __setitem__ (self, key, val): setattr(self, key, val) class Foo(object): def __init__(self): hidden = Rec(x=0, y=0) def SetX(val): hidden.x = val def SetY(val): hidden.y = val def GetX(): return hidden.x def GetY(): return hidden.y self.SetX = SetX self.SetY = SetY self.GetX = GetX self.GetY = GetY In other words, it's a useless no-op. In fact, I'd say this is even worse than useless. Creating accessor functions is a sort of blessing for external use. Knowing that there are accessor functions is likely to cause a user to show even less restraint. I think you completed missed the point. I'm not sure I missed the point so much as I failed to read your mind. Fine with me, it is just the other side of the coin. This is just a proof of concept thing. In a real example there would of course no Set en Get methods but just methods that in the course of their execution would access or update the hidden attributes Fair enough, but I don't see anything in your example that suggests a way to discriminate between access from within the class and access from outside the class, which is the crucial aspect of data hiding. The fact is that hidden and its attributes are not accessible from outside the instance. They are only accessible to the local functions of __init__. By binding those local functions as atributes to the instance, hidden can be modified by what for all practical purposes looks like a method call, but really is a closure call. You haven't hidden the data at all, all you've done is to change the means of accessing it. What difference does it make whether I write foo.getX() or foo.x? Everyone in the world still has full access to the data. You are not realizing that only useful(**) thing about data hiding is that some code has access to the data, other code does not. If you hide data equally from everyone it's just a useless spelling change. ** - Usefulness is questionable in most cases, but we assume it is here for the sake of argument. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Shed my a light :)
TheSaint wrote: On 19:06, lunedì 02 giugno 2008 Chris wrote: actions= ('print', 'sum', 'divide', 'myfunction') parameters=(5, 'nothing',5.63, object) 8 8 getattr(...) getattr(object, name[, default]) - value 8 8 for nn in actions: func = getattr(cp, nn) if callable(func): func(parameters) I got the point of Duncan and I should remain on evail() because the evaluation is made on a construct of string expression, which give me the final name of the function I want to call. Why do you still need eval? Can't you just construct the string describing the function to call and then use getattr() to get the actual function object to call? I've tried on Pyshell and clearly said the object str is not callable. Well no, but I don't think anyone suggested that it would be. The name of a function to call and the function itself are different things. getattr() helps you to get from the first to the latter. Some of those string are functions inside the module, so I was expecting a sequence of calls according the passed in functions names, but they *must* be processed as a python statements ;( This doesn't exactly make sense, as what you want isn't really clear... Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 9:07 am, Antoon Pardon [EMAIL PROTECTED] wrote: On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 6:40 am, Antoon Pardon [EMAIL PROTECTED] wrote: On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 5:38 am, Antoon Pardon [EMAIL PROTECTED] wrote: If you really need it, you can do data hiding in python. It just requires a bit more work. - Hide.py - class Rec(object): def __init__(__, **kwargs): for key,value in kwargs.items(): setattr(__, key, value) def __getitem__(self, key): return getattr(self, key) def __setitem__ (self, key, val): setattr(self, key, val) class Foo(object): def __init__(self): hidden = Rec(x=0, y=0) def SetX(val): hidden.x = val def SetY(val): hidden.y = val def GetX(): return hidden.x def GetY(): return hidden.y self.SetX = SetX self.SetY = SetY self.GetX = GetX self.GetY = GetY In other words, it's a useless no-op. In fact, I'd say this is even worse than useless. Creating accessor functions is a sort of blessing for external use. Knowing that there are accessor functions is likely to cause a user to show even less restraint. I think you completed missed the point. I'm not sure I missed the point so much as I failed to read your mind. Fine with me, it is just the other side of the coin. This is just a proof of concept thing. In a real example there would of course no Set en Get methods but just methods that in the course of their execution would access or update the hidden attributes Fair enough, but I don't see anything in your example that suggests a way to discriminate between access from within the class and access from outside the class, which is the crucial aspect of data hiding. The fact is that hidden and its attributes are not accessible from outside the instance. They are only accessible to the local functions of __init__. By binding those local functions as atributes to the instance, hidden can be modified by what for all practical purposes looks like a method call, but really is a closure call. You haven't hidden the data at all, all you've done is to change the means of accessing it. What difference does it make whether I write foo.getX() or foo.x? Everyone in the world still has full access to the data. Can't you look beyond the specific example? The GetX is just an example. Any local function of __init__ has access to hidden and its attributes and could manipulate them, even if the class wouldn't define getters and setters. You are not realizing that only useful(**) thing about data hiding is that some code has access to the data, other code does not. If you hide data equally from everyone it's just a useless spelling change. I already explained this. The code is a proof of concept. I agree that the example itself doesn't do anything usefull, that doesn't mean that the concept it is trying to illustrate is useless too. Your metaclass can be used to make attributes private. Your metaclass doesn't stop doing that because someone makes a class with a getter and a setter for those private attributes. The same goes for my example. The closure makes a variable inaccessable to the outside. That doesn't change because I defined a getter and a setter for that closure variable. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list
Re: mocking a logging object
On Jun 2, 12:34 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: Peter Bengtsson wrote: In my unittest I want to override the logger of a working module so that it puts all logging messages in /tmp/test.log instead so that in my unittest I can inspect that it logs things correctly. Hopefully this pseudo code will explain my problem:: import logging, os logging.basicConfig(filename='/tmp/real.log', level=logging.INFO) logger = logging.getLogger('Real') logger.info('Real stuff') os.path.isfile('/tmp/real.log') True # do the monkey patching like the unit test does logging.basicConfig(filename='/tmp/test.log', level=logging.INFO) logger = logging.getLogger('Test') logger.info('Test stuff') os.path.isfile('/tmp/test.log') False open('/tmp/real.log').read() 'INFO:Real:Real stuff\nINFO:Test:Test stuff\n' How can I change what file the logger should write to? You should simply attach a new handler to the logger in question that logs the data into a stream/StringIO-object for later retrieval. Then remove that handler after the test. Diez Thanks! I'll try that. -- http://mail.python.org/mailman/listinfo/python-list
Re: parse dates
brechmos [EMAIL PROTECTED] writes: Hi, I have been using PHP the last while and in particular strtotime. What I want to replicate is finding the second or fourth Monday of the next month. In PHP with strtotime it is easy (strtotime(second Monday, strtotime(next month), but I can't find an easy way to do it in Python. I have seen DateUtil, but it seems to be able to do only the simpler parsing (could be wrong). Any other ideas? http://code-bear.com/code/parsedatetime/ import parsedatetime.parsedatetime as pdt p = pdt.Calendar() p.parse('next month') ((2008, 7, 1, 9, 0, 0, 1, 183, -1), 1) Eddie -- http://mail.python.org/mailman/listinfo/python-list
Re: ValueError: unknown locale: UTF-8
On Jun 1, 8:43 pm, Martin v. Löwis [EMAIL PROTECTED] wrote: ValueError: unknown locale: UTF-8 This is on open bug or is there more to it? Do you have an environment variable set who is named either LANG or starts with LC_? Actually, yes: LC_CTYPE=UTF-8 where is it coming from? This is basically on a clean new machine... who might be setting it? It is coming from a program elsewhere on the system? How should python code deal with this? Thanks! mario -- http://mail.python.org/mailman/listinfo/python-list
Greetings, fellow Pythonistas!
Hello, all! This post is to announce a new Python-oriented blog. See my .sig for the URL. I also have a question: is there any official method for getting listed on Planet Python? Thanks! A fellow Pythonista -- code.py: A blog about life, the universe, and Python http://pythonista.wordpress.com ** Posted from http://www.teranews.com ** -- http://mail.python.org/mailman/listinfo/python-list
Re: Shed my a light :)
On 19:06, lunedì 02 giugno 2008 Chris wrote: actions= ('print', 'sum', 'divide', 'myfunction') parameters=(5, 'nothing',5.63, object) 8 8 getattr(...) getattr(object, name[, default]) - value 8 8 for nn in actions: func = getattr(cp, nn) if callable(func): func(parameters) I got the point of Duncan and I should remain on evail() because the evaluation is made on a construct of string expression, which give me the final name of the function I want to call. I've tried on Pyshell and clearly said the object str is not callable. Some of those string are functions inside the module, so I was expecting a sequence of calls according the passed in functions names, but they *must* be processed as a python statements ;( -- Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html -- http://mail.python.org/mailman/listinfo/python-list
Re: printf in python
On 2 Giu, 10:08, Hrvoje Niksic [EMAIL PROTECTED] wrote: gianluca [EMAIL PROTECTED] writes: Hy, I've a problem with may python library generated with swig from C code. I works and I can access all function but a simèple function that print a string don't work's. The function is like this: int PrintTEST() { printf(TEST ); return 1; } Finish the printed string with a newline, or call fflush(stdout). By default stdout is line-buffered when connected to interactive displays. Exuse me! How could use it?. I've tried with '\n' without result. Thanks Gianluca -- http://mail.python.org/mailman/listinfo/python-list
Re: ctypes, function pointers and a lot of trouble
Hi, okay, thanks now my DLL function seems to accept the functions given, but instead of running my program keeps crashing (with windows address violation errors). I did some further investigations on that and figured out that the contextH variable is not just an identifier as I thought, but a quite complicated datastreamhandler. Now to get it to work I additionally need to implement the following c Structures: --CODE- /* The internal data structure object of a stream */ typedefstructtagMemStreamData { cdCharmode; cdInt32lPos; cdUInt32dwVisibleSize; cdUInt32dwBufferSize; cdChar*cpBuffer; }MemStreamData; typedefstructtagFilStreamData { cdCharszFileName[MAX_PATH]; HANDLEhFile; }FilStreamData; --\CODE This function just creates the pStream filestream (which is what I have to do too) --CODE- BOOLCreateMyFilStream(cdStream*pStream, cdChar*szFileName ) { FilStreamData*pFilStrm; /* The domain for data is secured. */ pFilStrm = new FilStreamData; if( pFilStrm == NULL ) { returnFALSE; } /* Data is changed the first stage. */ pFilStrm-hFile = INVALID_HANDLE_VALUE; strcpy( pFilStrm-szFileName, szFileName ); pStream-contextH = (cdContext)pFilStrm; pStream-close = _CloseMyFilStream; pStream-open = _OpenMyFilStream; pStream-read = _ReadMyFilStream; pStream-seek = _SeekMyFilStream; pStream-tell = _TellMyFilStream; pStream-write = _WriteMyFilStream; returnTRUE; } --\CODE Now my solution is the following: --CODE- class MemStreamData(Structure): _fields_ = [(mode, c_byte), (lPos, c_uint), (dwVisibleSize, c_uint), (dwBufferSize, c_uint), (cpBuffer, POINTER(c_char))] class FilStreamData (Structure): _fields_ = [(szFileName, c_char * 30), (hFile, c_uint)] --\CODE and the code to fill all that with the right data is the following: --CODE- datainfo = cdReleaseImageInfo() databuf = c_char * 10 cbuffer=MemStreamData() cbuffer.mode = c_byte(0) cbuffer.lPos = c_uint(0) cbuffer.dwVisibleSize = c_uint(10) cbuffer.dwBufferSize = c_uint(10) #this line does not work, wrong datatype!? cbuffer.cpBuffer = POINTER(databuf) cpointer = cast(cbuffer, POINTER(c_uint)) stream = cdStream() stream.contextH=cpointer stream.open = cstreamopen(pystreamopen) stream.close = cstreamclose(pystreamclose) stream.write = cstreamwrite(pystreamwrite) stream.tell = cstreamtell(pystreamtell) stream.read = cstreamread(pystreamread) data = cdStgMedium() data.Type = c_uint(1) data.u.pStream = POINTER(cdStream)(stream) --\CODE Does anyone see where the errors are? Best regards and thanks a lot, Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: php vs python
In article [EMAIL PROTECTED], Ethan Furman [EMAIL PROTECTED] wrote: Jerry Stuckle wrote: As I've said before - good programmers can write good code in any language. So... an eloquent speaker of English is also an eloquent speaker of Spanish/French/German? Oh, Bull. Computer languages are so narrow they are more like dialects of a single language. If you know English you can learn to speak to a Brit, Scot, or Aussie. That's more like it...if you really want to stretch the definition of a language. And it is a stretch. Anyone speak C? Sheeze. -- -- Lou Pecora -- http://mail.python.org/mailman/listinfo/python-list
Re: ThreadPoolingMixIn
On May 31, 9:13 pm, Rhamphoryncus [EMAIL PROTECTED] wrote: On May 30, 2:40 pm, [EMAIL PROTECTED] wrote: Hi, everybody! I wrote a useful class ThreadPoolingMixIn which can be used to create fast thread-based servers. This mix-in works much faster than ThreadingMixIn because it doesn't create a new thread on each request. Do you have any benchmarks demonstrating the performance difference/ To benchmark this I used a simple tcp server which writes a small (16k) string to the client and closes the connection. I started 100 remote clients and got 500 replies/s for ThreadingMixIn and more than 1500 replies/s for ThreadPoolingMixIn. I tested it on FreeBSD 6.2 amd64. I'm very curious about the exactness of the number 500 for ThreadingMixIn. It seems to be the same for various packet sizes. I suspect there is some OS limit on thread creating rate. Below I include a bugfixed ThreadPoolingMixIn and the benchmarking utility. The utility can be used to start clients on localhost, though the reply rate will be slower (around 1000 replies/s). To start benchmarking server with localhost clients use: python ./TestServer.py --server=threading --n-clients=100 or python ./TestServer.py --server=threadpooling --n-clients=100 #--- ThreadPoolingMixIn.py from __future__ import with_statement from SocketServer import ThreadingMixIn import threading import Queue class ThreadPoolingMixIn(ThreadingMixIn): Mix-in class to handle requests in a thread pool. The pool grows and thrinks depending on load. For instance, a threadpooling TCP server class is created as follows: class ThreadPoolingUDPServer(ThreadPoolingMixIn, TCPServer): pass __author__ = 'Pavel Uvarov [EMAIL PROTECTED]' def init_thread_pool(self, min_workers = 5, max_workers = 100, min_spare_workers = 5): Initialize thread pool. self.q = Queue.Queue() self.min_workers = min_workers self.max_workers = max_workers self.min_spare_workers = min_spare_workers self.num_workers = 0 self.num_busy_workers = 0 self.workers_mutex = threading.Lock() self.start_workers(self.min_workers) def start_workers(self, n): Start n workers. for i in xrange(n): t = threading.Thread(target = self.worker) t.setDaemon(True) t.start() def worker(self): A function of a working thread. It gets a request from queue (blocking if there are no requests) and processes it. After processing it checks how many spare workers are there now and if this value is greater than self.min_spare_workers then the worker exits. Otherwise it loops infinitely. with self.workers_mutex: self.num_workers += 1 while True: (request, client_address) = self.q.get() with self.workers_mutex: self.num_busy_workers += 1 self.process_request_thread(request, client_address) self.q.task_done() with self.workers_mutex: self.num_busy_workers -= 1 if (self.num_workers self.min_workers and self.num_workers - self.num_busy_workers self.min_spare_workers): self.num_workers -= 1 return def process_request(self, request, client_address): Puts a request into queue. If the queue size is too large, it adds extra worker. self.q.put((request, client_address)) with self.workers_mutex: if self.q.qsize() 3 and self.num_workers self.max_workers: self.start_workers(1) def join(self): Wait for all busy threads self.q.join() #--- TestServer.py from __future__ import with_statement from SocketServer import * import socket import sys import threading import time import os from ThreadPoolingMixIn import * class ThreadPoolingTCPServer(ThreadPoolingMixIn, TCPServer): pass class TestServer(ThreadingTCPServer): allow_reuse_address = True request_queue_size = 128 def __init__(self, server_address, RequestHandlerClass, packet_size): TCPServer.__init__(self, server_address, RequestHandlerClass) self.packet_size = packet_size self.sum_t = 0 self.total_num_requests = 0 self.num_requests = 0 self.t0 = time.time() self.lock = threading.Lock() def reset_stats(self): with self.lock: self.total_num_requests += self.num_requests self.num_requests = 0 self.sum_t = 0 self.t0 = time.time() def update_stats(self, t0, t1): with self.lock: self.num_requests += 1 self.sum_t += t1 - t0 n = self.num_requests sum_t = self.sum_t avg_t = sum_t / n rate = n / (t1 -
Re: ThreadPoolingMixIn
On Jun 2, 7:09 pm, [EMAIL PROTECTED] wrote: On May 31, 9:13 pm, Rhamphoryncus [EMAIL PROTECTED] wrote: On May 30, 2:40 pm, [EMAIL PROTECTED] wrote: Hi, everybody! I wrote a useful class ThreadPoolingMixIn which can be used to create fast thread-based servers. This mix-in works much faster than ThreadingMixIn because it doesn't create a new thread on each request. Do you have any benchmarks demonstrating the performance difference/ To benchmark this I used a simple tcp server which writes a small (16k) string to the client and closes the connection. I started 100 remote clients and got 500 replies/s for ThreadingMixIn and more than 1500 replies/s for ThreadPoolingMixIn. I tested it on FreeBSD 6.2 amd64. I'm very curious about the exactness of the number 500 for ThreadingMixIn. It seems to be the same for various packet sizes. I suspect there is some OS limit on thread creating rate. Below I include a bugfixed ThreadPoolingMixIn and the benchmarking utility. The utility can be used to start clients on localhost, though the reply rate will be slower (around 1000 replies/s). To start benchmarking server with localhost clients use: python ./TestServer.py --server=threading --n-clients=100 or python ./TestServer.py --server=threadpooling --n-clients=100 I've just tested it on a linux box and got a 240 replies/s vs 2000 replies/s, that is 8x performance improvement. -- http://mail.python.org/mailman/listinfo/python-list
Re: ThreadPoolingMixIn
[EMAIL PROTECTED] wrote: To benchmark this I used a simple tcp server which writes a small (16k) string to the client and closes the connection. Just a general note: When benchmarking such a network service it would be valuable to see benchmark results for several data sizes. I'd expect better numbers for a ThreadPoolingMixIn when there are more requests with smaller data size. Ciao, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: mocking a logging object
On Jun 2, 12:34 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: Peter Bengtsson wrote: In my unittest I want to override the logger of a working module so that it puts all logging messages in /tmp/test.log instead so that in my unittest I can inspect that it logs things correctly. Hopefully this pseudo code will explain my problem:: import logging, os logging.basicConfig(filename='/tmp/real.log', level=logging.INFO) logger = logging.getLogger('Real') logger.info('Real stuff') os.path.isfile('/tmp/real.log') True # do the monkey patching like the unit test does logging.basicConfig(filename='/tmp/test.log', level=logging.INFO) logger = logging.getLogger('Test') logger.info('Test stuff') os.path.isfile('/tmp/test.log') False open('/tmp/real.log').read() 'INFO:Real:Real stuff\nINFO:Test:Test stuff\n' How can I change what file the logger should write to? You should simply attach a new handler to the logger in question that logs the data into a stream/StringIO-object for later retrieval. Then remove that handler after the test. Diez Yes! That worked. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Shed my a light :)
On 22:00, lunedì 02 giugno 2008 Paul Melis wrote: This doesn't exactly make sense, as what you want isn't really clear... Sorry, I'm bad to express my toughts even I my nature language :) I'll give a go to getattr() and see whether the results come in my taste :) -- Mailsweeper Home : http://it.geocities.com/call_me_not_now/index.html -- http://mail.python.org/mailman/listinfo/python-list
Re: ThreadPoolingMixIn
On Jun 2, 7:15 pm, Michael Ströder [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: To benchmark this I used a simple tcp server which writes a small (16k) string to the client and closes the connection. Just a general note: When benchmarking such a network service it would be valuable to see benchmark results for several data sizes. I'd expect better numbers for a ThreadPoolingMixIn when there are more requests with smaller data size. Ciao, Michael. Here are benchmarks for FreeBSD 6.2, amd64 packet_size x y 0499.57 1114.54 1024499.29 1130.02 3072500.09 1119.14 7168498.20 .76 15360499.29 1086.73 31744500.04 1036.46 64512499.43939.60 130048499.28737.44 261120498.04499.03 523264307.54312.04 1047552173.57185.32 2096128 93.61 94.39 x = ThreadingMixIn replies/s y = ThreadPoolingMixIn replies/s -- http://mail.python.org/mailman/listinfo/python-list
[Re] Checking each item in m.group()?
Hello I need to go through each line of a CSV file, and extract some fields using a regex. Then, I need to check each retrieved field, and if it looks like , turn this into NULL so that it's correct SQL. I tried a few things, but still can't it working: #Second field might be empty - #Col1, #Col1,Col2 p = re.compile('^(.+?),(.*?)$') for line in textlines: m = p.search(line) if m: #Check each column : if '', then turn into NULL for col in line: if col == : col = NULL for col in m.group(): if col == : col=NULL for col in m.group(0): if col == : col=NULL for i in range (0,len(line)): if line[i] == : line[i]=NULL for i in range(1,len(m.group(0))): if m.group(i) == : m.group(i)=NULL sql = INSERT INTO mytable (col1, col2) VALUES ('%s','%s') % (m.group(1),m.group(2)) print sql f.close() Does someone know the correct syntax? Thank you. -- http://mail.python.org/mailman/listinfo/python-list
python blogs
Hello! It seems like Python blogs are gaining popularity. It seems to me that they play a crucial role in promoting Python as a language. Do you agree with that? Just a few days ago I've finished setting up a dedicated Python blogging environment at: http://www.pythonblogs.com Do you think it will be useful for Python community? By the way, everyone is welcome to join. Sincerely yours, ~pyblog -- http://mail.python.org/mailman/listinfo/python-list
Re: printf in python
On 2 Giu, 17:54, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On Mon, 2 Jun 2008 00:32:33 -0700 (PDT), gianluca [EMAIL PROTECTED] declaimed the following in comp.lang.python: Hy, I've a problem with may python library generated with swig from C code. I works and I can access all function but a simèple function that print a string don't work's. The function is like this: int PrintTEST() { printf(TEST ); return 1; } If I call the function (myDLL.PrintTEST() ) the function print only the returned value, not the string TEST. Could anybody help me? Are you running from a command shell? If you are testing from inside something like IDLE (or PythonWin, or pretty much any other GUI development system) you may not have a stdout for display by C code... The return value would be printed by the Python interpreter in interactive mode... -- WulfraedDennis Lee Bieber KD6MOG [EMAIL PROTECTED] [EMAIL PROTECTED] HTTP://wlfraed.home.netcom.com/ (Bestiaria Support Staff: [EMAIL PROTECTED]) HTTP://www.bestiaria.com/ Hy, the problem exists both in command shell and in IDLE. The value is correct returned in both envirnment. gianluca -- http://mail.python.org/mailman/listinfo/python-list
Re: ValueError: unknown locale: UTF-8
Actually, yes: LC_CTYPE=UTF-8 where is it coming from? This is basically on a clean new machine... who might be setting it? There are many possible places. grep in your Library, as a starting point. As on Macintosh lists. It is coming from a program elsewhere on the system? How should python code deal with this? Python should do what it does: just crash. This setting is not supported. Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking each item in m.group()?
On Jun 2, 11:42 am, [EMAIL PROTECTED] Gilles@ wrote: Hello I need to go through each line of a CSV file, and extract some fields using a regex. Then, I need to check each retrieved field, and if it looks like , turn this into NULL so that it's correct SQL. I tried a few things, but still can't it working: #Second field might be empty - #Col1, #Col1,Col2 p = re.compile('^(.+?),(.*?)$') for line in textlines: m = p.search(line) if m: #Check each column : if '', then turn into NULL for col in line: if col == : col = NULL for col in m.group(): if col == : col=NULL for col in m.group(0): if col == : col=NULL for i in range (0,len(line)): if line[i] == : line[i]=NULL for i in range(1,len(m.group(0))): if m.group(i) == : m.group(i)=NULL sql = INSERT INTO mytable (col1, col2) VALUES ('%s','%s') % (m.group(1),m.group(2)) print sql f.close() Does someone know the correct syntax? Thank you. I think you want to use 'groups' instead of 'group'. Here is a brief example. Note that this code is very insecure and susceptible to a SQL injection attack. Hopefully these csv files are from a trusted source. sql = INSERT INTO mytable (col1, col2) VALUES ('%s','%s')%tuple( (c, NULL)[c == ''] for c in m.groups() ) Also, check out the csv module for parsing your csv file. Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing HTML
Ken Starks wrote: [EMAIL PROTECTED] wrote: I've searched the standard library docs, and, while there are a couple options for *reading* HTML from Python, I didn't notice any for *writing* it. Does anyone have any recommendations (particularly ones not listed on PyPI)? Thanks My approach is usually to write the data-load in XML rather than directly in HTML, and then use XSLT to produce the (X)HTML. You can do the whole of this, or just the second part, using FourSuite. If the XML, later, needs to be changed, you can use FourSuite to drill down to somewhere using XPath and change that particular part. Or lxml, for that purpose, which also is a lot nicer for generating XML and HTML in the first place. http://codespeak.net/lxml/ Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: ctypes, function pointers and a lot of trouble
Matt [EMAIL PROTECTED] wrote: class MemStreamData(Structure): _fields_ = [(mode, c_byte), (lPos, c_uint), (dwVisibleSize, c_uint), (dwBufferSize, c_uint), (cpBuffer, POINTER(c_char))] class FilStreamData (Structure): _fields_ = [(szFileName, c_char * 30), (hFile, c_uint)] and the code to fill all that with the right data is the following: datainfo = cdReleaseImageInfo() databuf = c_char * 10 cbuffer=MemStreamData() cbuffer.mode = c_byte(0) cbuffer.lPos = c_uint(0) cbuffer.dwVisibleSize = c_uint(10) cbuffer.dwBufferSize = c_uint(10) #this line does not work, wrong datatype!? cbuffer.cpBuffer = POINTER(databuf) [snip] Does anyone see where the errors are? databuf = c_char * 10 Is a type not an instance... You need to instantiate it, eg from ctypes import * class MemStreamData(Structure): ... _fields_ = [(cpBuffer, POINTER(c_char))] ... cbuffer=MemStreamData() databuf = c_char * 10 cbuffer.cpBuffer = POINTER(databuf) Traceback (most recent call last): File stdin, line 1, in module TypeError: expected LP_c_char instance, got _ctypes.PointerType databuftype = c_char * 10 databuf = databuftype() cbuffer.cpBuffer = databuf databuf __main__.c_char_Array_10 object at 0xb7d04dac There is a shorthand for exactly this though as it is a common operation databuf = create_string_buffer(1000) cbuffer.cpBuffer = databuf databuf ctypes.c_char_Array_1000 object at 0xb7d129bc -- Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
robotparser behavior on 403 (Forbidden) robot.txt files
I just discovered that the robotparser module interprets a 403 (Forbidden) status on a robots.txt file as meaning all access disallowed. That's unexpected behavior. A major site (http://www.aplus.net/robot.txt;) has their robots.txt file set up that way. There's no real robots.txt standard, unfortunately. So it's not definitively a bug. John Nagle SiteTruth -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting Output
On Jun 2, 3:38 am, Chris [EMAIL PROTECTED] wrote: On Jun 2, 9:34 am, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 The matter is that i don't know in advance how many columns there will be. By the way, each column will be actually FLOATs, not INTs. How can i do this ? Any help welcome. Regards, Victor import sys float_list = [1.0, 5.0, 6.0, 2.0, 2.0, 8.0, 2.0, 9.0, 5.0] num_columns = 3 for i,item in enumerate(float_list): sys.stdout.write('%.4f\t' % item) if not (i+1) % num_columns: sys.stdout.write('\n') Problem with this approach is it doesn't cater for instances where you exceed the standard 80 characters for a terminal window. That wouldn't be a problem if being re-directed to a file. A bigger problem would be if his list were actually [1.0,2.0,2.0,5.0,2.0,9.0,6.0,8.0,5.0] but he still wanted it printed 1 5 6 2 2 8 2 9 5 as if he always wants 3 rows, but doesn't know how many columns it will take. In which case, he could do something like this: import sys float_list = [1.0,2.0,2.0,5.0,2.0,9.0,6.0,8.0,5.0] num_rows = 3 num_cols = divmod(len(float_list),num_rows) for r in xrange(num_rows): for c in xrange(num_cols[0]): sys.stdout.write('%.4f\t' % float_list[c*num_rows + r]) if num_cols[1]0 and rnum_cols[1]: sys.stdout.write('%.4f\t' % float_list[(c+1)*num_rows + r]) sys.stdout.write('\n') 1. 5. 6. 2. 2. 8. 2. 9. 5. And then changing list size would merely add more columns, so [1.0,2.0,2.0,5.0,2.0,9.0,6.0,8.0,5.0,6.] would print as 1. 5. 6. 6. 2. 2. 8. 2. 9. 5. -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting Output
On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator [EMAIL PROTECTED] wrote: On Jun 2, 3:38 am, Chris [EMAIL PROTECTED] wrote: On Jun 2, 9:34 am, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 ... I have a related question: Does Python have (or can emulate) the formatted output capability found in Perl? For example, all I have to do to get nicely formatted (i.e., aligned) output is provide values for special STDOUT variables (i.e., STDOUT_TOP, STDOUT, STDOUT_BOTTOM, etc.), exemplified by: format STDOUT_TOP = -- ~ . format STDOUT = @ @ @ $res-{'full_name'}, $res-{'phone_1'}, $res-{'phone_1_type'} @ @ ~ $res-{'address_1a'},$res-{'address_2a'} @ @ ~ $res-{'address_1b'},$res-{'address_2b'} @ @ ~ $res-{'address_1c'},$res-{'address_2c'} @ @ ~ $city_1 $city_2 @ @ ~ $res-{'email_1'}, $res-{'email_2'} -- ~ . Then, all I have to do is populate my $res object/hash as desired -- in this example simple the results of a SQL query -- and lastly just call the write function: write; and Perl will produce very nicely formatted results. This is useful not only for producing human readable output, but also fixed-column-width data files, etc. I'd love to learn the Pythonistic way of doing the same thing. Thanks! Doug -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: equivalence 0.1
On Jun 1, 2008, at 6:16 PM, George Sakkis wrote: Equivalence is a class that can be used to maintain a partition of objects into equivalence sets, making sure that the equivalence properties (reflexivity, symmetry, transitivity) are preserved. Two objects x and y are considered equivalent either implicitly (through a key function) or explicitly by calling merge(x,y). I think this library would be very useful, and I like the interface (in particular the idea of the projecting function), but I think the algorithm is less than optimal. It can show quadratic behavior in some cases: $ python -m timeit -s 'import equivalence' -s 'eq = equivalence.Equivalence()' 'for i in xrange(1): eq.merge(i, i+1)' 10 loops, best of 3: 57.6 msec per loop $ python -m timeit -s 'import equivalence' -s 'eq = equivalence.Equivalence()' 'for i in xrange(1): eq.merge(i+1, i)' 10 loops, best of 3: 2.59 sec per loop Have you considered using the Union-Find algorithm, which would be (almost) linear in all cases? -- Giuseppe -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's doc problems: sort
Hi, On Wed, Apr 30, 2008 at 4:48 AM, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: For example, in last week, that page is fetched 550 times. The second most popular page, trails quite a distance. Here's the top yup that was me, i have access to a couple of machines and wanted to test some intercommunication, I faked most of the user agent strings but still was locked out by popular pages. I then figured it would be much better to fetch a useless page a couple of times. Sorry for the lognoise... /martin -- http://www.xing.com/profile/Martin_Marcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: platypus in page header
On Jun 2, 1:16 pm, Laszlo Nagy [EMAIL PROTECTED] wrote: Is it possible to use platypus in page header and footer? I need to create a document with long paragraphs but also I need to put tables and images in page header and multi line info in page footer with alignment etc. Thanks, Laszlo From looking at the docs, it sounds like you could do that, if you create your flowables correctly. You should probably ask over on the reportlab user's list though. Those guys would almost certainly know: http://two.pairlist.net/mailman/listinfo/reportlab-users Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 6:41 am, Carl Banks [EMAIL PROTECTED] wrote: You are not realizing that only useful(**) thing about data hiding is that some code has access to the data, other code does not. If you hide data equally from everyone it's just a useless spelling change. I think you're missing the point. As I see it, the primary value of data hiding is that it provides useful information on which data and methods are intended for the client and which are intended for internal use. It's like putting a front panel on a TV set with the main controls intended for the viewer. People seem to be preoccupied with whether or not the back panel of the TV is locked, but that is not the main issue. Sure, you probably want to make the back panel removable, but you don't want the viewer opening it up to change the channel, and you certainly don't want to put all the internal adjustments for factory technicians together with the controls for the end user. As far as I am concerned, the current Python method of using underscores to distinguish between internal and external methods and data is an ugly hack that goes completely against the elegance of the language in other areas. It is like a TV set with no back cover and the volume and channel controls intermingled with the factory controls. The underscores are just an afterthought like a red dot or something used to tell the TV viewer what to fiddle with. Python is a very nice language overall, but as far as I am concerned the underscore convention is a blemish. I just wish people wouldn't get so infatuated with the language that they cannot see the obvious staring them in the face. -- http://mail.python.org/mailman/listinfo/python-list
Cast list of objects to list of strings
I have this function: def write_err(obj): from sys import stderr stderr.write(str(obj)+\n) and I'd like to rewrite it to take a variable number of objects. Something like this: def write_err(*objs): from sys import stderr stderr.write( .join(objs)+\n) but I lose the property that the function works on any object. What's the simplest way to fix this? In essence, I need to cast a list of objects to a list of strings. I'd like to do just str(objs) but that (obviously) doesn't quite do what I need. -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing HTML
On Jun 2, 3:19 pm, Stefan Behnel [EMAIL PROTECTED] wrote: Or lxml, for that purpose, which also is a lot nicer for generating XML and HTML in the first place. http://codespeak.net/lxml/ Awesome. lxml looks like exactly what I'm after... and it's in the Ubuntu repos. :-) Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: robotparser behavior on 403 (Forbidden) robot.txt files
I just discovered that the robotparser module interprets a 403 (Forbidden) status on a robots.txt file as meaning all access disallowed. That's unexpected behavior. That's specified in the norobots RFC: http://www.robotstxt.org/norobots-rfc.txt - On server response indicating access restrictions (HTTP Status Code 401 or 403) a robot should regard access to the site completely restricted. So if a site returns 403, we should assume that it did so deliberately, and doesn't want to be indexed. A major site (http://www.aplus.net/robot.txt;) has their robots.txt file set up that way. You should try http://www.aplus.net/robots.txt; instead, which can be accessed just fine. Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Cast list of objects to list of strings
bukzor wrote: I have this function: def write_err(obj): from sys import stderr stderr.write(str(obj)+\n) and I'd like to rewrite it to take a variable number of objects. Something like this: def write_err(*objs): from sys import stderr stderr.write( .join(objs)+\n) but I lose the property that the function works on any object. What's the simplest way to fix this? In essence, I need to cast a list of objects to a list of strings. I'd like to do just str(objs) but that (obviously) doesn't quite do what I need. I think what you want is: def write_err(*args): from sys import stderr stderr.write(\n.join([str(o) for o in args])) but then I don't really understand why you would want such a function so I could be way wrong. -Larry -- http://mail.python.org/mailman/listinfo/python-list
Re: [Re] Checking each item in m.group()?
[EMAIL PROTECTED] wrote: I need to go through each line of a CSV file, and extract some fields using a regex. Then, I need to check each retrieved field, and if it looks like , turn this into NULL so that it's correct SQL. You are taking the wrong approach here. Don't build SQL statements as strings; you are enabling the next SQL injection attack. Pass parameters using the DB API instead. Don't use regular expressions to parse a CSV file. Python's csv module is more likely to deal correctly with the quirks of that standard. A self-contained example: import csv import sqlite3 as sqlite from cStringIO import StringIO def records(infile): for row in csv.reader(infile): # replace empty strings in the second column with None # which translates to NULL in the database yield row[0], row[1] or None def main(): # create sample data; you may use a real file infile = StringIO(\ alpha,beta,gamma zeta,,theta ) # create the database db = sqlite.connect(:memory:) cursor = db.cursor() cursor.execute(create table demo (first, second);) # safely insert data cursor.executemany(insert into demo values (?, ?);, records(infile)) # show contents for row in cursor.execute(select first, second, second is NULL from demo order by first, second;): print row if __name__ == __main__: main() Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: python blogs
On Jun 2, 2:49 pm, [EMAIL PROTECTED] wrote: It seems like Python blogs are gaining popularity. It seems to me that they play a crucial role in promoting Python as a language. Neat! Do blogs on your site have to be about Python programming, or can people blog about anything? -- http://mail.python.org/mailman/listinfo/python-list
Re: ThreadPoolingMixIn
On Jun 2, 12:41 pm, [EMAIL PROTECTED] wrote: On Jun 2, 7:15 pm, Michael Ströder [EMAIL PROTECTED] wrote: Here are benchmarks for FreeBSD 6.2, amd64 packet_size x y 0 499.57 1114.54 1024 499.29 1130.02 3072 500.09 1119.14 7168 498.20 .76 15360 499.29 1086.73 31744 500.04 1036.46 64512 499.43 939.60 130048 499.28 737.44 261120 498.04 499.03 523264 307.54 312.04 1047552 173.57 185.32 2096128 93.61 94.39 x = ThreadingMixIn replies/s y = ThreadPoolingMixIn replies/s Well, I'd say you've got yourself a winner. Performance (at least on FreeBSD) seems as good or better for your ThreadPoolingMixin than ThreadingMixin. Is this with the default values of min=5 and max=5 worker threads? -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking each item in m.group()?
On Jun 2, 5:06 pm, Peter Otten [EMAIL PROTECTED] wrote: You are taking the wrong approach here. Don't build SQL statements as strings; you are enabling the next SQL injection attack. Pass parameters using the DB API instead. Don't use regular expressions to parse a CSV file. Python's csv module is more likely to deal correctly with the quirks of that standard. I'd like to second both these statements. Regardless of whether these CSV files are from a trusted source or not, it's a virtual truism of programming that eventually, any application will be used in ways it was not intended. Since using a parameterized query is a simple way to avoid a common security hole, even if such a thing could never be exploited by the app in its current configuration, you should do things the Right Way. That way, even if your code is twisted to some other use in the future, it's less likely to cause problems. -- http://mail.python.org/mailman/listinfo/python-list
Re: python blogs
On Jun 2, 4:14 pm, [EMAIL PROTECTED] wrote: On Jun 2, 2:49 pm, [EMAIL PROTECTED] wrote: It seems like Python blogs are gaining popularity. It seems to me that they play a crucial role in promoting Python as a language. Neat! Do blogs on your site have to be about Python programming, or can people blog about anything? I personally like Doug Hellman's Python Module of the Week posts. The people over at Planet TurboGears are usually interesting too. http://blog.doughellmann.com/ http://planet.turbogears.org/ Other than those though, I haven't really seen much that's updated regularly. Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting Output
On Jun 2, 9:43 pm, Doug Morse [EMAIL PROTECTED] wrote: On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator [EMAIL PROTECTED] wrote: On Jun 2, 3:38 am, Chris [EMAIL PROTECTED] wrote: On Jun 2, 9:34 am, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 ... I have a related question: Does Python have (or can emulate) the formatted output capability found in Perl? For example, all I have to do to get nicely formatted (i.e., aligned) output is provide values for special STDOUT variables (i.e., STDOUT_TOP, STDOUT, STDOUT_BOTTOM, etc.), exemplified by: format STDOUT_TOP = -- ~ . format STDOUT = @ @ @ $res-{'full_name'}, $res-{'phone_1'}, $res-{'phone_1_type'} @ @ ~ $res-{'address_1a'}, $res-{'address_2a'} @ @ ~ $res-{'address_1b'}, $res-{'address_2b'} @ @ ~ $res-{'address_1c'}, $res-{'address_2c'} @ @ ~ $city_1 $city_2 @ @ ~ $res-{'email_1'}, $res-{'email_2'} -- ~ . Then, all I have to do is populate my $res object/hash as desired -- in this example simple the results of a SQL query -- and lastly just call the write function: write; and Perl will produce very nicely formatted results. This is useful not only for producing human readable output, but also fixed-column-width data files, etc. I'd love to learn the Pythonistic way of doing the same thing. Thanks! Doug Can't seem to do this with dictionaries but... preformatted_string = %s %20s %20s %s %30s %s %30s print preformatted_string % ('first name'[:20], 'contact num 1'[:20], 'contact num type'[:20], 'address line 1'[:30], 'address line 2'[:30] 'address line 3'[:30], 'address line 4'[:30]) You could do something like that. the [:20] etc @ the end of the inputs is ofc to trim the strings to a max length. The string formatter supports %number of characters to move to the rights so you can use that for alignment. It's a bit late so maybe I buggered up when I tried to use dictionary assignment with it, but who knows :p -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting Output
On Jun 2, 11:34 pm, Chris [EMAIL PROTECTED] wrote: On Jun 2, 9:43 pm, Doug Morse [EMAIL PROTECTED] wrote: On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator [EMAIL PROTECTED] wrote: On Jun 2, 3:38 am, Chris [EMAIL PROTECTED] wrote: On Jun 2, 9:34 am, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 ... I have a related question: Does Python have (or can emulate) the formatted output capability found in Perl? For example, all I have to do to get nicely formatted (i.e., aligned) output is provide values for special STDOUT variables (i.e., STDOUT_TOP, STDOUT, STDOUT_BOTTOM, etc.), exemplified by: format STDOUT_TOP = -- ~ . format STDOUT = @ @ @ $res-{'full_name'}, $res-{'phone_1'}, $res-{'phone_1_type'} @ @ ~ $res-{'address_1a'}, $res-{'address_2a'} @ @ ~ $res-{'address_1b'}, $res-{'address_2b'} @ @ ~ $res-{'address_1c'}, $res-{'address_2c'} @ @ ~ $city_1 $city_2 @ @ ~ $res-{'email_1'}, $res-{'email_2'} -- ~ . Then, all I have to do is populate my $res object/hash as desired -- in this example simple the results of a SQL query -- and lastly just call the write function: write; and Perl will produce very nicely formatted results. This is useful not only for producing human readable output, but also fixed-column-width data files, etc. I'd love to learn the Pythonistic way of doing the same thing. Thanks! Doug Can't seem to do this with dictionaries but... preformatted_string = %s %20s %20s %s %30s %s %30s print preformatted_string % ('first name'[:20], 'contact num 1'[:20], 'contact num type'[:20], 'address line 1'[:30], 'address line 2'[:30] 'address line 3'[:30], 'address line 4'[:30]) You could do something like that. the [:20] etc @ the end of the inputs is ofc to trim the strings to a max length. The string formatter supports %number of characters to move to the rights so you can use that for alignment. It's a bit late so maybe I buggered up when I tried to use dictionary assignment with it, but who knows :p Actually just realised I had the number on the wrong side... :D preformatted_string = %(first_name)s %(contact_num)20s %(contact_type)20s print preformatted_string % {'first_name':'Chris', 'contact_num':'555-', 'contact_type':'Home'} -- http://mail.python.org/mailman/listinfo/python-list
Re: php vs python
Yeah I would agree that a decent (a few steps below good in my book) programmer should be able to have a decent handle on a new language, given some acclimatization time of course. The amount of time this period lasts varies on the language said programmer is learning, as well as the languages he knows. If they are far different (say going from object oriented to functional) it will take longer to learn. But it's not to say that after learning it, his code will be bad. Maybe not as good as experts, but not bad. But I would say the jump from not bad code to good code; or the jump from good code to great code are huge leaps. And in a badly designed language, it could be argued that even the best code is lacking. To say that language doesn't matter at all is extremely silly, otherwise we wouldn't have 5 million languages. Most languages are created, not just for the fun of it, but to solve something lacking in what is available elsewhere. (OK, there are a lot of languages people have created just because they are geeks, but most languages that people actually use are purposeful). Python was created to have something easier than perl and more powerful than bash to write shell scripts with and automate system administration tasks. PHP was created to enhance html with a powerful integrated server language that was easier than c but not alien to it. It was a replacement for cgi. For me, php is far too flat, making it pretty tough to navigate and get a handle on. If you know what you want to do, it probably has something to fit that task, but for exploring it's not fun. I rather enjoy python, where I can play around at the interpreter and try new things. PHP also is not very strict when it comes to types, even less strict than python, and in my newbieness that has caused me loads of problems. But I know people who swear by PHP, and I admit it has been used for many great sites, so it can't be that bad. Still, outside of the web sphere, it is a bit odd to use PHP. Not necesarily bad, just a bit odd. And there are situations that are a bit odd for python also (sound editing,etc). -- http://mail.python.org/mailman/listinfo/python-list
Re: Cast list of objects to list of strings
On Jun 2, 4:02 pm, Larry Bates [EMAIL PROTECTED] wrote: I think what you want is: def write_err(*args): from sys import stderr stderr.write(\n.join([str(o) for o in args])) Slight nitpick. If you are using version = 2.4 you could use a generator expression instead of a list comprehension to avoid building and throwing away a list. \n.join(str(o) for o in args) http://www.python.org/dev/peps/pep-0289/ Very unlikely to yield a material time difference in this case but cleaner IMO. ... Jay Graves -- http://mail.python.org/mailman/listinfo/python-list
Re: Merging ordered lists
On Jun 1, 1:49 am, Peter Otten [EMAIL PROTECTED] wrote: Peter Otten wrote: #untested Already found two major blunders :( # still untested import difflib def _merge(a, b): sm = difflib.SequenceMatcher(None, a, b) for op, a1, a2, b1, b2 in sm.get_opcodes(): if op == insert: yield b[b1:b2] elif op == replace: yield a[a1:a2] yield b[b1:b2] else: # delete, equal yield a[a1:a2] def merge(a, b): return sum(_merge(a, b), []) def merge_to_unique(sources): return unique(reduce(merge, sorted(sources, key=len, reverse=True))) difflib.SequenceMatcher looks promising; I'll try it. Thanks! def unique(items): u = set(items) if len(u) == len(items): return items result = [] for item in items: if item in u: result.append(item) u.remove(item) return result You did right by preserving the original (non-alphabetical) ordering, but I'm less enthusiastic about the shape of this function. My original function used 7 lines of code, and only 1 for the unique() step. This uses up to three container objects. Is it really an improvement? (Secret: the reference list (or, any of the sources) is unlikely to be more than a few dozen elements long. The data set that puts merge_to_unique through a workout will be a giant list of comparatively short lists, so the unique() part just needs to be short and conceptually clean, while merge() should attempt sane behavior for large len(sources).) -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
Well that's nice: you're accusing me of missing the point after having quoted something I wrote as if it represented by own views, even though I footnoted it and said I was only doing it for the sake of argument. Perhaps, outside this discussion, I am totally getting the point. I can't tell, though, because I read your post twice and I have no idea what you consider the point to be. Best as I can tell you are claiming that data hiding isn't important, but then you go on to imply Python is blemished because it doesn't hide data. It really makes no sense: perhaps you can help us out by giving us an example of something that illustrates what you're saying? (FWIW, my actual view on the matter is I don't give a lick about data hiding or marking internals.) Do yourself a favor and google antoon's previous posts in this group. He sure is a clever guy I wouldn't call him a troll - but a bit trollish.. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting Output
On Jun 2, 2:43 pm, Doug Morse [EMAIL PROTECTED] wrote: On Mon, 2 Jun 2008 12:42:12 -0700 (PDT), Mensanator [EMAIL PROTECTED] wrote: On Jun 2, 3:38 am, Chris [EMAIL PROTECTED] wrote: On Jun 2, 9:34 am, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 ... I have a related question: Does Python have (or can emulate) the formatted output capability found in Perl? For example, all I have to do to get nicely formatted (i.e., aligned) output is provide values for special STDOUT variables (i.e., STDOUT_TOP, STDOUT, STDOUT_BOTTOM, etc.), exemplified by: format STDOUT_TOP = ------ ~ . format STDOUT = @ @ @ $res-{'full_name'}, $res-{'phone_1'}, $res-{'phone_1_type'} @ @ ~ $res-{'address_1a'}, $res-{'address_2a'} @ @ ~ $res-{'address_1b'}, $res-{'address_2b'} @ @ ~ $res-{'address_1c'}, $res-{'address_2c'} @ @ ~ $city_1 $city_2 @ @ ~ $res-{'email_1'}, $res-{'email_2'} ------ ~ . Then, all I have to do is populate my $res object/hash as desired -- in this example simple the results of a SQL query -- and lastly just call the write function: write; and Perl will produce very nicely formatted results. This is useful not only for producing human readable output, but also fixed-column-width data files, etc. I'd love to learn the Pythonistic way of doing the same thing. Thanks! Doug I didn't know you could do that in perl. Too bad I quit using it when I switched to Python. OTOH, I can do similar formatting directly in SQL. In Access, I create a query with this SQL: SELECT Chr(10) Format$(SampleEventCode,!) Format$ (SampleEventDescr,) Chr(10) Format$(EventSortOrder,!) Format$(From: Format$(DateFrom,/MM/ DD),) Chr(10) Format$(SampleEventReportLabel,!) Format$(To: Format$(DateTo,/MM/ DD),) AS Expr1 FROM tblSampleEvent; Then I simply call it from Python. (I could call with the SQL code instead of invoking the Access query, but the OBDC interface can't process this particular SQL statement, tries to put brackets around the format templates, so best render unto Access the things that belong to Access.) # code import dbi import odbc con = odbc.odbc(BPWR) cursor = con.cursor() cursor.execute( SELECT * FROM SQLFormatTest; ) results = cursor.fetchall() for i in results: for j in i: print j # /code which prints 2000Q1 First Quarter of 2000 2000100 From: 2000/01/01 1st-Qtr 2000 To: 2000/03/31 2000Q2Second Quarter of 2000 2000200 From: 2000/04/01 2nd-Qtr 2000 To: 2000/06/30 2000Q3 Third Quarter of 2000 2000300 From: 2000/07/01 3rd-Qtr 2000 To: 2000/09/30 2000Q4Fourth Quarter of 2000 2000400 From: 2000/10/01 4th-Qtr 2000 To: 2000/12/31 2000Q1LF Low Flow First Quarter of 2000 2000105 From: 2000/01/01 1st-Qtr 2000 To: 2000/03/31 2000Q2LOD LOD borings Second Quarter of 2000 2000206 From: 2000/04/01 2nd-Qtr 2000 To: 2000/06/30 . . . -- http://mail.python.org/mailman/listinfo/python-list
Re: Database Query Contains Old Data
On Wed, 21 May 2008 07:23:04 -0700 (PDT), Paul Boddie MySQL appears to use repeatable read by default [1] as its transaction isolation level, whereas PostgreSQL (for example) uses read committed by default [2]. I would guess that if you were using PostgreSQL, this particular problem would not have occurred, but there are other reasons to be aware of the effects of long duration transactions in PostgreSQL, and the practice of periodically performing a rollback would still be worth considering with that database system. If one has transactions open for a long time, or transactions that involve a great deal of data, this will result in poor performance or poor scalability. But one may have such large transactions without being aware of it. Is there any way to make transaction size salient to the developer? Any way to make sure one is committing as early and often as possible? -- -- We have the right to defend ourselves and our property, because of the kind of animals that we are. True law derives from this right, not from the arbitrary power of the omnipotent state. http://www.jim.com/ James A. Donald -- http://mail.python.org/mailman/listinfo/python-list
Importing xlrd
This is probably a simple question to most of you, but here goes. I've downloaded the xlrd (version 0.6.1) module and placed in in the site-packages folder. Now, when I write a script, I type: import sys import xlrd When I run it, there is an import error saying there is no module named xlrd. However when I type sys.path, the site-packages folder is definitely in the path. Do I somehow need to run the xlrd setup.py first before importing? Any help would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Cast list of objects to list of strings
bukzor wrote: I have this function: def write_err(obj): from sys import stderr stderr.write(str(obj)+\n) and I'd like to rewrite it to take a variable number of objects. Something like this: def write_err(*objs): from sys import stderr stderr.write( .join(objs)+\n) but I lose the property that the function works on any object. No you don't. If you were happy with printing the str(...) of a single objects, why not just printout the (concatenation) of the str(...) of each of many objects? stderr.write( .join([str(b) for b in objs])+\n) Gary Herron What's the simplest way to fix this? In essence, I need to cast a list of objects to a list of strings. I'd like to do just str(objs) but that (obviously) doesn't quite do what I need. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Better performance
On Mon, Jun 2, 2008 at 4:42 AM, Franck Y [EMAIL PROTECTED] wrote: Hello Folks, I am facing a problem where i need to parse around 200 files, i have a bit of knowledge in PHP/Perl/Python (the magic P :-P) Trite answer: Use whatever is going to work best in your circumstances. All 3 languages can do what you need. Strong points for the languages as related to your question: PHP: Easy to make web pages. Perl: Lots of libraries, good text processing support Python: Easy to read and maintain You could even use all 3, in their strong areas. eg, PHP for the web page, perl for quick dirty text-processing utils, and Python for the longer, more complicated scripts which you have to maintain. David. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 4:50 pm, Russ P. [EMAIL PROTECTED] wrote: On Jun 2, 6:41 am, Carl Banks [EMAIL PROTECTED] wrote: You are not realizing that only useful(**) thing about data hiding is that some code has access to the data, other code does not. If you hide data equally from everyone it's just a useless spelling change. I think you're missing the point. Well that's nice: you're accusing me of missing the point after having quoted something I wrote as if it represented by own views, even though I footnoted it and said I was only doing it for the sake of argument. Perhaps, outside this discussion, I am totally getting the point. I can't tell, though, because I read your post twice and I have no idea what you consider the point to be. Best as I can tell you are claiming that data hiding isn't important, but then you go on to imply Python is blemished because it doesn't hide data. It really makes no sense: perhaps you can help us out by giving us an example of something that illustrates what you're saying? (FWIW, my actual view on the matter is I don't give a lick about data hiding or marking internals.) Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 3:04 pm, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 4:50 pm, Russ P. [EMAIL PROTECTED] wrote: On Jun 2, 6:41 am, Carl Banks [EMAIL PROTECTED] wrote: You are not realizing that only useful(**) thing about data hiding is that some code has access to the data, other code does not. If you hide data equally from everyone it's just a useless spelling change. I think you're missing the point. Well that's nice: you're accusing me of missing the point after having quoted something I wrote as if it represented by own views, even though I footnoted it and said I was only doing it for the sake of argument. Perhaps, outside this discussion, I am totally getting the point. I can't tell, though, because I read your post twice and I have no idea what you consider the point to be. Best as I can tell you are claiming that data hiding isn't important, but then you go on to imply Python is blemished because it doesn't hide data. It really makes no sense: perhaps you can help us out by giving us an example of something that illustrates what you're saying? I think I made my point abundantly clear. I said that rigorously denied access to encapsulated data is not important, but a clear specification of what is intended for the client and what is intended for internal use *is* important. And an ugly naming convention for variable and function names is not the best way to do it. I suggest you ask yourself why C++, Java, Ada, and probably most other object-oriented languages, have encapsulation or data hiding. And no, I am not claiming that *every* application written in Python needs it, but certainly *some* could benefit from it. (FWIW, my actual view on the matter is I don't give a lick about data hiding or marking internals.) What kind of programming do you do? -- http://mail.python.org/mailman/listinfo/python-list
Re: Merging ordered lists
On Jun 1, 12:34 am, Raymond Hettinger [EMAIL PROTECTED] wrote: I would do it two steps. There's a number of ways to merge depending on whether everything is pulled into memory or not:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/491285http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/305269 After merging, the groupby itertool is good for removing duplicates: result = [k for k, g in groupby(imerge(*sources))] Raymond Thanks for the tip; itertools never ceases to amaze. One issue: groupby doesn't seem to remove all duplicates, just consecutive ones (for lists of strings and integers, at least): [k for k, g in itertools.groupby(list(asdfdfffdf))] ['a', 's', 'd', 'f', 'd', 'f', 'd', 'f'] Another issue: dropping everything into a heap and pulling it back out looks like it loses the original ordering, which isn't necessarily alphabetical, but however the user wants to organize the spreadsheet. That's why I originally avoided using sorted(set(itertools.chain(*sources))). Do you see another way around this? -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: equivalence 0.1
On Mon, Jun 2, 2008 at 6:31 AM, Giuseppe Ottaviano [EMAIL PROTECTED] wrote: On Jun 1, 2008, at 6:16 PM, George Sakkis wrote: Equivalence is a class that can be used to maintain a partition of objects into equivalence sets, making sure that the equivalence properties (reflexivity, symmetry, transitivity) are preserved. Two objects x and y are considered equivalent either implicitly (through a key function) or explicitly by calling merge(x,y). I think this library would be very useful, and I like the interface (in particular the idea of the projecting function), Thanks for trying it out! but I think the algorithm is less than optimal. It can show quadratic behavior in some cases: $ python -m timeit -s 'import equivalence' -s 'eq = equivalence.Equivalence()' 'for i in xrange(1): eq.merge(i, i+1)' 10 loops, best of 3: 57.6 msec per loop $ python -m timeit -s 'import equivalence' -s 'eq = equivalence.Equivalence()' 'for i in xrange(1): eq.merge(i+1, i)' 10 loops, best of 3: 2.59 sec per loop Interesting.. it took me a while to figure out why the second case is so much slower and you're right, it is indeed quadratic. I don't know how likely would such pathological cases be in practice, given that the preferred way to merge a batch of objects is eq.merge(*xrange(10001)), which is more than 3 times faster than the non-pathologic first case (and which I optimized even further to avoid attribute lookups within the loop so it's more like 5 times faster now). Also the batch version in this case remains linear even if you merge backwards, eq.merge(*xrange(1,-1,-1)), or in any order for that matter. Have you considered using the Union-Find algorithm, which would be (almost) linear in all cases? I am familiar with it and I will certainly consider it for the next version; for now I was primarily interested in functionality (API) and correctness. Thanks, George -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 6:39 pm, Russ P. [EMAIL PROTECTED] wrote: On Jun 2, 3:04 pm, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 4:50 pm, Russ P. [EMAIL PROTECTED] wrote: On Jun 2, 6:41 am, Carl Banks [EMAIL PROTECTED] wrote: You are not realizing that only useful(**) thing about data hiding is that some code has access to the data, other code does not. If you hide data equally from everyone it's just a useless spelling change. I think you're missing the point. Well that's nice: you're accusing me of missing the point after having quoted something I wrote as if it represented by own views, even though I footnoted it and said I was only doing it for the sake of argument. Perhaps, outside this discussion, I am totally getting the point. I can't tell, though, because I read your post twice and I have no idea what you consider the point to be. Best as I can tell you are claiming that data hiding isn't important, but then you go on to imply Python is blemished because it doesn't hide data. It really makes no sense: perhaps you can help us out by giving us an example of something that illustrates what you're saying? I think I made my point abundantly clear. I said that rigorously denied access to encapsulated data is not important, but a clear specification of what is intended for the client and what is intended for internal use *is* important. And an ugly naming convention for variable and function names is not the best way to do it. Well let me tell you what's confusing me here: I can't figure out, if this is your standpoint, what issue you could have had with what I said. What specifically did you disagree with? What did I say that was wrong? It seems like we are more in agreement than not. I suggest you ask yourself why C++, Java, Ada, and probably most other object-oriented languages, have encapsulation or data hiding. And no, I am not claiming that *every* application written in Python needs it, but certainly *some* could benefit from it. I know what it's there for, chief. That's exactly what I was saying to Antoon, and you took issue with it and claimed I was missing the point. What gives? Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Cast list of objects to list of strings
On Jun 2, 2:56 pm, jay graves [EMAIL PROTECTED] wrote: On Jun 2, 4:02 pm, Larry Bates [EMAIL PROTECTED] wrote: I think what you want is: def write_err(*args): from sys import stderr stderr.write(\n.join([str(o) for o in args])) Slight nitpick. If you are using version = 2.4 you could use a generator expression instead of a list comprehension to avoid building and throwing away a list. \n.join(str(o) for o in args) http://www.python.org/dev/peps/pep-0289/ Very unlikely to yield a material time difference in this case but cleaner IMO. ... Jay Graves Thanks! That's exactly what I was looking for. -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking each item in m.group()?
[EMAIL PROTECTED] wrote: On Jun 2, 5:06 pm, Peter Otten [EMAIL PROTECTED] wrote: You are taking the wrong approach here. Don't build SQL statements as strings; you are enabling the next SQL injection attack. Pass parameters using the DB API instead. Don't use regular expressions to parse a CSV file. Python's csv module is more likely to deal correctly with the quirks of that standard. I'd like to second both these statements. Regardless of whether these CSV files are from a trusted source or not, it's a virtual truism of programming that eventually, any application will be used in ways it was not intended. Since using a parameterized query is a simple way to avoid a common security hole, even if such a thing could never be exploited by the app in its current configuration, you should do things the Right Way. That way, even if your code is twisted to some other use in the future, it's less likely to cause problems. I don't have a problem with a response saying it's a good idea to use parameterized queries and explaining why, but I have seen way too many responses like this which are basically FUD. I'm not sure what a virtual truism is, but if it is like a truism, it's not true. There are many cases where one can accurately predict that the code will not be used in the future in some different app. I don't know what the OP was doing, but I have done many data conversion jobs where I have done things similar to the OP. The jobs were one-time, move data from system A to system B (with some munging in between) and I could and did predict the conversion code would not get reused. My accuracy rate is 100%. And if you do reuse code where you feed it untrusted input in a security sensitive context, and you don't bother to verify the security of said, code, you already have so many problems, one more probably won't make much difference. To the OP: The advice to use parameterized queries is good but overstated. There are cases when it is quite safe to use non-parameterized statements: * When you control the data going into the query )e.g., you've generated it yourself). * When the data come from trusted sources (including something like sys.input if the only people with access to the program are trusted). * When you can reliably check the data yourself, for example in: sql = SELECT * FROM foo WHERE id=%d % int(some_string) cursor.execute (sql) it doesn't really matter what some_string contains (if you are prepared for a Python exception). But note that checking and escaping strings in more general or complicated cases can be quite tricky.) In most cases a good reason to use a parameterized query is that it is no harder than to not use one, so why not and get the additional safety for free? A parameterized query can often run faster than a non-parameterized one since the database can reuse the cached compiled query. (But sometimes the opposite is true, see below). A parameterized form of the above is: sql = SELECT * FROM foo WHERE id=? % int(some_string) cursor.execute (sql, int(some_string)) so it is just as easy. There are times though when it is slightly harder. If idnums is an arbitrary list of ints: sql = SELECT * FROM foo WHERE id IN(%s) % ','.join(idnums) cursor.execute (sql) Using a parameterized query might look like: sql = SELECT * FROM foo WHERE id IN(%s) % ','.join(['?']*len(idnums)) cursor.execute (sql, idnums) When you have written such code a few times it becomes a natural idiom, but if you only do so occasionally, you are in a hurry, and the conditions above apply, then there is no reason not to go with the first form. And if you you already have a text string of comma-separated digits, the ease of using the direct sql form becomes even greater: sql = SELECT * FROM foo WHERE id IN(%s) % idnums_string cursor.execute (sql) But of course, if idnums_strings came from an untrusted source, then you need to validate it first, e.g.: if idnums_string.strip(0123456789 ,): then raise Error There are also times when using a parameterized query can dramatically (and I mean two or three *orders of magnitude*) slow down your query when using prepared queries. For example: sql = SELECT * FROM foo WHERE txt LIKE %s%% % some_string cursor.execute (sql) can be expected to run quite quickly in most database systems, since the database knows that the searched for text starts with a constant string and can thus use an index. The parameterized form: sql = SELECT * FROM foo WHERE txt LIKE ? cursor.execute (sql, [some_string + %]) will often run very very slowly, because when the query is prepared the database has no idea if the argument will start with a constant string of not, and thus can only assume the worst, and prepare the query so that it doesn't use an index. The bottom line is, as in all things, understanding the issues will lead to much better decisions than blindly following some dumbed down
Re: Code correctness, and testing strategies
In article [EMAIL PROTECTED], David [EMAIL PROTECTED] wrote: Clients generally require *working* software. Unfortunately it is all too easy to ship something broken because then you can claim you completed the coding on time and any slippage gets lost in the next 5 years of maintenance. That's why you have human testing QA. Unit tests can help, but they are a poor substitute. If the customer is happy with the first version, you can improve it, fix bugs, and add more unit tests later. The most important aspect of usnit testing is actually that it makes the code testable. This may sound lik an oxymoron but it is actually a really important property. Testable code has to have a level of modularity as well as simplicity and clarity in its interfaces that you will not achieve in code that lacks automated unit tests. You can easily convince yourself that this is true by adding complete coverage unit tests to some code after you have written it. It's tough work and more often than not, you need to refactor the code to make it testable. Another aspect that you are raising is the use of human testing and QA. I agree that these are important, but every bug they discover is a failure of the developers and their tests. Our testers can sail through a testbed in 30 minutes if there are no bugs. Every single bug adds 30-60 minutes of testers time in order to pinpoint the bug and supply the developers with enough information to locate and fix it. Add some 10 minutes to testing time on the next testbed to verify that the bug is actually fixed. In my end of the world, tester time is not cheaper than developer time. It is also a scarcer resource than developer time. The roundtrips between developers and testers also add to real required to develop the product. There will always be delays introduced by having to produce the tester with a testable system, wait for the tests to happen, have the report written up and allocated to a developer etc. With TDD most of these bugs will be caught in a no-delay loop at the developers desk. This last fact has another important property that is easily overlooked. Once the off-by-one errors and other trivial bugs don't clutter the minds of the testers they will start thinking more clearly about the application and where the really nasty bugs are found - the ones where your thinking went wrong but your implementation is correct. if the testers are busy tracking trivial bugs, your customers will find the nasty one. If you are lucky, they will tell you. Jacob Hallén -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking each item in m.group()?
On Mon, 2 Jun 2008 12:06:21 -0700 (PDT), Matimus [EMAIL PROTECTED] wrote: Here is a brief example. Note that this code is very insecure and susceptible to a SQL injection attack. Hopefully these csv files are from a trusted source. Yes they are, and this script will only run on my PC, so it doesn't need to be more secure than this. sql = INSERT INTO mytable (col1, col2) VALUES ('%s','%s')%tuple( (c, NULL)[c == ''] for c in m.groups() ) I don't understand this syntax :-/ Also, check out the csv module for parsing your csv file. Will do. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 4:17 pm, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 6:39 pm, Russ P. [EMAIL PROTECTED] wrote: On Jun 2, 3:04 pm, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 4:50 pm, Russ P. [EMAIL PROTECTED] wrote: On Jun 2, 6:41 am, Carl Banks [EMAIL PROTECTED] wrote: You are not realizing that only useful(**) thing about data hiding is that some code has access to the data, other code does not. If you hide data equally from everyone it's just a useless spelling change. I think you're missing the point. Well that's nice: you're accusing me of missing the point after having quoted something I wrote as if it represented by own views, even though I footnoted it and said I was only doing it for the sake of argument. Perhaps, outside this discussion, I am totally getting the point. I can't tell, though, because I read your post twice and I have no idea what you consider the point to be. Best as I can tell you are claiming that data hiding isn't important, but then you go on to imply Python is blemished because it doesn't hide data. It really makes no sense: perhaps you can help us out by giving us an example of something that illustrates what you're saying? I think I made my point abundantly clear. I said that rigorously denied access to encapsulated data is not important, but a clear specification of what is intended for the client and what is intended for internal use *is* important. And an ugly naming convention for variable and function names is not the best way to do it. Well let me tell you what's confusing me here: I can't figure out, if this is your standpoint, what issue you could have had with what I said. What specifically did you disagree with? What did I say that was wrong? It seems like we are more in agreement than not. I suggest you ask yourself why C++, Java, Ada, and probably most other object-oriented languages, have encapsulation or data hiding. And no, I am not claiming that *every* application written in Python needs it, but certainly *some* could benefit from it. I know what it's there for, chief. That's exactly what I was saying to Antoon, and you took issue with it and claimed I was missing the point. What gives? Carl Banks I thought you were saying that encapsulation or so-called data hiding is worthless. If I misunderstood you, then I apologize. I don't have time to go back and sort it all out. Here's what I think Python should have. I think it should have a keyword, something like priv, to identify data or functions as private. As I said earlier, private for class data or functions (methods) could be implemented like protected in C++. That means that derived classes would have access to it, but clients of the class would not. If the client really needs or wants access, he could be given a sort of back door access similar to the current Python rule regarding double leading underscores. Thus, the client would have access, but he would know very well that he is using something that the original designer did not intend for him to use. It's just a suggestion. I'm not a language expert, and I realize that I could be missing something important. I also realize, by the way, that Python allows a client of a class to define a new class member from completely outside the class definition. Obviously, that cannot be declared private. But if the same identifier is already declared private within the class, than the new definition should not be allowed (because it would defeat the whole idea of private class members). -- http://mail.python.org/mailman/listinfo/python-list
Ideas for master's thesis
Hello there, I am student of CS at the University of Warsaw, currently 4th year. I am attending Object Oriented Programming seminar and it is about time, I started looking for an idea of my master's degree project. As I like Python very much, I would like to do something with this language, yet I don't know if there are any needs/science fields, that could be used as a basis for a thesis. Therefore I would like to ask, if there is any way to access any person involved in development, who would help me find some field that need to be developed/researched (rather the first one) and would be close enough to the OOP, so I could present it to a mentor (he is cool with doing something for OSS community). -- Filip Gruszczyński -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
Russ P. [EMAIL PROTECTED] writes: I also realize, by the way, that Python allows a client of a class to define a new class member from completely outside the class definition. Obviously, that cannot be declared private. This is bogus about 95% of the time though. For the cases where it is really desired, I think it's best to require the target class to be enable it specifically somehow, maybe by inheriting from a special superclass. That could let the compiler statically resolve member lookups the rest of the time. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 7:41 pm, Russ P. [EMAIL PROTECTED] wrote: I thought you were saying that encapsulation or so-called data hiding is worthless. If I misunderstood you, then I apologize. I don't have time to go back and sort it all out. No, not at all. I was suggesting that Antoon's example of data hiding wasn't useful because it didn't really hide data: it was merely a spelling change. (I'm not, mind you, saying that it's ordinarily a good trade-off to encapsulate data, but I do get the point of it.) Here's what I think Python should have. I think it should have a keyword, something like priv, to identify data or functions as private. As I said earlier, private for class data or functions (methods) could be implemented like protected in C++. That means that derived classes would have access to it, but clients of the class would not. If the client really needs or wants access, he could be given a sort of back door access similar to the current Python rule regarding double leading underscores. Thus, the client would have access, but he would know very well that he is using something that the original designer did not intend for him to use. Reasonable enough. I've always thought C++ should have a private_cast. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing xlrd
Chanman wrote: This is probably a simple question to most of you, but here goes. I've downloaded the xlrd (version 0.6.1) module and placed in in the site-packages folder. Now, when I write a script, I type: import sys import xlrd When I run it, there is an import error saying there is no module named xlrd. However when I type sys.path, the site-packages folder is definitely in the path. Do I somehow need to run the xlrd setup.py first before importing? Any help would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list On what system? Windows or Unix? Which download? The exe or the zip? I'm guessing you downloaded the zip file on a Unix or Linux system. If so, you should not copy the files into the site-package directory yourself. You are supposed to use the included setup.py file like this: unzip into some local directory cd into that directory python setup.py build python setup.py install Then your import should work fine. Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 2, 10:14 am, Antoon Pardon [EMAIL PROTECTED] wrote: On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 9:07 am, Antoon Pardon [EMAIL PROTECTED] wrote: On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 6:40 am, Antoon Pardon [EMAIL PROTECTED] wrote: On 2008-06-02, Carl Banks [EMAIL PROTECTED] wrote: On Jun 2, 5:38 am, Antoon Pardon [EMAIL PROTECTED] wrote: If you really need it, you can do data hiding in python. It just requires a bit more work. - Hide.py - class Rec(object): def __init__(__, **kwargs): for key,value in kwargs.items(): setattr(__, key, value) def __getitem__(self, key): return getattr(self, key) def __setitem__ (self, key, val): setattr(self, key, val) class Foo(object): def __init__(self): hidden = Rec(x=0, y=0) def SetX(val): hidden.x = val def SetY(val): hidden.y = val def GetX(): return hidden.x def GetY(): return hidden.y self.SetX = SetX self.SetY = SetY self.GetX = GetX self.GetY = GetY In other words, it's a useless no-op. In fact, I'd say this is even worse than useless. Creating accessor functions is a sort of blessing for external use. Knowing that there are accessor functions is likely to cause a user to show even less restraint. I think you completed missed the point. I'm not sure I missed the point so much as I failed to read your mind. Fine with me, it is just the other side of the coin. This is just a proof of concept thing. In a real example there would of course no Set en Get methods but just methods that in the course of their execution would access or update the hidden attributes Fair enough, but I don't see anything in your example that suggests a way to discriminate between access from within the class and access from outside the class, which is the crucial aspect of data hiding. The fact is that hidden and its attributes are not accessible from outside the instance. They are only accessible to the local functions of __init__. By binding those local functions as atributes to the instance, hidden can be modified by what for all practical purposes looks like a method call, but really is a closure call. You haven't hidden the data at all, all you've done is to change the means of accessing it. What difference does it make whether I write foo.getX() or foo.x? Everyone in the world still has full access to the data. Can't you look beyond the specific example? The GetX is just an example. Any local function of __init__ has access to hidden and its attributes and could manipulate them, even if the class wouldn't define getters and setters. Ok. You could have made the proof-of-concept-ness of your example more clear by not, you know, binding those local functions to globally accessable names, thus thwarting your own concept. But still, we all knew functions could do that. How could you make data private to a group of functions (like, say, methods of a class, or all functions in a module). It seems you'd have to define all functions in the the same function scope, instead of where they'd more naturally be defined (in the class or module scope). Unwieldy to say the least, but doable, I suppose. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list