Febrl-0.3 released
Canberra, 7 April 2005 The ANU Data Mining Group is pleased to announce the release of Febrl 0.3, a prototype open source record linkage, deduplication and geocoding system intended to make probabilistic record linkage easier, faster and more accurate for biomedical and other researchers. The programs, known collectively as Febrl - Freely Extensible Biomedical Record Linkage - address the data cleaning and standardisation tasks which are essential first steps for most record linkage projects, and provide routines for probabilistic record linkage and record deduplication, as well as geocode matching based on the Australian G-NAF (Geocoded National Address File, www.g-naf.com.au) database. This fifth release Febrl Version 0.3 has been updated to Python 2.4 (also runs on Python 2.3). We would like to thank everybody who sent us bug-reports or other comments. The main features of the current release are: * Probabilistic and rules-based cleaning and standardisation routines for names, addresses, dates and telephone numbers. * A geocoding matching system based on the Australian G-NAF (Geocoded National Address File) database. * A variety of supplied look-up and frequency tables for names and addresses. * Various comparison functions for names, addresses, dates and localities, including approximate string comparisons, phonetic encodings, geographical distance comparisons, and time and age comparisons. Two new approximate string comparison methods (bag distance and compression based) have been added in this release. * Several blocking (indexing) methods, including the traditional compound key blocking used in many record linkage programs. * Probabilistic record linkage routines based on the classical Fellegi and Sunter approach, as well as a 'flexible classifier' that allows a flexible definition of the weight calculation. * Process indicators that give estimations of remaining processing times. * Access methods for fixed format and comma-separated value (CSV) text files, as well as SQL databases (MySQL and new PostgreSQL). * Efficient temporary direct random access data set based on the Berkeley database library. * Possibility to save linkage and deduplication results into a comma-separated value (CSV) text file (new). * One-to-one assignment procedure for linked record pairs based on the 'Auction' algorithm. * Supports parallelism for higher performance on parallel plat- forms, based on MPI (Message Passing Interface), a standard for parallel programming, and Pypar, an efficient and easy-to-use module that allows Python programs to run in parallel on multiple processors and communicate using MPI. * A data set generator which allows the creation of data sets of randomly generated records (containing names, addresses, dates, and phone and identifier numbers), with the possibility to include duplicate records with randomly introduced modifications. This allows for easy testing and evaluation of linkage (deduplication) processes. * Example project modules and example data sets allowing simple running of Febrl projects without any modifications needed. - An extensive 185 page manual. Febrl, which is written in the free open source Python programming language, is itself available under a free, open source license, which we hope will encourage others to contribute to its further development and support. Contact details, background information, documentation and, of course, the program code are all available from the project Web site at http://datamining.anu.edu.au/linkage.html as well as from 'sourceforge.net' at http://sourceforge.net/projects/febrl We would like to stress that the programs are still in the early stages of development, and we do not yet recommend them for production use, but we encourage you to try them and to provide us with feedback. We particularly welcome bug reports and ideas for future development. There are many ways to help with the project: testing, programming and software engineering, documentation and technical writing, translation, provision of (anonymous, non-confidential) training and example data sets, and testing. For the Febrl team, Peter Christen = Dr Peter Christen Lecturer / Graduate Advisor Department of Computer Science Faculty of Engineering and Information Technology CSIT Building (108), North Road The Australian National University Canberra ACT 0200 Australia T: +61 2 6125 5690 F: +61 2 6125 0010 W: http://cs.anu.edu.au/~Peter.Christen CRICOS Provider #00120C -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
[ANN] Voidspace Pythonutils Updates
Lots of updates to the Voidspace modules and recipes. Update to the Firedrop plugins see : http://www.voidspace.org.uk/python/programs.shtml#firedrop New version of FireSpell the spell checker (based on PyEnchant by Ryan Kelly) New plugin called FireMail which lets you send blog entries (or articles) by email - either as HTML or text. All the plugins are available for download. There is also docs on installing and using the plugins and docs on writing plugins. downman.py http://www.voidspace.org.uk/python/cgi.shtml#downman More improvements to downman.py - the simple download manager CGI 2005/03/15 Version 0.4.0 Configuration moved into external config file. HTML stuff mmoved into downman_templates.py Put quotes round filename (This stops firefox truncating filenames with spaces) Flush the output buffer as we serve the file (may help those who have to use env in the shebang line) guestbook.py http://www.voidspace.org.uk/python/guestbook.html Quite a big change - with a cleaning up of the default HTML templates and several new features to help combat guestbook spam. 2005/04/06 Version 1.3.0 Lots of restyling in the HTML templates - not *quite* so horrible now. We write new entries at the start of the file rather than the end (undoes a bug introduced in 1.2.0) Max of two URLs in an entry. A few extra words added to the banned list. (Still trying to combat guestbook spam) New value 'BREAKVAL' added to templates (so we can use the XHTML br / if needed) The '_charset_' field added to the form and value checked (in case page encoding changed by guests browser) Now use rel=nofollow in links to foil spammers Email addresses shown using javascript in an attempt to foil address harvesters. Obviously invalid email addresses and urls are removed. (very basic) Jalopy and Login Tools Not a huge amount of progress - but a few bugs fixed. http://www.voidspace.org.uk/python/jalopy.html http://www.voidspace.org.uk/python/logintools.html 2005/03/23 Admin levels are now preserved when new users are created or invited. Corrected bug causing accounts to not be editable. Changed all the shebang lines to '#! /usr/bin/python' Updated to latest versions of all the pythonutils modules (like ConfigObj) Fixed typo in HTML for editing users (causing crashes). cgiutils.py http://www.voidspace.org.uk/python/recipebook.shtml#utils cgitutils now has much better functions for handling emails. It includes a function for creating HTML emails and a single function will do the sending, rather than 3 functions for the three different cases as before (using sendmail, using smtplib, using smtplib with login). 2005/04/07 Version 0.3.0 Changed the email functions, this may break things (but it's better this way) Added createhtmlemail, removed loginmailme mailme is now a wrapper for sendmailme, mailme, *and* the old loginmailme upload.py http://www.voidspace.org.uk/python/cgi.shtml#upload 2005/04/07 Version 1.1.1 We now set I/O to binary mode for windows. googleCacheServer.py http://www.voidspace.org.uk/python/recipebook.shtml#google An innovative use of the google api ! Version 0.1.3 5th April 2005 This is a simple implementation of a proxy server that fetches web pages from the google cache. It's based on SimpleHTTPServer and lets you explore the internet from your browser, using the google cache. See the world how google sees it. Alternatively - retro internet - no CSS, no javascript, no images, this is back to the days of MOSAIC ! Best Regards, Fuzzy http://www.voidspace.org.uk/python -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
Epigrass-1.3.36 is available!
EpiGrass (epigrass.sourceforge.net) is a platform for network epidemiological simulation and analysis. It enables researchers to perform comprehensive spatio-temporal simulations incorporating epidemiological data and models for disease transmission and control in order to create sophisticated scenario analyses. Epigrass is a pure python application and plans to stay that way. It is supported on linux only but wellcomes volunteers willing to try to make it run on other platforms. enjoy! Flávio Codeço Coelho -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
BayPIGgies: April 14, 7:30pm (FIRST meeting at IronPort)
NOTE: we are no longer meeting at Stanford; the April meeting is at IronPort in San Bruno The next meeting of BayPIGgies will be Thurs, April 14 at 7:30pm. Guido van Rossum (and any other BayPIGgies who wish to contribute) will review the activities at PyCon 2005. BayPIGgies meetings alternate between IronPort (San Bruno, California) and Google (Mountain View, California). For more information and directions, see http://www.baypiggies.net/ Before the meeting, we may meet at 6pm for dinner. Discussion of dinner plans is handled on the BayPIGgies mailing list. Advance notice: The May 12 meeting agenda has not been set. Please send e-mail to [EMAIL PROTECTED] if you want to suggest an agenda (or volunteer to give a presentation). -- Aahz ([EMAIL PROTECTED]) * http://www.pythoncraft.com/ The joy of coding Python should be in seeing short, concise, readable classes that express a lot of action in a small amount of clear code -- not in reams of trivial code that bores the reader to death. --GvR -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations.html
Re: curious problem with large numbers
Steve Holden wrote: Scott David Daniels wrote: Terry Reedy wrote: On my Windows machine with 2.2.1, I get exactly what you expected: 1e1 1.#INF ... If you get wrong behavior on a later version, then a bug has been introduced somewhere, even perhaps in VC 7, used for 2.4. Nope, it is also there for 2.3.4 (May 25 2004, 21:17:02). This is not necessarily a bug in the sense of a fixable bug; floating point has vagaries that are not necessarily easily controllable from the C source side. While this may be true, it's pretty strange that Michael Spencer reports apparently correct results on Windows 2.3.3 and 2.4, and I confirm the Windows 2.4 result on my own system, while you report that 2.3.4 gives an error. Or isn't that what you are reporting? A further Windows data point from Cygwin: Python 2.4 (#1, Dec 4 2004, 20:10:33) [GCC 3.3.3 (cygwin special)] on cygwin Type help, copyright, credits or license for more information. 1e1 Inf regards Steve I guess the behavior is also hardware-dependent. FWIW, I tested on an Athlon XP box. Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Sockets
On Fri, 08 Apr 2005 03:06:38 -, Grant Edwards [EMAIL PROTECTED] wrote: Nope. You're thinking of C strings. Python strings aren't terminated with a null. Yes, that make sense. Thanks for the input, and the note on struct. Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple inheritance: Interface problem workaround, please comment this
Hello! (1) make A or B a mixin class that doesn't need __init__ called, or Would be a solution for classes that just give functionality, no data-structures. In that case, i would use functions, no classes ;-) I've seen code where there are classes without init and the hope that self has what they expect, not realy the blackbox oop should produce! (2) make class AB inherit from A and delegate to B (or vice versa) As every multiple inheritance can be solved as single inheritance, if no better solution for my hungry monster problem is found, this would be the best way. Don't take my AB example as a real word example ;-) Also think of to libraries with classes which have inheritance depth five or more (that's what I've got). Somtimes you want to stick things together without knowing the inheritance tree. So, if there are ready class-modules, and I want to use them for multiple inheritance, I've to rewrite the init's of classes in the module! For me, python is the best solution in oop-webprogramming (tried perl, got ugly code, tried php, got php ;-), tried C++, takes too long for web projects). The only thing I'm dislike is the super thing with multiple inheritance, maby I shoud see pythons multiple inheritance as a nice to have and not to use thing ;-) Thanks, AXEL. -- http://mail.python.org/mailman/listinfo/python-list
Re: change extensions
Bob Then [EMAIL PROTECTED] wrote: how can i change all files from one extension to another within a direcory? In Windows, the quickest way is: os.system( rename *.old *.new ) -- - Tim Roberts, [EMAIL PROTECTED] Providenza Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list
Parent module idea dropped? [WAS: import statement - package visibility problem]
But onto the point you're making. I think its possibly a mis-viewing of the package idea in Python. A package creates a name space. If you create Lib/Server/Db with all the __init__.py files, its because you want to import Lib.Server.Db, rather than a way of organising your source files. I went forward. I read all the documentation in the language reference. About the import statement: http://docs.python.org/ref/import.html For importing packages and using hierarchical module names, there was no detailed description but a link to an essay: http://www.python.org/doc/essays/packages.html There is a section in that essay named Intra-package references. It says: The submodules often need to refer to each other. For example, the surround module might use the echo module. In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path. Thus, the surround module can simply use |import echo| or |from echo import echofilter|. If the imported module is not found in the current package (the package of which the current module is a submodule), the import statement looks for a top-level module with the given name. When packages are structured into subpackage (as with the Sound package in the example), there's no shortcut to refer to submodules of sibling packages - the full name of the subpackage must be used. For example, if the module Sound.Filters.vocoder needs to use the echo module in the Sound.Effects package, it can use |from Sound.Effects import echo|. (One could design a notation to refer to parent packages, similar to the use of .. to refer to the parent directory in Unix and Windows filesystems. In fact, ni supported this using __ for the package containing the current module, __.__ for the parent package, and so on. This feature was dropped because of its awkwardness; since most packages will have a relative shallow substructure, this is no big loss.) So finally I got the answer: there was a parent package feature in the ni module but it was dropped of its awkwardness. This is not a big loss but this is exatly the feature that I need. Is there a person on this list who was against the parent package idea? He must know the answer or a workaround :-) -- _ Laszlo Nagy web: http://designasign.biz IT Consultantmail: [EMAIL PROTECTED] Python forever! -- http://mail.python.org/mailman/listinfo/python-list
Re: Problems extracting attachment from email
Lee Harr [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]... On 2005-04-07, foten [EMAIL PROTECTED] wrote: The problem I'm having is when I'm trying to extract the attachement using f=open(Filename, wb) f.write(msg.get_payload(decode=1)) f.close() Not the whole message is decoded and stored! When only trying f.write(msg.get_payload()) I see that the last 255 bytes are missing. What happens if you do... m = msg.get_payload(decode=1) f.write(m) f.write(m) f.close() ? Just wondering if maybe some buffer not being flushed properly. Maybe could replace the 2nd f.write() with an f.flush() Lee Harr [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]... On 2005-04-07, foten [EMAIL PROTECTED] wrote: The problem I'm having is when I'm trying to extract the attachement using f=open(Filename, wb) f.write(msg.get_payload(decode=1)) f.close() Not the whole message is decoded and stored! When only trying f.write(msg.get_payload()) I see that the last 255 bytes are missing. What happens if you do... m = msg.get_payload(decode=1) f.write(m) f.write(m) f.close() ? Just wondering if maybe some buffer not being flushed properly. Maybe could replace the 2nd f.write() with an f.flush() Thanx for the ideas! If I do f.write(m) f.write(m) nothing is changed. After the first write I'm missing 255 bytes and the second write just doubles the data. After this I'm missing 510 bytes. Doing a flush doesn't help either, if I'm not misstaken, calling close() on a file inplicitly calls flush(). get_payload() fails to deliver the correct amount of data, in this case ~82k... This drives my crazy! -- http://mail.python.org/mailman/listinfo/python-list
Re: Problems extracting attachment from email
foten wrote: The problem I'm having is when I'm trying to extract the attachement using f=open(Filename, wb) f.write(msg.get_payload(decode=1)) f.close() Not the whole message is decoded and stored! When only trying f.write(msg.get_payload()) I see that the last 255 bytes are missing. How is this possible, I receive every last byte from stdin? I don't think its a Python problem. Those methods are used in a lot of places. Your file is most likely not what it is supposed to be. -- hilsen/regards Max M, Denmark http://www.mxm.dk/ IT's Mad Science -- http://mail.python.org/mailman/listinfo/python-list
Re: logging as root using python script
Raghul wrote: Hi Is it possible to login as a root in linux using python script? What I need is when I execute a script it should login as root and execute my command and logout from root to my existing account. IS is possible? Thanx in advance. Hi, You can compile the small .c program and setuid it's executable. Change the path to your .py script of course. #include sys/types.h #include unistd.h int main(int argc,char **argv) { char *c1[]={/tmp/Chg.py\0,'\0'}; setuid(0); seteuid(0); execv(c1[0],c1); return 1; } -- http://mail.python.org/mailman/listinfo/python-list
Problems On Path
Hello NG, yesterday I installed Python 2.4.1 (together with all the site-packages I need for it, including Pythonwin, wxPython, py2exe, etc...), but then I found that for some of my py2exe generated application there were some problems. So, I came back and I re-installed Python 2.3.4 as before (together with all the site-packages I need for it). Now, however, I am having some strange problems in using Pythonwin. If I do: import wx That is the usual import for wxPython, Python thinks that my wx is: module 'wx' from 'C: \Python23\lib\site-packages\vtk_python\wx\__init__.pyc' so it imports VTK instead of wxPython. I know I can modify this behaviour by doing, at the beginning of the app: sys.path.insert(0, 'C:\\Python23\\lib\\site-packages\\wx-2.5.5-msw-ansi') So that wx is REALLY wxPython... but in the old installation I didn't need to da so, wx was recognised as wxPython and vtk_python as vtk. Am I missing something? Thanks to you all Andrea. -- Message for the recipient only, if received in error, please notify the sender and read http://www.eni.it/disclaimer/ -- http://mail.python.org/mailman/listinfo/python-list
__builtins__ wreidness
Given this module test.py: print type(__builtins__) I ran into a wreid thing. Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. __builtins__ module '__builtin__' (built-in) type(__builtins__) type 'module' import test type 'dict' What? __builtins__ is a dict when used in a module, but it is a module when used interactively? Why? -- _ Laszlo Nagy web: http://designasign.biz IT Consultantmail: [EMAIL PROTECTED] Python forever! -- http://mail.python.org/mailman/listinfo/python-list
Re: Grouping code by indentation - feature or ******?
I, Tim Tyler [EMAIL PROTECTED] wrote or quoted: What do you guys think about Python's grouping of code via indentation? Some relevant resources: http://c2.com/cgi/wiki?PythonWhiteSpaceDiscussion http://c2.com/cgi/wiki?IndentationEqualsGrouping http://c2.com/cgi/wiki?SyntacticallySignificantWhitespaceConsideredHarmful The first page talks quite a bit about distinguishing between tabs and spaces. -- __ |im |yler http://timtyler.org/ [EMAIL PROTECTED] Remove lock to reply. -- http://mail.python.org/mailman/listinfo/python-list
doubt regarding Conversion of date into timestamp
Dear All, I am new to Python I want to know how to change a time into timestamp eg Fri, 8 Apr 2005 09:22:14 +0900 like format date I want to change as a timestamp. I have used below code to get timestamp import time time.strptime(v,'%a, %d %b %Y %X +0900') function. This function will return time stamp. But If the format of date string is changed. This function raise error eg My web application always return different format of date Fri, 8 Apr 2005 10:31:37 +0530 Fri, 8 Apr 2005 10:11:03 +0900 Fri, 8 Apr 2005 09:40:47 +0800 Kindly help to resolve this problem. with regards, Prabahar Yahoo! India Matrimony: Find your life partner online Go to: http://yahoo.shaadi.com/india-matrimony -- http://mail.python.org/mailman/listinfo/python-list
changing from python2.3 to python2.4
I've written a little application which uses the bang-line #!/usr/bin/python to call the interpreter. As python 2.4 comes distributed with other distroes this breaks my application as its modules are installed in /usr/local/lib/python2.3/site-packages/... and python2.4 does not look there. How do you suggest dealing with this: - is calling /usr/bin/python2.3 in the bang-line problematic? - installing into both python2.3 and python2.4 - rebuilding (re- ./configure, make, make install) the app solves the problem Whats the usual way to deal with this? Thanks Uwe -- http://mail.python.org/mailman/listinfo/python-list
Re: curious problem with large numbers
Chris Fonnesbeck wrote: I have been developing a python module for Markov chain Monte Carlo estimation, in which I frequently compare variable values with a very large number, that I arbitrarily define as: inf = 1e1 Don't forget that you can write your own Infinity. (Warning: Buggy code, especially for unsigned Infinity) class Infinity(object): def __init__(self, sign=1): self._sign = cmp(sign, 0) def __repr__(self): return Infinity(%d) % self._sign def __str__(self): if self._sign == 0: return UInf elif self._sign 0: return -Inf else: return +Inf def __eq__(self, other): return isinstance(other, Infinity) and self._sign == other._sign def __ne__(self, other): return not (self == other) def __le__(self, other): if self._sign == 0: raise ValueError(Unsigned Infinity is incomparable) elif self._sign 0: return True else: return False def __ge__(self, other): if self._sign == 0: raise ValueError(Unsigned Infinity is incomparable) elif self._sign 0: return False else: return True def __lt__(self, other): return not (self = other) def __gt__(self, other): return not (self = other) def __add__(self, other): if isinstance(other, Infinity): if self._sign == other._sign: return self else: raise ValueError(%s + %s is undefined % (self, other)) else: return self __radd__ = __add__ def __neg__(self): return Infinity(-self._sign) def __sub__(self, other): return self + (-other) def __rsub__(self, other): return -self def __mul__(self, other): return Infinity(self._sign * other) __rmul__ = __mul__ def __div__(self, other): if instance(other, Infinity): raise ValueError(Inf/Inf is undefined) else: return Infinity(self._sign * other) __truediv__ = __div__ __floordiv__ = __div__ def __rtruediv__(self, other): return 0 __rdiv__ = __rtruediv__ def __rfloordiv__(self, other): if self._sign * cmp(other, 0) 0: return -1 else: return 0 POSITIVE_INFINITY = Infinity(1) NEGATIVE_INFINITY = Infinity(-1) UNSIGNED_INFINITY = Infinity(0) -- http://mail.python.org/mailman/listinfo/python-list
Re: doubt regarding Conversion of date into timestamp
praba kar wrote: Dear All, I am new to Python I want to know how to change a time into timestamp Is there any specific reason for not using datetime instead of time ? -- hilsen/regards Max M, Denmark http://www.mxm.dk/ IT's Mad Science -- http://mail.python.org/mailman/listinfo/python-list
Thoughts on some stdlib modules
I was messing around in google looking for the available python form validation modules when i found this: http://www.jorendorff.com/articles/python/path/, and i realized that is very similar to my python fileutils module,which encapsulate,path operations,file operations,etc. And those thoughts comes to mind again, if python is such a great language why does the stdlib is so bloated with duplication,bad bad library design,clumsy to use modules,etc. Why does people have to put wrappers around about half of the standart library modules? i have wrappers for urllib,urllib2,urlparse in urlutils. glob,shutils,os.path,os,filecmp,etc in fileutils. time,datetime.time,date,datetime.datetime,time.date,etc,etc in DateTime. And so the list goes on. I mean is this normal? i dont think so.I havent seen such a messy stdlib in any language.Is it because of legacy code and backwards compatibility or because not too much people in the python-dev cares about library design? i admit the python language design is really really great but the stdlib is totally forgotten. Are those issues being considered right now? i cant find any PEP addressing the issue especifically, at least cooking it for python 3000. specific topics could be: grouping related modules. removing useless legacy modules. refactoring duplicated functionality. removing/redesigning poorly written modules. adding a module versioning system. -- http://mail.python.org/mailman/listinfo/python-list
Re: __builtins__ wreidness
Laszlo Zsolt Nagy wrote: Given this module test.py: print type(__builtins__) I ran into a wreid thing. Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. __builtins__ module '__builtin__' (built-in) type(__builtins__) type 'module' import test type 'dict' What? __builtins__ is a dict when used in a module, but it is a module when used interactively? Why? __builtins__ (plural form) is a CPython implementation detail. if you want to access the __builtin__ module, import it as usual: import __builtin__ f = __builtin__.open(...) if you're interested in CPython implementation details, study the CPython source code. /F -- http://mail.python.org/mailman/listinfo/python-list
args attribute of Exception objects
Hi, When I need to make sense of a python exception, I often need to parse the string exception in order to retrieve the data. Example: try: print foo except NameError, e: print e.args symbol = e.args[0][17:-16] == (NameError: name 'foo' is not defined, ) or try: (4).foo except NameError, e: print e.args == ('int' object has no attribute 'foo',) Moreover, in the documentation about Exception, I read Warning: Messages to exceptions are not part of the Python API. Their contents may change from one version of Python to the next without warning and should not be relied on by code which will run under multiple versions of the interpreter. So even args could not be relied upon ! Two questions: 1) did I miss something in dealing with exceptions ? 2) Could this be changed to .args more in line with: a) first example: e.args = ('foo', NameError: name 'foo' is not defined) b) second example: e.args = (4, 'foo', 'int' object has no attribute 'foo',) the message of the string can even be retrieved with str(e) so it is also redundant. BTW, the Warning in the doc enables to change this :-) To be backward compatible, the error message could also be the first element of the tuple. Seb ps: There may be problems (that I am not aware) with an exception keeping references to other objects -- http://mail.python.org/mailman/listinfo/python-list
Re: 'Address already in use' when using socket
Peter Hansen wrote: Bearish wrote: I get 'Address already in use' errors when using sockets. Generally one can fix this using: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) where sock is the server socket in question. Do this prior to attempting to bind to the port. -Peter I agree on this, wanted to write the same thing when I saw this. -- http://mail.python.org/mailman/listinfo/python-list
decorating functions with generic signatures (not for the faint of heart)
I have realized today that defining decorators for functions with generic signatures is pretty non-trivial. Consider for instance this typical code: #traced_function.py def traced_function(f): def newf(*args, **kw): print calling %s with args %s, %s % (f.__name__, args, kw) return f(*args, **kw) newf.__name__ = f.__name__ return newf @traced_function def f1(x): pass @traced_function def f2(x, y): pass #/traced_function.py This is simple and works: from traced_function import traced_function, f1, f2 f1(1) calling f1 with args (1,), {} f2(1,2) calling f2 with args (1, 2), {} However, there is a serious disadvantage: the decorator replaces a function with a given signature with a function with a generic signature. This means that the decorator is *breaking pydoc*! $ pydoc2.4 traced_function.f1 Help on function f1 in traced_function: traced_function.f1 = f1(*args, **kw) You see that the original signature of f1 is lost: even if I will get an error when I will try to call it with a wrong number of arguments, pydoc will not tell me that :-( The same is true for f2: $ pydoc2.4 traced_function.f2 Help on function f2 in traced_function: traced_function.f2 = f2(*args, **kw) In general all functions decorated by 'traced_function' will have the same (too much) generic signature. This is a disaster for people like me that rely heavily on Python introspection features. I have found a workaround, by means of a helper function that simplifies the creation of decorators. Let's call this function 'decorate'. I will give the implementation later, let me show how it works first. 'decorate' expects as input two functions: the first is the function to be decorated (say 'func'); the second is a caller function with signature 'caller(func, *args, **kw)'. The caller will call 'func' with argument 'args' and 'kw'. 'decorate' will return a function *with the same signature* of the original function, but enhanced by the capabilities provided by the caller. In our case we may name the caller function 'tracer', since it just traces calls to the original function. The code makes for a better explanation: #traced_function2.py from decorate import decorate def tracer(f, *args, **kw): print calling %s with args %s, %s % (f.func_name, args, kw) return f(*args, **kw) def traced_function(f): This decorator returns a function decorated with tracer. return decorate(f, tracer) @traced_function def f1(x): pass @traced_function def f2(x, y): pass #/traced_function2.py Let me show that the code is working: from traced_function2 import traced_function, f1, f2 f1(1) calling f1 with args (1,), {} f2(1,2) calling f2 with args (1, 2), {} Also, pydoc gives the right output: $ pydoc2.4 traced_function2.f2 Help on function f1 in traced_function2: traced_function2.f1 = f1(x) $ pydoc2.4 traced_function2.f2 Help on function f2 in traced_function2: traced_function2.f2 = f2(x, y) In general all introspection tools using inspect.getargspec will give the right signatures (modulo bugs in my implementation of decorate). All the magic is performed by 'decorate'. The implementation of 'decorate' is not for the faint of heart and ultimately it resorts to 'eval' to generate the decorated function. I guess bytecode wizards here can find a smarter way to generate the decorated function. But my point is not about the implementation (which is very little tested at the moment). My point is that I would like to see something like 'decorate' in the standard library. I think somebody already suggested a 'decorator' module containing facilities to simplify the usage of decorators. This post is meant as a candidate for that module. In any case, I think 'decorate' makes a good example of decorator pattern. Here is my the current implementation (not very tested): #decorate.py def _signature_gen(varnames, default_args, n_args, rm_defaults=False): n_non_default_args = n_args - len(default_args) non_default_names = varnames[:n_non_default_args] default_names = varnames[n_non_default_args:n_args] other_names = varnames[n_args:] n_other_names = len(other_names) for name in non_default_names: yield %s % name for name, default in zip(default_names, default_args): if rm_defaults: yield name else: yield %s = %s % (name, default) if n_other_names == 1: yield *%s % other_names[0] elif n_other_names == 2: yield *%s % other_names[0] yield **%s % other_names[1] def decorate(func, caller): argdefs = func.func_defaults or () argcount = func.func_code.co_argcount varnames = func.func_code.co_varnames signature = , .join(_signature_gen(varnames, argdefs, argcount)) variables = , .join(_signature_gen(varnames, argdefs, argcount, rm_defaults=True)) lambda_src = lambda %s: call(func, %s) % (signature, variables) dec_func =
Re: Problem with access to shared memory(W2K) SOLVED
I have got a solution to my problem from Thomas Heller by email. The problem was solved by using .from_address() instead of causing trouble cast() - here the solution as a generalized example of code for reading access to shared memory area with given 'appropriateName': from ctypes import * FILE_MAP_READ = 2 strNameOfSharedMemoryAreaToAccess = 'appropriateName' handle = windll.kernel32.OpenFileMappingA( FILE_MAP_READ , 0 , strNameOfSharedMemoryAreaToAccess ) if not handle: raise WinError() addr = windll.kernel32.MapViewOfFile( handle ,FILE_MAP_READ ,0 ,0 ,0 ) if not addr: raise WinError() class structureOfSharedMemoryArea(Structure): _fields_ = [ (Elem01 , c_short ) # or any other ctype or ctype structure (Elem02 , c_long ) ... (ElemNN , c_long ) ] contentOfSharedMemoryArea = structureOfSharedMemoryArea.from_address(addr) print 'Elem01 ', contentOfSharedMemoryArea.Elem01 print 'Elem02 ', contentOfSharedMemoryArea.Elem02 ... print 'ElemNN ', contentOfSharedMemoryArea.ElemNN Claudio -- http://mail.python.org/mailman/listinfo/python-list
Re: curious problem with large numbers
Michael Spencer [EMAIL PROTECTED] wrote: Terry Reedy wrote: Chris Fonnesbeck [EMAIL PROTECTED] wrote However, on Windows (have tried on Mac, Linux) I get the following behaviour: inf = 1e1 inf 1.0 On my Windows machine with 2.2.1, I get exactly what you expected: 1e1 1.#INF On my Windows machine, both 2.3.3 and 2.4 give the same (expected) result: 1e1 1.#INF Likewise for me with 2.3.2 (W2K on a PIII). I'm not going to do a cut and paste because it's exactly the same as everyone else's, and anyway I'm typing this at my Linux machine. -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ ___ | Frankly I have no feelings towards penguins one way or the other \X/ |-- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump -- http://mail.python.org/mailman/listinfo/python-list
Re: Best editor?
Howdy, I'm sold on out Leo, http://leo.sf.net, pure Python amazingly easy to learn and powerful. Based on outlining, it provides a powerful and flexible way to manage content. Lots of built in Python code awareness. Extensible with plugins, a very active community is making I wish my editor could ... come true every day. A peek at the SF forum gives an idea of how active the development pace is http://sourceforge.net/forum/?group_id=3458 It's different than other editors, I'd suggest trying it. Thanks, Kent -- http://mail.python.org/mailman/listinfo/python-list
Re: decorating functions with generic signatures (not for the faint of heart)
I said it was very little tested! ;) This should work better: #decorate.py def _signature_gen(varnames, n_default_args, n_args, rm_defaults=False): n_non_default_args = n_args - n_default_args non_default_names = varnames[:n_non_default_args] default_names = varnames[n_non_default_args:n_args] other_names = varnames[n_args:] n_other_names = len(other_names) for name in non_default_names: yield %s % name for i, name in enumerate(default_names): if rm_defaults: yield name else: yield %s = arg[%s] % (name, i) if n_other_names == 1: yield *%s % other_names[0] elif n_other_names == 2: yield *%s % other_names[0] yield **%s % other_names[1] def decorate(func, caller): argdefs = func.func_defaults or () argcount = func.func_code.co_argcount varnames = func.func_code.co_varnames signature = , .join(_signature_gen(varnames, len(argdefs), argcount)) variables = , .join(_signature_gen(varnames, len(argdefs), argcount, rm_defaults=True)) lambda_src = lambda %s: call(func, %s) % (signature, variables) dec_func = eval(lambda_src, dict(func=func, call=caller, arg=argdefs)) dec_func.__name__ = func.__name__ dec_func.__doc__ = func.__doc__ dec_func.__dict__ = func.__dict__.copy() return dec_func #/decorate.py -- http://mail.python.org/mailman/listinfo/python-list
Re: Thoughts on some stdlib modules
vegetax [EMAIL PROTECTED] wrote: And those thoughts comes to mind again, if python is such a great language why does the stdlib is so bloated with duplication,bad bad library design,clumsy to use modules,etc. I mean is this normal? i dont think so.I havent seen such a messy stdlib in any language. Perl hardly covers itself with glory in this regard. And what of Java? AWT Swing, Date Calendar, Streams, Readers and java.nio... There's a lot of complex layering going on there, with many older features being buried and then deprecated (actually or effectively). The net result may be interesting for software archaeologists, but hardly inspires the notion of a coherently designed library. Is it because of legacy code and backwards compatibility or The full benefits and limitations of particular design decisions take a while to emerge, after which point people are depending on the code and you are limited to refactoring the implementation without changing the interface - unless you are prepared for the howls of protest from those whose code breaks. So to some extent the problems you mention are unavoidable - but I think you overstate your case. because not too much people in the python-dev cares about library design? I doubt that. admit the python language design is really really great but the stdlib is totally forgotten. This is a very extreme view. The standard library isn't perfect, but it is far from being the mess you imply. My own personal bugbear is the issue of consistency. Java's standard library might be a huge and clumsy beast with more than its fair share of overloading and obsolescence, but it at least has the virtue of more consistently following conventions on how classes and methods are named, for instance. Nick -- http://mail.python.org/mailman/listinfo/python-list
Re: decorating functions with generic signatures (not for the faint of heart)
Michele Simionato [EMAIL PROTECTED] writes: I have realized today that defining decorators for functions with generic signatures is pretty non-trivial. I've not completely read your post ;-), but I assume you're trying to do something that I've also done some time ago. Maybe the following code snippet is useful for you - it creates a source code string which can than be compiled. The code prints this when run: def f(a, b=42, c='spam', d=None): 'docstring' return f._api_(a, b, c, d) def g(*args, **kw): '' return g._api_(*args, **kw) Thomas code def make_codestring(func): import inspect args, varargs, varkw, defaults = inspect.getargspec(func) return def %s%s:\n%r\nreturn %s._api_%s % \ (func.func_name, inspect.formatargspec(args, varargs, varkw, defaults), func.func_doc or , func.func_name, inspect.formatargspec(args, varargs, varkw)) def f(a, b=42, c=spam, d=None): docstring def g(*args, **kw): pass print make_codestring(f) print make_codestring(g) /code -- http://mail.python.org/mailman/listinfo/python-list
Re: decorating functions with generic signatures (not for the faint of heart)
Yes, this is essentially the same idea. You compile the codestring to bytecode, whereas I just evalue the codestring to a lambda function. We are essentially implementing a runtime macro by hand. I wonder if there is any alternative approach to get the same result, without manipulation of the source code. BTW, I have fixed another small bug in my original code. 'decorator' should read import inspect def decorate(func, caller): args, varargs, varkw, defaults = inspect.getargspec(func) argdefs = defaults or () argcount = func.func_code.co_argcount varnames = args + (varargs or []) + (varkw or []) signature = , .join(_signature_gen(varnames, len(argdefs), argcount)) variables = , .join(_signature_gen(varnames, len(argdefs), argcount, rm_defaults=True)) lambda_src = lambda %s: call(func, %s) % (signature, variables) print func.__name__, -, lambda_src dec_func = eval(lambda_src, dict(func=func, call=caller, arg=argdefs)) dec_func.__name__ = func.__name__ dec_func.__doc__ = func.__doc__ dec_func.__dict__ = func.__dict__.copy() return dec_func -- http://mail.python.org/mailman/listinfo/python-list
compound strip() string problem
Hi, I'm new to python and i have a string problem. My problem is this -- import time time = time.asctime() time 'Fri Apr 08 22:14:14 2005' ti = time[0:13] me = time[14:16] time = ti + me time 'Fri Apr 08 2214' -- Now i need to compond that string remove the whitespace if you will.Well i read up on strip(), lstrip() and rstrip() and all i could deduce was that they striped the whitespace from the start and/or end of the string.But I tried that anyway and failed.Is there an easier way than i did it below? I'm sorry it's ugly and tedious. -- #!/bin/bash/env python import os, time #Figure out what os this is platform = os.name #Create string with date, time and extension for our pcap file ext = '.out' time = time.asctime() ti = time[0:13] # me = time[14:16] # time = ti + me #There has to be a better way to do this? fo = time[0:3] # rm = time[4:7]# at = time[11:18] time = fo + rm + at + ext #Get rid of yukkies del ti, me, ext, fo, rm, at #create command string flag = '-w' wincommand = 'c:/progra~1/ethereal/tethereal' lincommand = '/usr/sbin/./tethereal' #run tethereal and send the output to a pcap file DDDMMMHHMM.out if platform == 'nt': os.system('%s %s %s' % (wincommand, flag, time)) if platform == 'posix': os.system('%s %s %s' % (lincommand, flag, time)) -- Thanks, Dylan -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple inheritance: Interface problem workaround, please comment this
Axel: So, if there are ready class-modules, and I want to use them for multiple inheritance, I've to rewrite the init's of classes in the module! Or you use a metaclass that rewrites the __init___ method for you. This is a start (warning: written in 5 minutes and not tested more than you see): Given a hierarchy, makes __init__ cooperative. The only change needed is to add a line __metaclass__ = CooperativeInit to the base class of you hierarchy. from decorate import decorate # see today thread on decorators for this def make_cooperative_init(cls, name, bases, dic): def call_cooperatively(__init__, self, *args, **kw): super(cls, self).__init__(*args, **kw) __init__(self, *args, **kw) __init__ = cls.__dict__.get(__init__) if __init__: cls.__init__ = decorate(__init__, call_cooperatively) class CooperativeInit(type): __init__ = make_cooperative_init class Base: __metaclass__ = CooperativeInit def __init__(self): print B.__init__ class C1(Base): def __init__(self): print C1.__init__ class C2(Base): def __init__(self): print C2.__init__ class D(C1, C2): def __init__(self): print D.__init__ D() # you get # B.__init__ # C2.__init__ # C1.__init__ # D.__init__ Michele Simionato -- http://mail.python.org/mailman/listinfo/python-list
Re: compound strip() string problem
The time module has a function called 'strftime' which can retyrn the time in the the format you want to. So you really don't need to parse the string returned by asctime the way you are doing. On Apr 8, 2005 6:01 PM, Dylan Wilson [EMAIL PROTECTED] wrote: Hi, I'm new to python and i have a string problem. My problem is this -- import time time = time.asctime() time 'Fri Apr 08 22:14:14 2005' ti = time[0:13] me = time[14:16] time = ti + me time 'Fri Apr 08 2214' -- Now i need to compond that string remove the whitespace if you will.Well i read up on strip(), lstrip() and rstrip() and all i could deduce was that they striped the whitespace from the start and/or end of the string.But I tried that anyway and failed.Is there an easier way than i did it below? I'm sorry it's ugly and tedious. -- #!/bin/bash/env python import os, time #Figure out what os this is platform = os.name #Create string with date, time and extension for our pcap file ext = '.out' time = time.asctime() ti = time[0:13] # me = time[14:16] # time = ti + me #There has to be a better way to do this? fo = time[0:3] # rm = time[4:7]# at = time[11:18] time = fo + rm + at + ext #Get rid of yukkies del ti, me, ext, fo, rm, at #create command string flag = '-w' wincommand = 'c:/progra~1/ethereal/tethereal' lincommand = '/usr/sbin/./tethereal' #run tethereal and send the output to a pcap file DDDMMMHHMM.out if platform == 'nt': os.system('%s %s %s' % (wincommand, flag, time)) if platform == 'posix': os.system('%s %s %s' % (lincommand, flag, time)) -- Thanks, Dylan -- http://mail.python.org/mailman/listinfo/python-list -- http://blogs.applibase.net/sidharth -- http://mail.python.org/mailman/listinfo/python-list
Re: [JIM_SPAM] Microsoft supporting a .NET version of Python...
oops - Sorry for the posting. This wasn't meant for the newsgroup :-) J Steve Holden wrote: Jim Hargrave wrote: http://www.gotdotnet.com/workspaces/workspace.aspx?id=ad7acff7-ab1e-4bcb-99c0-57ac5a3a9742 You really shoud try and get out more: http://www.pycon.org/dc2005/talks/keynote regards Steve -- http://mail.python.org/mailman/listinfo/python-list
Re: curious problem with large numbers
Hi All-- Windows XP, uwin running on Athlon XP 3000+: 0 [/c/users/ivanlan][1] python Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. 1e1 1.#INF Metta, Ivan Michael Spencer wrote: A further Windows data point from Cygwin: Python 2.4 (#1, Dec 4 2004, 20:10:33) [GCC 3.3.3 (cygwin special)] on cygwin Type help, copyright, credits or license for more information. 1e1 Inf regards Steve I guess the behavior is also hardware-dependent. FWIW, I tested on an Athlon XP box. -- Ivan Van Laningham God N Locomotive Works http://www.andi-holmes.com/ http://www.foretec.com/python/workshops/1998-11/proceedings.html Army Signal Corps: Cu Chi, Class of '70 Author: Teach Yourself Python in 24 Hours -- http://mail.python.org/mailman/listinfo/python-list
Interpreter problem
I am using python 2.3.5 on a Linux system and have an odd problem dealing with the 'sha-bang' line. I have a file, driver.py which starts with #!/usr/bin/python and works fine (that is, when I type in ./driver.py at the command prompt the file runs as expected). I have another file, myotherfile.py which starts with the exact same line (#!/usr/bin/python) but I get : bad interpreter: No such file or directory H. That occurs when I have no trailing spaces after the word 'python' on the first line. When I place a single space at the end of the line I get: usr/bin/python: can't open file (note the lack of a slash at the start of the line...is that significant?) In the 2nd file I placed import sys on line 2, then sys.exit(0) on line 3 to isolate the behavior, so I'm pretty sure the problem deals with the first line of the file. If I remove the import sys and sys.exit(0) then type ./myotherfile.py at the command line it runs as expected. Both files have the same permission settings (744) and owner (me). I'm been looking at this for a couple of days and am at a loss. Have any of you seen anything like this and what can I do about it? Thanks, --greg -- Greg Lindstrom 501 975.4859 (office) Senior Programmer501 219-4455 (fax) NovaSys Health [EMAIL PROTECTED] Little Rock, Arkansas We are the music makers, and we are the dreamers of dreams. W.W. Confidentiality Notice -- This email and any attachments to it are privileged and confidential and are intended solely for use of the individual or entity to which they are addressed. If the reader of this message is not the intended recipient, any use, distribution, or copying of this communication, or disclosure of all or any part of its content to any other person, is strictly prohibited. If you have received this communication in error, please notify the sender by replying to this message and destroy this message and delete any copies held in your electronic files. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sockets
Dan wrote: On Thu, 7 Apr 2005 21:52:11 -0500, [EMAIL PROTECTED] wrote: Python strings always carry their length, and can have embedded NULs. s.write(\0\1\2\3) should write 4 bytes to the socket 's'. I'm taking binary data from a database, so it's not really a Python string. Is there an easy way to convert it? Unless I'm misunderstanding you, you don't need to. It's not that Python normal strings are encoded in some special format, it's just that a string object does not make a distrinction between what is traditionally called text and binary data. Play around with this at a Python prompt to see for yourself. For example: img = file('foo.jpg', 'rb').read() # use a real image filename though img[:50] '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00\xff\xdb\x00C\x00 \x05\x03\x04\x04\x04\x03\x05\x04\x04\x04\x05\x05\x05\x06\x07\x0c\x08\x07\x07\x07 \x07\x0f\x0b\x0b\t' So, the fact that you're getting the data from a database is probably immaterial. HTH, Dave -- http://mail.python.org/mailman/listinfo/python-list
Positions of regexp groups
Just a quick question: Does anyone have a simple way of finding the positions (start, end) of the groups in a regexp match? AFAICS, the re API can only return the contents...? -- Magnus Lie HetlandFall seven times, stand up eight http://hetland.org [Japanese proverb] -- http://mail.python.org/mailman/listinfo/python-list
Re: Positions of regexp groups
[Magnus Lie Hetland] Just a quick question: Does anyone have a simple way of finding the positions (start, end) of the groups in a regexp match? AFAICS, the re API can only return the contents...? Read the docs for match objects, esp. the start(), end(), and span() methods. -- http://mail.python.org/mailman/listinfo/python-list
unknown encoding problem
Hi, I need to read in a text file which seems to be stored in some unknown encoding. Opening and reading the files content returns: f.read() '\x00 \x00 \x00\x00l\x00o\x00g\x00E\x00n\x00t\x00r\x00y\x00... Each character has a \x00 prepended to it. I suspect its some kind of unicode - how do I get rid of it? str.replace('\x00', '') works but is not really nice. I don't quite get the hang of str.encode /str.decode Any Ideas? Thanks Ciao Uwe -- http://mail.python.org/mailman/listinfo/python-list
[ANN] Voidspace Pythonutils Updates
Lots of updates to the Voidspace modules and recipes. Update to the Firedrop plugins see : http://www.voidspace.org.uk/python/programs.shtml#firedrop New version of FireSpell the spell checker (based on PyEnchant by Ryan Kelly) New plugin called FireMail which lets you send blog entries (or articles) by email - either as HTML or text. All the plugins are available for download. There is also docs on installing and using the plugins and docs on writing plugins. downman.py http://www.voidspace.org.uk/python/cgi.shtml#downman More improvements to downman.py - the simple download manager CGI 2005/03/15 Version 0.4.0 Configuration moved into external config file. HTML stuff mmoved into downman_templates.py Put quotes round filename (This stops firefox truncating filenames with spaces) Flush the output buffer as we serve the file (may help those who have to use env in the shebang line) guestbook.py http://www.voidspace.org.uk/python/guestbook.html Quite a big change - with a cleaning up of the default HTML templates and several new features to help combat guestbook spam. 2005/04/06 Version 1.3.0 Lots of restyling in the HTML templates - not *quite* so horrible now. We write new entries at the start of the file rather than the end (undoes a bug introduced in 1.2.0) Max of two URLs in an entry. A few extra words added to the banned list. (Still trying to combat guestbook spam) New value 'BREAKVAL' added to templates (so we can use the XHTML br / if needed) The '_charset_' field added to the form and value checked (in case page encoding changed by guests browser) Now use rel=nofollow in links to foil spammers Email addresses shown using javascript in an attempt to foil address harvesters. Obviously invalid email addresses and urls are removed. (very basic) Jalopy and Login Tools Not a huge amount of progress - but a few bugs fixed. http://www.voidspace.org.uk/python/jalopy.html http://www.voidspace.org.uk/python/logintools.html 2005/03/23 Admin levels are now preserved when new users are created or invited. Corrected bug causing accounts to not be editable. Changed all the shebang lines to '#! /usr/bin/python' Updated to latest versions of all the pythonutils modules (like ConfigObj) Fixed typo in HTML for editing users (causing crashes). cgiutils.py http://www.voidspace.org.uk/python/recipebook.shtml#utils cgitutils now has much better functions for handling emails. It includes a function for creating HTML emails and a single function will do the sending, rather than 3 functions for the three different cases as before (using sendmail, using smtplib, using smtplib with login). 2005/04/07 Version 0.3.0 Changed the email functions, this may break things (but it's better this way) Added createhtmlemail, removed loginmailme mailme is now a wrapper for sendmailme, mailme, *and* the old loginmailme upload.py http://www.voidspace.org.uk/python/cgi.shtml#upload 2005/04/07 Version 1.1.1 We now set I/O to binary mode for windows. googleCacheServer.py http://www.voidspace.org.uk/python/recipebook.shtml#google An innovative use of the google api ! Version 0.1.3 5th April 2005 This is a simple implementation of a proxy server that fetches web pages from the google cache. It's based on SimpleHTTPServer and lets you explore the internet from your browser, using the google cache. See the world how google sees it. Alternatively - retro internet - no CSS, no javascript, no images, this is back to the days of MOSAIC ! Best Regards, Fuzzy http://www.voidspace.org.uk/python -- http://mail.python.org/mailman/listinfo/python-list
Re: Positions of regexp groups
On Fri, 8 Apr 2005 13:18:27 + (UTC), [EMAIL PROTECTED] (Magnus Lie Hetland) wrote: Just a quick question: Does anyone have a simple way of finding the positions (start, end) of the groups in a regexp match? AFAICS, the re API can only return the contents...? These documented methods of the match object aren't simple enough??? start( [group]) end( [group]) Return the indices of the start and end of the substring matched by group and there's span() which returns a tuple (start, end) ... what simpler could you ask for, Mr Occam? -- http://mail.python.org/mailman/listinfo/python-list
Re: Parent module idea dropped? [Python import parent package]
So finally I got the answer: there was a parent package feature in the ni module but it was dropped of its awkwardness. This is not a big loss but this is exatly the feature that I need. Is there a person on this list who was against the parent package idea? He must know the answer or a workaround :-) I just created a new module that can handle the 'from __.__.SomeModule import SomeClass' notation. Keywords: Python import parent package What is the right place to make it available for others? Laci 2.0 -- _ Laszlo Nagy web: http://designasign.biz IT Consultantmail: [EMAIL PROTECTED] Python forever! #!/usr/bin/env python Import hack module. Importing this module replaces the __builtin__.__import__ function (called by the import statement). After that you will be able to import parent packages this way: from __.__.SomeModule import SomeClass where __.__ means a package that is one level above in the filesystem. Please note that you can use the special '__.__' name only in the import statement. The name '__.__' itself cannot be used to reference the parent package elsewhere. NOTE: This syntax follows the convention introduced by the 'ni' package, first appeared in Python 1.5. The 'parent package' idea was dropped because of its awkwardness. For details, please visit http://www.python.org/doc/essays/packages.html If you still feel that you need to use this feature, you can use this module. LICENSE: Python license (http://www.python.org/psf/) @author: [EMAIL PROTECTED] @modified: 2005-04-08 15:42 import __builtin__ import imp import os # Keep a reference to the original __import__ function _orig_import = __builtin__.__import__ def __import__(name,globals,locals,fromlist): Specialized import function that can import names from parent packages using this special form: from __.__.SomeModule import SomeClass Please note that you can use the special '__.__' name only in the import statement. The name '__.__' itself cannot be used to reference the parent package elsewhere. global _orig_import def _tail_module_path(path): Returns the tail of a module path.split. If there is no tail (no dot in the module path) then raises an ImportError. Example: _tail_module_path('Lib.Db.PostgreSQL') will return 'Lib.Db' index = path.rfind('.') if index = 0: raise ImportError('Cannot import __parentpackage__ from a top level package.') return path[:index] #print __import__ was called with name %s % name if name.startswith(PARENTPACKAGE): #print Trying to import %s % name # Determine the file that needs to be imported package_fpath = globals['__file__'] (dir,fname) = os.path.split(package_fpath) double_parent = not fname.startswith('__init__.') # Determine the new package name package_ppath = globals['__name__'] upper_package_ppath = _tail_module_path(package_ppath) if double_parent: upper_package_ppath = _tail_module_path(upper_package_ppath) #print Package name in the module where import was executed: %s % package_ppath #print Package name for the parent package: %s % upper_package_ppath name = name.replace(PARENTPACKAGE,upper_package_ppath) return _orig_import(name,globals,locals,fromlist) # Install the new __import__ function __builtin__.__import__ = __import__ -- http://mail.python.org/mailman/listinfo/python-list
Re: Matplotlib question-- Numeric or numarray?
Matt == Matt Feinstein [EMAIL PROTECTED] writes: Matt I'm working my way through the matplotlib documentation Matt there's a point that's ambiguous-- the pdf file (dated Matt Mar. 1, 2005) warns of dire consequences if I use the Matt 'wrong' array package-- e.g., put numarray in the .matlabrc Matt file if the compile-time package is Numeric. But there's Matt only one current, unlabeled, windows installer and there Matt seems to have been a change, some time back before version Matt 0.7, in how this question is dealt with. Can someone Matt clarify? thnksndvnc Hi Matt -- it looks like the documentation is out of data. matplotlib can now be built to support Numeric and numarray simultaneously, and which one you are using is controlled at runtime by the numerix setting in your rc file. The windows installers now have support for both Numeric and numarray built in. I recommend you use the matplotlib.numerix module to import your Numeric/numarray symbols in matplotlib scripts, rather than using Numeric/numarray directly. The reason is that it is still important that the arrays you create match the type in your rc file, and the numerix module insures that this happens. One good way to do this is import matplotlib.numerix as nx a = nx.array(blah) The matplotlib numerix package structure mirros numarray. Eg to import mean, you would do from matplotlib.numerix.mlab import mean which in numarray is located in numarray.mlab and in Numeric is in MLab. If trying to figure out where all the various numerix functions live makes your head hurt, you can use the pylab module which aggregates all the numerix and plotting functions into a single namespace import pylab as p a = p.array([1,2,3]) n = p.randn(1) mu, sigma = p.mean(n), p.std(n) p.hist(n, 1000) p.show() Hope this helps, JDH -- http://mail.python.org/mailman/listinfo/python-list
Re: Interpreter problem
Greg Lindstrom wrote: I am using python 2.3.5 on a Linux system and have an odd problem dealing with the 'sha-bang' line. I have a file, driver.py which starts with #!/usr/bin/python and works fine (that is, when I type in ./driver.py at the command prompt the file runs as expected). I have another file, myotherfile.py which starts with the exact same line (#!/usr/bin/python) but I get : bad interpreter: No such file or directory There's almost certainly a carriage return as well as a newline in the shebang line. [...] regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Interpreter problem
Greg Lindstrom schreef: I am using python 2.3.5 on a Linux system and have an odd problem dealing with the 'sha-bang' line. I have a file, driver.py which starts with #!/usr/bin/python and works fine (that is, when I type in ./driver.py at the command prompt the file runs as expected). I have another file, myotherfile.py which starts with the exact same line (#!/usr/bin/python) but I get : bad interpreter: No such file or directory I remember encountering a similar problem once. It turned out that the python interpreter, when invoked through the #! line, will only work properly if the line ends in a Unix newline (only a LF: '\n'). When you create the line on a Windows or Mac platform the line ending will typically be different: CR/LF (\r\n) for Windows, CR (\r) for Mac. For the body of the python script this doesn't matter, but it does matter for the #! line. Not sure if this applies here, but who knows. Good luck, Ruud -- Ruud de Jong '@'.join('.'.join(s) for s in (['ruud','de','jong'],['tiscali','nl'])) -- http://mail.python.org/mailman/listinfo/python-list
Rotating arbitrary sets of elements within a list
I'm trying to come up with a good algorithm to do the following: Given a list 'A' to be operated on, and a list 'I' of indices into 'A', rotate the i'th elements of 'A' left or right by one position. Here's are some examples: A = [a, b, c, d, e, f] I = [0, 3, 4] rotate(A, I, 'left') -- [b, c, d, e, f, a] rotate(A, I, 'right') -- [b, a, c, f, d, e] I = [1, 3, 4] rotate(A, I, 'left') -- [b, a, d, e, c, f] rotate(A, I, 'right') -- [a, c, b, f, d, e] Any ideas? -- http://mail.python.org/mailman/listinfo/python-list
Re: compound strip() string problem
Dylan Wilson wrote: Now i need to compond that string remove the whitespace if you will.Well i read up on strip(), lstrip() and rstrip() and all i could deduce was that they striped the whitespace from the start and/or end of the string.But I tried that anyway and failed.Is there an easier way than i did it below? As Sidharth Kuruvila pointed out, time.strftime() is probably the best choice for your situation. For the general case of removing whitespace from a sting, one method is: ''.join(mystring.split()) Jeffrey -- http://mail.python.org/mailman/listinfo/python-list
Re: Interpreter problem
Steve Holden wrote: Greg Lindstrom wrote: I am using python 2.3.5 on a Linux system and have an odd problem dealing with the 'sha-bang' line. I have a file, driver.py which starts with #!/usr/bin/python and works fine (that is, when I type in ./driver.py at the command prompt the file runs as expected). I have another file, myotherfile.py which starts with the exact same line (#!/usr/bin/python) but I get : bad interpreter: No such file or directory There's almost certainly a carriage return as well as a newline in the shebang line. [...] regards Steve Not so. I get the same result with python 2.3 and 2.4 on Debian Linux Testing. Nothing odd at all in the shebang. -- http://mail.python.org/mailman/listinfo/python-list
Re: unknown encoding problem
Uwe Mayer wrote: I need to read in a text file which seems to be stored in some unknown encoding. Opening and reading the files content returns: f.read() '\x00 \x00 \x00\x00l\x00o\x00g\x00E\x00n\x00t\x00r\x00y\x00... Each character has a \x00 prepended to it. I suspect its some kind of unicode - how do I get rid of it? Intermittent '\x00' bytes are a indeed strong evidence for unicode. Use codecs.open() to access the data in such a file: import codecs f = codecs.open(filename, r, UTF-16-BE) f.read() u' logEntry' If you don't want unicode, convert back to str: _.encode(latin1) ' logEntry' Note that the last step may fail if the file contains characters not available in the string encoding you specify. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Rotating arbitrary sets of elements within a list
[EMAIL PROTECTED] wrote: I'm trying to come up with a good algorithm to do the following: Given a list 'A' to be operated on, and a list 'I' of indices into 'A', rotate the i'th elements of 'A' left or right by one position. Here's are some examples: A = [a, b, c, d, e, f] I = [0, 3, 4] rotate(A, I, 'left') -- [b, c, d, e, f, a] rotate(A, I, 'right') -- [b, a, c, f, d, e] I = [1, 3, 4] rotate(A, I, 'left') -- [b, a, d, e, c, f] rotate(A, I, 'right') -- [a, c, b, f, d, e] Any ideas? phil, Could you do a circlular buffer where the front starts at 'a' (0) and rotate left increments front. The i'th element is gotten as mod 6 (front+i) where 6 would be the length of the list. wes -- http://mail.python.org/mailman/listinfo/python-list
__iadd__ and __isub__ map to += and -= but don't return result
Hi! If I am missing a point here, what could it be? Watch the hot spots (***) Frederic # # Python 2.4, Windows ME X = 0, Y = 1 class Vertex (list): def __init__ (self, *coordinates): self [:] = list (coordinates [0:2]) def __add__ (self, V): return Vertex (self [X] + V [X], self [Y] + V [Y]) def __iadd__ (self, V): self [X] += V [X]; self [Y] += V [Y] V1 = Vertex (1, 2) V2 = Vertex (4, 6) V1 + V2[5, 8] #OK V1 += V2# *** V1# *** # *** died ? print V1None# *** V1 died ! V2[5, 8] # V2 is fine # Adding three traces to follow the state of self def __iadd__ (self, V): print 'A', self self [X] += V [X] print 'B', self self [Y] += V [Y] print 'C', self V1 += V2A [1, 2]B [5, 2]C [5, 8] # *** self is still OK when method terminates. #***Also shows that operator += did call __iadd__, #***(though the 2.4 doc no longer mentions it) # Explicit call works fine V1.__iadd__ (V2)A [1, 2]B [5, 2]C [5, 8] V1 [5, 8] OK !!! -- http://mail.python.org/mailman/listinfo/python-list
Re: import statement - package visibility problem
Laszlo, ... Importing from a not package related source code is not a problem, really. My problem is about importing inside a package. Lib/Server/Db/__init__.py __can__ import Lib/Server/Db/Adapters usign relative paths, but Lib/Server/Db/Adapters/PostgreSQLConnection.py __cannot__ import from Lib/Server/Db using relative paths Of course I can do the latter import using absolute paths but that is something I would like to avoid. In other words: it is possible to import using relative lib paths only in one direction. Why is that? Why can't I import something from the containing package? Yes it might be convenient for what you want to do, but importing from containing package means traversing up the directory structure from importing module. Or you can define any rules for mapping files and directories to the name space you desire. How to do that? By hacking? Well, yes, Python hacking. See the imp module for interesting functions such as load_module:- import imp filename = 'Lib2/Server/Db/DatabaseConnection.py' connection = load_module( 'connection', open(filename), filename,('.py', 'U', 1) ) There is a load_package function not documented, I haven't attempted to use it, but you could read the source (import.c). You could also look at the builtin function __import__. Lastly you could look at the new import hooks (PEP 302) in the sys module, implemented in 2.3, where you define a find_module that returns a loader object. Regards, Paul Clinch -- http://mail.python.org/mailman/listinfo/python-list
Re: Interpreter problem
rbt wrote: Steve Holden wrote: Greg Lindstrom wrote: I am using python 2.3.5 on a Linux system and have an odd problem dealing with the 'sha-bang' line. I have a file, driver.py which starts with #!/usr/bin/python and works fine (that is, when I type in ./driver.py at the command prompt the file runs as expected). I have another file, myotherfile.py which starts with the exact same line (#!/usr/bin/python) but I get : bad interpreter: No such file or directory There's almost certainly a carriage return as well as a newline in the shebang line. [...] regards Steve Not so. I get the same result with python 2.3 and 2.4 on Debian Linux Testing. Nothing odd at all in the shebang. What, you are telling me you've checked the file with a command like head driver.py | od -bc and verified the absence of any extraneous characters? regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Interpreter problem
Yes! I moved the file (which I had created on a windows box using a mounted drive), created a new one on the Linux box, typed in the shabang in Linux, then copied the old file (sans shabang) to the new file. It runs like a champ. My sysadmin suggests we run all of our files through dos2unix before they are moved to production. We both imagine that we would have the same problem with our Perl scripts as well. It's odd that deleting the line and reentering it on the Linux box did not correct the problem. Perhaps vim recognized the format as having cr-lf and inserted it even though I was editing on Linux. Anyhow, it would have been a long time before I got this one. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Can dictionary values access their keys?
This may be a very rudimentary question, but here goes: If I have a simple dictionary, where the value is a class or function, is there an interface through which it can discover what its key is? Similar to index() for list. For a list, assuming I new what the parent list was I could do something like this. class child: ... def get_parent_index(self, parent): ... return parent.index(self) ... a = child() l = [a] b = l[0] b.get_parent_index(a) b.get_parent_index(l) 0 Is there a way to do something like that with dicts? On a similar note, if one object is part of another, is there a way for the 'child' obj to discover what/who the 'parent' object is? That way parent does not have to be explicityly passed to get_parent_index? The idea is like this: class child: ... def get_parent_index(self): parent = self.magic_parent_discovery() ... return parent.index(self) ... Thanks much -- Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic loading of code, and avoiding package/module name collisions.
Dear John Perks and Sarah Mount, Long story short: what I'm looking for is information on how have a Python app that: * embeds an editor (or wxNoteBook full of editors) * loads code from the editors' text pane into the app * executes bits of it * then later unloads to make way for an edited version of the code. The new version needs to operate on a blank slate, so reload() may not be appropriate. This sounds like 'idle'. Regards, Paul C. -- http://mail.python.org/mailman/listinfo/python-list
THE GREATEST NEWS EVER ! °º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø
http://www.print-it-011.blogspot.com The Greatest News Ever ! -- http://mail.python.org/mailman/listinfo/python-list
Re: Interpreter problem
Steve Holden wrote: rbt wrote: Steve Holden wrote: Greg Lindstrom wrote: I am using python 2.3.5 on a Linux system and have an odd problem dealing with the 'sha-bang' line. I have a file, driver.py which starts with #!/usr/bin/python and works fine (that is, when I type in ./driver.py at the command prompt the file runs as expected). I have another file, myotherfile.py which starts with the exact same line (#!/usr/bin/python) but I get : bad interpreter: No such file or directory There's almost certainly a carriage return as well as a newline in the shebang line. [...] regards Steve Not so. I get the same result with python 2.3 and 2.4 on Debian Linux Testing. Nothing odd at all in the shebang. What, you are telling me you've checked the file with a command like head driver.py | od -bc and verified the absence of any extraneous characters? regards Steve You're right: [EMAIL PROTECTED]:~$ cd /usr/local/bin [EMAIL PROTECTED]:/usr/local/bin$ head web* | od -bc 000 043 041 057 165 163 162 057 142 151 156 057 160 171 164 150 157 # ! / u s r / b i n / p y t h o 020 156 015 012 144 145 146 040 167 145 142 137 142 141 143 153 165 n \r \n How can this be fixed? vim doesn't see it. -- http://mail.python.org/mailman/listinfo/python-list
Testing that a value is set.
I am using winGuiAuto to test a program. I want to check that a varible is set. In the following code I am doing if(len(str(hwnd)) 0) What is the python way? def clickNdSyncStopButton(hwnd=None): if(hwnd == None): hwnd = winGuiAuto.findTopWindow(NDSync) if(len(str(hwnd)) 0): raise Failed to find the NDSync Dialog StopButton = winGuiAuto.findControl(hwnd, Stop, WindowsForms10.BUTTON.app3) if(len(str(StopButton)) 0): raise Failed to find the StopButton winGuiAuto.clickButton(StopButton) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to name Exceptions that aren't Errors
Steve Holden wrote: I've even used an exception called Continue to overcome an irksome restriction in the language (you used not to be able to continue a loop from an except clause). Out of curiosity, how could you use an exception to do that? I would think you would need to catch it and then use continue, which wouldn't be possible because of the restriction you were trying to work around in the first place. -- http://mail.python.org/mailman/listinfo/python-list
Re: Rotating arbitrary sets of elements within a list
[EMAIL PROTECTED] wrote: I'm trying to come up with a good algorithm to do the following: Given a list 'A' to be operated on, and a list 'I' of indices into 'A', rotate the i'th elements of 'A' left or right by one position. Ok, here's what I've got. It seems to work right, but can it be improved upon? def list_rotate(A, I, direction=1): A1 = [A[i] for i in I] A2 = [A[i] for i in range(len(A)) if i not in I] if direction: I1 = [(i-1)%len(A) for i in I] # rotate left else: I1 = [(i+1)%len(A) for i in I] # rotate right I2 = [i for i in range(len(A)) if i not in I1] for i in range(len(I1)): A[I1[i]] = A1[i] for i in range(len(I2)): A[I2[i]] = A2[i] -- http://mail.python.org/mailman/listinfo/python-list
Re: Ó¿Ò, THE GREATEST NEWS EVER ! °º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø
Joe Spammer wrote: snip bah! I thought maybe they found a huge forest of rosewood trees in Brazil. Tom (disappointed) Reese -- http://mail.python.org/mailman/listinfo/python-list
Tutorial at Python-UK, Oxford, 19 May - handful of places left
Michele Simionato is giving a full day tutorial titled The Wonders of Python at the UK Python Conference, Randolph Hotel, Oxford on 19 May. (This replaces Alex Martelli, who is now working for Google in California). The program is here: https://www.accu.org/conference/python_tutorial.html This is a fantastic opportunity to boost your Python skills and catch up on the newer features of the language, for a fraction of the price of a professional training course. The course costs £135 for ACCU members and £160 for non-ACCU. There are still a few places left; anyone wishing to attend should directly contact the organisers, Archer Yates Associates, whose details are on the bottom left corner of the page. Best Regards Andy Robinson UK Python Conference program chair -- http://mail.python.org/mailman/listinfo/python-list
Re: text processing problem
Maurice LING wrote: Matt wrote: I'd HIGHLY suggest purchasing the excellent a href=http://www.oreilly.com/catalog/regex2/index.html;Mastering Regular Expressions/a by Jeff Friedl. Although it's mostly geared towards Perl, it will answer all your questions about regular expressions. If you're going to work with regexs, this is a must-have. That being said, here's what the new regular expression should be with a bit of instruction (in the spirit of teaching someone to fish after giving them a fish ;-) ) my_expr = re.compile(r'(\w+)\s*(\(\1\))') Note the \s*, in place of the single space . The \s means any whitespace character (equivalent to [ \t\n\r\f\v]). The * following it means 0 or more occurances. So this will now match: there (there) there (there) there(there) there (there) there\t(there) (tab) there\t\t\t\t\t\t\t\t\t\t\t\t(there) etc. Hope that's helpful. Pick up the book! M@ Thanks again. I've read a number of tutorials on regular expressions but it's something that I hardly used in the past, so gone far too rusty. Before my post, I've tried my_expr = re.compile(r'(\w+) \s* (\(\1\))') instead but it doesn't work, so I'm a bit stumped.. Thanks again, Maurice Maurice, The reason your regex failed is because you have spaces around the \s*. This translates to one space, followed by zero or more whitespace elements, followed by one space. So your regex would only match the two text elements separated by at least 2 spaces. This kind of demostrates why regular expressions can drive you nuts. I still suggests picking up the book; not because Jeff Friedl drove a dump truck full of money up to my door, but because it specifically has a use case like yours. So you get to learn solve your problem at the same time! HTH, M@ -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Is there a way to do something like that with dicts? Not without searching the dict's items through, or keeping a additional mapping between values and keys. On a similar note, if one object is part of another, is there a way for the 'child' obj to discover what/who the 'parent' object is? That way parent does not have to be explicityly passed to get_parent_index? No. -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Can you please elaborate on this? -Matthew Diez B. Roggisch wrote: keeping a additional mapping between values and keys. -- http://mail.python.org/mailman/listinfo/python-list
Re: curious problem with large numbers
Steve Holden wrote: Scott David Daniels wrote: Terry Reedy wrote: On my Windows machine with 2.2.1, I get exactly what you expected: 1e1 1.#INF ... If you get wrong behavior on a later version, then a bug has been introduced somewhere, even perhaps in VC 7, used for 2.4. Nope, it is also there for 2.3.4 (May 25 2004, 21:17:02) A further Windows data point from Cygwin: Python 2.4 (#1, Dec 4 2004, 20:10:33) [GCC 3.3.3 (cygwin special)] on cygwin Type help, copyright, credits or license for more information. 1e1 Inf regards Steve Aha! Same version (2.3.4): Idle: 1e1 1.0 import struct; struct.pack('d', 1e1) '\x00\x00\x00\x00\x00\x00\xf0?' (which is actually 1.0) python via command line (readline support): 1e1 1.#INF import struct; struct.pack('d', 1e1) '\x00\x00\x00\x00\x00\x00\xf0\x7f' Note the difference in the final byte. Same results (command line vs. Idle) for 2.4.1. --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: args attribute of Exception objects
On Fri, 08 Apr 2005 09:32:37 +, Sbastien de Menten wrote: Hi, When I need to make sense of a python exception, I often need to parse the string exception in order to retrieve the data. What exactly are you doing with this info? (Every time I started to do this, I found a better way. Perhaps one of them will apply for you.) (As a general comment, I'd point out that you don't have to check the entire error message; checking for a descriptive substring, while still not safe, is at least safe*r*.) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to name Exceptions that aren't Errors
Leif K-Brooks wrote: Steve Holden wrote: I've even used an exception called Continue to overcome an irksome restriction in the language (you used not to be able to continue a loop from an except clause). Out of curiosity, how could you use an exception to do that? I would think you would need to catch it and then use continue, which wouldn't be possible because of the restriction you were trying to work around in the first place. Here is a stupid way to calculate a 3-element median: class Success(Exception): pass def median_if_sorted(x,y,z): if x = y = z: raise Success(y) def median(a, b, c): try: median_if_sorted(a, b, c) median_if_sorted(c, b, a) median_if_sorted(a, c, b) median_if_sorted(c, a, b) median_if_sorted(b, a, c) median_if_sorted(b, c, a) except Success, instance: result, = instance.args return result else: raise ValueError, 'Nothing found' --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Hello! If I have a simple dictionary, where the value is a class or function, is there an interface through which it can discover what its key is? The key of a value may not be unique, so you can also get a tupe of keys, like dict(a=1, b=1), the key's of 1 are a and b. For unique values, I did something like that couple of weeks ago, the thing you would need is the getKey thing, it's fast, but needs much memory for big structures becouse I use two dicts. If speed does not matter: class ReverseDict(dict): def get_keys(self, value): keys = [] for k, v in self.items(): if v == value: keys.append(k) return keys class UniqueReverseDict(dict): A dictionary that can resolute reverse: A object to a key. Both, the Key and the Value must be unique. def __init__(self, **kws): super(UniqueReverseDict, self).__init__(kws) self.__reverse = {} def __setitem__(self, k, v): super(UniqueReverseDict, self).__setitem__(k, v) self.__reverse[v] = k def __update_reverse(self): self.__reverse.clear() for k, v in self.items(): self.__reverse[v] == k def has_value(self, v): return self.__reverse.has_key(v) def __delitem__(self, k): self.__reverse[self[k]] super(UniqueReverseDict, self).__delitem__(k) def clear(self): self.__reverse.clear() super(UniqueReverseDict, self).clear() def copy(self): return UniqueReverseDict(self) def pop(self, k): del self.__reverse[self[k]] return self.pop(k) def popitem(self, **kws): raise 'AxsPy.Misc.Structures.UniqueReverseDict', \ 'NotImplemented' def setdefault(self, **kws): raise 'AxsPy.Misc.Structures.UniqueReverseDict', \ 'NotImplemented' def update(self, **kws): super(UniqueReverseDict, self).update(**kws) self.__update_reverse() def getKey(self, v): return self.__reverse[v] Lg, AXEL -- Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in http://www.informatik-forum.at/showpost.php?p=206342postcount=10 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to name Exceptions that aren't Errors
Leif K-Brooks wrote: Steve Holden wrote: I've even used an exception called Continue to overcome an irksome restriction in the language (you used not to be able to continue a loop from an except clause). Out of curiosity, how could you use an exception to do that? I would think you would need to catch it and then use continue, which wouldn't be possible because of the restriction you were trying to work around in the first place. As long as the exception-handling code doesn't break then the loop automatically continues, so the trick was (IIRC) to have a loop body that essentially looked like this: for item in somelist: try: {loop body} except Continue: pass Then exceptions caught inside the loop body (which obviously had nested try: clauses) could raise Continue inside their except: clause to restart the loop at the next iteration. regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Matthew Thorley wrote: Can you please elaborate on this? Eh, just have two dicts that are the inverse of each other. You could do that by subclassinc dict: class mydict(dict): def __init__(self): self.__inverse_mapping = {} def __setitem__(self, key, value): dict.__setitem__(key, value) self.__inverse_mapping[value] = key def key4value(self, v): return self.__inverse_mapping[v] But of course this only works if your mapping is bijective. Consider this: d = mydict() d[10] = 20 print d.key4value(20) d[15] = 20 print d.key4value(20) This will of course not give you (10,15), but 15 only - the last mapping overwrites earlier ones. And beware of non-hashable objects like lists or dicts themselves, they aren't working as keys. So this produces an error: d[100] = [1,2,3,4] TypeError: list objects are unhashable -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list
Re: Interpreter problem
rbt wrote: Steve Holden wrote: rbt wrote: Steve Holden wrote: Greg Lindstrom wrote: I am using python 2.3.5 on a Linux system and have an odd problem dealing with the 'sha-bang' line. I have a file, driver.py which starts with #!/usr/bin/python and works fine (that is, when I type in ./driver.py at the command prompt the file runs as expected). I have another file, myotherfile.py which starts with the exact same line (#!/usr/bin/python) but I get : bad interpreter: No such file or directory There's almost certainly a carriage return as well as a newline in the shebang line. [...] regards Steve Not so. I get the same result with python 2.3 and 2.4 on Debian Linux Testing. Nothing odd at all in the shebang. What, you are telling me you've checked the file with a command like head driver.py | od -bc and verified the absence of any extraneous characters? regards Steve You're right: [EMAIL PROTECTED]:~$ cd /usr/local/bin [EMAIL PROTECTED]:/usr/local/bin$ head web* | od -bc 000 043 041 057 165 163 162 057 142 151 156 057 160 171 164 150 157 # ! / u s r / b i n / p y t h o 020 156 015 012 144 145 146 040 167 145 142 137 142 141 143 153 165 n \r \n Nice to know my psychic powers are still functioning correctly :-) How can this be fixed? vim doesn't see it. Do you have a dos2unix utility? Otherwise use tr to delete \013 characters, IIRC - it's a while since I used anything but dos2unix to do this. Of course you could just read and write the file in Python in text mode, that might get rid of the returns. There's probably a vim mode you can set to display the extraneous characters - it normally screams ^M at me for files with carriage returns in them. You may find it telling you the file is being edits in DOS mode when you open it up, and again there's probably vim magic you can do to force it to write in unix mode too - I'll leave that to the vimsters. regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Interpreter problem
tuba_ranger wrote: Yes! I moved the file (which I had created on a windows box using a mounted drive), created a new one on the Linux box, typed in the shabang in Linux, then copied the old file (sans shabang) to the new file. It runs like a champ. My sysadmin suggests we run all of our files through dos2unix before they are moved to production. We both imagine that we would have the same problem with our Perl scripts as well. It's odd that deleting the line and reentering it on the Linux box did not correct the problem. Perhaps vim recognized the format as having cr-lf and inserted it even though I was editing on Linux. Anyhow, it would have been a long time before I got this one. Thanks. A pleasure - just passing on payback for the huge amounts of help I've had here myself. See also my post in reply to rbt's latest for remarks on other possible fixes using vim. regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Axel Straschil wrote: For unique values, I did something like that couple of weeks ago, the thing you would need is the getKey thing, it's fast, but needs much memory for big structures becouse I use two dicts. Thanks for the tip, I may give that a try. I'll be interested to see what kind of speed penalty I pay. The data I am using has multiples dictionaries with maybe a thousand entries each. But each entry is an object containing a history of data with perhaps hundreds or even thousands more entries. So we're talking about maybe a million+ total nested key:values. I don't know if that counts as large or not. I can't even guess how much k memory that is. I must say I am *very* suprised that python does not have a way to look up what key is pointing to a given object--without scanning the whole list that is. Is that what list.index() does under-the-hood? I mean is list.index(y) just the same as itemnum = 0 for item in list: if y == item: return itemnum else: itemnum = itemnum+1 I think I am going to have to reevaluate my system design... grrr. thanks -- Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: Installing Python 2.4 on Linux
David Fraser wrote: Edward Diener wrote: I can install Python 2.4 on the Fedora 3 Linux system, but after I do a number of Linux utilities and commands, like yum, stop working because they were dependent on the Python 2.3 installation. What happens is that Python 2.4 replaces the /usr/bin/python module with the Python 2.4 version. If I replace /usr/bin/python with the Python 2.3 version executable, which is still on my system, that all the aforesaid modules depend on, they start working again, but I can no longer execute modules, like IDLE, which was part of my Python 2.4 distribution. What is the solution to this ? The operating system was installed with Python 2.3 and the development libraries but no tools, doc, or otherwise. I have installed Python 2.4 with all the RPMs and copied down the Python 2.4 documentation to my machine ( since python24-docs.rpm gives one very little ). I would naturally like to use Python 2.4 without killing all the commands that depend on Python 2.3. No doubt these commands have their modules in the site libraries for Python 2.3. Of course I would love to update these dependencies to use Python 2.4 but newer RPMs for these commands do not exist. I do not know whether this is a Python problem or a Fedora 3 problem but I thought I would ask here first and see if anybody else had the same problem. I imagine the problem might exist on other Linux systems. Actually the Fedora RPMS supplied on the Python website are fine: http://www.python.org/2.4.1/rpms.html Quoting from that page: # Q) Is it safe to install these RPMs on a Red Hat system? Will they over-write the system python and cause problems with other Red Hat applications that expect a different version of Python? # A) The RPMs that start with python2.4 are built to not interfere with the system Python. They install as /usr/bin/python2.4 and will not conflict with the system Python unless you are running on a system that ships the a version of Python which has the same major/minor number. To invoke the interpreter with these packages, you will explicitly have to run python2.4. Note that all Python RPMs provided by Python.org and Red Hat provide a /usr/bin/python2.4 (or similar, with major/minor number), even if they also provide /usr/bin/python. So, yes, it should be safe. Note that you may need to build and install a second copy of any packages which you need access to with the supplemental version of Python. You can build packages of these files for the Python 2.4 interpreters for packages which use Distutils, by using the command python2.4 setup.py bdist_rpm. This is by far the preferred way to install a different version of Python to the default version provided with a distribution, as you don't then conflict with packages that require the default version. I do have a separate package installed for Python2.4 and it coexists with Python2.3. The real problem is that a great number of other packages, which are initially part of the system, depend on Python2.3 so I must leave /usr/bin/python as Python2.3. I do not know of a way to reinstall these other packages to use Python2.4 instead. Does the line above, python2.4 setup.py bdist_rpm mean that I am supposed to run this against each of these other package's rpm files and substitute for bdist_rpm the name of the rpm file ? Also, it is better to do it like this using real packages than to rename files manually. I have used the above successfully on Fedora Core 3 (and other similar versions on other distro versions), if it doesn't work for you there is a source RPM available to rebuild from Is bdist_rpm the source rpm for these packages which currently depend on Python2.3 ? Further help getting my Fedora 3 packages which depend on Python2.3 to use Python2.4 would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Installing Python 2.4 on Linux
John Ridley wrote: --- Edward Diener [EMAIL PROTECTED] wrote: [snip] I do not know whether this is a Python problem or a Fedora 3 problem but I thought I would ask here first and see if anybody else had the same problem. I imagine the problem might exist on other Linux systems. On my Mandrake 10.1 system I have the default python 2.3 installation plus a separate python 2.4 installation which I compiled from source. There are two executables (python2.3 and python2.4) in /usr/bin, plus a hard-link (from python2.4) named python. If necessary, I could switch back to the default setup by replacing the hard-link with one from python2.3 - so the system would then have its preferred python version and I could selectively run python2.4 whenever appropriate. So far, Mandrake has not complained about using python 2.4 exclusively, so I might try removing the old installation eventually. (Then again, it's always handy to keep it for compatibility testing). I need python to be python2.3 else many utilities no longer work. The problem is then using IDLE and pydoc for Python2.4 since neither are .py scripts and need python to be a link to python2.4 and not python2.3. I guess the solution is to write a script a shell script which links python to python2.4, run IDLE let's say, then link back to python2.3. However while I am doing this, other system facilities which depend on python to be python2.3 will be dead. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Matthew Thorley wrote: This may be a very rudimentary question, but here goes: If I have a simple dictionary, where the value is a class or function, is there an interface through which it can discover what its key is? Similar to index() for list. No, because mapping types (of which dict is the canonical example) don;t guarantee uniqueness of values. In other words, there can be several keys mapping to the same value. You would therefore need to know how to handle such cases - would you want to return an arbitrary one of those keys, or a list of them all? A standard dict doesn't keep any record of the order in which keys were added, so there are some things you *can't* do, like return the first key added with a given value. For a list, assuming I new what the parent list was I could do something like this. class child: ... def get_parent_index(self, parent): ... return parent.index(self) ... a = child() l = [a] b = l[0] b.get_parent_index(a) b.get_parent_index(l) 0 Is there a way to do something like that with dicts? So you know about the .index() and .find() methods, and of course since there is no guarantee that a value appears only once in the list they disambiguate by returning the lowest index containing the value. A simple way to do what you appear to want would be: def get_parent_index(self, parent): for k, v in parent.items(): if v == self: return k This would return None if the required item wasn't found in the dict parent. On a similar note, if one object is part of another, is there a way for the 'child' obj to discover what/who the 'parent' object is? That way parent does not have to be explicityly passed to get_parent_index? This is like asking if you can find the name of an object. The fact is that a single object can be bound to many variables. Suppose I write: lst = [1, 2, 3] does that mean that the name of 2 is lst[1]? No - 2 is just a value that happens to be bound to that list element (among other things). If I then write otherlst = lst otherlst[1' = two you will find that lst[1] is also now bound to two because lst and otherlst are in fact references tot he same list object, so changing one also changes the other. Given that, what is the name of that list? The idea is like this: class child: ... def get_parent_index(self): parent = self.magic_parent_discovery() ... return parent.index(self) ... Given a set of objects and a known attribute or set of attributes for those objects that might be references to a specific search target you can perform the magic you want, but a generic search for any reference to the search target, while not impossible (using Python's excellent introspection facilities) is way beyond what most people would consider practical. Obviously the garbage collector has to solve this problem, but you *really* don't want to be doing this stuff in Python unless you absolutely *must*. regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Installing Python 2.4 on Linux
Another solution is to just install 2.4 and then make an alias for yum='/usr/bin/python2.3 yum' or whatever the path is :) Edward Diener wrote: I can install Python 2.4 on the Fedora 3 Linux system, but after I do a number of Linux utilities and commands, like yum, stop working because they were dependent on the Python 2.3 installation. What happens is that Python 2.4 replaces the /usr/bin/python module with the Python 2.4 version. If I replace /usr/bin/python with the Python 2.3 version executable, which is still on my system, that all the aforesaid modules depend on, they start working again, but I can no longer execute modules, like IDLE, which was part of my Python 2.4 distribution. What is the solution to this ? The operating system was installed with Python 2.3 and the development libraries but no tools, doc, or otherwise. I have installed Python 2.4 with all the RPMs and copied down the Python 2.4 documentation to my machine ( since python24-docs.rpm gives one very little ). I would naturally like to use Python 2.4 without killing all the commands that depend on Python 2.3. No doubt these commands have their modules in the site libraries for Python 2.3. Of course I would love to update these dependencies to use Python 2.4 but newer RPMs for these commands do not exist. I do not know whether this is a Python problem or a Fedora 3 problem but I thought I would ask here first and see if anybody else had the same problem. I imagine the problem might exist on other Linux systems. -- http://mail.python.org/mailman/listinfo/python-list
redirect stdout
I'd like to build a module that would redirect stdout to send it to a logging module. I want to be able to use a python module that expects to print results using print or sys.stdout.write() and without modifying that module, be able to redirect it's stdout to a logger which will send the messages via datagrams to a server. Any ideas? -- http://mail.python.org/mailman/listinfo/python-list
PPC OSX vs. x86 Linux
Hello -- I writing some python code to do some analysis of my mail logs. I took a 10,000 line snippet from them (the files are about 5-6 million usually) to test my code with. I'm developing it on a Powerbook G4 1.2GHz with 1.25GB of RAM and the Apple distributed Python* and I tested my code on the 10,000 line snippet. It took 2 minutes and 10 seconds to process that snippet. Way too slow -- I'd be looking at about 20 hours to process a single daily log file. Just for fun, I copied the same code and the same log snippet to a dual-proc P3 500MHz machine running Fedora Core 2* with 1GB of RAM and tested it there. This machine provides web services and domain control for my network, so it's moderately utilized. The same code took six seconds to execute. Granted I've got the GUI and all of that bogging down my Mac. However, I had nothing else fighting for CPU cycles and 700MB of RAM free when my testing was done. Even still, what would account for such a wide, wide, wide variation in the time required to process the data file? The code is 90% regular expressions and string finds. Theories? Thanks! -jag * versions are: Python 2.3 (#1, Sep 13 2003, 00:49:11) [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin and Python 2.3.3 (#1, May 7 2004, 10:31:40) [GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2 inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131-- http://mail.python.org/mailman/listinfo/python-list
Re: redirect stdout
Use *NIX magic. Make a named pipe to a python program that pushes stdin to syslog and when you execute your program, redirect stdout to the named pipe. -jag inline: Pasted Graphic.tiffJoshua Ginsberg -- [EMAIL PROTECTED] Brainstorm Internet Network Operations 970-247-1442 x131 On Apr 8, 2005, at 10:52 AM, Neal Becker wrote: I'd like to build a module that would redirect stdout to send it to a logging module. I want to be able to use a python module that expects to print results using print or sys.stdout.write() and without modifying that module, be able to redirect it's stdout to a logger which will send the messages via datagrams to a server. Any ideas? -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Matthew Thorley wrote: This may be a very rudimentary question, but here goes: From your questions, I believe you are not thinking of values as being distinct from the names and data structures that refer to them. What is the parent of 23 in the following expression? 1 + 11 * 2 If you know that, what is the parent of 293 in the same expression? If I have a simple dictionary, where the value is a class or function, is there an interface through which it can discover what its key is? Similar to index() for list. def keyfor(dictionary, element): for key, value in dictionary.iteritems(): if value == element: # value is element if identity quest return key raise ValueError, element On a similar note, if one object is part of another, This is the idea you have wrong. In C, C++, Java, and Fortran you might have objects part of other objects, but in Python objects refer to each other. How about this: class Holder(object): pass v = [1 + 11 * 2] w = [1, v, 3] d = {1: v} o = Holder() o.x = v What is the parent of v? Or even worse: v = [1] v[0] = v What is the parent of v now? --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Testing that a value is set.
[EMAIL PROTECTED] wrote: I am using winGuiAuto to test a program. I want to check that a varible is set. In the following code I am doing if(len(str(hwnd)) 0) What is the python way? you're expecting a string with negative length? that's pretty weird. what language uses that mechanism to report errors? def clickNdSyncStopButton(hwnd=None): if(hwnd == None): hwnd = winGuiAuto.findTopWindow(NDSync) that's usually written: if hwnd is None: ... if(len(str(hwnd)) 0): raise Failed to find the NDSync Dialog according to the winGuiAuto documentation, the findTopWindow function raises an exception if it cannot find a window; maybe you should just ignore that, and let the user code deal with that exception instead of your uncatchable string literals... (rereading the chapter on exceptions in your favourite Python tutorial cannot hurt) /F -- http://mail.python.org/mailman/listinfo/python-list
Re: curious problem with large numbers
On Fri, 08 Apr 2005 09:05:39 -0700, Scott David Daniels [EMAIL PROTECTED] wrote: Steve Holden wrote: Scott David Daniels wrote: Terry Reedy wrote: On my Windows machine with 2.2.1, I get exactly what you expected: 1e1 1.#INF ... If you get wrong behavior on a later version, then a bug has been introduced somewhere, even perhaps in VC 7, used for 2.4. Nope, it is also there for 2.3.4 (May 25 2004, 21:17:02) A further Windows data point from Cygwin: Python 2.4 (#1, Dec 4 2004, 20:10:33) [GCC 3.3.3 (cygwin special)] on cygwin Type help, copyright, credits or license for more information. 1e1 Inf regards Steve Aha! Same version (2.3.4): Idle: 1e1 1.0 import struct; struct.pack('d', 1e1) '\x00\x00\x00\x00\x00\x00\xf0?' (which is actually 1.0) python via command line (readline support): 1e1 1.#INF import struct; struct.pack('d', 1e1) '\x00\x00\x00\x00\x00\x00\xf0\x7f' Note the difference in the final byte. Same results (command line vs. Idle) for 2.4.1. Good aha, but ISTM one of them is more missing than different ;-) (I.e., returned packed string is length 7 vs 8) Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: THE GREATEST NEWS EVER ! °º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø°º¤ø,¸¸,ø¤º°`°º¤ø
From your site: The reason some people don't know for sure if they are going to Heaven when they die is because they just don't know. Isn't that like saying the reason I'm not a rocket scientist is because I'm not a rocket scientist? [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] http://www.print-it-011.blogspot.com The Greatest News Ever ! -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Hello! thousands more entries. So we're talking about maybe a million+ total nested key:values. I don't know if that counts as large or not. I can't even guess how much k memory that is. Mhh, maybe you should use a SQL-Database ;-) Lg, AXEL. -- Aber naja, ich bin eher der Forentyp. Wolfibolfi's outing in http://www.informatik-forum.at/showpost.php?p=206342postcount=10 -- http://mail.python.org/mailman/listinfo/python-list
Re: __iadd__ and __isub__ map to += and -= but don't return result
Anthra Norell wrote: If I am missing a point here, what could it be? the documentation? class Vertex (list): def __init__ (self, *coordinates): self [:] = list (coordinates [0:2]) def __add__ (self, V): return Vertex (self [X] + V [X], self [Y] + V [Y]) def __iadd__ (self, V): self [X] += V [X]; self [Y] += V [Y] V1 += V2 # *** V1# *** # *** died ? print V1 None # *** V1 died ! if you leave out the return statement, Python automagically inserts a return None at the end of your method. and the __iadd__ documentation says: These methods are called to implement the augmented arithmetic operations (+=, -=, *=, /=, %=, **=, =, =, =, ^=, |=). These methods should attempt to do the operation in-place (modifying self) and return the result (which could be, but does not have to be, self). in other words, it works exactly as documented. you return None, you get None. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: redirect stdout
Neal Becker wrote: I'd like to build a module that would redirect stdout to send it to a logging module. I want to be able to use a python module that expects to print results using print or sys.stdout.write() and without modifying that module, be able to redirect it's stdout to a logger which will send the messages via datagrams to a server. if you want to redirect sys.stdout, replace sys.stdout with an object that does the redirection. all it needs is a write method: import sys class myredirector: def write(self, x): sys.stderr.write(*** + repr(x) + \n) sys.stdout = myredirector() print hello sys.stdout.write(world) prints *** 'hello' *** '\n' *** 'world' /F -- http://mail.python.org/mailman/listinfo/python-list
Re: curious problem with large numbers
Bengt Richter wrote: Aha! Same version (2.3.4): Idle: 1e1 1.0 import struct; struct.pack('d', 1e1) '\x00\x00\x00\x00\x00\x00\xf0?' (which is actually 1.0) python via command line (readline support): 1e1 1.#INF import struct; struct.pack('d', 1e1) '\x00\x00\x00\x00\x00\x00\xf0\x7f' Note the difference in the final byte. Same results (command line vs. Idle) for 2.4.1. Good aha, but ISTM one of them is more missing than different ;-) (I.e., returned packed string is length 7 vs 8) In the first one, the final byte is '?' (I made the same mistake myself when I first read it). Also note: float('1e1') is 1.#INF both in Idle and from the command line. I suspect an Idle bug. --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Ply(LALR) and Yacc behaving differently
On Thu, 2005-04-07 at 14:51, Åsmund Grammeltvedt wrote: Hi. I am trying to implement a small compiler in python and, trying to use something a bit more pythonic than lex/yacc, ended up with ply (http://systems.cs.uchicago.edu/ply/). The only problem is that whereas yacc accepts the grammar and appears to parse it correctly, ply does not. Perhaps this belongs on some compiler list, but I couldn't decide if it was a compiler or a python problem, so bear with me. Maybe this is a PLY bug? LALR(1) support appears to be a relatively recent addition to PLY. Have you tried contacting PLY's author? For what it's worth, it appears that you can make your example grammar work in LALR-mode PLY by eliminating the empty production and making the Block rule right recursive like this: def p_Goal(p): Goal : Block def p_Block(p): Block : SEMI | T Block | S Block Of course, I don't know whether this rewrite is applicable to your larger grammar. Hope this helps, -- Carsten Haese - Software Engineer |Phone: (419) 861-3331 Unique Systems, Inc. | FAX: (419) 861-3340 1446 Reynolds Rd, Suite 313 | Maumee, OH 43537| mailto:[EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Installing Python 2.4 on Linux
Joseph Garvin wrote: Another solution is to just install 2.4 and then make an alias for yum='/usr/bin/python2.3 yum' or whatever the path is :) If it were just 'yum' under Fedora 3, I could probably do that but looking at the dependencies on the python2.3 package in Synaptic there must be about 50 other packages mentioned. Which one of these specifically use python2.3 to execute their .py scripts and which ones just use python2.3 shared libraries is another matter which will take much work to discover. But thanks for your suggestion nonetheless. Edward Diener wrote: I can install Python 2.4 on the Fedora 3 Linux system, but after I do a number of Linux utilities and commands, like yum, stop working because they were dependent on the Python 2.3 installation. What happens is that Python 2.4 replaces the /usr/bin/python module with the Python 2.4 version. If I replace /usr/bin/python with the Python 2.3 version executable, which is still on my system, that all the aforesaid modules depend on, they start working again, but I can no longer execute modules, like IDLE, which was part of my Python 2.4 distribution. What is the solution to this ? The operating system was installed with Python 2.3 and the development libraries but no tools, doc, or otherwise. I have installed Python 2.4 with all the RPMs and copied down the Python 2.4 documentation to my machine ( since python24-docs.rpm gives one very little ). I would naturally like to use Python 2.4 without killing all the commands that depend on Python 2.3. No doubt these commands have their modules in the site libraries for Python 2.3. Of course I would love to update these dependencies to use Python 2.4 but newer RPMs for these commands do not exist. I do not know whether this is a Python problem or a Fedora 3 problem but I thought I would ask here first and see if anybody else had the same problem. I imagine the problem might exist on other Linux systems. -- http://mail.python.org/mailman/listinfo/python-list
Re: Interpreter problem
Hi All-- Steve Holden wrote: It's odd that deleting the line and reentering it on the Linux box did not correct the problem. Perhaps vim recognized the format as having cr-lf and inserted it even though I was editing on Linux. Anyhow, it would have been a long time before I got this one. Thanks. A pleasure - just passing on payback for the huge amounts of help I've had here myself. See also my post in reply to rbt's latest for remarks on other possible fixes using vim. Vim is pretty smart. If you edit something in DOS mode, it'll preserve it. If you edit it in UNIX mode, it'll preserve that too. Thus, if you copy a dos file to a unix and edit with vim on both systems, it'll stay dos unless you change it; this is a Good Thing(tm). You run into problems with mixed line endings, or when it makes a difference to the program/OS (the shebang trick). In vim, if you run :se fileformat? it'll tell you exactly what file format you're using. The choices are (surprise) unix and dos. To change the file format of a file explicitly, simply issue the command :set fileformat=dos or :set fileformat=unix When you write the file, it'll be saved exactly the way you specify. Note that you get the ^M at the ends of lines when a file you've been editing in one mode shifts to another and you have to reload the file. Reading [open(foofile,rb)] re-writing [open(foofile,wb)] a file will do this if you are not excruciatingly careful. If you don't have dos2unix on your win system, but do have cat, you can use cat -d. #!/bin/sh cat -d $1 snot mv snot $1 (Prone to error, of course.) insert-std-disclaimers-about-vim-vs-classic-vi-behaviours-here-ly y'rs, Ivan -- Ivan Van Laningham God N Locomotive Works http://www.andi-holmes.com/ http://www.foretec.com/python/workshops/1998-11/proceedings.html Army Signal Corps: Cu Chi, Class of '70 Author: Teach Yourself Python in 24 Hours -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Steve Holden wrote: while not impossible (using Python's excellent introspection facilities) is way beyond what most people would consider practical. Obviously the garbage collector has to solve this problem, but you *really* don't want to be doing this stuff in Python unless you absolutely *must*. regards Steve Thanks very much for the detailed reply. Let me explain in a little more detail what I am doing, and perhaps you could let me know if I am going about it the wrong way. I am creating an object database to store information about network devices, e.g. switches and routers. At the top level there are device objects, every device object contains a mib object--and other various properties--where the actual data is stored. The mib object is a dicitionary--or at least subclasses dictionary--where the key is the oid (a unique string) and the value is a special object called Datapoint. Datapoints come in two types Static and Dynamic, and every Datapoint is unique. So a simple structure looks something like this: Device1 Mib1 {'1.3.6.1':Datapoint-x1, '1.3.6.2':Datapoint-y1, '1.1.5.4':Datapoint-z1, '1.2.7.3':Datapoint-q1} Device2 Mib2 {'1.3.6.1':Datapoint-x2, '1.3.6.2':Datapoint-y2, '1.1.5.4':Datapoint-z2, '1.2.7.3':Datapoint-q2} In the example Datapoint-xx should be read as, some unique data point in memory. The key in the Mib object is important because it is used by the Datapoint is points to to look up its current value on the given device, which is why I asked the two questions. In my program I use snmp to look up values on network devices. i.e. 'Go find the value of 1.3.6.1 from device1.' I want to be able to say something like Device1.Mib['1.3.6.1].update(), and the correct Datapoint discovers its key (oid) and parent device, then executes an snmp query that looks something like this snmpget(keyThatPointsToSelf, parentObj.ipAddress, parentObj.paramx) I know of course I could keep all this in a relational database and do a bunch of queries, but that would really suck! and I would have to track all the Devices and oids my self. What I really want is smart objects that think for me--thats what computers are for, right? ;) I thought my design was a wonderfuly beautiful use of python an oop, but perhaps I am mistaken and there is a much more pragmatic way to get the job done. In the end preformance doesn't matter a lot. The front end will be web based, so if the db can process faster than http/javascript and user Bob who has to mouse, then everything will be fine. Let me know what you think Thanks much --Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: curious problem with large numbers
Scott David Daniels wrote: Steve Holden wrote: Scott David Daniels wrote: Terry Reedy wrote: On my Windows machine with 2.2.1, I get exactly what you expected: 1e1 1.#INF ... If you get wrong behavior on a later version, then a bug has been introduced somewhere, even perhaps in VC 7, used for 2.4. Nope, it is also there for 2.3.4 (May 25 2004, 21:17:02) A further Windows data point from Cygwin: Python 2.4 (#1, Dec 4 2004, 20:10:33) [GCC 3.3.3 (cygwin special)] on cygwin Type help, copyright, credits or license for more information. 1e1 Inf regards Steve Aha! Same version (2.3.4): Idle: 1e1 1.0 import struct; struct.pack('d', 1e1) '\x00\x00\x00\x00\x00\x00\xf0?' (which is actually 1.0) python via command line (readline support): 1e1 1.#INF import struct; struct.pack('d', 1e1) '\x00\x00\x00\x00\x00\x00\xf0\x7f' Note the difference in the final byte. Same results (command line vs. Idle) for 2.4.1. Good catch! Same for me: Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 Type copyright, credits or license() for more information. ... IDLE 1.1 1e1 1.0 and note this happens only at the IDLE interactive interpreter: import testidleinf 1.#INF (where testidleinf is print 1e1) On a quick inspection, it seems that IDLE just passes the input to code.InteractiveInterpreter (after removing tabs and normalizing EOL), but calling code.InteractiveInterpreter directly does not reproduce the error... (still in IDLE at the interactive prompt): from code import InteractiveInterpreter II = InteractiveInterpreter() II.runsource(1e1) 1.#INF False hmmm... Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Scott David Daniels wrote: Matthew Thorley wrote: This may be a very rudimentary question, but here goes: From your questions, I believe you are not thinking of values as being distinct from the names and data structures that refer to them. What is the parent of 23 in the following expression? 1 + 11 * 2 If you know that, what is the parent of 293 in the same expression? If I have a simple dictionary, where the value is a class or function, is there an interface through which it can discover what its key is? Similar to index() for list. def keyfor(dictionary, element): for key, value in dictionary.iteritems(): if value == element: # value is element if identity quest return key raise ValueError, element On a similar note, if one object is part of another, This is the idea you have wrong. In C, C++, Java, and Fortran you might have objects part of other objects, but in Python objects refer to each other. How about this: class Holder(object): pass v = [1 + 11 * 2] w = [1, v, 3] d = {1: v} o = Holder() o.x = v What is the parent of v? Or even worse: v = [1] v[0] = v What is the parent of v now? --Scott David Daniels [EMAIL PROTECTED] I see what your saying, but I my situation the values of the dictionary are unique objects created by a parent object. Sorry :(, I didn't explain that very well in my first post. When the 'root object' is 'global' I see what your saying, but when class A: def __init__(self, container): self.container=container class B(dict): def magice_get_parent(self): ... class special_value(): def __init__(self, val): self.val = val def magic_get_key(self): ... parent = A(B) parent.container[x] = special_value(1) parent.container[y] = special_value(2) parent.container[z] = special_value(1) In this example, in my mind, the following should happen, and in theory using introspecition, actualy possible. child = parent.container D.magic_get_parent() #returns parent value1 = parent.container[x] value2 = parent.container[y] value3 = parent.container[z] value1.magic_get_key() #returns x value2.magic_get_key() #returns y value3.magic_get_key() #returns z Let me know if I'm nutz! --Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Matthew Thorley wrote: Steve Holden wrote: while not impossible (using Python's excellent introspection facilities) is way beyond what most people would consider practical. Obviously the garbage collector has to solve this problem, but you *really* don't want to be doing this stuff in Python unless you absolutely *must*. regards Steve Thanks very much for the detailed reply. Let me explain in a little more detail what I am doing, and perhaps you could let me know if I am going about it the wrong way. I am creating an object database to store information about network devices, e.g. switches and routers. At the top level there are device objects, every device object contains a mib object--and other various properties--where the actual data is stored. The mib object is a dicitionary--or at least subclasses dictionary--where the key is the oid (a unique string) and the value is a special object called Datapoint. Datapoints come in two types Static and Dynamic, and every Datapoint is unique. So a simple structure looks something like this: Device1 Mib1 {'1.3.6.1':Datapoint-x1, '1.3.6.2':Datapoint-y1, '1.1.5.4':Datapoint-z1, '1.2.7.3':Datapoint-q1} Device2 Mib2 {'1.3.6.1':Datapoint-x2, '1.3.6.2':Datapoint-y2, '1.1.5.4':Datapoint-z2, '1.2.7.3':Datapoint-q2} In the example Datapoint-xx should be read as, some unique data point in memory. The key in the Mib object is important because it is used by the Datapoint is points to to look up its current value on the given device, which is why I asked the two questions. In my program I use snmp to look up values on network devices. i.e. 'Go find the value of 1.3.6.1 from device1.' I want to be able to say something like Device1.Mib['1.3.6.1].update(), and the correct Datapoint discovers its key (oid) and parent device, then executes an snmp query that looks something like this snmpget(keyThatPointsToSelf, parentObj.ipAddress, parentObj.paramx) I know of course I could keep all this in a relational database and do a bunch of queries, but that would really suck! and I would have to track all the Devices and oids my self. What I really want is smart objects that think for me--thats what computers are for, right? ;) I thought my design was a wonderfuly beautiful use of python an oop, but perhaps I am mistaken and there is a much more pragmatic way to get the job done. In the end preformance doesn't matter a lot. The front end will be web based, so if the db can process faster than http/javascript and user Bob who has to mouse, then everything will be fine. Let me know what you think Thanks much --Matthew I think that since each Datapoint appears to be unique, the simplest thing to do is to include a reference to the parent object as an attribute of the datapoint. Presumably when you create the Datapoint you already know which Device and Mib it's going to be stored in, so why make work for yourself when you can trade a little storage and make the whole thing easy? You might also be interested to know that the Python Software Foundation funded the development of a Python package to support SNMP as a part of its first round of grant funding last year, so there's at least one other person working on this stuff! http://www.python.org/psf/grants/ regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Matthew Thorley wrote: I am creating an object database to store information about network devices, e.g. switches and routers. Possible usefull pages? http://www.python.org/doc/essays/graphs.html more at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/119466 -- hilsen/regards Max M, Denmark http://www.mxm.dk/ IT's Mad Science -- http://mail.python.org/mailman/listinfo/python-list