Re: Modifying Class Object
Arnaud Delobelle wrote: "Alf P. Steinbach" writes: * Chris Rebert: On Sun, Feb 7, 2010 at 5:05 PM, T wrote: Ok, just looking for a sanity check here, or maybe something I'm missing. I have a class Test, for example: class Test: def __init__(self, param1, param2, param3): self.param1 = param1 self.param2 = param2 self.param3 = param3 Next, I have a dictionary mytest that contains instances of Test. If I want to modify one of the Test instances within my dictionary, I have to rewrite the entire entry, correct (since Python passes by value, not reference)? Incorrect; Python uses neither. See http://effbot.org/zone/call-by-object.htm for a excellent explanation of what Python does use. Hm. While most everything I've seen at effbot.org has been clear and to the point, that particular article reads like a ton of obfuscation. Python passes pointers by value, just as e.g. Java does. Please! Not this again! This has been discussed to death and beyond more than enough times. Go search the c.l.p archives, read it all, and I'm sure you won't want to add anything anymore. +1 !! -- http://mail.python.org/mailman/listinfo/python-list
Re: SQLite3: preventing new file creation
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Gnarlodious wrote: > Every time I say something like: > > connection=sqlite3.connect(file) > > sqlite creates a new database file. Can this behavior be suppressed > through SQLite? Or am I forced to check for the file existing first? This is due to the API that pysqlite uses to talk to SQLite. (There is a more recent API but pysqlite remains backwards compatible with older SQLite versions). Note that although SQLite will create the file, it will be zero length (*) until you do a command that causes a database change. Also as a guideline be careful with SQLite files. In particular not only is there a database file, but there may also be a journal file. If the journal is removed then the main database file can be corrupted. (The journal contains data in order to rollback back incomplete transactions from the database.) (*) On Mac due to an operating system bug the file will actually be created as one byte in length containing the upper case letter 'S'. There is a dedicated mailing list for Python and SQLite: http://groups.google.com/group/python-sqlite You can use the newer SQLite database open API as well as many other SQLite APIs not supported by pysqlite by using APSW. (Disclaimer: I am the author of APSW.) Roger -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAktvxyoACgkQmOOfHg372QQjqwCglx0u6OgGgOsQm0Bwd7s6BmCS 7EgAoKDdMZyDaw3Ov+Uqzs3RFX/NSHEK =/E0N -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: execute sqlite3 dot commands in python
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 gintare statkute wrote: > Does anybody know if it possible to execute sqlite3 dot commands in python? The dot commands are parsed and executed by different code not part of the standard SQLite library. However if you want interactive shell functionality from Python then you can use APSW. It includes a shell you can just go ahead and use based on a shell class you can extend with your own methods, direct input and output as needed, completion etc. http://apsw.googlecode.com/svn/publish/shell.html (Disclaimer: I am the APSW author) Roger -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAktvyGMACgkQmOOfHg372QT5mgCgrMCtb3bHd3rF0L+lL/nZV6BX zrMAn1fcxS4CyKYF4KXVBcVcEXWhxoig =hpkY -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with regex search-and-replace (Perl to Python)
Schif Schaf wrote: On Feb 7, 8:57 am, Tim Chase wrote: Steve Holden wrote: Really? Under what circumstances does a simple one-for-one character replacement operation fail? Failure is only defined in the clarified context of what the OP wants :) Replacement operations only fail if the OP's desired output from the above mess doesn't change *all* of the ]/[ characters, but only those with some form of parity (nested or otherwise). But if the OP *does* want all of the ]/[ characters replaced regardless of contextual nature, then yes, replace is a much better solution than regexps. I need to do the usual "pipe text through and do various search/ replace" thing fairly often. The above case of having to replace brackets with braces is only one example. Simple string methods run out of steam pretty quickly and much of my work relies on using regular expressions. Yes, I try to keep focused on simplicity, and often regexes are the simplest solution for my day-to-day needs. Could you post a complex case? It's a kindness to your helpers to simplify your case, but if the simplification doesn't cover the full scope of your problem you can't expect the suggestions to cover it. Frederic -- http://mail.python.org/mailman/listinfo/python-list
Re: ctypes Structure serialization
OK, an easier question, hopefully. How to unpack all fields from ctypes Structure line by line and save into the name-value pairs? -- http://mail.python.org/mailman/listinfo/python-list
The Case For Do-Once
I wrote my first Python extension library over the last couple of weeks. I took note of all the recommendations to keep track of reference counts, to ensure that objects were not disposed when they shouldn’t be, and were when they should. However, the example code seems to use gotos. And the trouble with these is that they don’t nest and un-nest easily; try to do too much refactoring with them, and you run into the age-old “spaghetti code” problem. Which is where the do-once block comes in. The basic control flow is this: * Unconditionally initialize all dynamic storage to nil * Do the main body of the code, aborting in any error * Regardless of success or failure of the above, dispose of all allocated dynamic storage, using disposal calls which turn into noops if passed pointers that are already nil. For example, here’s a utility routine from my extension that, passed a Python array object, returns the address and length of the storage: static void GetBufferInfo ( PyObject * FromArray, unsigned long * addr, unsigned long * len ) /* returns the address and length of the data in a Python array object. */ { PyObject * TheBufferInfo = 0; PyObject * AddrObj = 0; PyObject * LenObj = 0; do /*once*/ { TheBufferInfo = PyObject_CallMethod(FromArray, "buffer_info", ""); if (TheBufferInfo == 0) break; AddrObj = PyTuple_GetItem(TheBufferInfo, 0); LenObj = PyTuple_GetItem(TheBufferInfo, 1); if (PyErr_Occurred()) break; Py_INCREF(AddrObj); Py_INCREF(LenObj); *addr = PyInt_AsUnsignedLongMask(AddrObj); *len = PyInt_AsUnsignedLongMask(LenObj); if (PyErr_Occurred()) break; } while (false); Py_XDECREF(AddrObj); Py_XDECREF(LenObj); Py_XDECREF(TheBufferInfo); } /*GetBufferInfo*/ You can pretty much determine by inspection that all the reference counts are properly maintained, no need to trace through convoluted flows of control. -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing Commands From Windows Service
Thanks for the suggestions - I think my next step is to try running it under an admin user account, as you guys both mentioned. Alf - you're absolutely right, Microsoft has srvany.exe, which allows you to run any EXE as a Windows service. I've done this in the past, but it's more of a "hack"..so this go around (since I will be distributing this program), I wanted to go the more professional route..which, unfortunately, involves learning the "scum". :) I posted this to comp.os.ms-windows.programmer.win32, so we'll see if what the Win32 programmers have to say as well. Thanks again! -- http://mail.python.org/mailman/listinfo/python-list
Re: xmlrpc slow in windows 7 if hostnames are used
"Gabriel Genellina" writes: > En Sat, 06 Feb 2010 22:15:48 -0300, Jean-Michel Pichavant > escribió: > >> I'm puzzled. >> Unless my english is failing me, everything would be solved using >> hostnames if I follow you. Why don't you do that ? >> I am no network/IP guru, but it sounds very weird to have requests >> rejected when using IP addresses. Are you sure your host names are >> resolved with the same IPM address you are using ? > > HTTP 1.1 requires a Host: header field; this way, multiple web > servers may share the same IP address. So you can't identify a host > by its IP alone; the host name is required. This was devised in > order to save IPv4 addresses; LACNIC (the Latin America addresses > register) does not assign addresses to ISP's based solely on web > hosting anymore - they MUST share existing IPs. And I think a > similar policy is used on other regions. If you really want to go for speed you should be able to set the Host: header to the name but use the IP address to make the connection. Something else that might be slowing you down is anti-spyware or anti-virus. Several products put a long list of blacklist sites in the hosts file. Windows can be rather slow to process that file. -- Pete Forman-./\.- West Sussex, UK -./\.- http://petef.22web.net -./\.- petef4+use...@gmail.com -./\.- -- http://mail.python.org/mailman/listinfo/python-list
Re: python admin abuse complaint
Daniel Fetchinson writes: > One more thing: Yeah, one more thing: since you are all for a better community why not reply without quoting the entire message? Just quote enough to provide some decent context. Xah is just a spammer. It amazes me how often people want to step in the role of Xah's sockpuppet. -- John Bokma j3b Hacking & Hiking in Mexico - http://johnbokma.com/ http://castleamber.com/ - Perl & Python Development -- http://mail.python.org/mailman/listinfo/python-list
Re: python admin abuse complaint
On Sun, 07 Feb 2010 13:57:13 +0100, Daniel Fetchinson wrote: > having a single c.l.p clown is tolerable if it makes him happy. Why should we care about his happiness if it comes at the expense of the happiness of hundreds of other people? I mean, if he decided that his happiness was best satisfied by following you home one day and smashing all your windows and pouring tar all over your furniture and smearing excrement over your clothes, should we tolerate that because it makes him happy? And later, in another post: > A village clown is tolerated in the purest form of the word 'tolerance' > by nor wishing him to change. Let him be the clown, let everybody > accept him as such, including all the annoyance and weird behavior. Why should we? What's in it for us? > Hoping for someone to change is the same as assigning him to a > correctional facility. That's a ridiculous comparison, which could only have been spoken to somebody who has never been in prison. You trivialise the problem of the punishment society by equating it to expecting a modicum of polite behaviour in public. > I'd say let's designate a post Python Community Jester, or PCJ for > short, let's name Xah Lee the PCJ and make it clear that he can engage > in his activities on c.l.p and #python as he wishes without > retribution and fear, and nobody should really bother him. The only > people should do who don't like him is ignoring him. What is very > important is that there can be only one PCJ and everybody else with > objectionable behavior will be banned, blacklisted, etc. with the full > force of available methods. Why should Xah Lee get special treatment? If other anti-social nuisances and trolls are banned, why should he get the privilege of being tolerated no matter what he does? What is so special about Xah Lee that he gets carte blanche permission to be as obnoxious as he wants, while everyone else has to follow the rules of polite society? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing Commands From Windows Service
> > It's working fine when I run it via " debug" - that's how > I was testing before. It's when I start the service that it fails - > and you can see that, when you run it with debug, plink.exe runs under > my username. When I run it as a service, it runs under System... You can have the service run as any user under the service properties. Perhaps set the service to run under your username? There may be some environment variables set in your session that aren't in the one its running as. So maybe check there as well. Off to drink beer. Good luck. -- http://mail.python.org/mailman/listinfo/python-list
Re: Overcoming python performance penalty for multicore CPU
Paul Rubin, 04.02.2010 02:51: > John Nagle writes: >> Analysis of each domain is >> performed in a separate process, but each process uses multiple >> threads to read process several web pages simultaneously. >> >>Some of the threads go compute-bound for a second or two at a time as >> they parse web pages. > > You're probably better off using separate processes for the different > pages. If I remember, you were using BeautifulSoup, which while very > cool, is pretty doggone slow for use on large volumes of pages. I don't > know if there's much that can be done about that without going off on a > fairly messy C or C++ coding adventure. Maybe someday someone will do > that. Well, if multi-core performance is so important here, then there's a pretty simple thing the OP can do: switch to lxml. http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/ Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
T wrote: > Oops, this one was my fault - the object I was having the issues with > was actually a shelve file, not a dictionary..so just re-assigning the > variable isn't working, but re-writing the object to the shelve file > does. So in this case, is there any way to just change a single > value, or am I stuck rewriting the entry? Either open the shelve with writeback=True or rewrite the entry. Rewriting the entry isn't hard: you can just re-use the same value: def changevalue(): for key in mytest.keys(): temp = mytest[key] temp.param3 = "newvalue" mytest[key] = temp If you really are changing every item in the shelve using writeback=True will be much simpler, but if you are only changing a few then just tell the shelve that you've updated them as above. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: threading+popen2 hang
On 7 fév, 17:00, a...@pythoncraft.com (Aahz) wrote: > In article > <188bfb67-3334-4325-adfc-3fa4d28f0...@d27g2000yqn.googlegroups.com>, > > lofic wrote: > > >Works fine on RHEL5/python 2.4.3 > >Hangs on RHEL4/python 2.3.4 > > Then use Python 2.4 -- surely you don't expect anyone to provide bugfixes > for a release that's several years old? > -- > Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ > > import antigravity 2.3 is the version provided with RHEL 4, which is still widely used in production environments. Sometimes it is not so easy to shift to another version in production systems. Some people don't work with the last bleeding edge Fedora release and must deal with (sometimes pretty old) existing system pools. I could deploy python 2.4 in addition to python 2.3, but it is much shake up for a little bug and a little program. I was not expected a bugfix, but maybe a workaround. Louis -- http://mail.python.org/mailman/listinfo/python-list
Re: Overcoming python performance penalty for multicore CPU
Stefan Behnel writes: > Well, if multi-core performance is so important here, then there's a pretty > simple thing the OP can do: switch to lxml. > > http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/ Well, lxml is uses libxml2, a fast XML parser written in C, but AFAIK it only works on well-formed XML. The point of Beautiful Soup is that it works on all kinds of garbage hand-written legacy HTML with mismatched tags and other sorts of errors. Beautiful Soup is slower because it's full of special cases and hacks for that reason, and it is written in Python. Writing something that complex in C to handle so much potentially malicious input would be quite a lot of work to write at all, and very difficult to ensure was really safe. Look at the many browser vulnerabilities we've seen over the years due to that sort of problem, for example. But, for web crawling, you really do need to handle the messy and wrong HTML properly. -- http://mail.python.org/mailman/listinfo/python-list
Re: intolerant HTML parser
Jim, 06.02.2010 20:09: > I generate some HTML and I want to include in my unit tests a check > for syntax. So I am looking for a program that will complain at any > syntax irregularities. First thing to note here is that you should consider switching to an HTML generation tool that does this automatically. Generating markup manually is usually not a good idea. > I am familiar with Beautiful Soup (use it all the time) but it is > intended to cope with bad syntax. I just tried feeding > HTMLParser.HTMLParser some HTML containing 'ab' and it > didn't complain. > > That is, this: > h=HTMLParser.HTMLParser() > try: > h.feed('ab') > h.close() > print "I expect not to see this line" > except Exception, err: > print "exception:",str(err) > gives me "I expect not to see this line". > > Am I using that routine incorrectly? Is there a natural Python choice > for this job? You can use lxml and let it validate the HTML output against the HTML DTD. Just load the DTD from a catalog using the DOCTYPE in the document (see the 'docinfo' property on the parse tree). http://codespeak.net/lxml/validation.html#id1 Note that when parsing the HTML file, you should disable the parser failure recovery to make sure it barks on syntax errors instead of fixing them up. http://codespeak.net/lxml/parsing.html#parser-options http://codespeak.net/lxml/parsing.html#parsing-html Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: The Case For Do-Once
Lawrence D'Oliveiro, 08.02.2010 09:53: > I wrote my first Python extension library over the last couple of weeks. I > took note of all the recommendations to keep track of reference counts, to > ensure that objects were not disposed when they shouldn’t be, and were when > they should. This sounds more like a case for Cython to me, should have saved you a lot of time. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Import question
2010/2/6 Gabriel Genellina > En Fri, 05 Feb 2010 13:21:47 -0300, Andrew Degtiariov > escribió: > > > Code of our project has split into several packages and we deploy the >> project using buildout. >> All worked fine until I need to dynamically inspect python modules. >> > > Entirely by luck, I'd say :) > > > ├───project.api.config >> │ ├───project >> │ │ └───api >> │ │ └───config >> │ │ └───settings >> │ └───project.api.config.egg-info >> ├───project.api.contacts >> │ ├───project >> │ │ └───api >> │ │ └───contacts >> │ │ ├───importer >> │ │ └───views >> │ └───project.api.contacts.egg-info >> > > Regular code like "import project.api.config" worked fine, but now I'm >> tryed >> __import__('project.api.config'): >> >> $ bin/python >> >> import project.api.config > __import__('project.api.config') > > 'c:\users\ad\project\src\project.api.contacts\project\__init__.pyc'> >> > > > If someone is interesting - __import__ works but imp doesn't. In my example: >>> m = __import__('project.api.config') >>> m >>> m.api >>> m.api.config Please note that the m and m.api pointed to first installing package with namespace project.api but m.api.config have pointed to the proper file. And releasing code with several distributions it is one of goals of Distribute. And you might look to pypi for... zope. There is lot of package which names starts from "zope." It's really convenient -- Andrew Degtiariov DA-RIPE -- http://mail.python.org/mailman/listinfo/python-list
Re: How to print all expressions that match a regular expression
En Mon, 08 Feb 2010 02:17:59 -0300, hzh...@gmail.com escribió: Please check out this example on the pyparsing wiki, invRegex.py:http://pyparsing.wikispaces.com/file/view/invRegex.py. This code implements a generator that returns successive matching strings for the given regex. [...] Of course, as other posters have pointed out, this inverter does not accept regexen with unbounded multiple characters '+' or '*', but '?' and "{min,max}" notation will work. Even '.' is supported, although this can generate a large number of return values. Thanks very much. This is exactly what I need now. I will check this function. Here you have another approach based on [1]. This is a generator-based approach, yielding all strings in increasing length order. In principle it can handle unbounded repetitions, except as written the maximum recursion limit is shortly reached (the original code is in Haskell, I almost blindly translated it into Python; certainly it can be rewritten more efficiently) You have to parse the R.E. and generate the corresponding function calls to the merge/prod/closure functions -- pyparsing certainly can help with that. "ab" becomes prod(a,b), "a|b" becomes merge(a,b), and "a*" becomes closure(a) By example, to find the language defined by this expression "(a|bc)*d", one has to evaluate: prod( closure( merge(['a'], prod(['b'],['c']))), ['d'] ) wich yields these strings: d ad aad bcd aaad abcd bcad ... bcbcbcbcaad bcbcbcbcbcd aaad and after 234 results aborts with a recursion error :( [1] http://www.cs.utexas.edu/users/misra/Notes.dir/RegExp.pdf -- Gabriel Genellina enumerate_regular_language.py Description: Binary data -- http://mail.python.org/mailman/listinfo/python-list
Re: intolerant HTML parser
In message <4b6fd672$0$6734$9b4e6...@newsspool2.arcor-online.net>, Stefan Behnel wrote: > Jim, 06.02.2010 20:09: > >> I generate some HTML and I want to include in my unit tests a check >> for syntax. So I am looking for a program that will complain at any >> syntax irregularities. > > First thing to note here is that you should consider switching to an HTML > generation tool that does this automatically. I think that’s what he’s writing. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to print all expressions that match a regular expression
En Mon, 08 Feb 2010 02:17:59 -0300, hzh...@gmail.com escribió: Please check out this example on the pyparsing wiki, invRegex.py:http://pyparsing.wikispaces.com/file/view/invRegex.py. This code implements a generator that returns successive matching strings for the given regex. [...] Of course, as other posters have pointed out, this inverter does not accept regexen with unbounded multiple characters '+' or '*', but '?' and "{min,max}" notation will work. Even '.' is supported, although this can generate a large number of return values. Thanks very much. This is exactly what I need now. I will check this function. Here you have another approach based on [1]. This is a generator-based approach, yielding all strings in increasing length order. In principle it can handle unbounded repetitions, except as written the maximum recursion limit is shortly reached (the original code is in Haskell, I almost blindly translated it into Python; certainly it can be rewritten more efficiently) You have to parse the R.E. and generate the corresponding function calls to the merge/prod/closure functions -- pyparsing certainly can help with that. "ab" becomes prod(a,b), "a|b" becomes merge(a,b), and "a*" becomes closure(a) By example, to find the language defined by this expression "(a|bc)*d", one has to evaluate: prod( closure( merge(['a'], prod(['b'],['c']))), ['d'] ) wich yields these strings: d ad aad bcd aaad abcd bcad ... bcbcbcbcaad bcbcbcbcbcd aaad and after 234 results aborts with a recursion error :( [1] http://www.cs.utexas.edu/users/misra/Notes.dir/RegExp.pdf -- Gabriel Genellina enumerate_regular_language.py Description: Binary data -- http://mail.python.org/mailman/listinfo/python-list
Re: convention for documenting function parameters in doc strings
danielx wrote: Is there a convention for how to document function (or method) parameters in doc strings? Recently, I've been doing alot of PHP programming, and in PHPdoc, you'd do it like this: /* * @param type $foo Description. * * @return type Description. */ function bar($foo) { ... } Is there an equivalent convention I (c|sh)ould be using in my Python docstrings? I saw that Python 3 has function annotations, which could be used for this purpose, but function annotations have no particular purpose within the language itself (which seems like a mistake to me), and they don't exist in the Python 2.x series (at least not the older versions). Different strategies here: 1/ most doc builders propose their own format. You can stick to it if you don't want to use another builder (e.g. epydoc has a specific syntax to document signatures) 2/ Use a 'standard' format, usually these formats are a bit more formal but they gain in portability, most builders support these formats. reStructuredText is one of them and supported by all python doc builders. http://epydoc.sourceforge.net/manual-othermarkup.html JM -- http://mail.python.org/mailman/listinfo/python-list
Re: intolerant HTML parser
Lawrence D'Oliveiro, 08.02.2010 11:19: > In message <4b6fd672$0$6734$9b4e6...@newsspool2.arcor-online.net>, Stefan > Behnel wrote: > >> Jim, 06.02.2010 20:09: >> >>> I generate some HTML and I want to include in my unit tests a check >>> for syntax. So I am looking for a program that will complain at any >>> syntax irregularities. >> First thing to note here is that you should consider switching to an HTML >> generation tool that does this automatically. > > I think that’s what he’s writing. I don't read it that way. There's a huge difference between - generating HTML manually and validating (some of) it in a unit test and - generating HTML using a tool that guarantees correct HTML output the advantage of the second approach being that others have already done all the debugging for you. Stefan -- http://mail.python.org/mailman/listinfo/python-list
use strings to call functions
Hello, I am writing a program that analyzes files of different formats. I would like to use a function for each format. Obviously, functions can be mapped to file formats. E.g. like this: if file.endswith('xyz'): xyz(file) elif file.endswith('abc'): abc(file) ... Yet, I would prefer to do something of the following kind: func = file[-3:] apply_func(func, file) Can something of this kind be done in Python? Klaus -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
Klaus Neuner a écrit : Hello, I am writing a program that analyzes files of different formats. I would like to use a function for each format. Obviously, functions can be mapped to file formats. E.g. like this: if file.endswith('xyz'): xyz(file) elif file.endswith('abc'): abc(file) ... Yet, I would prefer to do something of the following kind: func = file[-3:] A file extension is not necessarily 3 chars long. apply_func(func, file) Can something of this kind be done in Python? The simplest (and canonical) solution is to use a dict: def handle_txt(path): # code here def handle_py(path): # code here etc... def handle_default(path): # for anything else handlers = { ".txt" : handle_txt, ".py" : handle_py, # etc } import os def handle_file(path): dummy, ext = os.path.splitext(path) handler = handlers.get(ext, handle_default) return handler(path) HTH -- http://mail.python.org/mailman/listinfo/python-list
Create a backslash-escaped version of a string?
Hello, I'd like to have control characters in a string to be converted to their backslash-escaped counterparts. I looked in the encoders section of the string module but couldn't find anything appropriate. I could write it myself but I'm sure something of the sort exists. The hypothetical method "c_escaped()" would work like this: >>> a="abc\rdef" >>> print a.c_escaped() abc\rdef >>> Thanks, robert -- http://mail.python.org/mailman/listinfo/python-list
speed of server
hi all, how to find the speed of a particular server ( suppose it is hosting a site yahoo.com) how can i find it using python script whether it is slow or faster on that time help me thanks Bujji -- http://mail.python.org/mailman/listinfo/python-list
Re: Create a backslash-escaped version of a string?
On Mon, Feb 8, 2010 at 3:14 AM, boblatest wrote: > Hello, > > I'd like to have control characters in a string to be converted to > their backslash-escaped counterparts. I looked in the encoders section > of the string module but couldn't find anything appropriate. I could > write it myself but I'm sure something of the sort exists. The > hypothetical method "c_escaped()" would work like this: > a="abc\rdef" print a.c_escaped() > abc\rdef print a.encode("string-escape") Note that there are some wrinkles if the string contains quote marks (and possibly also if it contains Unicode; I didn't test). Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
> > A file extension is not necessarily 3 chars long. No, of course not. But it is, if I choose to use only (self-made) file endings that are 3 chars long. Anyway, it was just an example. > handlers = { > ".txt" : handle_txt, > ".py" : handle_py, > # etc > } > That is exactly what I would like to avoid: Having to map the function 'handle_txt' to '.txt'. Firstly, because I don't want to repeat anything and secondly, because I will one day add a new function and forget to add its name to the dictionary. (This is not severe if there is only one dictionary for mapping functions, but it will make life a lot harder, if a lot of mappings of this kind are used.) What I want is calling the string directly. In Prolog, I would use something like: get_file_ending(File, Ending), Predicate =.. [Ending, File], call(Predicate). -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
Klaus Neuner wrote: Hello, I am writing a program that analyzes files of different formats. I would like to use a function for each format. Obviously, functions can be mapped to file formats. E.g. like this: if file.endswith('xyz'): xyz(file) elif file.endswith('abc'): abc(file) ... Yet, I would prefer to do something of the following kind: func = file[-3:] apply_func(func, file) Can something of this kind be done in Python? Klaus You perhaps were intending to use the file extension , rather than the last three characters of the name. If so, consider os.path.splitext(). And you shouldn't shadow the builtin *file* type with a variable of the same name. More directly to your question, best suggestion is to build a (const) dictionary: apply_func = { "xyz":xyz, "abc":abc } which maps the strings to functions. This line can be at outer scope, as long as it follows all the appropriate function definitions. Notice that the individual functions need not be in the same module, if you use a fully qualified name in the dictionary. And of course, there's no necessity of naming the function exactly the same as the extension. So you could implement the functions in another module 'implem", and use the following: import implem apply_func = { "xyz":implem.process_xyz_files, "abc":implem.process_abc_files } Now, you use it by something like: dummy, func_ext = os.path.splitext(my_filename) apply_func(func_ext, my_filename) (all code untested) DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
Klaus Neuner wrote: > > handlers = { > > ".txt" : handle_txt, > > ".py" : handle_py, > > # etc > > } > > > > That is exactly what I would like to avoid: Having to map the function > 'handle_txt' to '.txt'. Firstly, because I don't want to repeat > anything and secondly, because I will one day add a new function and > forget to add its name to the dictionary. Use dictionary mantained by runtime: def handle(extensions): funname = "handle_" + extension return globals()[funname] handle('txt') # => function handle_txt w. -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
On 08/02/2010 11:26, Klaus Neuner wrote: A file extension is not necessarily 3 chars long. No, of course not. But it is, if I choose to use only (self-made) file endings that are 3 chars long. Anyway, it was just an example. handlers = { ".txt" : handle_txt, ".py" : handle_py, # etc } That is exactly what I would like to avoid: Having to map the function 'handle_txt' to '.txt'. Firstly, because I don't want to repeat anything and secondly, because I will one day add a new function and forget to add its name to the dictionary. (This is not severe if there is only one dictionary for mapping functions, but it will make life a lot harder, if a lot of mappings of this kind are used.) What I want is calling the string directly. In Prolog, I would use something like: get_file_ending(File, Ending), Predicate =.. [Ending, File], call(Predicate). You basically need a getattr lookup. If you're prepared to instantiate a class or to import a handlers module then you can just look up against that: def handle_py (stuff): "print handling py" def handle_default (stuff): "print handling default" import handlers ext = "py" handler = getattr (handlers, "handle_" + ext, handlers.handle_default) handler ("stuff") You can do the equivalent by having a Handlers class with the appropriate methods (handle_py, etc.) and which you then instantiate. If you want to keep everything in one module, you should be able to achieve the same effect by looking the module up in sys.modules and then proceeding as above: import sys def handle_py (stuff): print "handling py" def handle_default (stuff): print "handling default" ext = "py" me = sys.modules[__name__] handler = getattr (me, "handle_" + ext, me.handle_default) handler ("blah") (All untested...) TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
Steven D'Aprano wrote: > On Sun, 07 Feb 2010 22:03:06 -0500, Steve Holden wrote: > >> Alf: >> >> This topic was discussed at great, nay interminable, length about a year >> ago. I'd appreciate it if you would search the archives and read what >> was said then rather than hashing the whole topic over again to nobody's >> real advantage. > > Curse you Steve, I had just come up with a brilliant rebuttal of Alf's > position. It was sheer genius, the sort of thing that would have James > Gosling weeping with envy. > > Oh well, into the bitbucket it goes... > > You're a better man for it, Steven! Admirable self-restraint ... regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
Klaus Neuner wrote: Hello, I am writing a program that analyzes files of different formats. I would like to use a function for each format. Obviously, functions can be mapped to file formats. E.g. like this: if file.endswith('xyz'): xyz(file) elif file.endswith('abc'): abc(file) ... Yet, I would prefer to do something of the following kind: func = file[-3:] apply_func(func, file) Can something of this kind be done in Python? Klaus You won't need anything else than defining the proper function to support the extension with the following code: import os class Handlers: class NoHandler(Exception): pass @staticmethod def txt(fileName): print 'I am processing a txt file' @staticmethod def tar(fileName): print 'I am processing a tar file' @classmethod def default(cls, fileName): raise cls.NoHandler("I don't know how to handle %s " % fileName) for fileName in ['/tmp/test.txt', '/tmp/sdfsd.sfds']: _, extension = os.path.splitext(fileName) func = getattr(Handlers, extension.replace('.', ''), Handlers.default) try: func(fileName) except Handlers.NoHandler, exc: print exc JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
Alf P. Steinbach wrote: > * Steve Holden: [...] >> Alf: >> >> This topic was discussed at great, nay interminable, length about a year >> ago. I'd appreciate it if you would search the archives and read what >> was said then rather than hashing the whole topic over again to nobody's >> real advantage. > > Well that's my point, and thanks for backing me up on that :-): it's > very simple, and as demonstrated can be expressed in 10 words or less > (plus perhaps a terminology reference, as I did above), so all that > discussion and in particular the lengthy article at effbot serves as > obfuscation and nothing else. > Please don't assume I was trying to support you. Your remarks showed considerable ignorance of issue that were extremely nuanced. Whatever point you were trying to make was lost in your self-aggrandizing disrespect of Fredrik Lundh, a software engineer of some repute with a long history of contribution to Python. The fact that your post was basically a restatement of one of the several competing positions in that thread makes it no more right than any of the others. > By the way, most every programming language has some corner like that, > something that is utterly simple but somehow has some kind of > obfuscation-meme attached. > Why thank you for the education. Somehow in my 40-odd years of programming I had quite overlooked that fact. Which helps how? > In C++ it's "call" and "constructor". It doesn't help that the > language's standard lays down the law on it, it doesn't help that the > language's creator has laid down the law, it doesn't help that it's > utterly and completely simple. Somehow newbies and even some experienced > people manage to create their own terminological nightmare and drawing > conclusions about reality from that misguided obfuscated view, and then > discussing it up and down and sideways. > Which IMHO you have done little to assist. Just how exactly *do* we succeed in asking you not to discuss something? yours intemperate-ly - steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
Klaus Neuner, 08.02.2010 11:57: > I am writing a program that analyzes files of different formats. I > would like to use a function for each format. Obviously, functions can > be mapped to file formats. E.g. like this: > > if file.endswith('xyz'): > xyz(file) > elif file.endswith('abc'): > abc(file) > > ... > > Yet, I would prefer to do something of the following kind: > > func = file[-3:] > apply_func(func, file) > > Can something of this kind be done in Python? Others have already pointed you to the approach of using a dict, or a module/class namespace with functions/methods to do this. Either of the latter two would be my favourite, depending on the complexity of the handlers. A class is more suitable as a container for short, highly correlated handlers, whereas a module makes more sense for handlers that do rather different things, or that are longer than a single function. A mixture of the two, e.g. a module of classes, where an entire class is used to implement a complete handler over several methods (potentially including some inheritance hierarchy between handlers that share functionality) might also be a solution. Note that objects can be callable in Python (special method __call__), you can exploit that here. What you are implementing here is commonly called a dispatch mechanism, BTW. There are several ways to do that, also within in Python. A web search should reveal some more. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
Gerard Flanagan wrote: > Arnaud Delobelle wrote: >> "Alf P. Steinbach" writes: >> >>> * Chris Rebert: On Sun, Feb 7, 2010 at 5:05 PM, T wrote: > Ok, just looking for a sanity check here, or maybe something I'm > missing. I have a class Test, for example: > > class Test: >def __init__(self, param1, param2, param3): >self.param1 = param1 >self.param2 = param2 >self.param3 = param3 > > Next, I have a dictionary mytest that contains instances of Test. If > I want to modify one of the Test instances within my dictionary, I > have to rewrite the entire entry, correct (since Python passes by > value, not reference)? Incorrect; Python uses neither. See http://effbot.org/zone/call-by-object.htm for a excellent explanation of what Python does use. >>> Hm. While most everything I've seen at effbot.org has been clear and >>> to the point, that particular article reads like a ton of obfuscation. >>> >>> Python passes pointers by value, just as e.g. Java does. >> >> >> Please! Not this again! This has been discussed to death and beyond more >> than enough times. Go search the c.l.p archives, read it all, and I'm >> sure you won't want to add anything anymore. >> > > +1 !! > +1000 -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
Klaus Neuner wrote: >> A file extension is not necessarily 3 chars long. > > No, of course not. But it is, if I choose to use only (self-made) file > endings that are 3 chars long. Anyway, it was just an example. > >> handlers = { >> ".txt" : handle_txt, >> ".py" : handle_py, >> # etc >> } >> > > That is exactly what I would like to avoid: Having to map the function > 'handle_txt' to '.txt'. Firstly, because I don't want to repeat > anything and secondly, because I will one day add a new function and > forget to add its name to the dictionary. (This is not severe if there > is only one dictionary for mapping functions, but it will make life a > lot harder, if a lot of mappings of this kind are used.) > > What I want is calling the string directly. In Prolog, I would use > something like: > > get_file_ending(File, Ending), > Predicate =.. [Ending, File], > call(Predicate). > > -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Overcoming python performance penalty for multicore CPU
Le Tue, 02 Feb 2010 15:02:49 -0800, John Nagle a écrit : > I know there's a performance penalty for running Python on a multicore > CPU, but how bad is it? I've read the key paper > ("www.dabeaz.com/python/GIL.pdf"), of course. It would be adequate if > the GIL just limited Python to running on one CPU at a time, but it's > worse than that; there's excessive overhead due to a lame locking > implementation. Running CPU-bound multithreaded code on a dual-core CPU > runs HALF AS FAST as on a single-core CPU, according to Beasley. That's on certain types of workloads, and perhaps on certain OSes, so you should try benching your own workload to see whether it applies. Two closing remarks: - this should (hopefully) be fixed in 3.2, as exarkun noticed - instead of spawning one thread per Web page, you could use Twisted or another event loop mechanism in order to process pages serially, in the order of arrival Regards Antoine. -- http://mail.python.org/mailman/listinfo/python-list
Re: Calendar GUI
Jean-Michel Pichavant wrote: > Michael Torrie wrote: >> Gabriel wrote: >> >>> On Fri, Feb 5, 2010 at 9:08 PM, William Gaggioli >>> wrote: >>> I'm working on setting up some software for a Peruvian non-profit to help them organize their incoming volunteers. One of the features I'd like to add is a calendar-like view of the different volunteers arrival dates and staying time, with potentially some other info through some double-click action. Rather than writing a calendar gui myself, is there some open-source calendar program you guys could recommend that I could plug into? It has to be run locally, as the internet isn't so reliable down here, but other than that something simple and clean is ideal. Take a look at wxscheduler, and chandler. Johnf -- http://mail.python.org/mailman/listinfo/python-list
Python User Group near Cheltenham, UK ?
Anyone know of an 'active' Python User Group near Cheltenham, UK? I spotted one in Birmingham (http://www.pywm.eu), but would like one a little closer ... :-) Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: Python User Group near Cheltenham, UK ?
On 08/02/2010 14:22, Timothy W. Grove wrote: Anyone know of an 'active' Python User Group near Cheltenham, UK? I spotted one in Birmingham (http://www.pywm.eu), but would like one a little closer ... :-) Don't think there's anything further west than Northants / W. Midlands. There are Open Source things in Oxford but nothing Python-specific, I believe. TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: C/C++ Import
The folder does contain a file named '__init__.py'. However it contains nothing inside of the file. On Feb 8, 12:42 am, Austin Bingham wrote: > Does the 'python' directory contain a file named '__init__.py'? This > is required to let that directory act as a package > (see:http://docs.python.org/tutorial/modules.html#packages);without it, > you'll see the symptoms you're seeing. > > Austin > > On Mon, Feb 8, 2010 at 4:56 AM, 7H3LaughingMan > wrote: > > To make the background information short, I am trying to take a > > program that uses Python for scripting and recompile it for Linux > > since it originally was built to run on Win32. The program itself was > > designed to be able to be compiled on Linux and someone made there on > > release with source that added python scripting. After some issues I > > got it to compile but now it is unable to import the files that it > > needs. > > > The program is running the following code... > > PyImport_Import( PyString_FromString("python.PlayerManager") ); > > > This is meant to import the file PlayerManager.py inside of the python > > folder. However it throws the following Python Error (Gotten through > > PyErr_Print()) > > ImportError: No module named python.PlayerManager > > > I am using 2.6.4 so I can't call it by the filename, does anyone know > > how to do a proper import? > > -- > >http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple question about Queue.Queue and threads
On Feb 5, 7:45 am, "Frank Millman" wrote: > Hi all > > Assume you have a server process running, a pool of worker threads to > perform tasks, and aQueue.Queue() to pass the tasks to the workers. > > In order to shut down the server cleanly, you want to ensure that the > workers have all finished their tasks. I like the technique of putting a > None onto thequeue, and have each worker check for None, put None back onto > thequeue, and terminate itself. > > The main program would look something like this - > > q.put(None) > for worker in worker_threads: > worker.join() > > At this point you can be sure that each thread has completed its tasks and > terminated itself. > > However, thequeueis not empty - it still has the final None in it. > > Is it advisable to finalise the cleanup like this? - > > while not q.empty(): > q.get() > q.task_done() > q.join() > > Or is this completely redundant? > > Thanks > > Frank Millman Queue objects have support for this signaling baked in with q.task_done and q.join. After the server process has put all tasks into the queue, it can join the queue itself, not the worker threads. q.join() This will block until all tasks have been gotten AND completed. The worker threads would simply do this: task_data = q.get() do_task(task_data) q.task_done() Using pairs of get and task_done you no longer need to send a signal. Just exit the server process and the worker threads will die (assuming of course, you set .setDaemon(True) before starting each worker thread). Steven Rumbalski -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
Klaus Neuner wrote: Hello, I am writing a program that analyzes files of different formats. I would like to use a function for each format. Obviously, functions can be mapped to file formats. E.g. like this: if file.endswith('xyz'): xyz(file) elif file.endswith('abc'): abc(file) ... Yet, I would prefer to do something of the following kind: func = file[-3:] apply_func(func, file) As mentioned, a dictionary dispatch will do what you want, but you can also use the self-registering technique outlined here: http://effbot.org/zone/metaclass-plugins.htm [Fredrik Lundh] (For Plugin, read Handler in this case.) One idea might be to have Handler classes such as: class TextHandler(HandlerType): extensions = ['', 'txt', 'rst'] def run(self, *args, **kw): do stuff then the __init__ of HandlerType's metaclass: def __init__(cls, name, bases, attrs): for ext in attrs.get('extensions', []): registry[ext] = cls then use like: registry['txt']().run() If you don't need state, you could perhaps make 'run' a staticmethod and store it rather than the class, eg. registry[ext] = cls.run and then just: registry['txt']() hth G.F registry = {} class HandlerType(object): class __metaclass__(type): def __init__(cls, name, bases, attrs): for ext in attrs.get('extensions', []): registry[ext] = cls class TextHandler(HandlerType): extensions = ['', 'txt'] print registry -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
* Steve Holden: Alf P. Steinbach wrote: * Steve Holden: [...] Alf: This topic was discussed at great, nay interminable, length about a year ago. I'd appreciate it if you would search the archives and read what was said then rather than hashing the whole topic over again to nobody's real advantage. Well that's my point, and thanks for backing me up on that :-): it's very simple, and as demonstrated can be expressed in 10 words or less (plus perhaps a terminology reference, as I did above), so all that discussion and in particular the lengthy article at effbot serves as obfuscation and nothing else. Please don't assume I was trying to support you. Your remarks showed considerable ignorance of issue that were extremely nuanced. Whatever point you were trying to make was lost in your self-aggrandizing disrespect of Fredrik Lundh, a software engineer of some repute with a long history of contribution to Python. The fact that your post was basically a restatement of one of the several competing positions in that thread makes it no more right than any of the others. What on Earth are you babbling about? Perhaps next you'll drag in the Pope, saying I've shown him some disrespect. News for you: the Pope ain't participating. By the way, most every programming language has some corner like that, something that is utterly simple but somehow has some kind of obfuscation-meme attached. Why thank you for the education. Somehow in my 40-odd years of programming I had quite overlooked that fact. Which helps how? In C++ it's "call" and "constructor". It doesn't help that the language's standard lays down the law on it, it doesn't help that the language's creator has laid down the law, it doesn't help that it's utterly and completely simple. Somehow newbies and even some experienced people manage to create their own terminological nightmare and drawing conclusions about reality from that misguided obfuscated view, and then discussing it up and down and sideways. Which IMHO you have done little to assist. Just how exactly *do* we succeed in asking you not to discuss something? That's just a silly as the above. Hello? *Not* discuss an on-topic topic? Anyways, we haven't reached discussion yet, if it will ever come to that. All I see is a lot of silly noise about a simple trivially true technical statement, with incoherent allegations of disrespect to the Pope, the current king of France, and whomever, unfortunately as usual in this group. :-( Cheers & hth., - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: method names nounVerb or verbNoun
On Feb 5, 5:21 pm, Wanderer wrote: > On Feb 5, 4:53 pm, Chris Rebert wrote: > > > > > On Fri, Feb 5, 2010 at 1:49 PM, Wanderer wrote: > > > On Feb 5, 3:26 pm, Chris Rebert wrote: > > >> On Fri, Feb 5, 2010 at 11:53 AM, Wanderer > > >> wrote: > > >> > Which is the more accepted way to compose method names nounVerb or > > >> > verbNoun? > > > >> > For example voltageGet or getVoltage? getVoltage sounds more normal, > > >> > but voltageGet is more like voltage.Get. I seem to mix them and I > > >> > should probably pick one way and stick with it. > > > >> Use properties[1] and just call it `voltage`. Python is not Java [2]; > > >> explicit getters/setters are unpythonic. > > > >> [1]http://docs.python.org/library/functions.html#property > > > > Maybe I'm not using Get right either. I'm wrapping functions. > > > > def AbbeGet(self, Lens): > > > """ > > > Get the Abbe Number V for the material > > > where > > > V = (Nd - 1)/(Nf - Nc) > > > where the Index of Refractions are > > > Nd at the Fraunhofer D line 0.5892um > > > Nf at the Fraunhofer F line 0.4861um > > > Nc at the Fraunhofer C line 0.6563um > > > """ > > > > Nd = Lens.Mat.NtGet(0.5892) > > > Nf = Lens.Mat.NtGet(0.4861) > > > Nc = Lens.Mat.NtGet(0.6563) > > > > if (Nf - Nc) != 0: > > > V = (Nd - 1)/(Nf - Nc) > > > else: > > > V = 1e6 > > > > return V > > > This isn't even really a method; you don't refer to "self" in the body > > at all. Why isn't it just a function? > > > Cheers, > > Chris > > --http://blog.rebertia.com > > Okay, I guess it should be a function called abbe() instead of a > method called AbbeGet(). That should solve the problem of trying to > remember whether I called it GetAbbe or AbbeGet. > > Thanks Actually I decided it still should be a method, just not of doublet but of Lens. so the calls change from V1 = self.AbbeGet(Lens1) V2 = self.AbbeGet(Lens2) V1 = self.Lens1.abbe() V2 = self.Lens2.abbe() -- http://mail.python.org/mailman/listinfo/python-list
Re: Create a backslash-escaped version of a string?
On Feb 8, 12:28 pm, Chris Rebert wrote: > print a.encode("string-escape") How could I miss that? I was on that doc page already. Should have typed "/escape" in the browser ;-) Thanks, robert -- http://mail.python.org/mailman/listinfo/python-list
Re: [PyOpenGL-Users] Mouse wheel events?
Craig Berry wrote: On Sun, Feb 7, 2010 at 22:26, Gary Herron wrote: Didn't I answer this already? If you did, for whatever reason I didn't see it; I just rechecked my inbox to be sure. Thanks for doing so again! I assume, given the list we're on, that Freeglut can be used with Python. I'll look into it. Thanks for the pointer! Yes, easily. PyOpenGL has bindings for all the procedures in GLUT/FREEGLUT. Just tell PyOpenGL to use the freeglut library instead, or even just copy freeglut (.dll or .so or whatever) over your existing glut library (glut32.dll or whatever). It really can serve as a binary drop in replacement. Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: Overcoming python performance penalty for multicore CPU
Paul Rubin writes: > Stefan Behnel writes: >> Well, if multi-core performance is so important here, then there's a pretty >> simple thing the OP can do: switch to lxml. >> >> http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/ > > Well, lxml is uses libxml2, a fast XML parser written in C, but AFAIK it > only works on well-formed XML. The point of Beautiful Soup is that it > works on all kinds of garbage hand-written legacy HTML with mismatched > tags and other sorts of errors. Beautiful Soup is slower because it's > full of special cases and hacks for that reason, and it is written in > Python. Writing something that complex in C to handle so much > potentially malicious input would be quite a lot of work to write at > all, and very difficult to ensure was really safe. Look at the many > browser vulnerabilities we've seen over the years due to that sort of > problem, for example. But, for web crawling, you really do need to > handle the messy and wrong HTML properly. If the difference is great enough, you might get a benefit from analyzing all pages with lxml and throwing invalid pages into a bucket for later processing with BeautifulSoup. -- http://mail.python.org/mailman/listinfo/python-list
Re: threading+popen2 hang
In article <2542cf60-a193-4087-a1fe-1d60ee13c...@v25g2000yqk.googlegroups.com>, lofic wrote: >On 7 f=E9v, 17:00, a...@pythoncraft.com (Aahz) wrote: >> In article <188bfb67-3334-4325-adfc-3fa4d28f0...@d27g2000yqn.googlegroups= >.com>, >> lofic =A0 wrote: >>> >>>Works fine on RHEL5/python 2.4.3 >>>Hangs on RHEL4/python 2.3.4 >> >> Then use Python 2.4 -- surely you don't expect anyone to provide bugfixes >> for a release that's several years old? > >2.3 is the version provided with RHEL 4, which is still widely used in >production environments. Sometimes it is not so easy to shift to >another version in production systems. Believe me, I'm well aware of that (my last job was also stuck on 2.3). Nevertheless, the pool of people familiar with the minutiae of bugs for both Python 2.3 and 2.4 is dwindling daily, and your best recourse is intensive Googling. -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ import antigravity -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing Commands From Windows Service
On Feb 8, 1:28 am, Sean DiZazzo wrote: > On Feb 7, 4:57 pm, T wrote: > > > Thanks for the suggestions - I think my next step is to try running > > it under an admin user account, as you guys both mentioned. Alf - > > you're absolutely right, Microsoft has srvany.exe, which allows you to > > run any EXE as a Windows service. I've done this in the past, but > > it's more of a "hack"..so this go around (since I will be distributing > > this program), I wanted to go the more professional route..which, > > unfortunately, involves learning the "scum". :) I posted this to > > comp.os.ms-windows.programmer.win32, so we'll see if what the Win32 > > programmers have to say as well. Thanks again! > > I use windows services and they are very reliable. I would say though > that relying on plink.exe is much less reliable than either python or > the service that it is running under. > > Why not take a look at paramiko as the ssh client library? I think it > runs under windows. Also perhaps Twisted has something. Either way > would be light years ahead of using subprocess with plink. > > Just my thoughts. > > ~Sean I totally agree that it would be much more reliable to use a Python library for SSH - however, the program will need to execute other external binaries as well. So my goal at this point is to track down _why_ it's failing when running as a service. The actual command is as follows: C:\plink.exe -R :127.0.0.1:2020 -batch -i C:\keyfile.ppk u...@10.10.10.1 I tried having subprocess.Popen run plink.exe by itself and piping output to file, and this worked - so I know it's at least executing plink.exe. Sorry, I realize this isn't truly just a Python-related question, but any help would be greatly appreciated! So far no help at comp.os.ms-windows.programmer.win32.. -- http://mail.python.org/mailman/listinfo/python-list
Re: C/C++ Import
On 2/7/2010 10:56 PM, 7H3LaughingMan wrote: To make the background information short, I am trying to take a program that uses Python for scripting and recompile it for Linux since it originally was built to run on Win32. The program itself was designed to be able to be compiled on Linux and someone made there on release with source that added python scripting. After some issues I got it to compile but now it is unable to import the files that it needs. The program is running the following code... PyImport_Import( PyString_FromString("python.PlayerManager") ); This is meant to import the file PlayerManager.py inside of the python folder. However it throws the following Python Error (Gotten through PyErr_Print()) ImportError: No module named python.PlayerManager I am using 2.6.4 so I can't call it by the filename, does anyone know how to do a proper import? Your 'python' package directory must be in a directory listed in sys.path. I would print that check. -- http://mail.python.org/mailman/listinfo/python-list
Re: Overcoming python performance penalty for multicore CPU
On Mon, 2010-02-08 at 01:10 -0800, Paul Rubin wrote: > Stefan Behnel writes: > > Well, if multi-core performance is so important here, then there's a pretty > > simple thing the OP can do: switch to lxml. > > > > http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/ > > Well, lxml is uses libxml2, a fast XML parser written in C, but AFAIK it > only works on well-formed XML. The point of Beautiful Soup is that it > works on all kinds of garbage hand-written legacy HTML with mismatched > tags and other sorts of errors. Beautiful Soup is slower because it's > full of special cases and hacks for that reason, and it is written in > Python. Writing something that complex in C to handle so much > potentially malicious input would be quite a lot of work to write at > all, and very difficult to ensure was really safe. Look at the many > browser vulnerabilities we've seen over the years due to that sort of > problem, for example. But, for web crawling, you really do need to > handle the messy and wrong HTML properly. > Actually, lxml has an HTML parser which does pretty well with the standard level of broken one finds most often on the web. And, when it falls down, it's easy to integrate BeautifulSoup as a slow backup for when things go really wrong (as J Kenneth King mentioned earlier): http://codespeak.net/lxml/lxmlhtml.html#parsing-html At least in my experience, I haven't actually had to parse anything that lxml couldn't handle yet, however. -- John Krukoff Land Title Guarantee Company -- http://mail.python.org/mailman/listinfo/python-list
Re: Your beloved python features
Steven D'Aprano wrote: On Fri, 05 Feb 2010 18:29:07 +0100, mk wrote: Ethan Furman wrote: http://www1.american.edu/academic.depts/cas/econ/faculty/isaac/ choose_python.pdf Choose to get your difficult questions about threads in Python ignored. Oh well.. With an attitude like that, you're damn lucky if you don't get kill- filed. It was SIX MINUTES since you posted your question about timers, what did you expect? I didn't expect immediate answer to this particular question. I have repeatedly asked many questions about threads in the past, and IIRC every one of them was ignored. Threads are hard, and many people don't use them at all. You might never get an answer, even without alienating people. Complaining after six DAYS might be acceptable, if you do it with a sense of humour, but after six minutes? Well, it's 4 days now. I would be happy to get 50% response rate. Apparently nobody is really using threads. regards, mk -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
On Feb 8, 4:00 am, Duncan Booth wrote: > T wrote: > > Oops, this one was my fault - the object I was having the issues with > > was actually a shelve file, not a dictionary..so just re-assigning the > > variable isn't working, but re-writing the object to the shelve file > > does. So in this case, is there any way to just change a single > > value, or am I stuck rewriting the entry? > > Either open the shelve with writeback=True or rewrite the entry. > Rewriting the entry isn't hard: you can just re-use the same value: > > def changevalue(): > for key in mytest.keys(): > temp = mytest[key] > temp.param3 = "newvalue" > mytest[key] = temp > > If you really are changing every item in the shelve using writeback=True > will be much simpler, but if you are only changing a few then just tell the > shelve that you've updated them as above. > > -- > Duncan Boothhttp://kupuguy.blogspot.com Duncan - Thanks for your help. So each of the shelve entries I'm modifying look something like this: myshelve[key] = TestClassObject(param1, param2, param3, param4, param5, etc.). In this case, with quite a few parameters, would you suggest setting writeback=True and just modifying the needed value, or rewriting the entire entry? I have to admit the writeback option looks good, and if the TestClassObject format ever changes (i.e. if I ever change the order of the params), this particular function will be unchanged. However, I don't know what the performance hit would be in using it. -- http://mail.python.org/mailman/listinfo/python-list
Re: intolerant HTML parser
Stefan Behnel wrote: > I don't read it that way. There's a huge difference between > > - generating HTML manually and validating (some of) it in a unit test > > and > > - generating HTML using a tool that guarantees correct HTML output > > the advantage of the second approach being that others have already done > all the debugging for you. Anyone TDDing around HTML or XML should use or fork my assert_xml() (from django-test-extensions). The current version trivially detects a leading tag and uses etree.HTML(xml); else it goes with the stricter etree.XML(xml). The former will not complain about the provided sample HTML. Sadly, the industry has such a legacy of HTML written in Notepad that well-formed (X)HTML will never be well-formed XML. My own action item here is to apply Stefan's parser_options suggestion to make the etree.HTML() stricter. However, a generator is free to produce arbitrarily restricted XML that avoids the problems with XHTML. It could, for example, push any Javascript that even dreams of using & instead of & out into .js files. So an assert_xml() hot-wired to process only XML - with the true HTML doctype - is still useful to TDD generated code, because its XPath reference will detect that you get the nodes you expect. -- Phlip http://c2.com/cgi/wiki?ZeekLand -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
T wrote: > Duncan - Thanks for your help. So each of the shelve entries I'm > modifying look something like this: myshelve[key] > TestClassObject(param1, param2, param3, param4, param5, etc.). In > this case, with quite a few parameters, would you suggest setting > writeback=True and just modifying the needed value, or rewriting the > entire entry? I have to admit the writeback option looks good, and if > the TestClassObject format ever changes (i.e. if I ever change the > order of the params), this particular function will be unchanged. > However, I don't know what the performance hit would be in using it. Only you know your data and pattern of use. Try a few tests to get a feel of the trade-offs between explicitly flagging items as changed or using writeback=True. -- http://mail.python.org/mailman/listinfo/python-list
Re: intolerant HTML parser
and the tweak is: parser = etree.HTMLParser(recover=False) return etree.HTML(xml, parser) That reduces tolerance. The entire assert_xml() is (apologies for wrapping lines!): def _xml_to_tree(self, xml): from lxml import etree self._xml = xml try: if ' 0, xpath + ' not found in ' + self._xml) node = nodes[0] if kw.get('verbose', False): self.reveal_xml(node) # "here have ye been? What have ye seen?"--Morgoth return node def reveal_xml(self, node): 'Spews an XML node as source, for diagnosis' from lxml import etree print etree.tostring(node, pretty_print=True) # CONSIDER does pretty_print work? why not? def deny_xml(self, xml, xpath): 'Check that a given extent of XML or HTML does not contain a given XPath' tree = self._xml_to_tree(xml) nodes = tree.xpath(xpath) self.assertEqual(0, len(nodes), xpath + ' should not appear in ' + self._xml) -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
Am 08.02.10 02:51, schrieb Alf P. Steinbach: * Chris Rebert: On Sun, Feb 7, 2010 at 5:05 PM, T wrote: Ok, just looking for a sanity check here, or maybe something I'm missing. I have a class Test, for example: class Test: def __init__(self, param1, param2, param3): self.param1 = param1 self.param2 = param2 self.param3 = param3 Next, I have a dictionary mytest that contains instances of Test. If I want to modify one of the Test instances within my dictionary, I have to rewrite the entire entry, correct (since Python passes by value, not reference)? Incorrect; Python uses neither. See http://effbot.org/zone/call-by-object.htm for a excellent explanation of what Python does use. Hm. While most everything I've seen at effbot.org has been clear and to the point, that particular article reads like a ton of obfuscation. Python passes pointers by value, just as e.g. Java does. There, it needed just 10 words or so. :-) Or perhaps some more words to point out that in the Java language spec those reference values are called pointers, but that this terminology isn't (apparently) used for Python, and isn't even well known among Java programmers. But that's just one extra little para. One just has to be clear about exactly what it is that's passed by value. Not Python objects, but references (pointers) to them, the id(o) values. Whao. You just needed 10 words, plus a paragraph to explain something in terms of a spec that's just about 600 pages strong. Amazing display of conciseness, and certainly the most valuable thing for some programming newbie to work with. Thanks! Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
* Diez B. Roggisch: Am 08.02.10 02:51, schrieb Alf P. Steinbach: * Chris Rebert: On Sun, Feb 7, 2010 at 5:05 PM, T wrote: Ok, just looking for a sanity check here, or maybe something I'm missing. I have a class Test, for example: class Test: def __init__(self, param1, param2, param3): self.param1 = param1 self.param2 = param2 self.param3 = param3 Next, I have a dictionary mytest that contains instances of Test. If I want to modify one of the Test instances within my dictionary, I have to rewrite the entire entry, correct (since Python passes by value, not reference)? Incorrect; Python uses neither. See http://effbot.org/zone/call-by-object.htm for a excellent explanation of what Python does use. Hm. While most everything I've seen at effbot.org has been clear and to the point, that particular article reads like a ton of obfuscation. Python passes pointers by value, just as e.g. Java does. There, it needed just 10 words or so. :-) Or perhaps some more words to point out that in the Java language spec those reference values are called pointers, but that this terminology isn't (apparently) used for Python, and isn't even well known among Java programmers. But that's just one extra little para. One just has to be clear about exactly what it is that's passed by value. Not Python objects, but references (pointers) to them, the id(o) values. Whao. You just needed 10 words, plus a paragraph to explain something in terms of a spec that's just about 600 pages strong. Amazing display of conciseness, and certainly the most valuable thing for some programming newbie to work with. Thanks! I apologize for assuming that "pointer" is a known word to [c.l.p.] denizens. But, if it can help, although for clarity I had to provide a concrete reference, you don't need to read the complete Java language spec to understand that word. Just Google it. Cheers & hth., - ALf -- http://mail.python.org/mailman/listinfo/python-list
Re: Import question
En Mon, 08 Feb 2010 06:37:53 -0300, Andrew Degtiariov escribió: 2010/2/6 Gabriel Genellina En Fri, 05 Feb 2010 13:21:47 -0300, Andrew Degtiariov escribió: Code of our project has split into several packages and we deploy the project using buildout. All worked fine until I need to dynamically inspect python modules. Entirely by luck, I'd say :) ├───project.api.config │ ├───project │ │ └───api │ │ └───config │ │ └───settings │ └───project.api.config.egg-info ├───project.api.contacts │ ├───project │ │ └───api │ │ └───contacts │ │ ├───importer │ │ └───views │ └───project.api.contacts.egg-info If someone is interesting - __import__ works but imp doesn't. In my example: m = __import__('project.api.config') m m.api m.api.config Please note that the m and m.api pointed to first installing package with namespace project.api but m.api.config have pointed to the proper file. And releasing code with several distributions it is one of goals of Distribute. And you might look to pypi for... zope. There is lot of package which names starts from "zope." It's really convenient Those are called namespace packages. Zope and Plone (ab)use them extensively. The intended usage is to break up a big, monolithic package [0] in parts that can be distributed independently. To implement a namespace package, you need an empty __init__.py file with only these lines [1]: from pkgutil import extend_path __path__ = extend_path(__path__, __name__) But think carefully if you really need namespace packages; they solve a specific problem, aren't a general purpose technique. See [2] for a discussion. [0] Think of a huge behemoth with a "Z O P E" sign on both sides :) [1] http://docs.python.org/library/pkgutil.html [2] http://weblion.psu.edu/news/are-we-overusing-namespace-packages -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
On 8 fév, 11:57, Klaus Neuner wrote: > Hello, > > I am writing a program that analyzes files of different formats. I > would like to use a function for each format. Obviously, functions can > be mapped to file formats. E.g. like this: > > if file.endswith('xyz'): > xyz(file) > elif file.endswith('abc'): > abc(file) > > ... > > Yet, I would prefer to do something of the following kind: > > func = file[-3:] > apply_func(func, file) > > Can something of this kind be done in Python? and with eval(), did you try ? import sys def functext(): print "texte" def funcdoc(): print "doc" def funcabc(): print "abc" if __name__ == "__main__": #replace filename with suitable value filename = sys.argv[1].split('.')[1] try: eval('func' + filename + '()') except: print 'error' -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
OdarR wrote: On 8 fév, 11:57, Klaus Neuner wrote: Hello, I am writing a program that analyzes files of different formats. I would like to use a function for each format. Obviously, functions can be mapped to file formats. E.g. like this: if file.endswith('xyz'): xyz(file) elif file.endswith('abc'): abc(file) ... Yet, I would prefer to do something of the following kind: func = file[-3:] apply_func(func, file) Can something of this kind be done in Python? I may have missed a bit of this thread -- so I have to ask: Has anyone mentioned using getattr yet? It's a way of looking up *any* attribute using a string to specify the name. Like this for your particular example: class Functions: # This could be a module instead of a class def xyz(...): ... def abc(...): ... ... and so on ... ext = os.path.splitext(file) # Parses out the extension fn = getattr(Functions, ext) # Lookup the correct function fn(...) # and call it Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
setuptools/distutil and pywin32
Hi, I have a program which requires pywin32 as a dependancy and I'd like to make setuptools automatically retrieve it from internet and install it. My setup.py script looks like this: from setuptools import setup setup( ... ... install_requires = ['pywin32'] ... ) When I run setup.py install I get this error: Processing dependencies for psutil==0.1.3 Searching for pywin32 Reading http://pypi.python.org/simple/pywin32/ Reading http://sf.net/projects/pywin32 Reading http://sourceforge.net/project/showfiles.php?group_id=78018 Best match: pywin32 214 Downloading http://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/pywin32-214.win32-py2.6.exe download Processing download error: Not a recognized archive type: c:\users\giampa~1\appdata\local \temp\easy_ install-xefzrd\download I've googled a lot about this but it seems that as of right now it's just not possible to install pywin32 via setuptools but it seems pretty strange to me as it is a very important third party module. Can someone confirm to me that there's no way to solve this problem? Thanks in advance, Giampaolo http://code.google.com/p/psutil http://code.google.com/p/pyftpdlib -- http://mail.python.org/mailman/listinfo/python-list
Re: Your beloved python features
On Mon, Feb 8, 2010 at 12:07 PM, mk wrote: > Steven D'Aprano wrote: >> >> On Fri, 05 Feb 2010 18:29:07 +0100, mk wrote: >> >>> Ethan Furman wrote: >>> http://www1.american.edu/academic.depts/cas/econ/faculty/isaac/ >> >> choose_python.pdf >>> Choose to get your difficult questions about threads in Python ignored. >>> Oh well.. >> >> With an attitude like that, you're damn lucky if you don't get kill- >> filed. It was SIX MINUTES since you posted your question about timers, >> what did you expect? > > I didn't expect immediate answer to this particular question. I have > repeatedly asked many questions about threads in the past, and IIRC every > one of them was ignored. > >> >> Threads are hard, and many people don't use them at all. You might never >> get an answer, even without alienating people. Complaining after six DAYS >> might be acceptable, if you do it with a sense of humour, but after six >> minutes? > > Well, it's 4 days now. I would be happy to get 50% response rate. Apparently > nobody is really using threads. > > regards, > mk I use threads. Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Does anyone have a Python Logic Map/Flow Chart? (Example Provided)
Has anyone been able to come across a Python logic map or flow chart? An example can be seen here on the right: http://en.wikipedia.org/wiki/Usenet This would be very helpful for users. -- http://mail.python.org/mailman/listinfo/python-list
Python Logic Map/Logic Flow Chart. (Example Provided)
Has anyone been able to come across a Python logic map or Python logic flow chart? An example can be seen on the right under History: http://en.wikipedia.org/wiki/Usenet#History This would be very helpful for all users. -- http://mail.python.org/mailman/listinfo/python-list
Awful book warning: How to think like a (Python) programmer - non-working examples
Page 7: Very first example doesn't compile: syntax error Pate 11: 2nd example: syntax error Page 12, printing digits: syntax error Page 13, printing a number: syntax error page 14, statements: syntax error -- http://mail.python.org/mailman/listinfo/python-list
Re: Awful book warning: How to think like a (Python) programmer - non-working examples
On Mon, Feb 8, 2010 at 3:36 PM, Dave Peterson wrote: > Page 7: Very first example doesn't compile: syntax error > Pate 11: 2nd example: syntax error > Page 12, printing digits: syntax error > Page 13, printing a number: syntax error > page 14, statements: syntax error > Let me guess, you're using Python 3.1. That book was written for Python 2.x and there were several backwards-incompatible changes. For instance, print was changed from a statement to a function. Which is why the "Hello, World" doesn't work any more. If you want to use the older books, use Python 2.6 instead. > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Awful book warning: How to think like a (Python) programmer - non-working examples
On 2010-02-08 14:36 PM, Dave Peterson wrote: Page 7: Very first example doesn't compile: syntax error Pate 11: 2nd example: syntax error Page 12, printing digits: syntax error Page 13, printing a number: syntax error page 14, statements: syntax error This book was written for the 2.x versions of Python. Are you using Python 3.1? Python changed some of its syntax for version 3.0, notably print "Hello, world!" becomes print("Hello, world!") This accounts for all of the SyntaxErrors that you are seeing. The examples aren't broken for the version of Python it is teaching. You may want to try _Dive Into Python 3_ to learn about Python 3 in particular: http://diveintopython3.org/ -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Awful book warning: How to think like a (Python) programmer - non-working examples
The book covers Python 2.x syntax. You might have downloaded Python 3.1, which has different syntax then Python 2.x. From what I can tell, the first example on page 7 is ">>> print 1 + 1". Try issuing this command: print(1 + 1) If everything goes well, and you get '2' as the answer, then you're probably using Python 3.x. You will have to download the Python 2.x binaries from the Python website, install Python 2.x, and try the example from the book again. -- http://mail.python.org/mailman/listinfo/python-list
glibc detected double free or corruption
I'm encountering the following error on my fastcgi web server and would greatly appreciate ANY pointers for debugging/fixing this problem. *** glibc detected *** /usr/bin/python2.5: double free or corruption (fasttop): 0x08b47d60 *** If I don't set MALLOC_CHECK_ then the server just hangs and the above message appears in the fastcgi error log. With MALLOC_CHECK_=0 I get an error message in the browser instead. I'm using the following components (on a shared hosting account): Debian lenny 64 bit Python 2.5.2 lighttpd 1.4.19 Django 1.1.1 latest flup (today's hg tip, flup-server-ae5fe54fba18) postgresql 8.3.9 psycopg2 2.0.13 What's interesting about the problem is it seems to happen only when new fcgi processes are spawned and only when a db query is made. That is, the frequency of the problem corresponds exactly with the maxrequests setting on my fcgi process. If I set maxrequests to 1 then I get this error for each request that hits the database. If I set it to 3 then I get the error with every third request, if that request hits the database. If I put Django into DEBUG and set MALLOC_CHECK_=0 then I get the following error with traceback: Exception Type: OperationalError Exception Value: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. Exception Location: /home/altaurog/django/db/backends/util.py in execute, line 19 Regardless of which url/view I request, the exception always is raised on a sql execute statement. --- Aryeh Leib Taurog -- http://mail.python.org/mailman/listinfo/python-list
Re: Awful book warning: How to think like a (Python) programmer - non-working examples
On Mon, 2010-02-08 at 12:53 -0800, Andrej Mitrovic wrote: > The book covers Python 2.x syntax. > > You might have downloaded Python 3.1, which has different syntax then > Python 2.x. From what I can tell, the first example on page 7 is ">>> > print 1 + 1". > > Try issuing this command: > print(1 + 1) > > If everything goes well, and you get '2' as the answer, then you're > probably using Python 3.x. You will have to download the Python 2.x > binaries from the Python website, install Python 2.x, and try the > example from the book again. Sorry to nitpick; the main thrust of the above sounds correct, in that: print 1 + 1 works in Python 2 but fails in Python 3, but, a minor correction, note that: print(1+1) does work in Python 2 as well as in Python 3; the parentheses are treated (in the former) as denoting grouping of a subexpression, rather than function invocation (in the latter): Python 2.6.2 (r262:71600, Jan 25 2010, 13:22:47) [GCC 4.4.2 20100121 (Red Hat 4.4.2-28)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print(1+1) 2 This can be useful if you're trying to write short fragments of code that work with both. Look at the startup message, or run this command, which should work on both python2 and python3: import sys; print(sys.version) Hope this is helpful Dave -- http://mail.python.org/mailman/listinfo/python-list
Re: Simulating logging.exception with another traceback
On Feb 7, 11:22 am, Joan Miller wrote: > I would want to get the output from `logging.exception` but with > traceback from the caller function (I've already all that > information). > > This would be the error withlogging.exception: > > ERROR: > PipeError('/bin/ls -l | ', 'no command after of pipe') > Traceback (most recent call last): > File "/data/neo/Proyectos/Python/Scripy/lib/scripy/shell.py", line > 160, in __call__ > raise PipeError(command, 'no command after of pipe') > PipeError: ('/bin/ls -l | ', 'no command after of pipe') > > > And I've trying it with: > > message = "File \"{0}\", line {1}, in {2}\n\n {3}".format( > file, line, function, caller)logging.error(message) > > ERROR: > File "/data/neo/Proyectos/Python/Scripy/lib/scripy/shell.py", line > 163, in __call__ return self.throw(PipeError, command, 'no > command after of pipe') > > > Could be used `logging.LogRecord` [1] to get it? How? > > [1]http://docs.python.org/library/logging.html#logging.LogRecord Sorry, Joan, I don't understand your question. Can you create a short script which throws an exception, and show exactly how you want it formatted? The logger.exception method does the same as logger.error, except that it prints exception trace information and is intended to be called from exception handling clauses. You can format exceptions how you like by subclassing Formatter and overriding formatException. Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
Ternary plus
Just for the hell of it ... I can easily define __plus__() with three parameters. If the last one is optional the + operation works as expected. Is there a way to pass the third argument to "+" -- http://mail.python.org/mailman/listinfo/python-list
Re: Ternary plus
On 2010-02-08 14:59 PM, Martin Drautzburg wrote: Just for the hell of it ... I can easily define __plus__() with three parameters. If the last one is optional the + operation works as expected. Is there a way to pass the third argument to "+" No. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Awful book warning: How to think like a (Python) programmer - non-working examples
On Feb 8, 10:14 pm, David Malcolm wrote: > On Mon, 2010-02-08 at 12:53 -0800, Andrej Mitrovic wrote: > > The book covers Python 2.x syntax. > > > You might have downloaded Python 3.1, which has different syntax then > > Python 2.x. From what I can tell, the first example on page 7 is ">>> > > print 1 + 1". > > > Try issuing this command: > > print(1 + 1) > > > If everything goes well, and you get '2' as the answer, then you're > > probably using Python 3.x. You will have to download the Python 2.x > > binaries from the Python website, install Python 2.x, and try the > > example from the book again. > > Sorry to nitpick; the main thrust of the above sounds correct, in that: > print 1 + 1 > works in Python 2 but fails in Python 3, but, a minor correction, note > that: > print(1+1) > does work in Python 2 as well as in Python 3; the parentheses are > treated (in the former) as denoting grouping of a subexpression, rather > than function invocation (in the latter): > > Python 2.6.2 (r262:71600, Jan 25 2010, 13:22:47) > [GCC 4.4.2 20100121 (Red Hat 4.4.2-28)] on linux2 > Type "help", "copyright", "credits" or "license" for more information.>>> > print(1+1) > > 2 > > This can be useful if you're trying to write short fragments of code > that work with both. > > Look at the startup message, or run this command, which should work on > both python2 and python3: > import sys; print(sys.version) > > Hope this is helpful > Dave Oops, you're right. I'm used to Python 3 syntax so I'm only aware of some basic differences. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
In article <0efe23a6-b16d-4f92-8bc0-12d056bf5...@z26g2000yqm.googlegroups.com>, OdarR wrote: > >and with eval(), did you try ? WARNING: eval() is almost always the wrong answer to any question -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ import antigravity -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Logic Map/Logic Flow Chart. (Example Provided)
spike wrote: Has anyone been able to come across a Python logic map or Python logic flow chart? An example can be seen on the right under History: http://en.wikipedia.org/wiki/Usenet#History This would be very helpful for all users. Huh??? What aspect of Python were you thinking of mapping out? Your example us a bad ascii art graph of -- I've no clue what? Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: Import question
Those are called namespace packages. Zope and Plone (ab)use them > extensively. The intended usage is to break up a big, monolithic package > [0] in parts that can be distributed independently. To implement a > namespace package, you need an empty __init__.py file with only these > lines [1]: > > from pkgutil import extend_path > __path__ = extend_path(__path__, __name__) > > But think carefully if you really need namespace packages; they solve a > specific problem, aren't a general purpose technique. See [2] for a > discussion. > > [0] Think of a huge behemoth with a "Z O P E" sign on both sides :) > [1] http://docs.python.org/library/pkgutil.html > [2] http://weblion.psu.edu/news/are-we-overusing-namespace-packages > > Hm.. We are using pkg_resources.declare_namespace(__name__) but I think pkgutil is much better. And we are using buildout so the omelette might help me. Link [2] very interesting. Thank you, Gabrial. -- Andrew Degtiariov DA-RIPE -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
On 8 fév, 22:28, a...@pythoncraft.com (Aahz) wrote: > In article > <0efe23a6-b16d-4f92-8bc0-12d056bf5...@z26g2000yqm.googlegroups.com>, > > OdarR wrote: > > >and with eval(), did you try ? > > WARNING: eval() is almost always the wrong answer to any question warning : it works ! another question ? > -- > Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ > > import antigravity -- http://mail.python.org/mailman/listinfo/python-list
Read PGM's with more than 256 range in PIL1.1.7
I have a PGM format image file with 4096 range. When I reads it with PIL, I get an image with 8-bit values and alternate columns are zero. Does PIL support reading and writing PGM's with more than 8-bits? Davo -- http://mail.python.org/mailman/listinfo/python-list
Re: Stephen -- Bruce?
On Feb 8, 3:02 am, Stefan Behnel wrote: > Mensanator, 05.02.2010 00:36: > > > On Feb 4, 5:13 pm, "Alf P. Steinbach" wrote: > >> What's this about all the Stephen'ses here? > > >> Shouldn't it be Bruce? > > > Of course. We just call everyone Stephen to avoid confusion. > > Some people even manage to adapt the spellings accordingly. > > Stefan You appear to be confused. -- http://mail.python.org/mailman/listinfo/python-list
Re: equivalent of Ruby's Pathname?
In article , Sean DiZazzo wrote: >On Feb 3, 6:08=A0pm, alex23 wrote: >> >> There was also a PEP with another possible implementation: >> http://www.python.org/dev/peps/pep-0355/ > >Why did Path() get rejected? Is it the idea itself, or just the >approach that was used? What are the complaints? You should search for the discussiona around it. -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ import antigravity -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
In article <5790c33c-13d0-4596-91b0-b3c9aeebf...@f8g2000yqn.googlegroups.com>, OdarR wrote: >On 8 f=E9v, 22:28, a...@pythoncraft.com (Aahz) wrote: >> In article <0efe23a6-b16d-4f92-8bc0-12d056bf5...@z26g2000yqm.googlegroups= >.com>, >> OdarR =A0 wrote: >>> >>>and with eval(), did you try ? >> >> WARNING: eval() is almost always the wrong answer to any question > >warning : it works ! Works for what? -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ import antigravity -- http://mail.python.org/mailman/listinfo/python-list
Re: Your beloved python features
geremy condra wrote: On Mon, Feb 8, 2010 at 12:07 PM, mk wrote: Steven D'Aprano wrote: On Fri, 05 Feb 2010 18:29:07 +0100, mk wrote: Ethan Furman wrote: http://www1.american.edu/academic.depts/cas/econ/faculty/isaac/ choose_python.pdf Choose to get your difficult questions about threads in Python ignored. Oh well.. With an attitude like that, you're damn lucky if you don't get kill- filed. It was SIX MINUTES since you posted your question about timers, what did you expect? I didn't expect immediate answer to this particular question. I have repeatedly asked many questions about threads in the past, and IIRC every one of them was ignored. Threads are hard, and many people don't use them at all. You might never get an answer, even without alienating people. Complaining after six DAYS might be acceptable, if you do it with a sense of humour, but after six minutes? Well, it's 4 days now. I would be happy to get 50% response rate. Apparently nobody is really using threads. regards, mk I use threads. So do I, where appropriate. -- http://mail.python.org/mailman/listinfo/python-list
Re: equivalent of Ruby's Pathname?
On Feb 8, 2:36 pm, a...@pythoncraft.com (Aahz) wrote: > >> There was also a PEP with another possible implementation: > >>http://www.python.org/dev/peps/pep-0355/ > > >Why did Path() get rejected? Is it the idea itself, or just the > >approach that was used? What are the complaints? > > You should search for the discussiona around it. I, OTOH, am burning rubber with Python 2.6.1, so leading edge concerns are not mine - yet! I went with this version, generously ripped out & plopped into our project: # URL: http://www.jorendorff.com/articles/python/path # Author: Jason Orendorff (and others - see the url!) # Date:7 Mar 2004 class path(_base): """ Represents a filesystem path. """ Gods bless http://www.google.com/codesearch, huh?! -- Phlip http://c2.com/cgi/wiki?ZeekLand -- http://mail.python.org/mailman/listinfo/python-list
Re: Your beloved python features
In article <28c6967f-7637-4823-aee9-15487e1ce...@o28g2000yqh.googlegroups.com>, Julian wrote: > >I want to design a poster for an open source conference, the local >usergroup will have a table there, and in the past years there were >some people that came to the python-table just to ask "why should I >use python?". Readability -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ import antigravity -- http://mail.python.org/mailman/listinfo/python-list
Re: Building a multiline string
In article , lallous wrote: > >x = ( >"line1" # can use comments >"line2" >"line3" >) You should indent the second and following lines (I changed the name to "xyz" to make clear that the following lines use a regular Python indent rather than lining up under the open paren): xyz = ( "line1" # can use comments "line2" "line3" ) -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ import antigravity -- http://mail.python.org/mailman/listinfo/python-list
ANN: obfuscate
I am pleased to announce the first public release of obfuscate 0.2.2a. http://pypi.python.org/pypi/obfuscate/0.2.2a obfuscate is a pure-Python module providing classical encryption algorithms suitable for obfuscating and unobfuscating text. obfuscate includes the following ciphers: - Caesar, rot13, rot5, rot18, rot47 - atbash - Playfair, Playfair6 and Playfair16 - Railfence (encryption only) - Keyword - Affine - Vigenere - frob (xor) and others. DISCLAIMER: obfuscate is not cryptographically strong, and should not be used where high security is required. (The ciphers provided in obfuscate may have been state of the art centuries ago, but should not be used where strong encryption is required. obfuscate is released under the MIT licence. Requires Python 2.5 or 2.6. -- Steven D'Aprano -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: obfuscate
Steven D'Aprano schrieb: > I am pleased to announce the first public release of obfuscate 0.2.2a. > > http://pypi.python.org/pypi/obfuscate/0.2.2a > > obfuscate is a pure-Python module providing classical encryption > algorithms suitable for obfuscating and unobfuscating text. > > obfuscate includes the following ciphers: > - Caesar, rot13, rot5, rot18, rot47 > - atbash > - Playfair, Playfair6 and Playfair16 > - Railfence (encryption only) > - Keyword > - Affine > - Vigenere > - frob (xor) Nice work! Your work should be interesting for everybody who has read Simon Sing's "The Code Book: The Science of Secrecy from Ancient Egypt to Quantum". Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: equivalent of Ruby's Pathname?
On Feb 4, 7:10 pm, Sean DiZazzo wrote: > On Feb 3, 6:08 pm, alex23 wrote: > > > > > On Feb 4, 8:47 am, Phlip wrote: > > > > Yes, calling os.path.walk() and os.path.join() all the time on raw > > > strings is fun, but I seem to recall from my Ruby days a class called > > > Pathname, which presented an object that behaved like a string at > > > need, and like a filesystem path at need. path + 'folder' would > > > call .join() and insert the / correctly, for example. > > > > What's the best equivalent in Python-land? > > > It's no longer supported, but the 3rd party 'path' module used to be > > the go-to module for this: > > > >>> from path import path > > > C:\Python26\lib\site-packages\path.py:32: DeprecationWarning: the md5 > > module is deprecated; use hashlib instead > > import sys, warnings, os, fnmatch, glob, shutil, codecs, md5>>> c = > > path('C:\\') > > >>> c.listdir() > > > [path(u'C:\\AUTOEXEC.BAT'), path(u'C:\\boot.ini'), ...]>>> (c + > > 'python26').listdir() > > > [path(u'C:\\python26\\circuits.pth_disabled_for_egg'), path(u'C:\ > > \python26\\DLLs'), ...]>>> (c / 'python26').listdir() > > > [path(u'C:\\python26\\circuits.pth_disabled_for_egg'), path(u'C:\ > > \python26\\DLLs'), ...] > > > I've hand edited the results for brevity... > > > The module could do with some TLC to bring it up to date, but warning > > aside it still seems to work fine under 2.6. > > > (From memory, I think the original author gave up when it became clear > > it would never be integrated into the standard lib[1], which is a > > shame, I think there's scope for a pathtools addition to the lib that > > provides this level of convenience...) > > > There was also a PEP with another possible > > implementation:http://www.python.org/dev/peps/pep-0355/ > > > Hope this helps. > > It's too bad that something like this can't be agreed to. I used a > homegrown module like this for a couple of years in my early days with > python. It was great, but I didn't know enough at the time to make it > really useful. > > Why did Path() get rejected? Is it the idea itself, or just the > approach that was used? What are the complaints? I don't know if it was the reason it was rejected, but a seriously divisive question is whether the path should be a subset of string. Under ordinary circumstances it would be a poor choice for inheritance (only a few string methods would be useful fot a pathname), but some people were fiercely adamant that paths should be passable to open() as-in (without having to explicity convert to string). IIRC, the guy who maintained path wavered between subclassing and not subclassing str. I don't remember if the idea of modifying open to accept path objects came up. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: obfuscate
On Mon, Feb 8, 2010 at 6:46 PM, Steven D'Aprano wrote: > I am pleased to announce the first public release of obfuscate 0.2.2a. > > http://pypi.python.org/pypi/obfuscate/0.2.2a > > obfuscate is a pure-Python module providing classical encryption > algorithms suitable for obfuscating and unobfuscating text. > > obfuscate includes the following ciphers: > - Caesar, rot13, rot5, rot18, rot47 > - atbash > - Playfair, Playfair6 and Playfair16 > - Railfence (encryption only) > - Keyword > - Affine > - Vigenere > - frob (xor) > > and others. > > DISCLAIMER: obfuscate is not cryptographically strong, and should not be > used where high security is required. (The ciphers provided in obfuscate > may have been state of the art centuries ago, but should not be used > where strong encryption is required. > > obfuscate is released under the MIT licence. > > Requires Python 2.5 or 2.6. > > > -- > Steven D'Aprano > -- > http://mail.python.org/mailman/listinfo/python-list > Nice! Maybe someday you can extend it with a pen-and-paper signature scheme ;) Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
* alex23: "Alf P. Steinbach" wrote: Hm. While most everything I've seen at effbot.org has been clear and to the point, that particular article reads like a ton of obfuscation. Must. Resist. Ad hominem. Python passes pointers by value, just as e.g. Java does. There, it needed just 10 words or so. :-) 10 words _plus_ an understanding of Java. No, one only needs an understanding of "pointer". "Pointer" is a mostly language independent concept. The reference to the Java language spec, where that term is used for this, was just an unsuccessful attempt to keep out word-play arguments based on the incompatibility of some incompatible meaning of "pointer"... Cheers & hth., - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: Read PGM's with more than 256 range in PIL1.1.7
According the pil manual it handles PGM files with "'1', 'L', or 'RGB' data" which leads me to believe 16bit data is not supported. You CAN write your own decoder for that though: http://www.pythonware.com/library/pil/handbook/decoder.htm It would be trivial to write a decoder for the pgm format. On Mon, Feb 8, 2010 at 4:47 PM, Davo wrote: > I have a PGM format image file with 4096 range. When I reads it with > PIL, I get an image with 8-bit values and alternate columns are zero. > Does PIL support reading and writing PGM's with more than 8-bits? > > Davo > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Your beloved python features
On Mon, 08 Feb 2010 18:07:56 +0100, mk wrote: >> Threads are hard, and many people don't use them at all. You might >> never get an answer, even without alienating people. Complaining after >> six DAYS might be acceptable, if you do it with a sense of humour, but >> after six minutes? > > Well, it's 4 days now. I would be happy to get 50% response rate. > Apparently nobody is really using threads. Please see my response to your post "timer for a function". -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: timer for a function
On Fri, 05 Feb 2010 18:23:09 +0100, mk wrote: [...] > On paramiko mailing list I got the suggestion to build a timer and then > quit this by myself: > >> The timeout option in connect() is for the socket, not for the entire >> operation. You are connected, so that timeout is no longer relevant. >> You would probably have to wrap the transport.connect() method in a >> timer to break out of this early. > > Question: how can I do that? Use another threaded class? Is there some > other way? I don't use threads enough (or at all) to answer that question directly, but I can point you at these. Hopefully they will help, or at least give you some ideas: http://code.activestate.com/recipes/534115/ http://code.activestate.com/recipes/473878/ Found by googling for "python timeout any function". > Additional snag is SSHClient() is a class that internally uses threads. > How do I kill brutally its threads? Is there any way to do it? You can't kill threads in Python. You have to ask them nicely to die. Google on "python kill thread" for more. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Ternary plus
On Mon, 08 Feb 2010 21:59:18 +0100, Martin Drautzburg wrote: > Just for the hell of it ... > > I can easily define __plus__() with three parameters. If the last one is > optional the + operation works as expected. Is there a way to pass the > third argument to "+" How do you give three operands to a binary operator? Binary operators only have two sides, a left and a right, so you can only fit two operands around them. Mathematicians solve this problem by using functions: add(a, b, c, d) In Python, you can do this: >>> class F: ... def __add__(self, other, foo=None): ... print self, other, foo ... return 1 ... >>> >>> F() + 3 <__main__.F instance at 0xb7f06f4c> 3 None 1 >>> F().__add__(3, 4) <__main__.F instance at 0xb7f06d8c> 3 4 1 but if you do, people will laugh and point at you in the street. *wink* -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: use strings to call functions
Aahz wrote: In article <0efe23a6-b16d-4f92-8bc0-12d056bf5...@z26g2000yqm.googlegroups.com>, OdarR wrote: and with eval(), did you try ? WARNING: eval() is almost always the wrong answer to any question Some say that eval is evil ! JM -- http://mail.python.org/mailman/listinfo/python-list