In case you heard of both setuptools and distribute: distribute fully replaces setuptools. Just use distribute. Setuptools is “maintained” (for various historically dubious values of “maintain”) by one person (whom all should applaud for creating the darn thing in the first place, btw!). Distribute is maintained by …
Articles tagged python
-
-
On using .__getstate__()
Do not, I repeat, do not define .__getstate__() idly.
Thank you very much
-
Tuple unpacking goodness
Todays pleasant surprise:
>>> a = {'a': (0, 1), 'b': (2, 3)} >>> for k, (v1, v2) in a.iteritems(): ... print k, v1, v2 ... a 0 1 b 2 3 >>>
Nice!
-
Finding memory leaks in extension modules
After reading Ned Batchelder's post on his experience hunting a memory leak (which turned out to be a reference counting error) it occurred to me that even tough I have a script to check memory usage I should also really be checking reference counts with sys.gettotalrefcount(). And indeed, after …
-
New Python System Information release!
I've just released a new version of PSI! PSI is a cross-platform Python package providing real-time access to processes and other miscellaneous system information such as architecture, boottime and filesystems. Among the highlights of this release are:
- Improved handling of time
- We now have our own object to represent time …
-
Synchronous classes in Python
What I'd like to build is an object that when doing anything with it would first acquire a lock and release it when finished. It's a pattern I use fairly-regularly and I am getting bored of always manually defining a lock next to the other object and manually acquiring and …
-
Python modules and the GPL: I still don't get it
I've never understood if you can use GPL python modules (or packages) in GPL-incompatibly licensed code. Today I re-read the GPL looking for this and am tempted to think yes, you can. The GPL says:
- You may copy and distribute verbatim copies of the Program's source code as you receive …
-
Delny 0.4.1
A little while ago I released a new version of Delny (my python wrapper around Qhull for Delaunay triangulations), the main purpose to use numpy instead of numeric. Impressively enough people actually seemed to care and I got a few bug reports and hints for improvements.
So I just released …
-
New Delny release
-
Should bare except statements be allowed in the Python stdlib?
Firstly to clarify the terminology, this is bare except statement:
try: ... except: ...
And this is a non-bare except statement, but bear in mind the type of the exception that is caught can be anything:
try: ... except Exception: ...
The point is that both fragments are a catch-all exception handler, only the …
-
Pain
I've spent the last 2 days trying to get a stack trace from a crashing python extension module in windows. And I still haven't figured it out. That's sooo very motivating.
Give me GNU/Linux any day.
-
PyCon UK 2009
So there's a PyCon UK 2009 Unconference. I wish I could go but have already planned to be on Pabbay and Mingulay in the Outer Hebrides, Scotland. So instead of hearing all about python I'll be clinging on to their amazing cliffs. Guess I'm not that bad off anyway...
-
How to bring a running python program under debugger control
Of course pdb has already got functions to start a debugger in the middle of your program, most notably pdb.set_trace(). This however requires you to know where you want to start debugging, it also means you can't leave it in for production code.
But I've always been envious of …
-
Importing modules in C extension modules
It seems that if you need another module in a function of your extension module, the way modules in the standard library seem to solve this is like this:
static PyObject * func(void) { PyObject *foo; foo = PyImport_ImportModuleNoBlock("foo"); if (foo == NULL) return NULL; /* do stuff with foo */ Py_DECREF(foo); return …
-
Singletons in a Python extension module
If you want a singleton in a C extension module for CPython you basically have to do the same as when doing this in plain Python: the .__new__() method needs to return the same object each time it is called. Implementing this has a few catches though.
static PyObject * MyType_new …
-
Raising exceptions in threads
It's not simply raising an exception in a thread that I want. I want to raise an exception in a thread from another thread. It's like sending signals to threads, only signals in pyhon can only be delivered to the main thread (for portability). But Python has a other asynchronous …
-
New home for PSI
PSI, the Python System Information package that pulls interesting information from the kernel and makes it available in a nice Python API, has a new home at bitbucket. This means that the source conde now lives inside a mercurial repository instead of subversion.
This actually happened about a week ago …
-
Python System Information 0.3b1^W0.3b1.1
Short summary: PSI is alive and we've just released the first beta of a much improved upcoming 0.3 release!
Back in 2007 Chris Miles announced PSI - Python System Information, a Python extension module to provide access to some system information not normally available to Python. Most notably it allows …
-
Compiling applications in setup.py
If you need to compile a random application in setup.py this is not that hard:
cc = distutils.ccompiler.new_compiler() distutils.sysconfig.customize_compiler(cc) cc.link_executable(['test.c'], 'a.out')
There is no need to create the object files explicitly with cc.compile() first if you have no need for …
-
What's new in Python 2.6: logging
The What's New in Python 2.6 document is very good and contains loads of information, congrats for making it. But it misses out a nice addition to the logging module: LoggerAdapter Objects. It allows you to create a logger object with extra contextual information really easy:
log = logging.LoggerAdapter …
-
Time, floating points and python modules handling time
Some memory in the back of my mind tells I've once read a rant about storing time that argued you should never store time in floating point variables. My memory seems to think the rant convinced me and it does indeed seem better to store time in integers so that …
-
Compiling 32-bit Python on amd64
If you ever feel the need to compile a 32-bit version of Python on a amd64 bit machine, this is how you do it on a Debian/Ubuntu system.
Firstly you need the correct compiler stuff, this means you need gcc-multilib and 32-bit development libraries of at least libc. On …
-
Resistance to change
Why can C developers be happy with using config.h to get constants about where to look for configuration or data files for example, yet Python developers seem to refuse to think of any way they might want to support finding these files in a portable way?
-
datetime.datetime.totimestamp()
There exists a datetime.datetime.fromtimestamp() and many a time I've wondered why there is no .totimestamp() equivalent. The answer is that not all datetime objects can be represented by a timestamp. But if you know this restriction and want it anyway, this is the magic:
time.mktime(datetime_object.timetuple …
-
Porting a C extension module to py3k
This was not that hard! First look at what everyone else has to say about this, nice aggregated on the python wiki. It covers a lot.
Now for what I discovered on top of this:
When you're defining a new type self->ob_type doesn't exist anymore. This is a problem …
-
Generating source files in setup.py
I have a Python extension module written in C and one of the weirder things it needs to do is have a number of module constants with their values which are defined (e.g. #define PREFIX_FOO 0x01 etc.) in an external C header file. All the defined names in that …
-
import skynet
So who's going to make the skynet module?
-
Finding memory leaks in Python extension modules
Valgrind is amazing, even if you've never used it before it's basic usage is really simple:
$ valgrind --tool=memcheck --leak-check=full /usr/bin/python ./test.py
This is enough to point you at the places in your extension module where you allocated stuff that didn't get freed later. This is …
-
Mocking away the .__init__() method
I wanted to test a method on a class that didn't really depend on a lot of other stuff off the class. Or rather what it did depend on I had already turned into Mock objects with appropriate .return_value and asserting with .called etc. Problem was that the .__init__() method …
-
Generative Tests
Both nose and py.test allow you to write generative tests. These are basically generators that yield the actual test functions and their arguments. The test runners will then test all the yielded functions with their arguments.
However I'm left wondering why this is better then just iterating in the …