Re: 'if name is not None:' v. 'if name:'
Victor Noagbodji wrote: > Why do people use if name is not None: instead of simply > writing if not name? To differentiate from the case where name == '', or some other non-None false value. So the question is, do you want to test for identity with None, or for truth in general? Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: while var, but var ==16 != true
Tim Roberts wrote: > Everything has a boolean value in > Python. 0, None, False, '' (empty string), [] (empty list), () (empty > tuple), and {} (empty dictionary) all have a False value. Everything else > has a True value. Empty set objects also evaluate as false in a boolean context. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: read file into list of lists
Laurent Rahuel wrote that antar2 wrote: >> following text for example should be considered as a list of lists (3 >> columns and 3 rows), so that when I make the print statement list[0] >> [0], that the word pear appears >> >> >> pear noun singular >> books nouns plural >> table noun singular File objects are themselves iterable, returning one line per iteration. So a simple approach is: >>> table = [line.split() for line in open('sentences.txt')] >>> table[0][0] 'pear' Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQLdb will only import for root
Diez B. Roggisch wrote: >> Is it possible the module was installed with priviledges set too >> strict? Perhaps the interpreter cannot see the module when it is run >> from a normal user account. > > Possible - certainly. Yet unrealistic, because usually root access is > required to system-wide install a package - thus the normal install > processes ensure proper rights. According to the OP, it is root that *does* have access to MySQLdb and a normal user that does not. It is a very realistic possibility to install something as root to which normal users do not have access. Installation scripts that assume a particular umask during install, for example, are especial culprits. I have noticed this problem myself with python modules that include compiled C extensions, as MySQLdb does. However, in the case where the extension module itself has incorrect permissions, the error would point to a missing _mysql, rather than a missing MySQLdb. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: python scalability
Tim Mitchell wrote: > One of my project managers questions is: "Are we the only company in the > world with this kind and size of project?" I can't provide a bigger success story personally (my largest project is currently about 15k lines of code, eminently manageable by one person.) But Google comes to mind. Just a feeling I get from quotes like: "Python is big at Google ... being used for everything from build tools to managing ads." -- Guido Van Rossum http://www.artima.com/weblogs/viewpost.jsp?thread=143947 "When programming in python, one should design for scalability. If you design properly, the you should be able to just throw more machines at the problem and your app should scale fine." -- Greg Stein's PyCON keynote, paraphrased by Matt Harrison http://panela.blog-city.com/python_at_google_greg_stein__sdforum.htm Maybe that's not exactly the sort of scalability you're referring to, but certainly Goolge is colossal in its deployment. I have a hard time imagining that scalability based on "N lines of code" is going to be any better in any other language. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: automatically import modules upon interpreter invocation
Daniel Fetchinson wrote: > Is there a way of making python execute the above whenever it starts > up so that I don't have to type it all the time? Create a script containing these statements, and specify its location with the PYTHONSTARTUP environment variable. Your script will run whenever python starts. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Using Python to run SSH commands on a remote server
John Salerno wrote: > I guess a blanket process might be a tad risky, but don't you want all CGI > files to be executable by all? Typically, I prefer CGI scripts to be executable only the owner. If the web server runs those scripts as a different user, then that user must also be permitted to execute the scripts of course. Also note that "all .py files on my web server" is not necessarily restricted to CGI scripts -- and therein lies the real gist of my cautionary note. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Using Python to run SSH commands on a remote server
John Salerno wrote: > Generally speaking, what tools would I use to do this? Is there a built-in > module for it? I've had a very nice experience using the 3rd-party package "paramiko" for ssh communication. There's nothing in the standard library that I know of. > I looked at the telnetlib module, but the documentation > wasn't really complete enough for me to get a good idea of it. Is Telnet > and SSH even the same thing? Telnet is not the same protocol. SSH is an encrypted transport, telnet is not. > Basically, I want to write a script that will automate the process of > making all .py files on my web server executable (chmod 755, or something > similar). Be careful, this procedure sounds potential risky, security-wise ;-) Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Who is using python-ldap with Python 1.5.x and 2.0-2.2?
Michael Ströder wrote: > Please tell me > which Python version you're using We do have one venerable machine here at work using python2.2 with python-ldap2.0.0pre04. As you can see, we haven't bothered to update either in quite a while ;-) > and why it'd be important for you to > have python-ldap updates still supporting it. For us, it is not important. Should we want or need to update python-ldap on that one machine, we'll update python along with it (and vice versa.) Thank you, Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Ternary operator alternative in Ptyhon
jeremie fouche wrote: > You can also use : > self.SomeField = params.has_key("mykey") and params["mykey"] or None Have caution with this solution: it may not provide the desired result in the case where params["mykey"] is a false value, such as 0, or [] Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: php vs python
notbob wrote: > I > persevere because it's more fun/challenging than video games This is the crux of the matter from where I'm sitting. If the purpose of learning a programming language is fun, then the primary relevant question is: Is it more fun to code in Python or PHP? The answer is a no-brainer for me. It seems to me that Python is designed from the ground up with my enjoyment in mind. Your Fun May Vary :-) Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Are rank noobs tolerated, here?
notbob wrote: > Do python scripts require the: > > #!/usr/bin/env python An appropriate shebang is required if you intend to use the module itself as a script, from the command line, like: $ ./my_module.py argument argument ... It is not required merely to import the module into a python program or interpreter. Even without the shebang, the module can be run indirectly with: $ python ./my_module.py argument argument ... or, if the module is somewhere on your sys.path: $ python -m my_module argument argument ... Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Are rank noobs tolerated, here?
notbob wrote: > I'm running > vers 2.5.1 on slackware 12. Nice to see another Slackware user around here! > "Here is an example of a user-defined function that has a parameter: > > > def print_twice(bruce): > print bruce, bruce > is this just an example of how the def should be written and it doesn't > really do anthing... yet? That's correct. A function doesn't generally *do* anything until it is called. Here, it is only defined. The only thing this function does when called is to print the value of bruce twice. > I define myfirstfunction in the pyth > editor and give the command print myfirstfuntion and I get back this: > Functions are objects too, and this is a printed representation of that function object. It still hasn't been "called" at this point. > when I add the ._doc_, it get this: > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'function' object has no attribute '_doc_' > so, there is a no go there, too. The functions docstring is stored in the attribute named "__doc__". Like other python "special" attributes, it starts and ends with TWO underscores. > ok, I try and follow the above, but where is he getting the script? So, I > make a script called chap03.py and put it in ~/pyth/chap03/. You are correct to create this script yourself from scratch. > I then try an do the import thing above, but the pyth ed accepts none of > it. not from: > from ~/pyth/chap03/ import * > from chap03 import *#taken from ~/pyth/ > nor from any other configuration I can imagine, so that "from" command > makes no sense. Modules are imported from your PYTHONPATH, which is a search path similar to bash's PATH: It is a list of directories that are searched for *module* names when you attempt an import. You can examine your python path by doing: >>> import sys >>> sys.path Your module files, i.e., chap03.py, should be in one of the directories on that path. "from" modifies a *module* name, not a path component. So "from module import one_function, another_function". This allows you to use one_function without referencing it through the imported module name. In other words, these two are equivalent: >>> import chap03 >>> chap03.print_twice() and: >>> from chap03 import print_twice >>> print_twice() In the above examples, "chap03" is the *module* file, chap03.py. Hope that helps clarifies things a bit, Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Recurring patterns: Am I missing it, or can we get these added to the language?
Erich wrote: > def iterable(item, count_str=False): > if not count_str and isinstance(item, str): > return False > try: > iter(item) > except: > return False > return True Beware the "except" clause here, as it catches *all* errors. Thus, if you happen to have an unfortunate typo: try: iter(iten) except: return False return True then your code will happily return False for everything. This code should catch TypeErrors only for better results: try: iter(item) except TypeError: return False return True Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Recurring patterns: Am I missing it, or can we get these added to the language?
Tim Chase wrote: >def nsplit(s, delim=None, maxsplit=None): > if maxsplit: >results = s.split(delim, maxsplit) >result_len = len(results) >if result_len < maxsplit: > results.extend([''] * (maxsplit - result_len) >return results > else: >return s.split(delim) I'll add a couple more suggestions: 1. Delay the test for maxsplit, as str.split() does the right thing if maxsplit is None. 2. Use a generator to pad the list, to avoid interim list creation. This works fine, because list.extend() accepts any iterable. This also shortens the code a bit, because xrange() does the right thing in this case with negative numbers. For example: def nsplit(s, delim=None, maxsplit=None): results = s.split(delim, maxsplit) if maxsplit is not None: results.extend('' for i in xrange(maxsplit - len(results))) return results Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding classes to modules at runtime from outside that module
[EMAIL PROTECTED] wrote: > In Python, is it possible to add classes to a module at run-time? > > Say I have a module foo and a module bar. Foo has class A and B, and > bar has class C. I want to add class C to foo so I can access it as > foo.C, but i want to do it without modifying foo's source. > > Is this at all possible? Yes, possible and easy: # bar.py import foo class C(object): ... foo.C = C Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble with list comprehension
Shane Lillie wrote: > goats = [ x for x in range(2) if doors[x] == 'G' ] > > but for some reason the list comprehension is not always returning a > list with 2 elements in it (sometimes it will be just 1 element). The problem here is with your usage of the range() function. You provide an endpoint of 2, but that endpoint is not included in the range. Thus, you are only checking the indexes 0 and 1. You'll get two results when those two indexes are 'G', and one otherwise. You want range(3). By the way, the enumerate() function is good for this task, as it does not require you to know the length of your list in advance: goats = [index for index, item in enumerate(doors)] -- Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Coping with cyclic imports
Torsten Bronger wrote: > I know that cyclic imports work in Python under certain > circumstances. Can anyone refer me to a page which explains when > this works? I don't know of a specific URL offhand. Cyclic imports are not a problem by themselves, but cyclic definitions are. Thus: # a.py import b x = 1 # b.py import a x = 2 works fine, but: # a.py import b x = 1 # b.py import a x = a.x + 1 # circular definition does not. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: list.sort(): heaviest item?
Steven Clark wrote: > If I have a list of items of mixed type, can I put something into it > such that after a list.sort(), is guaranteed to be at the end of the > list? > It looks like "None" always ends up at the start ("lightest"), but I > want the opposite ("heaviest"). I don't know of an object in the standard library that is guaranteed to be "heaviest", but it is trivial to create one: >>> class Heaviest(object): ... def __cmp__(self, other): ... return 1 ... >>> Heaviest = Heaviest() >>> L = [tuple(), list(), dict(), Heaviest, str(), int(), None] >>> L.sort() >>> L [None, 0, {}, [], '', (), <__main__.Heaviest object at 0xb7c04a8c>] Ordering with respect to another object that defines a similar __cmp__ function will still be arbitrary, but maybe you don't have to deal with any such objects ;-) Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Tips for load balancing multiple Python apps on dual/quad core processors?
Malcolm Greene wrote: > I'm looking for tips on how to load balance running multiple Python > applications in multi-CPU environments. My understanding is that Python > applications and their threads are limited to a specific CPU. > > Background: I have a Python utility that processes email messages. I > suspect there's a lot of idle time while this utility waits on a remote > email server. While it's true that python's threads are limited to using a single CPU at a time, it sounds like your bottleneck is network IO, not CPU power. I imagine that your one CPU is doing a lot of idling, thumb-twiddling, etc., while waiting for the remote email server to respond. In such a scenario, python's threads will work fine to speed your application. On the other hand, adding CPUs to an IO-bound application isn't likely to speed it up at all. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: reading dictionary's (key,value) from file
[EMAIL PROTECTED] wrote: > so this is what my option files look like: > > 1opt.txt > { '-cc': '12', > '-I': r'/my/path/work/'} You can turn these strings read from text files into actual dictionaries using eval: >>> d = eval("{ '-cc': '12', '-I': r'/my/path/work/'}") >>> d {'-I': '/my/path/work/', '-cc': '12'} >>> type(d) Note that eval will happily execute all sorts of arbitrary code, so this is not a good solution if you don't fully trust your option file creators. It's also a bit clunky compared to simply importing. Since you already have dictionary-literal syntax in your text file, why not add a left-hand-operator and make it a module instead? For example: # opt1.py d = { '-cc': '12', '-I': r'/my/path/work/'} # main.py from opt1 import d -- Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Python2.5 and MySQLdb
writeson wrote: > I'm running a CentOS 4 server and have installed Python2.5 on there > (it's our development machine) in preparation of moving to Python2.5 > everywhere. All looks good with our code and 2.5, except where it > comes to MySQLdb, I can't get that to install on the machine. It > generates a huge lists of errors and warnings from gcc when I run the > python2.5 setup.py build script that comes with the tar file. Anyone > have any suggestions? MySQLdb compiles fine for me with python2.5 on CentOS-4. I suggest that you examine the end of that long of warnings and errors to determine why it won't compile on your machine. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Signal problem
Fabio Durieux Lopes wrote: > def signalHandler(signum, frame): > terminate = True This creates a new variable named "terminate" inside the signalHandler function's local scope. To adjust the value of a module-level global from inside a function, use the "global" keyword: def signalHandler(signum, frame): global terminate terminate = True Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Is subprocess.Popen completely broken?
Skip Montanaro wrote: > I am trying to replace os.system calls with subprocess.Popen. This simple > example fails miserably: > proc = subprocess.Popen ("ls /tmp") Popen expects a list of program arguments. When passed a single string instead of a list, as in your example, it assumes that the string is the command, and looks for an executable named "ls /tmp", which of course does not exist. Split your command into a list of separate parameters, with the executable as the first parameter, and it will work: >>> subprocess.Popen(['ls', '/tmp']) >>> (ls output here) Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: subprocess.popen function with quotes
skunkwerk wrote: > p = subprocess.Popen(['rename','-vn','s/(.*)\.htm$/ > model.html/','*.htm'],stdout=subprocess.PIPE,stderr=subprocess.PIPE) > print p.communicate()[0] > > i change to print p.communicate()[1] in case the output is blank the > first time > > this is the output: > *.htm renamed as model.html Without shell=True, your glob characters will not be expanded. Hence, the command looks for a file actually named "*.htm" > when I add shell=True to the subprocess command, I get the following > output: > Usage: rename [-v] [-n] [-f] perlexpr [filenames] Here the use of the shell may be confounding the arguments passed. Your command will probably work better if you avoid using shell=True. However, you will need to perform your own globbing: # Untested (no perl-rename here): command = ['rename','-vn', 's/(.*)\.htm$/model.html/'] files = glob.glob('*.htm') command.extend(files) p = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: New to group
pythonnubie wrote: > The exeption > is " no such attribute " in module random Is your own source file named "random.py" by chance? If so, rename it, and be sure to remove the leftover random.pyc file as well. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Import a file to namespace
Pedro Machado Santa wrote: > import testpackage > > class testClass(): > #... > > testpackage.testClass = testClass This method should work fine. Modules are effectively singletons, so running this code one time anywhere in your application will cause the changes to appear in all references to the original module. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Change user on UNIX
Giampaolo Rodola' wrote: > I mainly need to temporarily impersonate another user to > execute a command and then come back to the original user. If the script is run as root, you can freely impersonate other users with the os.seteuid() and os.setegid() methods. If the script is not run as root (either directly or through sudo, as suggested by other posters), then perhaps it should be. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Decode email subjects into unicode
Laszlo Nagy wrote: > I know that "=?UTF-8?B" means UTF-8 + base64 encoding, but I wonder if > there is a standard method in the "email" package to decode these > subjects? The standard library function email.Header.decode_header will parse these headers into an encoded bytestring paired with the appropriate encoding specification, if any. For example: >>> raw_headers = [ ... '=?koi8-r?B?4tnT1NLP19nQz8zOyc3PIMkgzcHMz9rB1NLB1M7P?=', ... '[Fwd: re:Flags Of The World, Us States, And Military]', ... '=?ISO-8859-2?Q?=E9rdekes?=', ... '=?UTF-8?B?aGliw6Fr?=', ... ] >>> from email.Header import decode_header >>> for raw_header in raw_headers: ... for header, encoding in decode_header(raw_header): ... if encoding is None: ... print header.decode() ... else: ... print header.decode(encoding) ... Быстровыполнимо и малозатратно [Fwd: re:Flags Of The World, Us States, And Military] érdekes hibák Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Intelligent Date & Time parsing
[EMAIL PROTECTED] wrote: > I need > something to parse user input for a django app, and it's awesome to be > able to write "last monday", "a year ago", or "10pm tuesday" like > PHP's strtotime. Django comes with some pretty handy filters for doing this sort of formatting. Check out the "date", "now", "timesince" and "timeuntil" filters here: http://www.djangoproject.com/documentation/templates/#built-in-filter-reference Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: for-else
Carl Banks wrote: > there is a > rationale behind the name "else". If you consider a for loop to be a > rolled-up if...elif...else statement This is an interesting angle. I've always considered "for/else" to be unintuitive, not because of "else", but because of the coupling with "for". Instead, I think of this as a "break/else" statement, and everything gels for me. The same applies to my comprehension of "while/else". I wonder how this came to be called "for/else" or "for-else". I haven't spotted that expression in the python docs yet. With whatever name, I find the construct quite elegant and regularly useful. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd behaviour with list comprehension
Ken Pu wrote: > So the list comprehension actually creates a variable x which is > somewhat unexpected. > Is there a way for me keep the iterating variable in list > comprehension local to the list comprehension? Not with a list comprehension, but generator expressions do not leak their iterating variable: >>> list(x for x in range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> x Traceback (most recent call last): File "", line 1, in NameError: name 'x' is not defined Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: string split without consumption
robert wrote: > thanks. Yet this does not work "naturally" consistent in my line > processing algorithm - the further buffering. Compare e.g. > ss.split('\n') .. > > >>> 'owi\nweoifj\nfheu\n'.split('\n') > ['owi', 'weoifj', 'fheu', ''] > >>> 'owi\nweoifj\nfheu\nxx'.split('\n') > ['owi', 'weoifj', 'fheu', 'xx'] Maybe this works for you? >>> re.split(r'(\n)', ss) ['owi', '\n', 'weoifj', '\n', 'fheu', '\n', ''] Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Cannot catch _mysql_exceptions.OperationalError
Bob wrote: > Here's the code that did not work: > > import _mysql_exceptions > from _mysql_exceptions import OperationalError > > try: > database_code() > except (_mysql_exceptions.OperationalError, OperationalError), e: > error_handling() Both of the above forms work fine here, as does using MySQLdb.OperationalError: >>> import MySQLdb >>> try: ... db = MySQLdb.connect(user='jeffrey', host='localhost') ... except MySQLdb.OperationalError: ... print 'caught' ... caught >>> import _mysql_exceptions >>> try: ... db = MySQLdb.connect(user='jeffrey', host='localhost') ... except _mysql_exceptions.OperationalError: ... print 'caught' ... caught >>> from _mysql_exceptions import OperationalError >>> try: ... db = MySQLdb.connect(user='jeffrey', host='localhost') ... except OperationalError: ... print 'caught' ... caught >>> MySQLdb.OperationalError is _mysql_exceptions.OperationalError True >>> MySQLdb.__version__ '1.2.2' Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Running unmodified CGI scripts persistently under mod_wsgi.
Jeffrey Froman wrote: > While recently > considering whether to re-write a standalone mod_python application as CGI > or WSGI, I was scared off by this paragraph from PEP333: As a followup, I did go ahead and convert my CGI handler to WSGI, and doing so was not difficult at all. The steps were basically: 1. accept environ and start_response parameters in the handler's __init__ 2. return output from __iter__ instead of writing it to stdout. 3. pass environ and wsgi.input to the cgi.FieldStorage() parser. I'd still be interested in a mod_wsgi wrapper for 3rd-party CGI scripts. Thank you, Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Running unmodified CGI scripts persistently under mod_wsgi.
Graham Dumpleton wrote: > The other question is whether there is even a demand for this. Do > people want to be able to take unmodified Python CGI scripts and try > to run them persistently in this way, or would they be better off > converting them to proper WSGI applications. I would personally be interested in such an adapter. While recently considering whether to re-write a standalone mod_python application as CGI or WSGI, I was scared off by this paragraph from PEP333: - Note: although we refer to it as an "application" object, this should not be construed to mean that application developers will use WSGI as a web programming API! It is assumed that application developers will continue to use existing, high-level framework services to develop their applications. WSGI is a tool for framework and server developers, and is not intended to directly support application developers. - Do you have any thoughts about this warning? I'd much rather have the performance of mod_wsgi for my application, so if you can provide any mollifying views about WSGI's suitability for standalone applications my first choice would be to use WSGI directly. Regardless of its appropriateness for new applications, a CGI/WSGI adapter would still be useful for third-party CGI scripts that I have no interest in rewriting myself, as well as for the interim period during which my own CGI applications are being rewritten. It would be advantageous for the hosting company I work for, for example, to be able to boast that "your python CGI scripts run faster on our mod_wsgi-enabled servers." Thank you, Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Populating a dictionary, fast [SOLVED SOLVED]
Steven D'Aprano wrote: > Can you try it running in 64-bit mode? Here are my results using the following test.py: $ cat test.py #!/usr/bin/python import time print "Starting: %s" % time.ctime() v = {} for line in open('keys.txt'): v[long(line.strip())] = True print "Finished: %s" % time.ctime() 32-bit architecture: - [machine1]$ python2.3 test.py Starting: Fri Nov 16 11:51:22 2007 Finished: Fri Nov 16 11:52:39 2007 [machine2]$ python2.5 test.py Starting: Fri Nov 16 11:57:57 2007 Finished: Fri Nov 16 11:58:39 2007 64-bit architecture (64-bit mode): - [machine3]$ python2.3 test.py Starting: Fri Nov 16 11:51:44 2007 Finished: Fri Nov 16 12:31:54 2007 [machine3]$ python2.5 test.py Starting: Fri Nov 16 11:50:03 2007 Finished: Fri Nov 16 11:50:31 2007 Jeffrey Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Populating a dictionary, fast
Hrvoje Niksic wrote: > Note that both machines are x86_64. Please try your test on a 32-bit > machine and report the results. I suspect performance won't degrade > there. This theory seems to be supported by my findings. Running the test on a 32-bit machine took 45 seconds, while the same test on a 64-bit machine is taking ... 30 minutes SO FAR (I'm getting impatient ;-) Both machines are running Python2.3.4 under CentOS-4, and both have 1G of RAM. The 64-bit machine has 2 processors weighing in at 2792.097 Mhz each, while the 32-bit machine has only 1 1194.857 Mhz processor. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: unit testing
Chris Mellon wrote: > Doctest is commonly given as the alternative to people who feel this > way. Personally, I find that anything worth testing is worth having a > test directory and independent unit tests for. I like keeping my tests separate as well, and doctest does allow this, using doctest.testfile(). That is, despite the name, doctests do not necessarily need to appear in docstrings :-) Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: How to find script's directory
Papalagi Pakeha wrote: > I guess I could do it with a little help of os.path.realpath() for all > those cases where absolute or relative path was used. But how should I > approach it when it's invoked as plain 'blah.py' because its directory > name is in $PATH? Use the value of __file__ rather than sys.argv[0] -- it contains the directory information. You may still need to normalize the value using various os.path functions. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Why PHP is so much more popular for web-development
walterbyrd wrote: > The point is: PHP framework makers are very considerate of the > realities of shared hosting. I think the opposite is true: PHP applications typically ignore the realities of shared hosting in order to be considerate to non-developers (that is to say, "users"). In particular, security in a shared hosting environment is almost always discarded. Consider a PHP-based CMS that allows users to upload files. Because the application runs as the webserver user, uploaded files, and the directory where they reside, must be accessible and writable by that user. It is the same user that any other hosting customer on that machine has access to. Thus, any user on the shared host could write a quick CGI script that accesses, adds, removes, or defaces your uploaded content. While it is true that PHP can be deployed through fastcgi/suexec to avoid this problem, doing so is just as complicated as deploying python applications through fastcgi. Deploying python applications through mod_python suffers the same drawbacks on a shared host that using mod_php does, but it is quite simple to set up: 3 lines or so in an .htaccess file is all that is required to get mod_python configured. Surely PHP developers need to write their own .htaccess files as well in most cases? On a related note, most PHP hosting providers offer only PHP4. While this is again fine for the typical PHP *user*, I would hope that PHP *developers* are looking primarily (if not exclusively) for PHP5 at this point. PHP5 providers are much rarer. All that said, I am sympathetic to your concerns, and am currently working hard to set up a commercial shared hosting environment that offers turn-key Django and Plone provisioning. Certainly, the automated, generic set-up won't suit everyone's fine-tuned requirements, but I agree that more hosts should think about providing a working default configuration for python frameworks. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Python version changes, sys.executable does not
Jim Langston wrote: > I think it's because your python directory is in the path before your > python2.5 directory. Thanks for the tip. In fact, /usr/local/bin/python (2.5) is on my PATH before /usr/bin/python (2.3). I did find the problem however -- it turns out that caching the executable path is a feature of the bash shell, possibly a buggy one. After installing the new executable in /usr/local/bin, bash claimed to be running that executable, but was actually invoking the cached "python" at /usr/bin/python. What sorted out the confusion for me was when someone demonstrated to me how sys.executable could be fooled: $ exec -a /usr/bin/foobar python Python 2.5.1 (r251:54863, May 4 2007, 16:52:23) [GCC 4.1.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.executable '/usr/bin/foobar' To remove the cached version, I ran: $ hash -d python After which, running "python" invoked a properly named /usr/local/bin/python as expected. Thanks, Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Python version changes, sys.executable does not
Hello All, I have two python versions installed, one in /usr/bin, and one in /usr/local/bin. However, when invoking python without a full path, I get the wrong executable with the right sys.executable string! [EMAIL PROTECTED] ~]# ls -l /usr/local/bin/python* -rwxr-xr-x 2 root root 3783810 Jul 19 09:15 /usr/local/bin/python -rwxr-xr-x 2 root root 3783810 Jul 19 09:15 /usr/local/bin/python2.5 -rwxr-xr-x 1 root root1281 Jul 19 09:16 /usr/local/bin/python2.5-config lrwxrwxrwx 1 root root 16 Jul 19 09:16 /usr/local/bin/python-config -> python2.5-config [EMAIL PROTECTED] ~]# ls -l /usr/bin/python* -rwxr-xr-x 2 root root 5396 May 2 16:28 /usr/bin/python lrwxrwxrwx 1 root root6 Jul 18 12:20 /usr/bin/python2 -> python -rwxr-xr-x 2 root root 5396 May 2 16:28 /usr/bin/python2.3 [EMAIL PROTECTED] ~]# which python /usr/local/bin/python [EMAIL PROTECTED] ~]# /usr/local/bin/python -c "import sys; print sys.executable; print sys.version; set()" /usr/local/bin/python 2.5 (r25:51908, Jul 19 2007, 09:13:48) [GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] [EMAIL PROTECTED] ~]# env python -c "import sys; print sys.executable; print sys.version; set()" /usr/local/bin/python 2.5 (r25:51908, Jul 19 2007, 09:13:48) [GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] [EMAIL PROTECTED] ~]# python -c "import sys; print sys.executable; print sys.version; set()" /usr/local/bin/python 2.3.4 (#1, May 2 2007, 19:26:00) [GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] Traceback (most recent call last): File "", line 1, in ? NameError: name 'set' is not defined - On a different machine, with same setup (as far as I can tell), I get the expected results: - [EMAIL PROTECTED] ~]# /usr/local/bin/python -c "import sys; print sys.executable; print sys.version; set()" /usr/local/bin/python 2.5 (r25:51908, Feb 8 2007, 16:29:18) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] [EMAIL PROTECTED] ~]# env python -c "import sys; print sys.executable; print sys.version; set()" /usr/local/bin/python 2.5 (r25:51908, Feb 8 2007, 16:29:18) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] [EMAIL PROTECTED] ~]# python -c "import sys; print sys.executable; print sys.version; set()" /usr/local/bin/python 2.5 (r25:51908, Feb 8 2007, 16:29:18) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] - Can anyone tell me what might be causing the erroneous behavior in the first example? Thanks, Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: "Subscribing" to topics?
Mizipzor wrote: > I discovered that Google has a free newsserver, so I > joined this group from there. Sadly, I cant see a feature here that > lets me subscribe to an individual topic, so if anyone else is using > Google groups and know how to do something like this, please tell > me. I don't know about Google Groups, but you may be able to accomplish what you want to do with "filters" provided by your newsreader software. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Parameter lists
Mizipzor wrote: > class Stats: > def __init__(self, *li): > self.speed = li[0] > self.maxHp = li[1] > (...) > > Or maybe there is an even niftier way that lets me iterate through > them? Using keyword arguments instead of positional parameters makes this easy: >>> class Stats: ... def __init__(self, **kw): ... self.__dict__.update(kw) ... >>> stats = Stats(speed=10, maxHp=100, armor='plate mail') >>> stats.speed 10 >>> stats.maxHp 100 >>> stats.armor 'plate mail' Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: another newbie question: why should you use "*args" ?
stef wrote: >> And one note more. Just to be more pythonic you shouldn't use form >> range(len(blabla)). Instead use: >> >> for i in list: >> blabla... >> >> > I would love that, > but please tell me how (I need an integer counter for something else too): for index, item in enumerate(args): ... Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterate through list two items at a time
Dave Dean wrote: > I'm looking for a way to iterate through a list, two (or more) items at a > time. Here's a solution, from the iterools documentation. It may not be the /most/ beautiful, but it is short, and scales well for larger groupings: >>> from itertools import izip >>> def groupn(iterable, n): ... return izip(* [iter(iterable)] * n) ... >>> list(groupn(myList, 2)) [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11)] >>> list(groupn(myList, 3)) [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)] >>> list(groupn(myList, 4)) [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)] >>> for a,b in groupn(myList, 2): ... print a, b ... 0 1 2 3 4 5 6 7 8 9 10 11 >>> Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Unescaping URLs in Python
John Nagle wrote: > What's the appropriate Python function to call to unescape a URL which > might contain things like that? xml.sax.saxutils.unescape() > Will this interfere with the usual "%" > type escapes in URLs? Nope, and urllib.unquote() can be used to translate URL escapes manually. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: is mod_python borked?
walterbyrd wrote: > Researching further, it looks to me like mod_python may only work well > on apache 2.X. mod_python works splendidly with Apache 2.0.x as well as Apache 1.3.x. There is a different mod_python version for each Apache branch. The version for Apache 2.0.x has newer features, but the version for Apache 1.3.x is still enjoyable. Django may use features from the Apache 2.0.x mod_python version that are not available in the Apache 1.3.x version, thus making Django incompatible with Apache 1.3.x. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: New to Python: Do we have the concept of Hash in Python?
A.M wrote: > I am asking this because I learned that DB-API in Python doesn't offer > access to cursor columns by name. The only option is access by index. I > hope that I've got it wrong! While it's not part of the DB-API as far as I know, the MySQLdb package (and perhaps other DB access modules as well, I don't know) provides a "DictCursor" class that returns query results as a tuple of column-keyed dictionaries (hashes, already demonstrated by Brian). Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for a language/framework
Alex Martelli wrote: > I've never seen an "object-relational mapping" (technical > term for cruft that tries to avoid people having to learn and use SQL) > which doesn't drive me into a murderous, foam-at-mouth rage in a very > short time -- I WANT my SQL, I LOVE SQL, it's WAY more powerful > and suitable for access to data than all those simulated "OO DB" people > lay on top of it How refreshing to discover I'm not the only person on earth who feels this way. > (of course, that does depend on having a REAL > relational DB underneath, not, say, MySQL;-). Well, you lost me there ... I prefer MySQL to the alternatives, but I still feel validated :-) Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQLDB - return strange type of variable
Grzegorz Smith wrote: > Hi all. I'm trying get data from text field in MySQl 5.0 with my National > characters. Data are stored in utf8 encodings. Here is the script: > import MySQLdb, MySQLdb.cursors > conn = MySQLdb.connect(host='localhost', user='root', passwd='123456', > db='profile_locale') > c = conn.cursor(MySQLdb.cursors.DictCursor) > c.execute("SET CHARACTER SET utf8") > c.execute("SELECT string_value FROM lang_pl_pl WHERE id=8") > row = c.fetchone() > print row > and i get: > {'string_value': array('c', 'Zmie\xc5\x84 has\xc5\x82o')} > where it come array type? Recent versions of MySQL/MySQLdb return "binary strings" as Python array objects, rather than bytestrings (I think the change was around version 4.2 of MySQL.) Details on array objects are here: http://www.python.org/doc/lib/module-array > How can i get value 'Zmie\xc5\x84 has\xc5\x82o' ?? array('c', 'Zmie\xc5\x84 has\xc5\x82o').tostring() > If I do c.fetchone -shouldn't i get type tuple? If you use the default MySQLdb cursor object you will get a tuple. Using the DictCursor as you are returns a dictionary instead. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Server.sendmail with no "to_addrs" parameter.
EdWhyatt wrote: > But are you serious about that RFC Compliant thing? RFC 2822 obsoletes RFC 822, and I don't know of any specification in RFC 2822 that requires an email address to be present in the To: header. My mail seems to generally work fine without a To: header. I haven't memorized RFC 2822 though, so you may want to read it yourself if you're concerned ;-) Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Server.sendmail with no "to_addrs" parameter.
EdWhyatt wrote: > I would like to have the ability to send a mail > message with no-one email address in the To field. The to_addrs parameter is for the SMTP "RCPT TO", which must contain at least one address. It has nothing to do with the To: header of the email. You can *also* add the recipient addresses to either of the Cc: or To: header fields as necessary, or leave them out of any header (the equivalent of adding the addresses to the Bcc: field.) As Arne Ludwig pointed it, it may not be RFC compliant to leave an entirely blank To: header, but in either case, the To: header need not contain any of the addresses specified in the to_addrs list. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Quote-aware string splitting
Bengt Richter wrote: > Oops, note some spaces inside quotes near ss and missing double quotes in > result. And here I thought the main problem with my answer was that it didn't split unquoted segments into separate words at all! Clearly I missed the generalization being sought, and a more robust solution is in order. Fortunately, others have been forthcoming with them. Thank you, Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Quote-aware string splitting
J. W. McCall wrote: > For example, given the string: > > 'spam "the life of brian" 42' > > I'd want it to return: > > ['spam', 'the life of brian', '42'] The .split() method of strings can take a substring, such as a quotation mark, as a delimiter. So a simple solution is: >>> x = 'spam "the life of brian" 42' >>> [z.strip() for z in x.split('"')] ['spam', 'the life of brian', '42'] Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorator Syntax For Recursive Properties
Peter Otten wrote: >> something like this didn't work for me: > But this will, I suppose: > > @property > def ancestors(self): > if self.parent: > return self.parent.ancestors + [self.parent] > return [] > > A non-recursive variant: > > @property > def ancestors(self): > result = [] > node = self.parent > while node: > result.append(node) > node = node.parent > result.reverse() > return result Indeed, both of these patterns suit my needs. Thanks very much for the eye-opening insights. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Decorator Syntax For Recursive Properties
Consider the following class: class Node(object): def __init__(self, name, parent=None): self.name = name self.parent = parent def _ancestors(self, ants=None): if ants is None: ants = [] else: ants.insert(0, self.name) if self.parent is None: return ants return self.parent._ancestors(ants) ancestors = property(_ancestors) The ancestor property generates a list ancestor nodes by having each parent examine its own name recursively. The recursion is managed by calling the method underlying the parent property, rather than calling the property directly. Is it possible to rewrite this property using decorator syntax? Does the @property decorator create some underlying method that I can call directly? Alternately, perhaps there is a way to rewrite the recursion so that such a call is unnecessary? Note that the property should not add its own name if it is the originating node (the node trying to find _its_ ancestors). So something like this didn't work for me: @property def ancestors(self): if self.parent is None: return [self.name] return [self.name] + self.parent.ancestors In other words, since there is no longer a list passing from child to parent during the recursion, is there a graceful way to examine the state of the ancestor list so far? Thanks, Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: compound strip() string problem
Dylan Wilson wrote: > Now i need to compond that string remove the whitespace if you will.Well > i read up on strip(), lstrip() and rstrip() and all i could deduce was > that they striped the whitespace from the start and/or end of the > string.But I tried that anyway and failed.Is there an easier way than i > did it below? As Sidharth Kuruvila pointed out, time.strftime() is probably the best choice for your situation. For the general case of removing whitespace from a sting, one method is: ''.join(mystring.split()) Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: why are LAMP sites slow?
Paul Rubin wrote: > I don't know that the browser necessarily renders that faster than it > renders a table, but there's surely less crap in the HTML, which is > always good. I may start using that method. Using tables for layout is also a cardinal faux pas if you care about accessibility, as such tables can really mess up things like screenreader software for the sight-impaired. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: why are LAMP sites slow?
M.E.Farmer wrote: > Div is a block level tag and span isn't. > You can also group them together and nest them. One caveat here -- I don't believe you can (should) nest a inside a , or for that matter, nest any block-level element inside an inline element. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: mod_python and xml.sax
Fredrik Lundh wrote: > iirc, both apache and python uses the expat parser; if you don't make sure > that both use the same expat version, you may get into trouble. Thank you very much Fredrik, this does seem to be the problem I was having. > this > poster claims to have a fix: > > http://www.modpython.org/pipermail/mod_python/2004-May/015569.html In fact two fixes are offered in this one short post :-) The second fix, linking Python against the system Expat library, is specifically warned against in Modules/Setup: """ #... Source of Expat 1.95.2 is included in # Modules/expat/. Usage of a system shared libexpat.so/expat.dll is # not advised. """ So I went with the first fix -- I upgraded the system library to Expat 1.95.8 to match the Expat bundled with Python 2.4 (the excerpt above is apparently a little outdated). Everything is now working again. Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
mod_python and xml.sax
I am having difficulty getting mod_python and xml.sax to play nicely with each other. I am using: Python 2.4 Mod_python 2.7.10 Apache 1.3.33 Using the mod_python.publisher handler to invoke the go function in the following script: from xml.sax import make_parser def go(): x = make_parser() return 'OK' This is the entire script. The import works fine, but creating the parser fails, producing no traceback, and no Apache errors, but apparently crashing mod_python. A wget of the URL looks like: ~$ wget http://mysite.com/live/test.py/go --07:41:51-- http://future.uselesstree.org/live/test.py/go => `go' Resolving mysite.com ... 6x.xxx.xxx.xxx Connecting to mysite.com[6x.xxx.xxx.xxx]:80... connected. HTTP request sent, awaiting response... 07:41:51 ERROR -1: No data received. Changing the import scheme to import the entire xml.sax module, or attempting to create the parser using xml.sax.parse() produces the same results: mod_python crashes as soon as an attempt is made to create the parser. Searching on the web, I found one other person who claims to have a similar problem with mod_python 2.7.8 and Python 2.3, but no solutions. Does anyone here know a fix/workaround for this apparent incompatibility? Thank you, Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: using os
Juliano Freitas wrote: > how can i get just the directories in other directorie without a files > using the os module in Python?? You can test using os.path.isdir, for example: >>> import os >>> [x for x in os.listdir('.') if os.path.isdir(x)] Jeffrey -- http://mail.python.org/mailman/listinfo/python-list