Articles tagged python

  1. setuptools vs distribute

    Last updated:
    By Floris Bruynooghe

    Reinout van Rees:

    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 …
  2. Tuple unpacking goodness

    By Floris Bruynooghe

    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!

  3. Synchronous classes in Python

    By Floris Bruynooghe

    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 …

  4. Python modules and the GPL: I still don't get it

    By Floris Bruynooghe

    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:

    1. You may copy and distribute verbatim copies of the Program's source code as you receive …
  5. Delny 0.4.1

    By Floris Bruynooghe

    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 …

  6. New Delny release

    By Floris Bruynooghe

    A few days ago I got another of those two-a-year inquiries about Delny, the python wrapper around Qhull for Delaunay triangulations. But increadably indirectly it finally got me to do a new release, it's only been a few years since the last one! Nothing has changed really, the only difference …

  7. Should bare except statements be allowed in the Python stdlib?

    By Floris Bruynooghe

    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 …

  8. Pain

    By Floris Bruynooghe

    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.

  9. How to bring a running python program under debugger control

    By Floris Bruynooghe

    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 …

  10. Importing modules in C extension modules

    By Floris Bruynooghe

    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 …
  11. Singletons in a Python extension module

    By Floris Bruynooghe

    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 …
  12. Raising exceptions in threads

    By Floris Bruynooghe

    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 …

  13. New home for PSI

    By Floris Bruynooghe

    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 …

  14. Compiling applications in setup.py

    By Floris Bruynooghe

    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 …

  15. Time, floating points and python modules handling time

    By Floris Bruynooghe

    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 …

  16. Compiling 32-bit Python on amd64

    By Floris Bruynooghe

    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 …

  17. Resistance to change

    By Floris Bruynooghe

    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?

  18. datetime.datetime.totimestamp()

    By Floris Bruynooghe

    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 …
  19. Porting a C extension module to py3k

    By Floris Bruynooghe

    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 …

  20. Generating source files in setup.py

    By Floris Bruynooghe

    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 …

  21. Finding memory leaks in Python extension modules

    By Floris Bruynooghe

    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 …

  22. Mocking away the .__init__() method

    By Floris Bruynooghe

    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 …

  23. Generative Tests

    By Floris Bruynooghe

    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 …

« Page 2 / 4 »