[issue4561] Optimize new io library
David M. Beazley [EMAIL PROTECTED] added the comment: bash-3.2$ uname -a Darwin david-beazleys-macbook.local 9.5.1 Darwin Kernel Version 9.5.1: Fri Sep 19 16:19:24 PDT 2008; root:xnu-1228.8.30~1/RELEASE_I386 i386 bash-3.2$ ./python.exe -c import sys; print(sys.version) 3.1a0 (py3k:67609, Dec 6 2008, 08:47:06) [GCC 4.0.1 (Apple Inc. build 5465)] bash-3.2$ ___ Python tracker [EMAIL PROTECTED] http://bugs.python.org/issue4561 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4428] io.BufferedWriter does not observe buffer size limits
David M. Beazley [EMAIL PROTECTED] added the comment: I agree with previous comments that write() should definitely write all data when in blocking mode. ___ Python tracker [EMAIL PROTECTED] http://bugs.python.org/issue4428 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4447] exec inside a function
New submission from David M. Beazley [EMAIL PROTECTED]: Is the following code valid Python 3 or not? def foo(): x = 1 exec(x = 42) print(x)# Prints 1 (exec has no effect) I know there are a variety of issues surrounding exec(), function bodies, and other matters. Just wondering if this sort of thing is now forbidden or not. -- components: Interpreter Core messages: 76508 nosy: beazley severity: normal status: open title: exec inside a function type: behavior versions: Python 3.0 ___ Python tracker [EMAIL PROTECTED] http://bugs.python.org/issue4447 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4447] exec inside a function
David M. Beazley [EMAIL PROTECTED] added the comment: For what it's worth, I hope this behavior gets well-documented. Thanks. ___ Python tracker [EMAIL PROTECTED] http://bugs.python.org/issue4447 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4428] io.BufferedWriter does not observe buffer size limits
New submission from David M. Beazley [EMAIL PROTECTED]: The Buffered I/O interface in the io module has the user specify buffer limits such as size and max_buffer_size. The first limit (size) is easy to understand as a buffering threshold at which writes will occur. However, no apparent attempt is made to strictly limit the internal buffer size to max_buffer_size. In BuffererWriter.write(), one of the first operations is self._write_buf.extend(b) which simply extends the buffer by the full data being written. If b happens to be a large string (e.g., megabytes or even the entire contents of a big file), then the internal I/O buffer makes a complete copy of the data, effectively doubling the memory requirements for carrying out the write operation. I suppose most programmers might not notice given that everyone has gigabytes of RAM these days, but you certainly don't see this kind of buffering behavior in the operating system kernel or in the C library. Some patch suggestions (details left to the maintainers of this module): 1. Don't extend self._write_buf by more than the max_buffer_size. fragment = b[:self.max_buffer_size - len(self._write_buf)] self._write_buf.extend(fragment) 2. For large blocking writes, simply carry out the remaining I/O operations in the write() method instead of in the _flush_locked() method. Try to use the original input data b as the data source instead of making copies of it. And if you have to copy the data, don't do it all at once. -- components: Library (Lib) messages: 76408 nosy: beazley severity: normal status: open title: io.BufferedWriter does not observe buffer size limits type: resource usage versions: Python 2.6, Python 2.7, Python 3.0 ___ Python tracker [EMAIL PROTECTED] http://bugs.python.org/issue4428 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4362] FileIO object in io module
New submission from David M. Beazley [EMAIL PROTECTED]: The FileIO object defined in the new io library has name and mode properties. However, attempts to access either value result in an AttributeError exception. The C source code in _fileio.c doesn't even implement a name attribute and it uses a different name for mode (mode instead of _mode that the property is looking for). Broken in 2.6 and 3.0rc2. -- components: Library (Lib) messages: 76100 nosy: beazley severity: normal status: open title: FileIO object in io module type: behavior versions: Python 2.6 ___ Python tracker [EMAIL PROTECTED] http://bugs.python.org/issue4362 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4017] Tkinter cannot find Tcl/Tk on Mac OS X
David M. Beazley [EMAIL PROTECTED] added the comment: Just a quick comment from the Python training universe--this bug makes it impossible to use Python 2.6 in any kind of Python teaching environment where IDLE tends to be used a lot. I'm having to tell students to stick with Python-2.5.2. -- nosy: +beazley ___ Python tracker [EMAIL PROTECTED] http://bugs.python.org/issue4017 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Testing out Newsproxy.
Going to see if Newsproxy actually blocks google groups. -=___=- David M Lemcoe Jr. Roswell, Georgia http://www.davidlemcoe.com/ [EMAIL PROTECTED] QRZ: KI4YJL AIM: lemcoe9 YIM: lemcoe9 GTalk: [EMAIL PROTECTED] MSN: [EMAIL PROTECTED] Xfire: shawtylo1 ICQ: 359114839 Alternate e-mail: [EMAIL PROTECTED] -=___=- -- http://mail.python.org/mailman/listinfo/python-list
Re: Question
Hello [EMAIL PROTECTED], No language is better than another because Python is not intended for the same uses and/or people. Your question has no place here. David -=___=- David M Lemcoe Jr. Roswell, Georgia http://www.davidlemcoe.com/ [EMAIL PROTECTED] QRZ: KI4YJL AIM: lemcoe9 YIM: lemcoe9 GTalk: [EMAIL PROTECTED] MSN: [EMAIL PROTECTED] Xfire: shawtylo1 ICQ: 359114839 Alternate e-mail: [EMAIL PROTECTED] -=___=- Why is Perl so much better than python? -- http://mail.python.org/mailman/listinfo/python-list
Re: Any Game Developers here?
Hello Michael, Any people that use Python as the predominant language for their game development here? ~Michael Well, I make little CLI games that are extremely basic and have no actual graphics, but i'm sure a few people actually use them in video games. David -- http://mail.python.org/mailman/listinfo/python-list
Re: setting a breakpoint in the module
Jason Jiang [EMAIL PROTECTED] writes: Hi, I have two modules: a.py and b.py. In a.py, I have a function called aFunc(). I'm calling aFunc() from b.py (of course I import module a first). The question is how to directly set a breakpoint in aFunc(). The way I'm doing now is to set a breakpoint in b.py at the line to call aFunc(), 'c' to it, then 's' to step in, then set the breakpoint inside aFunc() by 'b lineNumber'. It's too cumbersome. You can also add in your source import pdb; pdb.set_trace() at the point you want the debugger to stop. Useful if you want to break after some failing condition, for instance. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Coding style
Carl Banks [EMAIL PROTECTED] writes: Patrick Maupin wrote: PTY wrote: It looks like there are two crowds, terse and verbose. I thought terse is perl style and verbose is python style. BTW, lst = [] was not what I was interested in :-) I was asking whether it was better style to use len() or not. It's not canonical Python to use len() in this case. From PEP 8: - For sequences, (strings, lists, tuples), use the fact that empty sequences are false. Yes: if not seq: if seq: No: if len(seq) if not len(seq) The whole reason that a sequence supports testing is exactly for this scenario. This is not an afterthought -- it's a fundamental design decision of the language. That might have made sense when Python and string, list, tuple were the only sequence types around. Nowadays, Python has all kinds of spiffy types like numpy arrays, interators, generators, etc., for which empty sequence is false just doesn't make sense. If Python had been designed with these types in mind, I'm not sure empty list is false would have been part of the language, let alone recommend practice. Bruno's already mentioned that iterators and generators aren't sequences. Numpy arrays act like the other sequence types: a = numpy.array([]) a array([], dtype=int64) len(a) 0 bool(a) False (0-dimensional numpy arrays are pathological anyways) -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Coding style
[EMAIL PROTECTED] (David M. Cooke) writes: Bruno's already mentioned that iterators and generators aren't sequences. Numpy arrays act like the other sequence types: a = numpy.array([]) a array([], dtype=int64) len(a) 0 bool(a) False (0-dimensional numpy arrays are pathological anyways) *cough* as a Numpy developer I should know better. Numpy arrays that have more than one element don't work in a boolean context: a = numpy.array([1,2]) bool(a) Traceback (most recent call last): File stdin, line 1, in ? ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() The reason for this is that it really was a common source of errors, because of the rich comparision semantics used. If a and b are numpy arrays, 'a == b' is an array of booleans. Numpy arrays of one element act like scalars in boolean contexts: a = numpy.array([0]) bool(a) False a = numpy.array([1]) bool(a) True (this is partly because we define a comphensive hierarchy of scalar types to match those available in C). -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: NumPy 0.9.8 released
Travis E. Oliphant [EMAIL PROTECTED] writes: NumPy 0.9.8 has been released. It can be downloaded from http://numeric.scipy.org The release notes are attached. Best regards, -Travis Oliphant NumPy 0.9.8 is a bug-fix and optimization release with a few new features. The C-API was changed so that extensions compiled against NumPy 0.9.6 will need re-compilation to avoid errors. The C-API should be stabilizing. The next release will be 1.0 which will come out in a series of release-candidates during Summer 2006. There were many users and developers who contributed to the fixes for this release. They deserve much praise and thanks. For details see the Trac pages where bugs are reported and fixed. http://projects.scipy.org/scipy/numpy/ * numpy should install now with easy_install from setuptools Note that you'll need to use the latest setuptools (0.6b1). The hacks I added to get easy_install and numpy.distutils to get along are hard enough without trying to be backward compatible :-( -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: pow (power) function
Russ [EMAIL PROTECTED] writes: Ben Cartwright wrote: Russ wrote: Does pow(x,2) simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)? The former, using binary exponentiation (quite fast), assuming x is an int or long. If x is a float, Python coerces the 2 to 2.0, and CPython's float_pow() function is called. This function calls libm's pow(), which in turn uses logarithms. I just did a little time test (which I should have done *before* my original post!), and 2.0**2 seems to be about twice as fast as pow(2.0,2). That seems consistent with your claim above. I'm a bit surprised that pow() would use logarithms even if the exponent is an integer. I suppose that just checking for an integer exponent could blow away the gain that would be achieved by avoiding logarithms. On the other hand, I would think that using logarithms could introduce a tiny error (e.g., pow(2.0,2) = 3.96 - made up result) that wouldn't occur with multiplication. It depends on the libm implementation of pow() whether logarithms are used for integer exponents. I'm looking at glibc's (the libc used on Linux) implementation for Intel processors, and it does optimize integers. That routine is written in assembly language, btw. Does x**0.5 use the same algorithm as sqrt(x), or does it use some other (perhaps less efficient) algorithm based on logarithms? The latter, and that algorithm is libm's pow(). Except for a few special cases that Python handles, all floating point exponentation is left to libm. Checking to see if the exponent is 0.5 is not one of those special cases. I just did another little time test comparing 2.0**0.5 with sqrt(2.0). Surprisingly, 2.0**0.5 seems to take around a third less time. None of these differences are really significant unless one is doing super-heavy-duty number crunching, of course, but I was just curious. Thanks for the information. And if you are, you'd likely be doing it on more than one number, in which case you'd probably want to use numpy. We've optimized x**n so that it does handle n=0.5 and integers specially; it makes more sense to do this for an array of numbers where you can do the special manipulation of the exponent, and then apply that to all the numbers in the array at once. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Python advocacy in scientific computation
Robert Kern [EMAIL PROTECTED] writes: sturlamolden wrote: 5. Versioning control? For each program there is only one developer and a single or a handful users. I used to think like that up until two seconds before I entered this gem: $ rm `find . -name *.pyc` Okay, I didn't type it exactly like that; I was missing one character. I'll let you guess which. I did that once. I ended up having to update decompyle to run with Python 2.4 :-) Lost comments and stuff, but the code came out great. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Scientific Computing with NumPy
linda.s [EMAIL PROTECTED] writes: where to download numpy for Python 2.3 in Mac? Thanks! Linda I don't know if anybody's specifically compiled for 2.3; I think most of the developers on mac are using 2.4 :-) But (assuming you have the developer tools installed) it's really to compile: python setup.py build python setup.py install. Do you need Tiger (10.4) or Panther (10.3) compatibility? -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: good library for pdf
Murali [EMAIL PROTECTED] writes: Pulling out pages from existing PDF files can be done with Open Source stuff. The simplest would be pdftk (PDF Toolkit). The most fancy will be using latex and the pdfpages package together with pdflatex. - Murali There's also pyPDF, at http://pybrary.net/pyPdf/. I haven't tried it, but it looks interesting. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
problem adding list values
Hi all, I am fairly new to Python and trying to figure out a syntax error concerning lists and iteration through the same. What I am trying to do is sum a list of float values and store the sum in a variable for use later. The relevant code looks like this - def getCredits(): This function asks the user to input any credits not shown on their bank statement It returns the sum(converted to float) of the entered credits global credits credlist = [] credits = 0.0 temp = 0.0 print Now you need to enter any credits not shown on your bank statement \n print Please enter a zero (0) once all credits have been entered \n raw_input(Hit 'Enter' to continue \n) temp = float(raw_input(Please enter the first credit \n)) while temp != 0: credlist.append(temp) temp = float(raw_input(Please enter the next credit \n)) i = 0 for i in credlist: credits += credlist[i] i = i + 1 return credits And the syntax error I get is this - Traceback (most recent call last): File ./BankReconciler_Rev1.py, line 129, in ? main() File ./BankReconciler_Rev1.py, line 116, in main getCredits() File ./BankReconciler_Rev1.py, line 60, in getCredits credits += credlist[i] TypeError: list indices must be integers If anyone can point me in the right direction, I would greatly appreciate it. Thanks in advance -- http://mail.python.org/mailman/listinfo/python-list
Re: idea of building python module using pyrex
[EMAIL PROTECTED] [EMAIL PROTECTED] writes: For example stastical module like commulative probability function for t distribution, or other numerical module which incorporate looping to get the result. I found that pyrex is very helpfull when dealing with looping things. Pyrex is indeed quite helpful. If you're interested in statistical distributions, you'll want to look at the scipy.stats module in scipy (http://www.scipy.org/), which has lots (including the t distribution). In SciPy, we use Pyrex for the random-number generator module scipy.random. It's actually used to wrap some C code, but it does the job well. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Reading binary data
OK so here is my task. I want to get at the data stored in /var/account/pacct, which stores process accounting data, so that I can make it into a more human understandable format then what the program sa can do. The thing is, its in a binary format and an example program that reads some data from the file is done in C using a struct defined in sys/acct.h. http://www.linuxjournal.com/articles/lj/0104/6144/6144l2.html So I was wondering how can I do the same thing, but in python? I'm still learning so please be gentle. David -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading binary data
Thanks but the C Struct describing the data doesn't match up with the list on the module-struct page. this is the acct.h file #ifndef _SYS_ACCT_H #define _SYS_ACCT_H 1 #include features.h #define __need_time_t #include time.h #include sys/types.h __BEGIN_DECLS #define ACCT_COMM 16 /* comp_t is a 16-bit floating point number with a 3-bit base 8 exponent and a 13-bit fraction. See linux/kernel/acct.c for the specific encoding system used. */ typedef u_int16_t comp_t; struct acct { char ac_flag; /* Accounting flags. */ u_int16_t ac_uid; /* Accounting user ID. */ u_int16_t ac_gid; /* Accounting group ID. */ u_int16_t ac_tty; /* Controlling tty. */ u_int32_t ac_btime; /* Beginning time. */ comp_t ac_utime;/* Accounting user time. */ comp_t ac_stime;/* Accounting system time. */ comp_t ac_etime;/* Accounting elapsed time. */ comp_t ac_mem; /* Accounting average memory usage. */ comp_t ac_io; /* Accounting chars transferred. */ comp_t ac_rw; /* Accounting blocks read or written. */ comp_t ac_minflt; /* Accounting minor pagefaults. */ comp_t ac_majflt; /* Accounting major pagefaults. */ comp_t ac_swaps;/* Accounting number of swaps. */ u_int32_t ac_exitcode; /* Accounting process exitcode. */ char ac_comm[ACCT_COMM+1]; /* Accounting command name. */ char ac_pad[10];/* Accounting padding bytes. */ }; enum { AFORK = 0x01, /* Has executed fork, but no exec. */ ASU = 0x02, /* Used super-user privileges. */ ACORE = 0x08, /* Dumped core. */ AXSIG = 0x10/* Killed by a signal. */ }; #define AHZ 100 /* Switch process accounting on and off. */ extern int acct (__const char *__filename) __THROW; __END_DECLS #endif /* sys/acct.h */ What are u_ini16_t and comp_t? And what about the enum section? -- http://mail.python.org/mailman/listinfo/python-list
Re: Underscores in Python numbers
Peter Hansen [EMAIL PROTECTED] writes: Steven D'Aprano wrote: Dealing with numeric literals with lots of digits is a real (if not earth-shattering) human interface problem: it is hard for people to parse long numeric strings. I'm totally unconvinced that this _is_ a real problem, if we define real as being even enough to jiggle my mouse, let alone shattering the planet. What examples does anyone have of where it is necessary to define a large number of large numeric literals? Isn't it the case that other than the odd constants in various programs, defining a large number of such values would be better done by creating a data file and parsing it? One example I can think of is a large number of float constants used for some math routine. In that case they usually be a full 16 or 17 digits. It'd be handy in that case to split into smaller groups to make it easier to match with tables where these constants may come from. Ex: def sinxx(x): computes sin x/x for 0 = x = pi/2 to 2e-9 a2 = -0.1 4 a4 = 0.00833 33315 a6 = -0.00019 84090 a8 = 0.0 27526 a10= -0.0 00239 x2 = x**2 return 1. + x2*(a2 + x2*(a4 + x2*(a6 + x2*(a8 + x2*a10 (or least that's what I like to write). Now, if I were going to higher precision, I'd have more digits of course. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: authentication for xmlrpc via cgi
[EMAIL PROTECTED] writes: I'm using python 2.2 (hopefully we'll be upgrading our system to 2.3 soon) and I'm trying to prototype some xml-rpc via cgi functionality. If I override the Transport class on the xmlrpclib client and add some random header like Junk, then when I have my xmlrpc server log it's environment when running, I see the HTTP_JUNK header. If I do this with AUTHORIZATION, the header is not found. Does this ring a bell for anyone? Am I misunderstanding how to use this header? I'm guessing that Apache might be eating this header, but I don't know why. By default, Apache does eat that. It's a compile time default; the Apache developers think it's a security hole. Here's a note about it: http://httpd.apache.org/dev/apidoc/apidoc_SECURITY_HOLE_PASS_AUTHORIZATION.html From what I can see, this is still true in Apache 2. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to determine -- when parsing -- if a word contains a builtin name or other imported system module name?
Casey Hawthorne [EMAIL PROTECTED] writes: Is there a way to determine -- when parsing -- if a word contains a builtin name or other imported system module name? Like iskeyword determines if a word is a keyword! Look in the keyword module; there is actually an iskeyword function there :) For modules, sys.modules is a dictionary of the modules that have been imported. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: without shell
Donn Cave [EMAIL PROTECTED] writes: In article [EMAIL PROTECTED], Grant Edwards [EMAIL PROTECTED] wrote: On 2005-06-10, Mage [EMAIL PROTECTED] wrote: py file_list = os.popen(ls).read() Stores the output of ls into file_list. These commands invoke shell indeed. Under Unix, popen will not invoke a shell if it's passed a sequence rather than a single string. I suspect you're thinking of the popen2 functions. On UNIX, os.popen is posix.popen, is a simple wrapper around the C library popen. It always invokes the shell. The no-shell alternatives are spawnv (instead of system) and the popen2 family (given a sequence of strings.) Don't forget the one module to rule them all, subprocess: file_list = subprocess.Popen(['ls'], stdout=subprocess.PIPE).communicate()[0] which by default won't use the shell (unless you pass shell=True to it). -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Annoying behaviour of the != operator
Robert Kern [EMAIL PROTECTED] writes: greg wrote: David M. Cooke wrote: To solve that, I would suggest a fourth category of arbitrary ordering, but that's probably Py3k material. We've got that: use hash(). [1+2j, 3+4j].sort(key=hash) What about objects that are not hashable? The purpose of arbitrary ordering would be to provide an ordering for all objects, whatever they might be. How about id(), then? And so the circle is completed... Or something like def uniquish_id(o): try: return hash(o) except TypeError: return id(o) hash() should be the same across interpreter invocations, whereas id() won't. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Annoying behaviour of the != operator
Greg Ewing [EMAIL PROTECTED] writes: Rocco Moretti wrote: This gives the wacky world where [(1,2), (3,4)].sort() works, whereas [1+2j, 3+4j].sort() doesn't. To solve that, I would suggest a fourth category of arbitrary ordering, but that's probably Py3k material. We've got that: use hash(). [1+2j, 3+4j].sort(key=hash) Using the key= arg in sort means you can do other stuff easily of course: by real part: import operator [1+2j, 3+4j].sort(key=operator.attrgetter('real')) by size: [1+2j, 3+4j].sort(key=abs) and since .sort() is stable, for those numbers where the key is the same, the order will stay the same. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: computer algebra packages
Fernando Perez [EMAIL PROTECTED] writes: Rahul wrote: Hi. The reason is simple enough. I plan to do some academic research related to computer algebra for which i need some package which i can call as a library. Since i am not going to use the package myself..(rather my program will)..it will be helpful to have a python package since i wanted to write the thing in python. if none is available then probably i will need to work on an interface to some package written in some other language or work in that language itself. I've heard of people writing a Python MathLink interface to Mathematica, which essentially turns Mathematica into a Python module. But I don't have any references handy, sorry, and as far as I remember it was done as a private contract. But it's doable. It should also be doable with Maple, using the OpenMaple API. I've looked at it, and it should be possible. I haven't had the time to actually do anything, though :-) -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: editor for shelve files
Amir Michail [EMAIL PROTECTED] writes: Hi, Is there a program for editing shelve databases? Amir I doubt it. A shelf is basically just a file-based dictionary, where the keys must be strings, while the values can be arbitrary objects. An editor could handle the keys, but most likely not the values. If you have an editor for arbitrary objects, you could probably make an editor for shelfs easily enough :-) Do you have a specific use in mind? That would be easier to handle than the general case. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a package with convolution and related methods?
Charles Krug [EMAIL PROTECTED] writes: List: Is there a Python package with Convolution and related methods? I'm working on modeling some DSP processes in Python. I've rolled one up, but don't feel much like reinventing the wheel, especially if there's already something like Insanely Efficient FFT for Python already. Thanks You most certainly want to look at the numerical python packages Numeric and numarray (http://numeric.scipy.org/) for array manipulations, and scipy (http://scipy.org) has wraps for FFTW (Fast Fourier Transform in the West). -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Threads and variable assignment
Gregory Bond [EMAIL PROTECTED] writes: I've had a solid hunt through the (2.3) documentation but it seems silent on this issue. I have an problem that would naturally run as 2 threads: One monitors a bunch of asyncrhonous external state and decides if things are good or bad. The second thread processes data, and the processing depends on the good or bad state at the time the data is processed. Sort of like this: Thread 1: global isgood while 1: wait_for_state_change() if new_state_is_good(): isgood = 1 else: isgood = 0 Thread 2: s = socket() s.connect(...) f = s.makefile() while 1: l = f.readline() if isgood: print goodfile, l else: print badfile, l You said that the processing depends on the good or bad state at the time the data is processed: I don't know how finely-grained your state changes will be in thread 1, but it doesn't seem that thread 2 would notice at the right time. If the socket blocks reading a line, the state could change i What guarantees (if any!) does Python make about the thread safety of this construct? Is it possible for thread 2 to get an undefined variable if it somehow catches the microsecond when isgood is being updated by thread 1? It won't be undefined, but it's possible that (in thread 1) between the if new_state_is_good() and the setting of isgood that thread 2 will execute, so if new_state_is_good() was false, then it could still write the line to the goodfile. It really depends on how often you have state changes, how often you get (full) lines on your socket, and how much you care that the correct line be logged to the right file. If you needed this to be robust, I'd either: - Try to rewrite wait_for_status_change()/new_state_is_good() to be asynchronous, particularly if wait_for_status_change() is blocking on some file or socket object. This way you could hook it into asynchat/asyncore or Twisted without any threads. - Or, if you need to use threads, use a Queue.Queue object where timestamps w/ state changes are pushed on in thread 1, and popped off and analysed before logging in thread 2. (Or something; this just popped in my head.) -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: python/svn issues....
bruce [EMAIL PROTECTED] writes: david... thanks for the reply... it's starting to look as though the actual /usr/lib/libdb-4.2.so from the rpm isn't exporting any of the symbols... when i do: nm /usr/lib/libdb-4.2.so | grep db_create i get nm: /usr/lib/libdb-4.2.so: no symbols which is strange... because i should be getting the db_create symbol... i'll try to build berkeley db by hand and see what i get... if you could try the 'nm' command against your berkely.. i'd appreciate you letting me know what you get.. Not surprising; plain 'nm' doesn't work for me on shared libraries. I need to use 'nm -D'. In that case, I get a db_create (or rather, a versioned form, db_create_4002). Running 'nm -D -g' on the libsvn_fs_base library shows it uses the same db_create_4002 function. -- ||\/| /--\ |David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/ |[EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: richcmpfunc semantics
harold fellermann [EMAIL PROTECTED] writes: Thank you Greg, I figured most of it out in the meantime, myself. I only differ from you in one point. What has to be done, if the function is invoked for an operator I don't want to define? Return Py_NotImplemented. (Note that's return, *not* raise.) I used PyErr_BadArgument(); return NULL; instead. What is the difference between the two and which one is to prefer. If you do it your way you're a bad neighbour: If your object is the first one (left-hand side) of the operator, it will prevent the other object from handling the case if it can. This is the same advice as for all of the other operators (__add__, etc.) Consider the pure-python version: class A: def __init__(self, what_to_do='return'): self.what_to_do = what_to_do def __eq__(self, other): print 'A.__eq__' if self.what_to_do == 'return': return NotImplemented else: raise Exception class B: def __eq__(self, other): print 'B.__eq__' return True a = A('return') b = B() a == b A.__eq__ B.__eq__ True b == a B.__eq__ True a == a A.__eq__ A.__eq__ A.__eq__ A.__eq__ True So the B class handles the case where A doesn't know what to do. Also note the last case, where Python falls back on id() comparisions to determine equality. Now, compare with this: a = A('raise') b = B() a == b A.__eq__ Traceback (most recent call last): File stdin, line 1, in ? File x.py, line 9, in __eq__ raise Exception Exception b == a B.__eq__ True a == a A.__eq__ Traceback (most recent call last): File stdin, line 1, in ? File x.py, line 9, in __eq__ raise Exception Exception So now comparing A and B objects can fail. If you *know* A and B objects can't be compared for equality, it'd be ok to raise a TypeError, but that should be after a type test. Also, do you need to increment the reference count of Py_NotImeplemented before returning it? Yes; it's a singleton like Py_None. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: curious problem with large numbers
Chris Fonnesbeck [EMAIL PROTECTED] writes: 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 However, on Windows (have tried on Mac, Linux) I get the following behaviour: inf = 1e1 inf 1.0 while I would have expected: 1.#INF Smaller numbers, as expected, yield: inf = 1e100 inf 1e+100 Obviously, I cannot use the former to compare against large (but not infinite) numbers, at least not to get the result I expect. Has anyone seen this behaviour? I don't do Windows, so I can't say this will work, but try inf = 1e308*2 I think your problem is how the number is being parsed; perhaps Windows is punting on all those zeros? Computing the infinity may or may not work, but it gets around anything happening in parsing. Alternatively, if you have numarray installed (which you should probably consider if you're doing numerical stuff ;-) you could use import numarray.ieeespecial numarray.ieeespecial.plus_inf inf (there's minus_inf, nan, plus_zero, and minus_zero also) -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Gnuplot.py and, _by far_, the weirdest thing I've ever seen on my computer
syd [EMAIL PROTECTED] writes: I don't even know where to begin. This is just bizarre. I just picked up the Gnuplot.py module (a light interface to gnuplot commands) and was messing around with it today. I've got a tiny script, but it only works from the command line about half the time! In the python interpreter, 100%. Ipython, 100%. I'm not kidding. #!/bin/env python import Gnuplot g = Gnuplot.Gnuplot(debug=1) g.title('A simple example') g('set data style linespoints') g('set terminal png small color') g('set output myGraph.png') g.plot([[0,1.1], [1,5.8], [2,3.3], [3,100]]) Here's just one example -- it does not work, then it works. It seems totally random. It will work a few times, then it won't for a few times... bash-2.05b$ ./myGnu.py gnuplot set title A simple example gnuplot set data style linespoints gnuplot set terminal png small color gnuplot set output myGraph.png gnuplot plot '/tmp/tmp5LXAow' notitle gnuplot plot '/tmp/tmp5LXAow' notitle ^ can't read data file /tmp/tmp5LXAow line 0: util.c: No such file or directory bash-2.05b$ ./myGnu.py gnuplot set title A simple example gnuplot set data style linespoints gnuplot set terminal png small color gnuplot set output myGraph.png gnuplot plot '/tmp/tmpHMTkpL' notitle (and it makes the graph image just fine) I mean what the hell is going on? My permissions on /tmp are wide open (drwxrwxrwt). It does the same thing when I run as root. And it _always_ works when I use the interpreter or interactive python. Any clues would be greatly appreciated. I'm baffled. What's your OS? Python version? Gnuplot.py version (I assume 1.7)? Put a 'import sys; print sys.version' in there to make sure /bin/env is using the same python as you expect it to. It looks like any temporary file it's writing to is deleted too early. Have a look at gp_unix.py in the Gnuplot source. There's some customization options that might be helpful. In particular, I'd try import Gnuplot Gnuplot.GnuplotOpts.prefer_fifo_data = 0 ... then the data will be save to a temporary file instead of piped through a fifo. Alternatively, try Gnuplot.GnuplotOpts.prefer_inline_data = 1 ... then no file will be used. [I don't use Gnuplot myself; this is just what I came up with after a few minutes of looking at it] -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: numeric module
[EMAIL PROTECTED] writes: Hello, What's the problem with this code? I get the following error message: File test.py, line 26, in test print tbl[wi][bi] IndexError: index must be either an int or a sequence ---code snippet from Numeric import * tbl = zeros((32, 16)) def test(): val = testme() wi = val 4 bi = val 0xFL [above changed to use val instead of crc, as you mentioned in another post] print wi print bi print tbl[wi][bi] tbl[wi][bi] would be indexing the bi'th element of whatever tbl[wi] returns. For Numeric arrays, you need tbl[wi,bi] Now, you'll have another problem as Terry Reedy mentioned: the indices (in Numeric) need to be Python ints, not longs. You could rewrite your test() function as def test(): val = testme() wi = int(val 4) bi = int(val 0xF) print wi print bi print tbl[wi,bi] and that'll work. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: numeric module
coffeebug [EMAIL PROTECTED] writes: I cannot import numarray and I cannot import numeric using python 2.3.3 numarray and Numeric are separate modules available at http://numpy.sourceforge.net/ If you're doing anything numerical in Python, you'll want them :-) -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: itertools to iter transition
Steven Bethard [EMAIL PROTECTED] writes: Terry Reedy wrote: But if classmethods are intended to provide alternate constructors But I do not remember that being given as a reason for classmethod(). But I am not sure what was. Well I haven't searched thoroughly, but I know one place that it's referenced is in descrintro[1]: Factoid: __new__ is a static method, not a class method. I initially thought it would have to be a class method, and that's why I added the classmethod primitive. Unfortunately, with class methods, upcalls don't work right in this case, so I had to make it a static method with an explicit class as its first argument. Ironically, there are now no known uses for class methods in the Python distribution (other than in the test suite). Not true anymore, of course (it was in 2.2.3). In 2.3.5, UserDict, tarfile and some the Mac-specific module use classmethod, and the datetime extension module use the C version (the METH_CLASS flag). And staticmethod (and METH_STATIC) aren't used at all in 2.3 or 2.4 :-) [if you ignore __new__] -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: breaking up is hard to do
bbands [EMAIL PROTECTED] writes: For example I have a class named Indicators. If I cut it out and put it in a file call Ind.py then from Ind import Indicators the class can no longer see my globals. This is true even when the import occurs after the config file has been read and parsed. Don't use globals? Or put all the globals into a separate module, which you import into Ind and into whatever uses Ind. Putting the globals into a separate namespace (module, class, class instance, whatever) also makes it easier to know what is a global :-) -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Using python to extend a python app
dataangel [EMAIL PROTECTED] writes: I'm writing a python app that works as a replacement for the menu that comes with most minimalist wms when you right click the root window. It's prettier and written completely in python. I'd like to provide hooks or some system so that people can write their own extensions to the app, for example adding fluxbox options, and then fluxbox users can choose to use that extension. But I'm not sure how to implement it. Right now the best idea I have is to have all desired extensions in a folder, import each .py file in that folder as a module using __import__, and then call some predetermined method, say start, and pass it the menu as it exists so far so they can add to it, start(menu). This seems kind of hackish. That looks pretty reasonable, and easy. There have been some recent threads (in the past month or so) on plugins, so you might want to search the archives. Most of it's revolved around not using exec :-) I just had a look at pyblosxom (one program that I know that uses plugins), and it uses exactly this approach, with some extra frills: looking in subdirectories, for instance. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Passing arguments to python from URL
Casey Bralla [EMAIL PROTECTED] writes: I've got a python cgi-bin application which produces an apache web page. I want to pass arguments to it on the URL line, but the parameters are not getting passed along to python properly. I've been using sys.argv to pick up command line arguments, and it works fine when I call the python program from the command line. Unfortunately, when I pass data to the program from the URL, many of the parameters are being clobbered and **NOT** passed to python. For example: http://www.nobody.com/cgi-bin/program.py?sort=ascending; only passes the parameter /usr/lib/cgi-bin/program.py. This is expected. However, http://www.nobody.com/cgi-bin/program.py?sort%20ascending; passes a 2-place tuple of (/usr/lib/cgi-bin/program.py, sort ascending). I don't know why this actually works, it's not (AFAIK) defined behaviour. Somehow, adding the = in the argument list prevents **ANY** parameters from being passed to python. I could re-write the python program to work around this, but I sure would like to understand it first. You're going to have to rewrite. CGI scripts get their arguments passed to them through the environment, not on the command line. QUERY_STRING, for instance, will hold the query string (the stuff after the ?). Use Python's cgi module to make things easier on yourself; the documentation has a good overview: http://www.python.org/doc/2.4/lib/module-cgi.html In this case, your script would look something like this: import cgi form = cgi.FieldStorage() if form.getvalue('sort') == 'ascending': ... sort in ascending order ... etc. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: how to handle repetitive regexp match checks
Matt Wette [EMAIL PROTECTED] writes: Over the last few years I have converted from Perl and Scheme to Python. There one task that I do often that is really slick in Perl but escapes me in Python. I read in a text line from a file and check it against several regular expressions and do something once I find a match. For example, in perl ... if ($line =~ /struct {/) { do something } elsif ($line =~ /typedef struct {/) { do something else } elsif ($line =~ /something else/) { } ... I am having difficulty doing this cleanly in python. Can anyone help? rx1 = re.compile(r'struct {') rx2 = re.compile(r'typedef struct {') rx3 = re.compile(r'something else') m = rx1.match(line) if m: do something else: m = rx2.match(line) if m: do something else: m = rx3.match(line) if m: do something else: error I usually define a class like this: class Matcher: def __init__(self, text): self.m = None self.text = text def match(self, pat): self.m = pat.match(self.text) return self.m def __getitem__(self, name): return self.m.group(name) Then, use it like for line in fo: m = Matcher(line) if m.match(rx1): do something elif m.match(rx2): do something else: error -- ||\/| David M. Cooke cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: SAX parsing problem
anon [EMAIL PROTECTED] writes: So I've encountered a strange behavior that I'm hoping someone can fill me in on. i've written a simple handler that works with one small exception, when the parser encounters a line with '#38;' in it, it only returns the portion that follows the occurence. For example, parsing a file with the line : keymykey/keyvaluesome%20#38;%20value/value results in getting %20value back from the characters method, rather than some%20#38;%20value. After looking into this a bit, I found that SAX supports entities and that it is probably believing the #38; to be an entity and processing it in some way that i'm unware of. I'm using the default EntityResolver. Are you sure you're not actually getting three chunks: some%20, , and %20value? The xml.sax.handler.ContentHandler.characters method (which I presume you're using for SAX, as you don't mention!) is not guaranteed to get all contiguous character data in one call. Also check if .skippedEntity() methods are firing. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write python plug-ins for your own python program?
Simon Wittber [EMAIL PROTECTED] writes: You mean like 'import'? :) That's how I would do it. It's the simplest thing, that works. exec(import %s as plugin % pluginName) plugin.someMethod() where pluginName is the name of the python file, minus the .py extension. You'd better hope someone doesn't name their plugin 'os; os.system(rm -rf /); import sys' Use __import__ instead. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: binutils strings like functionality?
cjl [EMAIL PROTECTED] writes: Fredrik Lundh wrote: something like this could work: import re text = open(file, rb).read() for m in re.finditer(([\x20-\x7f]{4,})[\n\0], text): print m.start(), repr(m.group(1)) Hey...that worked. I actually modified: for m in re.finditer(([\x20-\x7f]{4,})[\n\0], text): to for m in re.finditer(([\x20-\x7f]{4,}), text): and now the output is nearly identical to 'strings'. One problem exists, in that if the binary file contains a string monkey/chicken/dog/cat it is printed as mokey//chicken//dog//cat, and I don't know enough to figure out where the extra / is coming from. Are you sure it's monkey/chicken/dog/cat, and not monkey\chicken\dog\cat? The later one will print monkey\\chicken... because of the repr() call. Also, you probably want it as [\x20-\x7e] (the DEL character \x7f isn't printable). You're also missing tabs (\t). The GNU binutils string utility looks for \t or [\x20-\x7e]. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: [ANN] Python 2.4 Quick Reference available
Pete Havens [EMAIL PROTECTED] writes: The is awesome! Thanks. I did notice one thing while reading it. In the File Object section, it states: Created with built-in functions open() [preferred] or its alias file(). ...this seems to be the opposite of the Python documentation: The file() constructor is new in Python 2.2. The previous spelling, open(), is retained for compatibility, and is an alias for file(). Except if you look at the current development docs (http://www.python.org/dev/doc/devel/lib/built-in-funcs.html) it says The file() constructor is new in Python 2.2 and is an alias for open(). Both spellings are equivalent. The intent is for open() to continue to be preferred for use as a factory function which returns a new file object. The spelling, file is more suited to type testing (for example, writing isinstance(f, file)). ... which more accurately reflects what I believe the consensus is about the usage of open vs. file. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: LinearAlgebra incredibly slow for eigenvalue problems
drife [EMAIL PROTECTED] writes: Hello, I need to calculate the eigenvectors and eigenvalues for a 3600 X 3600 covariance matrix. The LinearAlgebra package in Python is incredibly slow to perform the above calculations (about 1.5 hours). This in spite of the fact that I have installed Numeric with the full ATLAS and LAPACK libraries. Also note that my computer has dual Pentium IV (3.1 GHz) processors with 2Gb ram. Every Web discussion I have seen about such issues indicates that one can expect huge speed ups if one compiles and installs Numeric linked against the ATLAS and LAPACK libraries. Are you *sure* that Numeric is linked against these? Even more perplexing is that the same calculation takes a mere 7 min in Matlab V6.5. Matlab uses both ATLAS and LAPACK. Moreover, the above calculation takes the same amount of time for Numeric to complete with --and-- without ATLAS and PACK. I am certain that I have done the install correctly. This is good evidence that Numeric *isn't* linked to them. If you're on a Linux system, you can check with ldd: [EMAIL PROTECTED] ldd /usr/lib/python2.3/site-packages/Numeric/lapack_lite.so liblapack.so.3 = /usr/lib/atlas/liblapack.so.3 (0x002a95677000) libblas.so.3 = /usr/lib/atlas/libblas.so.3 (0x002a95e55000) libg2c.so.0 = /usr/lib/libg2c.so.0 (0x002a96721000) libpthread.so.0 = /lib/libpthread.so.0 (0x002a96842000) libc.so.6 = /lib/libc.so.6 (0x002a96957000) libm.so.6 = /lib/libm.so.6 (0x002a96b96000) /lib64/ld-linux-x86-64.so.2 = /lib64/ld-linux-x86-64.so.2 (0x00552000) You can see that lapack and blas (the Atlas versions) are linked to the lapack_lite.so. To install Numeric using Lapack: - remove the build/ directory in your Numeric sources, so you don't any old binaries - edit setup.py and follow the comments on using Lapack (you need to comment out a few lines, and set some directories) Also set use_dotblas to 1. - do the 'python setup.py build', 'python setup.py install' dance. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: LinearAlgebra incredibly slow for eigenvalue problems
drife [EMAIL PROTECTED] writes: Hi David, I performed the above check, and sure enough, Numeric is --not-- linked to the ATLAS libraries. I followed each of your steps outlined above, and Numeric still is not linking to the ATLAS libraries. My setup.py file is attached below. # delete all but the first one in this list if using your own LAPACK/BLAS sourcelist = [os.path.join('Src', 'lapack_litemodule.c')] # set these to use your own BLAS; library_dirs_list = ['/d2/lib/atlas'] libraries_list = ['lapack', 'ptcblas', 'ptf77blas', 'atlas', 'g2c'] # set to true (1), if you also want BLAS optimized matrixmultiply/dot/innerproduct use_dotblas = 1 include_dirs = ['/d2/include'] This all look right (assuming you've got the right stuff in /d2). When it compiles, does it look like it's actually doing the linking? After doing python setup.py build, you can run ldd on the libraries in the build directory (something like build/lib.linux-i386-2.3/lapack_lite.so). If that's linked, then it's not being installed right. You don't have a previous Numeric installation that's being picked up instead of the one you're trying to install, do you? At the interpreter prompt, check that import Numeric Numeric.__file__ gives you something you're expecting, and not something else. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: pickling extension class
harold fellermann [EMAIL PROTECTED] writes: Hi all, I have a problem pickling an extension class. As written in the Extending/Embedding Manual, I provided a function __reduce__ that returns the appropreate tuple. This seams to work fine, but I still cannot pickle because of the following error: from model import hyper g = hyper.PeriodicGrid(4,4,1) g.__reduce__() (type 'hyper.PeriodicGrid',(4.,4.,1.)) import pickle pickle.dump(g,file(test,w)) Traceback (most recent call last): File pickle_test.py, line 5, in ? pickle.dump(g,file(test,w)) File /sw/lib/python2.4/pickle.py, line 1382, in dump Pickler(file, protocol, bin).dump(obj) File /sw/lib/python2.4/pickle.py, line 231, in dump self.save(obj) File /sw/lib/python2.4/pickle.py, line 338, in save self.save_reduce(obj=obj, *rv) File /sw/lib/python2.4/pickle.py, line 414, in save_reduce save(func) File /sw/lib/python2.4/pickle.py, line 293, in save f(self, obj) # Call unbound method with explicit self File /sw/lib/python2.4/pickle.py, line 760, in save_global raise PicklingError( pickle.PicklingError: Can't pickle type 'hyper.PeriodicGrid': it's not found as hyper.PeriodicGrid dir(hyper) ['Dir', 'Neighbors', 'PeriodicGrid', 'PeriodicPos', '__doc__', '__file__', '__name__', 'refcount'] hyper.PeriodicGrid type 'hyper.PeriodicGrid' ^ I think that's your error. The extension type is declared to be hyper.PeriodicGrid, where it actually is model.hyper.PeriodicGrid (because hyper is in the model package). Pickle stores g.__class__.__module__ (which is hyper) and g.__class__.__name__ (=PeriodicGrid) to find the class object for reimporting, and on unpickling, tries to do __import__(hyper), which fails. The tp_name slot of your extension type should be model.hyper.PeriodicGrid. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: readline, rlcompleter
[EMAIL PROTECTED] writes: This a case where the documentation is lacking. The standard library documentation (http://www.python.org/dev/doc/devel/lib/module-rlcompleter.html) gives this example try: import readline except ImportError: print Module readline not available. else: import rlcompleter readline.parse_and_bind(tab: complete) but I don't find a list of recognized key bindings. For instance, can I would like to bind shift-tab to rlcompleter, is that possible? Can I use function keys? I did various attempt, but I did not succed :-( Is there any readline-guru here with some good pointers? Michele Simionato Basically, you could use any key sequence that is sent to the terminal. So shift-tab is out (that's not sent as a key to any terminal program). Function keys would have to be specified as the key sequence sent by a function key (\e[11~ for F1, for instance). Have a look at the readline info page, or the man page. The syntax of readline.parse_and_bind is the same as that of an inputrc file. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: MoinMoin and Mediawiki?
Paul Rubin http://[EMAIL PROTECTED] writes: Alexander Schremmer [EMAIL PROTECTED] writes: lists of incoming links to wiki pages, It does. Huh? I don't see those. How does it store them, that's resilient across crashes? Or does it just get wedged if there's a crash? Most Wiki implementations (MoinMoin included) have this, by using a search. Usually, following the original Wiki (http://c2.com/cgi/wiki) model, you get at it by clicking on the title of the page. Searching instead of indexing makes it very resilient :-) -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: why not datetime.strptime() ?
Joshua Spoerri [EMAIL PROTECTED] writes: Skip Montanaro skip at pobox.com writes: josh Shouldn't datetime have strptime? If someone wants to get their feet wet with extension module programming this might be a good place to start. Mostly, I think nobody who has needed/wanted it so far has the round tuits available to spend on the task. OK, it was pretty straightforward. Thanks for the direction. To whom should I send the patch (attached)? Submit it to the patch tracker on sourceforge. But first, some constructive criticism: --- Modules/datetimemodule.c.orig 2003-10-20 10:34:46.0 -0400 +++ Modules/datetimemodule.c 2005-01-10 20:58:38.884823296 -0500 @@ -3774,6 +3774,32 @@ return result; } +/* Return new datetime from time.strptime(). */ +static PyObject * +datetime_strptime(PyObject *cls, PyObject *args) +{ + PyObject *result = NULL, *obj, *module; + const char *string, *format; + + if (!PyArg_ParseTuple(args, ss:strptime, string, format)) + return NULL; + if ((module = PyImport_ImportModule(time)) == NULL) + return NULL; + obj = PyObject_CallMethod(module, strptime, ss, string, format); + Py_DECREF(module); You don't check for errors: an exception being thrown by PyObject_CallMethod will return obj == NULL. If there's a module in sys.path called time that overrides the stdlib time, things will fail, and you should be able to catch that. + result = PyObject_CallFunction(cls, iii, + PyInt_AsLong(PySequence_GetItem(obj, 0)), + PyInt_AsLong(PySequence_GetItem(obj, 1)), + PyInt_AsLong(PySequence_GetItem(obj, 2)), + PyInt_AsLong(PySequence_GetItem(obj, 3)), + PyInt_AsLong(PySequence_GetItem(obj, 4)), + PyInt_AsLong(PySequence_GetItem(obj, 5)), + PyInt_AsLong(PySequence_GetItem(obj, 6))); Are you positive those PySequence_GetItem calls will succeed? That they will return Python integers? + Py_DECREF(obj); + return result; +} + /* Return new datetime from date/datetime and time arguments. */ static PyObject * datetime_combine(PyObject *cls, PyObject *args, PyObject *kw) @@ -4385,6 +4411,11 @@ PyDoc_STR(timestamp - UTC datetime from a POSIX timestamp (like time.time()).)}, + {strptime, (PyCFunction)datetime_strptime, + METH_VARARGS | METH_CLASS, + PyDoc_STR(strptime - new datetime parsed from a string +(like time.strptime()).)}, + {combine, (PyCFunction)datetime_combine, METH_VARARGS | METH_KEYWORDS | METH_CLASS, PyDoc_STR(date, time - datetime with same date and time fields)}, It probably would help to add some documentation to add to the datetime module documentation. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Python3: on removing map, reduce, filter
Steven Bethard [EMAIL PROTECTED] writes: Some timings to verify this: $ python -m timeit -s def square(x): return x*x map(square, range(1000)) 1000 loops, best of 3: 693 usec per loop $ python -m timeit -s [x*x for x in range(1000)] 1000 loops, best of 3: 0.0505 usec per loop Maybe you should compare apples with apples, instead of oranges :-) You're only running the list comprehension in the setup code... $ python2.4 -m timeit -s def square(x): return x*x map(square, range(1000)) 1000 loops, best of 3: 464 usec per loop $ python2.4 -m timeit [x*x for x in range(1000)] 1000 loops, best of 3: 216 usec per loop So factor of 2, instead of 13700 ... -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: getattr() woes
[EMAIL PROTECTED] (Aahz) writes: In article [EMAIL PROTECTED], Thomas Rast [EMAIL PROTECTED] wrote: class dispatcher: # ... def __getattr__(self, attr): return getattr(self.socket, attr) import asyncore class Peer(asyncore.dispatcher): ... def _get_foo(self): ... # caused by a bug, several stack levels deeper ... raise AttributeError('hidden!') ... foo = property(_get_foo) ... You're not supposed to use properties with classic classes. Even if dispatcher was a new-style class, you still get the same behaviour (or misbehaviour) -- Peer().foo still raises AttributeError with the wrong message. A simple workaround is to put a try ... except AttributeError block in his _get_foo(), which would re-raise with a different error that wouldn't be caught by getattr. You could even write a property replacement for that: class HiddenAttributeError(Exception): ... pass def robustprop(fget): ... def wrapped_fget(self): ... try: ... return fget(self) ... except AttributeError, e: ... raise HiddenAttributeError(*e.args) ... return property(fget=wrapped_fget) Ideally, I think the better way is if getattr, when raising AttributeError, somehow reused the old traceback (which would point out the original problem). I don't know how to do that, though. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Namespaces and the timeit module
Roy Smith [EMAIL PROTECTED] writes: I'm playing with the timeit module, and can't figure out how to time a function call. I tried: def foo (): x = 4 return x t = timeit.Timer (foo()) print t.timeit() and quickly figured out that the environment the timed code runs under is not what I expected: Traceback (most recent call last): File ./d.py, line 10, in ? print t.timeit() File /usr/local/lib/python2.3/timeit.py, line 158, in timeit return self.inner(it, self.timer) File timeit-src, line 6, in inner NameError: global name 'foo' is not defined In fact, trying to time print dir() gets you: ['_i', '_it', '_t0', '_timer'] It seems kind of surprising that I can't time functions. Am I just not seeing something obvious? Like the documentation for Timer? :-) class Timer([stmt='pass' [, setup='pass' [, timer=timer function]]]) You can't use statements defined elsewhere, you have to define them in the setup arguments (as a string). Like this: define_foo = ''' def foo(): x = 4 return x ''' t = timeit.Timer(foo() setup=define_foo) print t.timeit() One common idiom I've seen is to put your definition of foo() in a module (say x.py), then, from the command line: $ python -m timeit -s 'from x import foo' 'foo()' (the -m is for python 2.4 to run the timeit module; use the full path to timeit.py instead for earlier pythons) Alternatively, the examples for the timeit module has another way to time functions defined in a module. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list
Re: Distutils vs. Extension header files
Mike Meyer [EMAIL PROTECTED] writes: I've got a package that includes an extension that has a number of header files in the directory with the extension. They are specified as depends = [...] in the Extension class. However, Distutils doesn't seem to do anything with them. If I do an sdist, the include files aren't added to the tarball. If I do a bdist_rpm, the source files get copied into the build directory and the build starts, but the header files aren't copied with the source file, so the build fails with a missing header file. I find it hard to believe that this is a bug in distutils, so I'd appreciate it if someone could tell me what I'm doing wrong. vincent has the solution (you need to specify them in MANIFEST.in), but I'll add my 2 cents. depends = [...] is used in building (it's like dependencies in make). If one of those files change, distutils will rebuild the extension. But that's all distutils does with it. It's braindead including stuff in the source distribution, including depends, data files, and other stuff you'd think it would do. When in doubt, add it to MANIFEST.in. -- ||\/| /--\ |David M. Cooke |cookedm(at)physics(dot)mcmaster(dot)ca -- http://mail.python.org/mailman/listinfo/python-list