Re: subprocess.Popen and replacing the shell pipe line
Probably the quoting of the argument to grep. try this instead: > p1 = Popen(['grep', 'Sep 22', '/var/log/auth.log'], stdout=PIPE) etc Jeff -- http://mail.python.org/mailman/listinfo/python-list
Re: Using '__mul__' within a class
For the same reason that def f(z): z *= z c = 1 f(c) print c prints 1. Jeff pgpF5jQBO3otJ.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Using '__mul__' within a class
Oops! I should have used '2' in the example, or some other number. I was trying to make a point about what x *= ... means when it is in the context of a function (or method) body. I think this is key to understanding what Python did in the case the user posted. Being rude wasn't my intent at all. Jeff pgpLp61GyJ0st.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting a number out of a string
You can index into a string: >>> s = '06897' >>> s[2] '8' You can also turn each character into an integer, in various ways: >>> [int(c) for c in s] [0, 6, 8, 9, 7] >>> map(int, s) [0, 6, 8, 9, 7] Jeff pgpDMq5e3RucB.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Human readable number formatting
Compared to your program, I think the key to mine is to divide by "limit" before taking the log. In this way, things below the "limit" go to the next lower integer. I think that instead of having 'step' and 'base', there should be a single value which would be 1000 or 1024. import math def MakeFormat(prefixes, step, limit, base): def Format(n, suffix='B', places=2): if abs(n) < limit: if n == int(n): return "%s %s" % (n, suffix) else: return "%.1f %s" % (n, suffix) magnitude = math.log(abs(n) / limit, base) / step magnitude = min(int(magnitude)+1, len(prefixes)-1) return '%.1f %s%s' % ( float(n) / base ** (magnitude * step), prefixes[magnitude], suffix) return Format DecimalFormat = MakeFormat( prefixes = ['', 'k', 'M', 'G', 'T'], step = 3, limit = 100, base = 10) BinaryFormat = MakeFormat( prefixes = ['', 'ki', 'Mi', 'Gi', 'Ti'], step = 10, limit = 100, base = 2) values = [0, 1, 23.5, 100, 1000/3, 500, 100, 12.345e9] print [DecimalFormat(v) for v in values] print [BinaryFormat(v) for v in values] pgpJgQXD2YDZW.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Wits end with Python and cron
You need to "export" the variables. otherwise, they exist only in the shell, not in the processes it starts. Jeff pgplXE5VuF44A.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing and prompting adds a mysterious extra space
Use sys.stdout.write instead of print. It will solve these problems you are having. If you really want to know what's going on, read the language manual, http://docs.python.org/ref/print.html It explains the behavior of this extra space, which is output by a successive 'print' statement. The implementation uses an attribute called 'softspace', which is described in http://docs.python.org/lib/bltin-file-objects.html Jeff pgpRTjpCVfUyv.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Statement orders
Here's one case where it's bad to call update. def perform_longrunning_calculation(): time.sleep(1) app.update() time.sleep(1) suppose you kick this off with a keybinding, such as: app.bind("c", lambda e: perform_longrunning_calculation()) the calculation takes 2 seconds, and after 1 second update() is called, possibly to make sure that the user interface is repainted. But imagine that in the first second, "c" has been pressed. That event will be handled, and perform_longrunning_calculation will be entered *again*, recursively. The nesting looks something like this: app's mainloop handling keypress perform_longrunning_calculation() app.update() handling keypress perform_longrunning_calculation() by calling update_idletasks instead of update, the keypress event is not handled, so it's impossible to enter perform_longrunning_calculation a second time. Jeff pgpF8UhzLmmsd.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Statement orders
> would it be advisable to guard against this with something like this? > > def perform_longrunning_calculation(): > if not app.busy: > app.busy = 1 [...] By using that kind of construct, instead of using update_idletasks(), you force all code to be aware of and manage the app.busy flag. Another difference is that with the original code changed to use update_idletasks(), perform_longrunning_calculation *will* be called twice, the second time after the first one completes. With your code, it will be called once if the second keypress comes within one second, and twice if called after one second is up. (though a second update before setting busy back to false will make it be called only once) So in also depends on what you want your application to do in these cases. Jeff pgpZrbYdr8Uyk.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert hex to string
it *is* a string. because most of the characters are not printable, they're presented using hex notation when you repr() the string. That's what entering an expression in the interactive interpreter does (except that it never prints 'None'). If you really want to write whatever those bytes are, then do something like >>> print x or >>> sys.stdout.write(x) Jeff pgpmf6usUJ6jW.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception raising, and performance implications.
As for performance, you'll need to benchmark it. However, I think the functionality you're asking for is available as inspect.currentframe(), and if the implementation is in "C" it may have a tiny performance advantage over the Python version. Jeff pgpENIs7apfaF.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception raising, and performance implications.
On Mon, Oct 03, 2005 at 02:34:40PM -0700, leo wrote: > I come from a java background, where Exceptions are a big Avoid Me, but > are the performance implications the same in Python? We're expecting a > big load on our app (100,000 users/hour) , so we'd like to be as tuned > as possible. I don't know what you do for each user, but here's a data point: My 1.8GHz Sempron (firmly in the "budget" category) uses a fairly unoptimized piece of software called aether[1] to serve my blog and a few other things. It uses Apache and good old fashioned cgi-bin one-process-per-request to serve up most pages. It parses a substantial amount of Python code each time with execfile (not using byte-compiled files). It still does this in 87ms on average (albeit for a simple page), or about 41k requests per hour. By simply buying a faster CPU, or by avoiding execfile, or by using a higher-performance technology than CGI, or with judicious use of caching, I'm sure I could reach 100,000 requests per hour, and by using several of the techniques together I might be able to reach 400k requests per hour. Probably it would be after these fairly obvious, high-level optimization ideas were exhausted that I would look at the code at a microscopic level for optimization ideas. But because my website is stuck on the slow end of a DSL line, there's not much point to any of this. Jeff [1] http://www.logarithmic.net/pfh/aether (not my site) pgpnD6QduJu28.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie regular expression ?
Here are two ideas that come to mind: files = glob.glob("UNQ*.dat") + glob.glob("Unq*.dat") + glob.glob("unq.dat") files = [f for f in glob.glob("*.dat") if f[:3] in ("UNQ", "Unq", "unq")] Jeff pgp30Rue2EGi7.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: how to debug when "Segmentation fault"
I've rewritten your middle example to be clearer. $ python2.4 Python 2.4.1 (#1, May 16 2005, 15:15:14) [GCC 4.0.0 20050512 (Red Hat 4.0.0-5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> type("Y", (type,), {"mro": lambda x: [float]})("X", (), {}) and here's another one for you to enjoy: >>> import marshal >>> f = lambda: None >>> code = >>> marshal.loads(marshal.dumps(f.func_code).replace('d\0\0S','d\xff\xffS')) >>> f.func_code = code >>> f() Jeff pgpJRlU4jt6JK.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get any available port
Apparently, calling bind() with a zero "port" will choose some available port number, as demonstrated by this program: import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("", 0)) print s.getsockname() Here's how it behaved over several runs: $ python soc.py ('0.0.0.0', 34205) $ python soc.py ('0.0.0.0', 34206) $ python soc.py ('0.0.0.0', 34207) I don't know for sure whether this is standard behavior for sockets, or whether it's a special behavior of linux. Jeff pgpdXrF07MIY9.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get any available port
On Tue, Oct 04, 2005 at 05:19:37PM -0400, Mohammed Smadi wrote: > what else would you do? I am using examples from the web and they all > bind to a port at the localhost before connecting to the remote host. [...] the web must be stupider than I thought. Here's how Python's own ftplib connects to an ftp server (dedented for clarity): try: self.sock = socket.socket(af, socktype, proto) self.sock.connect(sa) except socket.error, msg: ... there's no need to call bind() before connect. Jeff pgpHhphJklBxU.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: how to debug when "Segmentation fault"
Interesting. I'd noticed that the metaclass one didn't crash my 2.2, but I didn't check the marshal one. This one core dumps in 'Python 2.2.2 (#1, Feb 24 2003, 19:13:11) ... linux2' but inexplicably prints 'keys' when I ran it on the 2.4.1 I used in my last post. >>> import marshal >>> f = lambda: None >>> d = marshal.dumps(f.func_code).replace('\0\0S', '~~S') >>> f.func_code = marshal.loads(d) >>> f() This crashes both my 2.2.2 and 2.4.1: >>> import marshal >>> f = lambda: None >>> d = marshal.dumps(f.func_code).replace('\0\0S', '\xff\xffS') >>> f.func_code = marshal.loads(d) >>> f() Jeff pgpHJKzMCPaq6.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: How to create temp file in memory???
Here's how: 1. open your web browser 2. visit the url http://docs.python.org 3. click the link "Library Reference" (keep this under your pillow) 4. scan down the list of modules until you see these two items: 4.6 StringIO -- Read and write strings as files 4.7 cStringIO -- Faster version of StringIO 5. ? 6. profit Jeff pgpH8v1zzL5Ma.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: updating local()
I'm surprised you found any example of 'locals().update' that worked. Here's one that doesn't work: def f(y): locals().update({'x': y}) return x print f(3) # prints 3? Jeff pgpLVe48NBWmT.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: 2d array slicing problem
Do you have a simple program that demonstrates the problem? I have an x86 machine with Python 2.3, and an x86_64 machine with Python 2.4 available. I wrote a simple test program which performs a slice operation, but it behaves the same on both platforms. Here's the program: # import numarray, sys, os print "python: ", sys.version_info print "numarray:", numarray.__version__ print "CPU: ", os.uname()[-1] n = numarray.arange(10) o = numarray.outerproduct(n,n) p = o[3:7,6:10].copy() q = numarray.outerproduct([3,4,5,6], [6,7,8,9]) print "Success: ", numarray.alltrue(p.ravel() == q.ravel()) # Here are the two results I gathered: python: (2, 4, 1, 'final', 0) numarray: 1.3.3 CPU: x86_64 Success: True python: (2, 3, 2, 'final', 0) numarray: 0.9 CPU: i686 Success: 1 Jeff pgpBMzxSrksM3.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Function decorator that caches function results
On Sat, Oct 08, 2005 at 01:53:28PM +, Duncan Booth wrote: > Unless the results stored in the cache are very large data structures, I > would suggest that you simply store (args,kwargs) as the cache key and > accept the hit that sometime you'll cache the same call multiple times. ... except that dicts cannot be dict keys Another 'memoize' decorator uses this to get the key: kw = kwargs.items() kw.sort() key = (args, tuple(kw)) http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/325905 Jeff pgp4a5R0heoxU.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: List performance and CSV
You'll probably see a slight speed increase with something like for a in CustomersToMatch: for b in Customers: if a[2] == b[2]: a[1] = b[1] break But a really fast approach is to use a dictionary or other structure that turns the inner loop into a fast lookup, not a slow loop through the 'Customers' list. Preparing the dictionary would look like custmap = {} for c in Customers: k = c[2] if k in custmap: continue custmap[k] = c and the loop to update would look like for a in customerstomatch: try: a[1] = custmap[a[2]][1] except KeyError: continue (all code is untested) In "big-O" terms, I believe this changes the complexity from O(m*n) to O(m+n). Jeff pgpiNVhiQU0Jm.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: non descriptive error
On Mon, Oct 10, 2005 at 09:12:13AM +1000, Timothy Smith wrote: > FAYI i have already found it and it was a wrongly indented code block :/ When indentation leaves an illegal program structure, Python gives a very informative error message, such as File "/tmp/x.py", line 3 return 3 ^ IndentationError: unindent does not match any outer indentation level Jeff pgpQJn4I7qnof.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Idle bytecode query on apparently unreachable returns
On Mon, Oct 10, 2005 at 12:20:13AM +0100, Tom Anderson wrote: > What puzzles me, though, are bytecodes 17, 39 and 42 - surely these aren't > reachable? Does the compiler just throw in a default 'return None' > epilogue, with routes there from every code path, even when it's not > needed? If so, why? I think the short answer is "yes". They're unreachable, and they're thrown in by default. It's possible that this could be fixed with a slightly smarter compiler, but the performance difference is likely to be in the noise. What's one cache miss (because the bytecode is slightly larger) compared to the total cycles used in, say, the LOAD_GLOBAL 'fib'? My bet is that this optimization would not pay off in measurable run-time gains. Jeff pgpPYgLkYVBPg.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Wanted: Python module allowing direct access to raw sectors of harddrives (MFT, boot sector, etc.) in MS Windows
I took the advice from this web page: http://support.microsoft.com/kb/q100027/ (I don't know how this extends to floppies, and the 9x family of OSes isn't listed in "applies to", so this may not help your case) Here, I open "physical drive 0" and see that the magic number indicates a valid boot record. I believe it is the MBR. >>> f = open('.\\PhysicalDrive0', 'rb') >>> f.read(512)[-2:] 'U\xaa' # that is, hex 55 AA I don't know much about low-level filesystem or partition details--I got the tidbit about the 55 AA magic number from http://www-uxsup.csx.cam.ac.uk/pub/doc/suse/sles9/adminguide-sles9/ch08.html Jeff pgpGGGmmqn6Yd.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Python name lookups
On Mon, Oct 10, 2005 at 03:02:30PM -0700, Dave wrote: > Hello All, > > As far as I understand, Python deals with a lot of > string objects, i.e. it looks up all names. Is there a > way to find out how many name lookup operations take > place in a Python program? Is it the name lookup > operation or hash operation that degrades performance? > What function does Python use for name lookup > operations? Is it the lookdict function in > Objects/dictobject.c? lookdict_string is used for most lookups of the form obj.attr because they are never found to have non-string keys entered or searched. Furthermore, most of these string keys are "interned", which I believe makes the check if (ep->me_key == NULL || ep->me_key == key) return ep; return a result without comparing the contents of the strings. Using gdb, I tried to get a rough estimate of how often pystone calls _PyString_Eq while running a benchmark program, compared to how many times it calls lookdict_string. Here's a simplified gdb session: $ gdb ./python (gdb) break _PyString_Eq (gdb) ignore 1 10 (gdb) break lookdict_string (gdb) ignore 2 10 (gdb) r Lib/test/pystone.py 200 Pystone(1.1) time for 200 passes = 0.13 This machine benchmarks at 1538.46 pystones/second (the low score is because of the overhead of gdb breaking on those functions) (gdb) info b Num Type Disp Enb AddressWhat 1 breakpoint keep y 0x0807e126 in _PyString_Eq breakpoint already hit 4040 times 2 breakpoint keep y 0x0807573c in lookdict_string breakpoint already hit 57254 times So there seem to be less than .1 string comparisons per name lookup. In fact, for 200 or for 5 passes, there are still just 4040 calls to _PyString_Eq, which would give .003 string comparisons per name lookup, assuming the numbers for lookdict_string scale with the number of passes. Jeff pgpSzIG9XJIPN.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: pyparallel and MAKE controller board for CRYDOM AC/DC switches
Recalling the parallel pinout, pin 3 is data bit 2. Have you tried def BIT(x): return 1< pgprfRaNUTBBH.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Why asci-only symbols?
I'm not aware of any PEPs on the subject, but google groups turns up some past threads. Here's one from February 2004: http://groups.google.com/group/comp.lang.python/browse_frm/thread/d5fcc1c8825a60dc/96856af647ce71d5 I didn't immediately find this message of Guido's that everyone's talking about as this thread began, though. Jeff pgpIReeQj97C5.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for info on Python's memory allocation
While there may be a discussion somewhere in the archives, the comments in listobject.c are enlightening too. /* Ensure ob_item has room for at least newsize elements, and set * ob_size to newsize. If newsize > ob_size on entry, the content * of the new slots at exit is undefined heap trash; it's the caller's * responsiblity to overwrite them with sane values. * The number of allocated elements may grow, shrink, or stay the same. * Failure is impossible if newsize <= self.allocated on entry, although * that partly relies on an assumption that the system realloc() never * fails when passed a number of bytes <= the number of bytes last * allocated (the C standard doesn't guarantee this, but it's hard to * imagine a realloc implementation where it wouldn't be true). * Note that self->ob_item may change, and even if newsize is less * than ob_size on entry. */ [...] /* This over-allocates proportional to the list size, making room * for additional growth. The over-allocation is mild, but is * enough to give linear-time amortized behavior over a long * sequence of appends() in the presence of a poorly-performing * system realloc(). * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ... */ "linear-time amortized behavior" (i.e., that list.append() is O(1) on average) is the important characteristic you're probably looking for. CVS source for listobject.c can be seen here: http://cvs.sourceforge.net/viewcvs.py/python/python/dist/src/Objects/listobject.c?rev=2.227&view=markup Jeff pgp4hldyPHMvN.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionnaries and lookup tables
On Tue, Oct 11, 2005 at 11:06:32AM -0700, [EMAIL PROTECTED] wrote: > Note that when I type: > >>>dir(D) [...] > the functions __ge__, __gt__, __lt__, __le__ seem to be non-implemented > but there is some __doc__ in them. Is there the intention to do > something similar as is described above or are they here for some > (future) dictionnary comparison purposes? Sure they're implemented. That's why I can compare dictionaries for equality or order. >>> d = {1: None} >>> e = {1: None} >>> f = {2: None} >>> d == e True >>> d == f False >>> d < f or f < e True If the operation you need to optimize is "find the largest element that is smaller than X", then perhaps you want to use the bisect module. This involves keeping your information in a sorted list of tuples, instead of in a dictionary. However, removing or inserting items has O(n) complexity instead of O(1). Jeff pgpoNBi46YeD2.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: unable to import os
Concatenating filenames with "+" is seldom what you want. Instead, you should use os.path.join (or, to decrease portability, nt.path.join). Jeff pgpUKTuVnB2qh.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Profiling Python using gprof
It should be the same as for any program $ program-compiled-with-pg $ gprof /path/to/program-compiled-with-pg you'll need to make sure that python is not only *compiled* with -pg but that the *link line* contains -pg as well. That's a common "gotcha" when it comes to profiling. Jeff pgpQyzwYpYrtn.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: are there internal functions for these ?
there's 'os.rename' for moving (single) files, and several functions in 'shutil' for moving and copying trees of files. Jeff pgpS5MgPvRJQZ.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: [PIL]: Question On Changing Colour
If you're always going from grey to tinted, then the easiest way is to treat it as a 'P' image with a special palette. I believe this function will prepare the palette: def make_palette(tr, tg, tb): l = [] for i in range(255): l.extend([tr*i / 255, tg*i / 255, tb*i / 255]) return l Here's an example: import Image A, B, C = map(chr, [64, 128, 192]) i = Image.fromstring( 'P', (4,4), ''.join([ B,B,B,B, B,C,C,A, B,C,C,A, B,A,A,A])) i.putpalette(make_palette(64,64,255)) i.show() Jeff pgp0XmK9KEsfO.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Python's garbage collection was Re: Python reliability
On Mon, Oct 10, 2005 at 08:37:03PM +0100, Tom Anderson wrote: > So python doesn't use the old SmallTalk 80 SmallInteger hack, or similar? > Fair enough - the performance gain is nice, but the extra complexity would > be a huge pain, i imagine. I tried to implement this once. There was not a performance gain for general code, and I also made the interpreter buggy in the process. I wrote in 2002: | Many Lisp interpreters use 'tagged types' to, among other things, let | small ints reside directly in the machine registers. | | Python might wish to take advantage of this by designating pointers to odd | addresses stand for integers according to the following relationship: | p = (i<<1) | 1 | i = (p>>1) | (due to alignment requirements on all common machines, all valid | pointers-to-struct have 0 in their low bit) This means that all integers | which fit in 31 bits can be stored without actually allocating or deallocating | anything. | | I modified a Python interpreter to the point where it could run simple | programs. The changes are unfortunately very invasive, because they | make any C code which simply executes | o->ob_type | or otherwise dereferences a PyObject* invalid when presented with a | small int. This would obviously affect a huge amount of existing code in | extensions, and is probably enough to stop this from being implemented | before Python 3000. | | This also introduces another conditional branch in many pieces of code, such | as any call to PyObject_TypeCheck(). | | Performance results are mixed. A small program designed to test the | speed of all-integer arithmetic comes out faster by 14% (3.38 vs 2.90 | "user" time on my machine) but pystone comes out 5% slower (14124 vs 13358 | "pystones/second"). | | I don't know if anybody's barked up this tree before, but I think | these results show that it's almost certainly not worth the effort to | incorporate this "performance" hack in Python. I'll keep my tree around | for awhile, in case anybody else wants to see it, but beware that it | still has serious issues even in the core: | >>> 0+0j | Traceback (most recent call last): | File "", line 1, in ? | TypeError: unsupported operand types for +: 'int' and 'complex' | >>> (0).__class__ | Segmentation fault | | http://mail.python.org/pipermail/python-dev/2002-August/027685.html Note that the tree where I worked on this is long since lost. Jeff pgp73wpVIhmAA.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: reading hebrew text file
I looked for "VAV" in the files in the "encodings" directory (/usr/lib/python2.4/encodings/*.py on my machine). I found that the following character encodings seem to include hebrew characters: cp1255 cp424 cp856 cp862 iso8859-8 A file containing hebrew text might be in any one of these encodings, or any unicode-based encoding. To open an encoded file for reading, use f = codecs.open(file, 'r', encoding='...') Now, calls like 'f.readline()' will return unicode strings. Here's an example, using a file in UTF-8 I have laying around: >>> f = codecs.open("/users/jepler/txt/UTF-8-demo.txt", "r", "utf-8") >>> for i in range(5): print repr(f.readline()) ... u'UTF-8 encoded sample plain-text file\n' u'\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\n' u'\n' u'Markus Kuhn [\u02c8ma\u02b3k\u028as ku\u02d0n] <[EMAIL PROTECTED]> \u2014 1999-08-20\n' u'\n' Jeff pgpIIx2zTStwL.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Bloodhound.Exploit.49 trojan found in MIMEBase.pyc
It's almost certainly a false positive. In my day job we run into false positive antivirus detections like this once or twice a year, and typically get a runaround from the AV vendor (who often have the gall to suggest that we should buy a copy of their broken software before they'll fix their problems) Jeff pgpjaq8H7Vm4a.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Set an environment variable
In Unix, you generally can't affect the environment of your parent program (in a broad sense, which includes environment variables, current working directory, opened files, effective user ID, etc). You have two basic choices to achieve an effect like this. First, you can start a subshell with the desired environment, something like (untested) os.environ['VARIABLE'] = 'value' os.system(os.environ['shell']) (commands like 'su' work in this way) Second, you can produce shell commands that have the effect of changing a shell's environment when they are executed. Something like: print "VARIABLE=value" To use this, the user must write something like eval `myscript.py` instead of just myscript.py this can typically be encapsulated in a shell alias or function. (commands like 'resize' (which sets the shell's idea of a terminal's size) work this way) Finally, you might be able to use an OS-specific interface like linux' ptrace to change the actual contents of a calling process's memory. I didn't immediately find an example of this, but on Linux it would consist of using PTRACE_PEEKDATA and PTRACE_POKEDATA to locate the environment in another process and modify it. The ptrace interface is not available from any standard Python module, and isn't portable anyway. (though my manpage actually says 'CONFORMING TO SVr4, ..., X/OPEN, BSD 4.3' so maybe ptrace is more portable than I believed) Jeff pgpGRSQal48ZE.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: nested escape chars in a shell command
I think you're mistaken about how 'sh -c' works. The next argument after "-c" is the script, and following arguments are the positional arguments. (what, you've never used -c in conjunction with positional arguments? me either!) Example: $ /bin/sh -c 'echo $#' a b c # i.e., a blank line $ /bin/sh -c 'echo $# 0=$0 1=$1 2=$2' a b c 2 0=a 1=b 2=c # i.e., a, b, c went to positional arguments Additionally, unless pexpect.spawn behaves differently than os.spawnv, "-c" is actually going to /bin/sh's argv[0], and you end up trying to execute /usr/bin/ssh as a shell script, which is probably not what you want! def shellquote(arg): # shell quoting technique I first read about on the 'git' development list # Everything is safely quoted inside a ''-quoted string, except a ' itself, # which can be written as '\'' (a backslash-escaped ' outside of the ''-quoted # string) return "'" + arg.replace("'", "'\\''") + "'" def shellquotelist(args): return " ".join([shellquote(arg) for arg in args]) # XXX: you may wish to assemle 'command' using shellquotelist too command1 = shellquotelist(['/bin/sh', '-c','/usr/bin/ssh','-t','-o', 'StrictHostKeyChecking no',host,command]) command2 = shellquotelist(['awk','{print %s:$0}'%host]) child = \ pexpect.spawn('/bin/sh', args=['/bin/sh', '-c', command1 + "|" + command2], timeout=30) Finally, in your case the use of awk would seem to be gratuitous. Instead, prepend the hostname to each line when you read the lines in. (this could easy or hard depending on the structure of the code where you read the output generated by child---if you do it with readline() or by iterating over the file, it is probably easy) Jeff pgp6UVusIacVl.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
os.makedirs should not succeed when the directory already exists (was Re: Python Doc Error: os.makedirs)
On Wed, Oct 19, 2005 at 09:26:16AM -0700, Dr. Who wrote: > The fact that the directory already exists is irrelevant to the function...it > still failed to create the directory. That's not true. Imagine that os.makedirs() is used inside tempfile.mkdtemp() (I looked, and it isn't) and the proposed behavior (do not raise an exception when the directory already exists) is adopted. In this case, there is a race condition between you and the attacker who guesses the next directory you will attempt to make. If he calls mkdir() before you do, then your os.makedirs() returns successfully (instead of raising an exception) and you place your files into a location that is under the control of someone else. If the attacker then makes the directory setuid himself, that files created in the directory are owned by him. Now, he can view and change the contents of these files. This can lead to a local priviledge escalation. Errors should never pass silently. Unless explicitly silenced. -- from the Zen of Python ('import this') ... and wanting them to do so may introduce a security bug in your software. If you know more about your users and their environments than I do (for instance, that none of them will ever use a multi-user computer system) maybe you should choose to wrap os.makedirs with something that silences EEXIST. But I'm glad Python does the secure thing and treats EEXIST as a failure by default. Jeff pgpWhf794NILT.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: TK question
In the FileDialog module there are both LoadFileDialog and SaveFileDialog. Is the latter what you want? Jeff pgptzMbfYw5VI.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: C replacement for Queue module
does collections.deque have a blocking popleft()? If not, it's not very suitable to replace Queue.Queue. Jeff pgp9S3BEmciKx.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Extention String returning
I think that you want to use return PyString_FromStringAndSize(buf, 8); Jeff pgp3nNxegNjmk.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Errno 8 Exec format error
On Tue, Oct 25, 2005 at 04:11:39PM +0100, Matthew Fowler wrote: > "problem with execution of xhpl on AxisProduct: [Errno 8] Exec format > error" In this case, it appears that Python is reporting the error of some operating system call, such as execve. On my operating system, here's a program which creates an executable file which is not a valid ELF file. When it is executed with execve, -1 is returned and errno is set to ENOEXEC ("Exec format error") #--- import tempfile, os t, n = tempfile.mkstemp() os.write(t, "\x7fELF\1\1\1\0\0\0\0\0\0\0\0\0\0\0\0") os.close(t) os.chmod(n, 0700) try: print os.execv(n, ['example']) finally: os.unlink(n) #--- $ python badexec.py Traceback (most recent call last): File "badexec.py", line 9, in ? print os.execv(n, ['example']) OSError: [Errno 8] Exec format error Jeff > Hi there > > I have been porting python to the Etrax platform and everything seems to > be working. > > However when I run the program in question I get: > > > Im looking for any help to try and solve this issue. Am I correct in > thinking that Python is reporting this problem? If so has anybody > experienced this problem before? > > Thank You > > Matt > -- > http://mail.python.org/mailman/listinfo/python-list > pgpc3VIheL4DC.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: textwidget.tag_bind("name", "", self.donothing) not working
I'm not sure why Tk behaves this way, but you can observe the same behavior with a "wish" script. It would seem that "break" skips any other scripts associated with item tags, but still proceeds to bindings at the widget level. Using a binding with "break" on the canvas itself may help you get the behavior you want. canvas .c -width 70 -height 70 set it [.c create text 20 20 -tags x -text example -anchor nw] .c bind x { puts "item binding"; break; } bind .c { puts "widget binding"; break } bind . { puts "toplevel binding"; break } pack .c focus .c .c focus $it Jeff pgp8HO6LWzuTY.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: sys.exit call from pythonw.exe gives error
I wrote the following small program: #--- import sys, Tkinter t = Tkinter.Tk() b = Tkinter.Button(command=lambda: sys.exit(0), text="Click to exit") b.pack() t.mainloop() #--- and ran it with pythonw.exe from python 2.3.4 on a machine running Windows NT 4.0. (I actually used Start > Run and entered "d:\python23\pythonw.exe x:\app.pyw" rather than clicking on the app.pyw icon, but this shouldn't make a difference) When the button is clicked, the application exits without giving any error like the one you described. Jeff pgp2abPBqEct6.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: taking x-window screen shot
I recommend using 'xwd' to actually get the screenshot, because xwd is installed nearly everwhere X is. xwd-format images are "documented" by the header file X11/XWDFile.h. This program works in a pipeline to convert certain xwd-format images to postscript. You can use it something like this: $ xwd | python xwdtops.py | lpr .. it should print a greyscale rendition of the clicked window to the default printer, if it accepts postscript. It's been quite awhile since I wrote this program (and later re-wrote it in C because it's a bit slow), so I'm not really familiar with this code anymore. However, I do believe that it works on common 16 and 32-bit X displays. One more thing --- if you have imagemagick installed, you may want to consider using its command "import" instead. Instead of the xwd format, it can write the image in many formats, instead of just xwd. Jeff #--- import struct, sys print "%!PS-Adobe-1.0" def get_shift(n): for i in range(32): if n & (1<> rs) gsc = .587*256/(green_mask >> gs) bsc = .114*256/(blue_mask >> bs) print "%", red_mask, green_mask, blue_mask print "%", rs, gs, bs print "%", rsc, gsc, bsc print "%", bits_per_pixel wscale = 558./window_width hscale = 720./window_height scale = min(1, wscale, hscale) rwscale = 558./window_height rhscale = 720./window_width rscale = min(1, rwscale, rhscale) print "% scale -", scale, rscale print "/Helvetica findfont 16 scalefont setfont" print "18 756 moveto" print "(%s) show" % " ".join(sys.argv[1:]) or window_name if scale < rscale: scale = rscale #print "%d %d translate" % (window_height*scale+18, 36) print "%d %d translate" % (8.5*72, 0) print "90 rotate" print "18 %d translate" % (594-window_height*scale) # 8.25" sz = 8.5*72 else: print "18 %d translate" % (750-window_height*scale) # 10.5" sz = 11*72 print "%d %d scale" % (window_width*scale, window_height*scale) print "/picstr %d string def" % window_width bytes_per_pixel = (bits_per_pixel+7)/8 row_format = "%c%d%c" % ("<>"[byte_order], window_width, "HI"[bits_per_pixel > 16]) print "%", row_format, bytes_per_line, bytes_per_pixel*window_width print "%", len(rest) print "%d %d 8 [%d 0 0 %d 0 0] {currentfile picstr readhexstring pop } image" % (window_width, window_height, window_width, window_height) if 0: print "00"* window_width for row in range(window_height-2): print "00" + "c0" * (window_width-2) + "00" print "00"* window_width else: for row in range(window_height-1, -1, -1): a = row * bytes_per_line b = a + bytes_per_pixel*window_width for pixel in struct.unpack(row_format, rest[a:b]): r = (pixel & red_mask) >> rs g = (pixel & green_mask) >> gs b = (pixel & blue_mask) >> bs gray = int(rsc*r+gsc*g+bsc*b) sys.stdout.write("%02x" % gray) sys.stdout.write("\n") print print "showpage" #--- pgph6lA0qUdrs.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter problem
On Mon, Oct 31, 2005 at 03:17:05PM -0800, dale cooper wrote: > Thanks, but I've got another question: > > can't find Tcl configuration script "tclConfig.sh" This file comes from the following package: $ rpm -qf /usr/lib*/tclConfig.sh tcl-devel-8.4.9-3 Fedora generally splits packages which are libraries into "foo" and "foo-devel" (and maybe others). Jeff pgpNF5sPkcOqr.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Where to save classes? How to access classes?
This section of the tutorial discusses the module search path: http://docs.python.org/tut/node8.html 6.1.1 The Module Search Path When a module named spam is imported, the interpreter searches for a file named spam.py in the current directory, and then in the list of directories specified by the environment variable PYTHONPATH. This has the same syntax as the shell variable PATH, that is, a list of directory names. When PYTHONPATH is not set, or when the file is not found there, the search continues in an installation- dependent default path; on Unix, this is usually .:/usr/local/lib/python. Actually, modules are searched in the list of directories given by the variable sys.path which is initialized from the directory containing the input script (or the current directory), PYTHONPATH and the installation-dependent default. This allows Python programs that know what they're doing to modify or replace the module search path. Note that because the directory containing the script being run is on the search path, it is important that the script not have the same name as a standard module, or Python will attempt to load the script as a module when that module is imported. This will generally be an error. See section 6.2, ``Standard Modules,'' for more information. Jeff pgpfmXpeuyJhL.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: install warning
You are importing and using, directly or indirectly, the "strop" module. Here's an example from the interactive interpreter which triggers the warning: $ python2.3 Python 2.3.3 (#1, May 7 2004, 10:31:40) [GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import strop >>> strop.strip(" abc ") __main__:1: DeprecationWarning: strop functions are obsolete; use string methods 'abc' Most of the things in strop are now simply methods on string objects: >>> " abc ".strip() 'abc' Another way to prevent the warning from being printed is through use of the 'warnings' module, which is documented on docs.python.org. Jeff pgpyzifxJbrUA.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: fcntl.flock() not working when called from a function
The file you open() may be closed as soon as it is no longer possible to refer to it. So in the first case, because the top-level variable 'f' continues to refer to the opened file, the file may not be closed. In the second case, no variable refers to the opened file after lock() returns, so Python is free to close the file at any time. In fact, Python happens to close the function exactly when lock() returs. If you want an open file descriptor that is not automatically closed, use os.open(). Or, store the file descriptor somewhere so you can later close() or unlock it at an appropriate time. Jeff pgpv7jRCugW7o.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter and X11
There should be no problem with this. After all, even the "greeter" is just an X application. Depending on which login manager you use (xdm/kdm/gdm/whatever) the details of getting your Tkinter app to actually be run will vary, though. In gdm, it looks like adding it to the file /etc/X11/gdm/Init/default may be the ticket. It is probably best to run app.tk.call("rename", "send", "") in your program, for the reasons outlined in the send(n) manpage: SECURITY The send command is potentially a serious security loophole. On Unix, any application that can connect to your X server can send scripts to your applications. These incoming scripts can use Tcl to read and write your files and invoke subprocesses under your name. Jeff pgpboAbTGH8cL.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: O_DIRECT on stdin?
I think this is fcntl(..., F_SETFL, ...), so something like import os, fcntl, sys flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL) flags |= os.O_DIRECT fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags) Jeff pgpx52DBavw3Y.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: O_DIRECT on stdin?
Here's some text from my open(2) manpage: Transfer sizes, and the alignment of user buffer and file offset must all be multiples of the logical block size of the file system. It's unlikely that in practice you can get Python's sys.stdin.read() or os.read() to reliably use a buffer that fits the alignment restriction. However, a small "C" extension could provide something like os.read() which *does* meet the restriction. The meat of it would look something like #define PAGESIZE 4096 // a guess which may be right for x86 linux #define REQUESTSIZE 1048576 ssize_t result; char *buf, *base; PyObject *pyresult; buf = malloc(bufsize, REQUESTSIZE + PAGESIZE - 1); base = round_up(buf, PAGESIZE); result = read(0, base, REQUESTSIZE); if(result == -1) { set python error from errno pyresult = NULL; goto DONE; } pyresult = PyString_FromStringAndSize(base, result); DONE: free(buf); return pyresult; Here's a clever but untested "C" macro that claims to implement round_up: #define round_up(amount, align) amount) - 1) | ((align) - 1)) + 1) Jeff pgpvapMvYEo9L.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: user account logon from python
"login APIs" vary widely from system to system. Classic Unix systems use calls like getpwent and crypt to check passwords, and then call setuid, setgid and setgroups to set the identity of the user who is logging in. These are all available in stock Python, check the library reference for more details. Other login-time activities, like writing utmp entries, may not be directly available in stock Python modules. Many modern Linux systems use something called 'pam' for login-related activities, and there seems to be something called 'python-pam' out there, but I've never used it. Graphical login managers have their own additional requirements, such as starting and stopping the X server, managing the X authentication information, etc. Jeff pgpGY28NFZu6j.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: struct, IEEE-754 and internal representation
Use 'd' as the format character for 64-bit double precision numbers with struct. >>> x = 148.73 >>> unpack(">> unpack(" pgpB2b9owxZs7.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: x, y coordinates in Tkinter canvas
Let's find out! This program creates a series of rectangles, with the color going from black to nearly white. White corresponds to higher x and y coordinate values. So the answer to your question can be seen in the output of the program. import Tkinter c = Tkinter.Canvas(width=220, height=220) c.pack() for i in range(0, 200, 20): lum = i * 255 / 200 color = "#%02x%02x%02x" % (lum, lum, lum) c.create_rectangle( i, i, i+40, i+40, fill=color) c.mainloop() pgpSbn6OJyaNx.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: changeing users on linux
On Fri, Nov 11, 2005 at 11:25:56PM -0500, Mike Meyer wrote: > You didn't say what the password was for, so I skipped asking for it. You're a real comedian. Jeff pgpxqmFz5jalu.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter and the re/sizing of columns in a grid
Normally, an entry widget requests horizontal space equal to the value of the width= option times the "average" character width of the font it displays. Setting it to 0 makes the entry request exactly enough width to show the string it contains. Making them sticky to the east and west sides of their areas make them grow to the size of the grid cell, if it is larger than their requested size. Making the grid column have nonzero weight means that if the user resizes the window to be wider, extra space will be given to the entry widgets. Perhaps this program does what you're looking for: #--- from Tkinter import * t = Tk() l1 = Label(t, text="String 1:") l2 = Label(t, text="String 2:") e1 = Entry(t, width=0); e1.insert("end", "eggs"); e1.focus() e2 = Entry(t, width=0); e2.insert("end", "spam") l1.grid(row=0, column=0, sticky="w") l2.grid(row=1, column=0, sticky="w") e1.grid(row=0, column=1, sticky="ew") e2.grid(row=1, column=1, sticky="ew") t.grid_columnconfigure(1, weight=1, minsize=120) t.wm_title("entry width demo") t.mainloop() #--- pgp6Rij7z8hDA.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterator addition
On Sun, Nov 13, 2005 at 09:44:43PM +, Bengt Richter wrote: > even if expr1 had a __unaryop__ method, > expr1 - expr2 > could not become > expr1.__unaryop__(-expr2) > unless you forced the issue with > expr1 (-expr2) as opposed to being a function call? I don't think you've solved the ambiguity problem yet. Jeff pgpywgKNiDRtk.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting dominoes
So if you have the dominoes (1 2), (3 2) and (3 1) you must arrange them as 1|2|3 2|3|1 ? If so, then it seems to me your algorithm is this: 1. pick an arbitrary domino to be first, and an arbitrary side to be the "top" 2a. until the dominoes are exhausted, pick the other domino with the same digit as the "bottom" of the last domino picked, and put that digit on the top. 2b. If there is no domino with the same digit, then pick an arbitrary domino as in step 1 Jeff pgp3e5SVRjwKK.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: how do i use "tkinter.createfilehandler" with a regular c program?
Compared to your program, I * Made sure that the slave program actually flushed its stdout buffers * didn't call read(), which will by default continue reading until it reaches EOF, not merely read the available data #!/usr/bin/env python import sys, time, Tkinter, itertools, _tkinter, os if '-slave' in sys.argv: for i in itertools.count(): time.sleep(1) print "This is a line of output:", i sys.stdout.flush() raise SystemExit root = Tkinter.Tk() root.wm_withdraw() fh = os.popen('%s -slave' % sys.argv[0]) def reader(*args): line = fh.readline() if not line: print "EOF from slave" raise SystemExit print "from slave: %r" % line _tkinter.createfilehandler(fh, Tkinter.READABLE, reader) root.mainloop() pgpGq56Vnc7dS.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Tix BUG? Where to submit?
Since this is a bug in Python (Tix.py), it should be submitted to the Python Patch tracker at sf.net/projects/python You need a free "sourceforge" account to submit an item to the bug or patch tracker. Jeff pgp1zmMFcYHrz.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone tell me if pygame and Tkinter can work together?
It's likely to be a challenge; each one has its own "event loop", and the exact pitfalls of trying to use both at the same time probably depend on the operating system too. For instance, on X, Tk and SDL probably both expect full control of the handlers registered with XSetErrorHandler and XSetIOErrorHandler, but Xlib only permits a single handler. Jeff pgpitiDFj2031.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone tell me if pygame and Tkinter can work together?
On Tue, Nov 15, 2005 at 06:33:40PM -0700, Nathan Pinno wrote: > Thanks. I was going to use TKInter to pop up a message box, then use pygame > to run the game and display the score on the playing surface. Is this still > possible (I'm using Python 2.4.1 and Pygame 1.7.0 on WinXP with Service Pack > 2)? This is more likely to work than the scenario I first considered, where both GUI libraries would be in use at the same time. With my understanding of how X works, and my knowledge of Tk, you'd probably be successful if what you want to do is first use GUI Library A, and then GUI Library B. This resolves the issue with XSetErrorHandler, for instance, because you'll be completely done with Library A at the time you call the initialization routines of Library B. It also resolves the event loop problem, because you never need to allow Libraries A and B to receive events during the same phase of the program. I don't know anything about Windows, and I haven't actually given this scenario a try on Linux either, so it's still all idle speculation on my part. Actual experimentation on your part wouldn't be that hard, though. Just write the simplest possible Tk program as a callable function 't()' and the simplest possible pygame program as a callable function 'p()', and then see what happens when you run them back to back: def t(): import Tkinter t = Tkinter.Tk() Tkinter.Button(t, command = t.destroy).pack() t.mainloop() def p(): likewise t() p() Jeff pgpa7tscRa2rv.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter: scrollbar in grid-managed frame
Tkinter "frame"s don't scroll. Instead, you need to use something like bwidget's "ScrollableFrame" widget. You may want to combine this with bwidget's "ScrolledWindow". Below is an example which uses my "pybwidget" package, available at http://tkinter.unpy.net/bwidget/ # -- import Tkinter, bwidget t = Tkinter.Tk() s = bwidget.ScrolledWindow(t, auto="vertical", scrollbar="vertical") f = bwidget.ScrollableFrame(s, constrainedwidth=True) g = f.getframe() for i in range(20): Tkinter.Label(g, text="Field %d: " % i).grid(row=i, column=0, sticky="w") Tkinter.Entry(g, width=25).grid(row=i, column=1, sticky="ew") g.grid_columnconfigure(1, weight=1) s.setwidget(f) s.pack(fill="both", expand=1) t.mainloop() # -- Jeff pgplDevMyZAn5.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: strange file.write() behavior on windows: $ConvertToNonresident, $ReplaceAttribute2
This is really just a snide joke at the expense of Microsoft, but have you checked the MSDN documentation about ConvertToNonresident or ReplaceAttribute2? Your search - site:msdn.microsoft.com ConvertToNonresident OR ReplaceAttribute2 - did not match any documents. -- google.com Internally, Python also simply calls fwrite(). Are you using the same C runtime for both sets of tests? Jeff pgpbAHQzcNQfx.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Quitting a Tkinter application with confirmation
Sure, there's a difference. Consider how this program behaves. Quit only quits the mainloop, Destroy destroys the root widget. When you Quit, you can enter the mainloop again. from Tkinter import * t = Tk() Button(t, command=t.quit, text="Quit").pack() Button(t, command=t.destroy, text="Destroy").pack() t.mainloop() t.mainloop() Jeff pgpY0B9ITm2VC.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Hashing Function
As Fredrik suggests, consult the source to find out. Almost all of this is in the realm of being an implementation detail, and hashes aren't guaranteed to be the same from version to version or machine to machine. I'm not even sure they're guaranteed to be the same from run to run. Here's an example: On a 32-bit linux machine running python 2.3: $ python -c 'print hash("a" * 1000)' -884397280 On a 64-bit linux machine running python 2.4: $ python -c 'print hash("a" * 1000)' 2513399373082733344 A little more research suggests that, at least for strings, the hashing algrithm is unchanged from 1.5.2 to 2.4, but is dependant on the range of a platform 'long'. Jeff pgpIPqBnSQiwT.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: writing a generic method
You could use a format like "#s#sO", and then use PyFloat_Check, PyFloat_AsDouble, and the equivalent PyInt macros to get the "C" value out of the Python object. You should be able to use the converter "O&" to get what you want. The conversion function might look like: int double_only(PyObject *o, double *d) { if(!PyFloat_Check(o)) return 0; *d = PyFloat_AsDouble(o); } and the call would be if(PyArg_ParseTuple(args,"s#s#O&",&cname,&lname,&cprop,&lprop,double_only,&x)) mdbgetd_(cname,cprop,&x,&z,lname,lprop); (untested) I believe that if you write if(PyArg_ParseTuple(...)) else(PyArg_ParseTuple(...)) you're missing a PyErr_Clear() before the second PyArg_ParseTuple(). Returning non-NULL when an exception has been set in C code will lead to a hard-to-trace failure down the road. Jeff pgpq4yB9LZ1AX.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Python install minimum requirements
If I found the right "U3" when I googled, then maybe this is relevant: http://www.voidspace.org.uk/python/movpy/ Jeff pgp1AjuUdEskN.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: after sorted from the lists
>>> ll = [[1,2],[2,1],[3,1],[1,4],[3,3],[1,4]] >>> ls = [frozenset(i) for i in ll] >>> ss = set(ls) >>> ss set([frozenset([1, 3]), frozenset([1, 2]), frozenset([1, 4]), frozenset([3])]) >>> [list(i) for i in ss] [[1, 3], [1, 2], [1, 4], [3]] pgphz7iINDVUi.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: user-defined operators: a very modest proposal
If your proposal is implemented, what does this code mean? if [1,2]+[3,4] != [1,2,3,4]: raise TestFailed, 'list concatenation' Since it contains ']+[' I assume it must now be parsed as a user-defined operator, but this code currently has a meaning in Python. (This code is the first example I found, Python 2.3's test/test_types.py, so it is actual code) I don't believe that Python needs user-defined operators, but let me share my terrible proposal anyway: Each unicode character in the class 'Sm' (Symbol, Math) whose value is greater than 127 may be used as a user-defined operator. The special method called depends on the ord() of the unicode character, so that __u2044__ is called when the source code contains u'\N{FRACTION SLASH}'. Whatever alternate syntax is adopted to allow unicode identifier characters to be typed in pure ASCII will also apply to typing user-defined operators. "r" and "i" versions of the operators will of course exist, as in __ru2044__ and __iu2044__. Also, to accomodate operators such as u'\N{DOUBLE INTEGRAL}', which are not simple unary or binary operators, the character u'\N{NO BREAK SPACE}' will be used to separate arguments. When necessary, parentheses will be added to remove ambiguity. This leads naturally to expressions like \N{DOUBLE INTEGRAL} (y * x**2) \N{NO BREAK SPACE} dx \N{NO BREAK SPACE} dy (corresponding to the call (y*x**2).__u222c__(dx, dy)) which are clearly easy to love, except for the small issue that many inferior editors will not clearly display the \N{NO BREAK SPACE} characters. Some items on which I think I'd like to hear the community's ideas are: * Do we give special meaning to comparison characters like \N{NEITHER LESS-THAN NOR GREATER-THAN}, or let users define them in new ways? We could just provide, on object, def __u2279__(self, other): return not self.__gt__(other) and other.__gt__(self) which would in effect satisfy all users. * Do we immediately implement the combination of operators with nonspacing marks, or defer it? If we implement it, do we allow the combination with pure ASCII operators, as in u'\N{COMBINING LEFT RIGHT ARROW ABOVE}+' or treat it as a syntax error? (BTW the method name for this would be __u20e1u002b__, even though it might be tempting to support __u20e1x2b__, __u2oe1add__ and similar method names) How and when do we normalize operators combined with more than one nonspacing mark? * Which unicode operator methods should be supported by built-in types? Implementing __u222a__ and __iu222a__ for sets is a no-brainer, obviously, but what about __iu2206__ for integers and long? * Should some of the unicode mathematical symbols be reserved for literals? It would be greatly preferable to write \u2205 instead of the other proposed empty-set literal notation, {-}. Perhaps nullary operators could be defined, so that writing \u2205 alone is the same as __u2205__() i.e., calling the nullary function, whether it is defined at the local, lexical, module, or built-in scope. * Do we support characters from the category 'So' (symbol, other)? Not doing so means preventing programmers from using operators like \u"n{HEAVY CONCAVE-POINTED BLACK RIGHTWARDS ARROW}". Who are we to make those kinds of choices for our users? Jeff pgpziuZQLsEh3.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: user-defined operators: a very modest proposal
On Tue, Nov 22, 2005 at 04:08:41PM -0800, Steve R. Hastings wrote: > Actually, that's a better syntax than the one I proposed, too: > > __+__ > # __add__ # this one's already in use, so not allowed > __outer*__ Again, this means something already. >>> __ = 3 >>> __+__ 6 >>> __outer = 'x' >>> __outer*__ 'xxx' Jeff pgpNa1qktcWxk.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: strange behaviour when writing a large amount of data on stdout
That code works here. Python2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 It's Windows XP, Pentium 4, unknown amount of RAM. I'm running python.exe in a console window. It also worked in IDLE. Jeff pgptwrbVpG8CR.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Why is dictionary.keys() a list and not a set?
You can already get a set from a dictionary's keys in an efficient manner: >>> l = dict.fromkeys(range(10)) >>> set(l) Set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) Jeff pgplWRjKoPA4t.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Would cgi be the only option if my webhosting doesn't have psp, zpt, cheetah or mod_python?
You might benefit some from scgi. From the httpd side, you can either use "mod_scgi" in the server or "cgi2scgi" if can't install mod_scgi. cgi2scgi doesn't have all the performance benefit of mod_scgi, but cgi2scgi is a fairly lightweight "C" program. http://www.mems-exchange.org/software/scgi/ Jeff pgpmJkXuaAabd.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Why is dictionary.keys() a list and not a set?
One reason might be Practicality. The zip() versions handily beat the listcomp versions on a 10kitem dict. (python2.4) $ timeit.py -s 'd = dict.fromkeys(range(1))' '[(v, k) for (k, v) in d.iteritems()]' 100 loops, best of 3: 5.05 msec per loop $ timeit.py -s 'd = dict.fromkeys(range(1))' '[(v, k) for (k, v) in d.items()]' 100 loops, best of 3: 7.14 msec per loop $ timeit.py -s 'd = dict.fromkeys(range(1))' 'zip(d.itervalues(), d.iterkeys())' 100 loops, best of 3: 3.13 msec per loop $ timeit.py -s 'd = dict.fromkeys(range(1))' 'zip(d.values(), d.keys())' 100 loops, best of 3: 3.28 msec per loop For comparison, $ timeit.py -s 'd = dict.fromkeys(range(1))' 'd.items()' 100 loops, best of 3: 2.19 msec per loop Maybe there are also other reasons to promise this property that I'm not aware of. Certainly it seems like this property is useful and not hard to provide for "non-perverse" implementations, much like the now-documented stability of sort(). Jeff pgpFswMIA8C6O.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: How to find the port a server is listening on (from within the server)
If I understand correctly, you're looking for the socket method getsockname(), documented at http://docs.python.org/lib/socket-objects.html Jeff pgppauvaXgXhK.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: exception KeyboardInterrupt and os.system command
You can tell by the exit code from system() whether the subprocess exited due to a signal. Consider this code: import os while 1: print os.system("sleep 1") unless you happen to hit ctrl-c at the right time, you'll see it print "2" (and "0" when the sleep finishes). The exit code can be interpreted according to the waitpid manpage, though I am not sure where the Python equivalent of the WIFSIGNALED and WTERMSIG macros are. Jeff pgpm5kEy1m2f8.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Data Structure in Python like STL Stack?
What property of the STL stack is important to you? You can use a Python list as a stack. It has methods append() and pop() which run in amortized-constant-time. It can be tested for empty/nonempty in constant time too (if st: # stack is not empty). Jeff pgpU1CCrfIPhk.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Death to tuples!
On Tue, Nov 29, 2005 at 10:41:13AM +, Bengt Richter wrote: > Seems like str.__mod__ could take an arbitary (BTW, matching length, > necessarily? > Or just long enough?) iterable in place of a tuple, just like it can take > an arbitrary mapping object in place of a dict for e.g. '%(name)s'% > {'name':''} What, and break reams of perfectly working code? s = set([1, 2, 3]) t = [4, 5, 6] u = "qwerty" v = iter([None]) print "The set is: %s" % s print "The list is: %s" % t print "The string is: %s" % u print "The iterable is: %s" % v Jeff pgpb5oni2WBfO.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: run runs away
"run" is not the name of a Python built-in function. That leaves everyone but you somewhat in the dark about why it does or does not wait for the created process to complete. If you want a function that starts a new process and waits for it to complete, you probably want to use os.spawnv(os.P_WAIT, ...). Jeff pgpcb8p6UbphM.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: unicode speed
On Tue, Nov 29, 2005 at 09:48:15AM +0100, David Siroky wrote: > Hi! > > I need to enlighten myself in Python unicode speed and implementation. > > My platform is AMD [EMAIL PROTECTED] (x86-32), Debian, Python 2.4. > > First a simple example (and time results): > > x = "a"*5000 > real0m0.195s > user0m0.144s > sys 0m0.046s > > x = u"a"*5000 > real0m2.477s > user0m2.119s > sys 0m0.225s > > So my first question is why creation of a unicode string lasts more then 10x > longer than non-unicode string? string objects have the optimization described in the log message below. The same optimization hasn't been made to unicode_repeat, though it would probably also benefit from it. r30616 | rhettinger | 2003-01-06 04:33:56 -0600 (Mon, 06 Jan 2003) | 11 lines Optimize string_repeat. Christian Tismer pointed out the high cost of the loop overhead and function call overhead for 'c' * n where n is large. Accordingly, the new code only makes lg2(n) loops. Interestingly, 'c' * 1000 * 1000 ran a bit faster with old code. At some point, the loop and function call overhead became cheaper than invalidating the cache with lengthy memcpys. But for more typical sizes of n, the new code runs much faster and for larger values of n it runs only a bit slower. If you're a "C" coder too, consider creating and submitting a patch to do this to the patch tracker on http://sf.net/projects/python . That's the best thing you can do to ensure the optimization is considered for a future release of Python. Jeff pgpHIYtCvVjwy.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: A bug in struct module on the 64-bit platform?
I'm guessing that the expected behavior is >>> struct.calcsize('idi') 20 because the double should be aligned to an 8-byte boundary. This is the case on my linux/x86_64 machine: $ python -c 'import struct; print struct.calcsize("idi")' 20 I don't know much about 'itanium', but i'd be surprised if they chose 4-byte alignment for doubles. http://h21007.www2.hp.com/dspp/tech/tech_TechDocumentDetailPage_IDX/1,1701,180,00.html Jeff pgpgMOnsB8Jx5.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there no compression support for large sized strings in Python?
On this system (Linux 2.6.x, AMD64, 2 GB RAM, python2.4) I am able to construct a 1 GB string by repetition, as well as compress a 512MB string with gzip in one gulp. $ cat claudio.py s = '1234567890'*(1048576*50) import zlib c = zlib.compress(s) print len(c) open("/tmp/claudio.gz", "wb").write(c) $ python claudio.py 1017769 $ python -c 'print len("m" * (1048576*1024))' 1073741824 I was also able to create a 1GB string on a different system (Linux 2.4.x, 32-bit Dual Intel Xeon, 8GB RAM, python 2.2). $ python -c 'print len("m" * 1024*1024*1024)' 1073741824 I agree with another poster that you may be hitting Windows limitations rather than Python ones, but I am certainly not familiar with the details of Windows memory allocation. Jeff pgp5mU0n0xkOj.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: URI http get
The "urllib2" module is designed for tasks like this. The tutorial shows how to use urllib2.urlopen() to fetch the contents of a URL using the http protocol. Jeff pgpdV8higv7SR.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: How to execute an EXE via os.system() with spaces in the directory name?
This comes up from time to time. The brain damage is all Windows', not Python's. Here's one thread which seems to suggest a bizarre doubling of the initial quote of the commandline. http://groups.google.com/group/comp.lang.python/browse_frm/thread/89d94656ea393d5b/ef40a65017848671 pgp1T5KPY01oo.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Constructing RFC2822 Message
On Mon, Dec 05, 2005 at 06:27:44AM -0800, Narendra wrote: > I was able to do that but my e-mail is ending in bulk when i'm trying > to senf to hotmail/msn. Perhaps this is an over-zealous filter on the hotmail/msn side, not a problem with Python. I don't know for sure, but some news stories indicate that without the presence of a special DNS record, microsoft treats messages as spam-- e.g. http://news.zdnet.com/2100-1009-5758365.html Jeff pgpiCUTsOPQDx.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Detect character encoding
Perhaps this project's code or ideas could be of service: http://freshmeat.net/projects/enca/ Jeff pgpYyDfS0xrTp.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: apply()?
First, get the method with getattr() and then use the *-notation or apply to call it with args. getattr(obj, func_name)(*args) or apply(getattr(obj, func_name), args) Jeff pgpm38MC4TzIU.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: hash()
> [John Marshall] > > For strings of > 1 character, what are the chances > > that hash(st) and hash(st[::-1]) would return the > > same value? > On Mon, Dec 05, 2005 at 09:11:14PM -0500, Tim Peters wrote: > First, if `st` is a string, `st[::-1]` is a list. Do you really mean > to compare string hashes with list hashes here? I'm going to assume > not. It is? >>> st = "french frogs" >>> st[::-1] 'sgorf hcnerf' (Python 2.3) Jeff pgpqcPNSOBZiC.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: junk pointer ????
It means there is a bug in a Python extension or in Python itself. If you can reproduce the bug by importing only modules written in Python or included with Python, then you should document the steps to do so in a bug report in the bug tracker on http://sourceforge.net/python If you can only reproduce the bug by importing third-party modules, report the bug to the third-party module author or maintainer. Jeff pgpYS20PlpVID.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: uuDecode problem
Note that you can use the 'uu' encoding, which will handle arbitrary-length input and give multi-line uuencoded output, including the 'begin' and 'end' lines: >>> print ("\377" * 120).encode("uu") begin 666 M M > end Otherwise, you should use something like encoded = [binascii.b2a_uu(chunk) for chunk in iter(lambda: someFile.read(45), "")] to send at most 45 bytes to each call to b2a_uu. Jeff pgpK0PRYLozaB.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Overloading
On Fri, Dec 09, 2005 at 06:29:12PM +0100, Johannes Reichel wrote: > Hi! > > In C++ you can overload functions and constructors. For example if I have a > class that represents a complex number, than it would be nice if I can > write two seperate constructors Python doesn't support this, but it does support default arguments: class Complex: def __init__(self, real=0, imag=0): self.real = real self.imag = imag > And by the way, is it possible to overload operators like +,-,*? > > def operator+(self,complex2): The special methods have names like __add__. http://docs.python.org/ref/numeric-types.html Jeff pgpBaCTVXSEn0.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficiency of using long integers to hold bitmaps
You'll find that using Python Longs unsuitable if you *change* the bitmaps---All numeric types are immutable, so you'll copy the bitmap each time you perform an operation like "set bit". numarray has a 'bit' typecode, though I'm not sure how such an array is actually stored---from a quick look, it appears it's stored one bit per byte: >>> numarray.ones((3,3), 'Bool').tostring() '\x01\x01\x01\x01\x01\x01\x01\x01\x01' A Python object layer around array.array('B') would not necessarily be fast, but it would at least avoid the copying problem, and be memory efficient. import array class BitArray2D: def __init__(self, rows, cols): self.rows = rows self.cols = cols nbytes = (rows * cols + 7) / 8 self._data = array.array('B', [0]) * nbytes def __getitem__(self, (r,c)): # TODO: check r, c in bounds idx = r + c * self.rows byte = idx / 8 bit = 1 << (idx%8) return bool(self._data[byte] & bit) def __setitem__(self, (r, c), v): # TODO: check r, c in bounds idx = r + c * self.rows byte = idx / 8 bit = 1 << (idx%8) if v: self._data[byte] |= bit else: self._data[byte] &= ~bit b = BitArray2D(10, 10) print b._data for x in range(10): b[x,x] = b[9-x,x] = 1 print b._data print for x in range(10): for y in range(10): print " *"[b[x,y]], print Jeff pgpswUV2pQdjG.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: time string conversion
def smadi(s): h, m, s = s.split(":") h = int(h) m = int(m) s = float(s) return s + m*60 + h * 3600 >>> print smadi("16:23:20.019519") 59000.019519 Jeff pgpWjc1O3xtLf.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between " and '
The only difference is when you want to include " or ' inside the string. If you want to include the "like" quote, then escape it ("\"", '\''). If you include the "unlike" quote, no escape is needed ("'" or '"'). I think that people new to programming will use '' if it is unshifted on their keyboards. People from a "C" background may use "" for strings (since in that language, '' is for a single character only). Jeff pgpEe2VSQ8HTg.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: calling functions
Without a 'global' statement, all variables which are assigned in the body of a function are local to that function. Here is an example showing that f() does not create a module-level variable, but g() does. >>> def f(): ... z = 3 ... >>> def g(): ... global z ... z = 3 ... >>> z Traceback (most recent call last): File "", line 1, in ? NameError: name 'z' is not defined >>> f() >>> z Traceback (most recent call last): File "", line 1, in ? NameError: name 'z' is not defined >>> g() >>> z 3 You also have a fencepost error in your slicing. You want to write child = parent[:position] + gene + parent[position+1] otherwise you end up including too few characters in child, and if position is 0 you get an even more unexpected result. However, instead of using 'global' you should just have mutate() return the new child. Here's a version of mutate() that I wrote: import string, random valid = string.lowercase + " " def mutate(parent): position = random.randrange(len(parent)) newgene = random.choice(valid) return parent[:position] + newgene + parent[position+1:] My mutate() returns the new string after it is mutated, so there's no need to use 'global' Here, I repeatedly mutate child to give a new child: >>> child 'forest of grass' >>> for i in range(5): ... child = mutate(child) ... print child ... forest of grays forqst of grays fooqst of grays zooqst of grays zooqst of brays Here, I find many mutations of parent: >>> for i in range(5): ... child = mutate(parent) ... print child ... foresf of grass forestsof grass forest ofpgrass forest oj grass forest cf grass If you have a fitness function f() which returns a higher number the more fit a string is, and you're using Python 2.4, here is some code to order several mutations of parent according to fitness: >>> children = sorted((mutate(parent) for i in range(5)), key=f, reverse=True) >>> fittest_child = children[0] Here's a stupid fitness function: def f(s): return f.count(" ") And it's fairly successful at breeding a string with lots of spaces: >>> child = "forest of grass" >>> for i in range(10): ... children = (mutate(child) for j in range(100)) ... child = sorted(children, key=f, reverse=True)[0] ... print child ... f rest of grass f rest of g ass f rest yf g ass f rest y g ass f rest y g a s t rest y g a s t rest y g a t re t y g a t e t y g a t e y g a Over 10 generations, the most fit of 100 mutations is used as the basis for the next generation. Jeff pgpY2E3Q4Lpmd.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: How to execute code when a module is imported ?
Depending what you mean, it may be dirt simple: -- foo.py -- print "bar" -- end of foo.py -- When a module is first imported, all the statements in it are executed. "def" statements define functions, and "class" statements define clasess. But you can have any type of statement you like at the top level of a module. However, the module is only executed at the first import. On subsequent imports, nothing happens except that the name 'foo' gets assigned the existing foo module from sys.modules['foo']: >>> import foo bar >>> import foo >>> import foo If you really need to do something each time 'import foo' is executed, then you'll have more work to do. One thing to do is override builtins.__import__. Something like this (untested): class ArthasHook: def __init__(self): import __builtin__ self.old_import = __builtin__.__import__ __builtin__.__import__ = self.do_import def do_import(self, *args): m = self.old_import(*args) f = getattr(m, "__onimport__", None) if f: f() hook = ArthasHook() After ArthasHook is created, then each 'import' statement will call hook.do_import() That will look for a module-level function __onimport__, which will be called with no arguments if it exists. I don't recommend doing this. Jeff pgpFEnkgmOzgl.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list