"…mais ce serait peut-être l'une des plus grandes opportunités manquées de notre époque si le logiciel libre ne libérait rien d'autre que du code…"

Posts Tagged ‘ctypes’

An other ctypes based python module : pyUSB 1.0 (alpha) is out !

Posted by patrick sur avril 22, 2010


PyUSB 1.0 – Easy USB access from Python


The PyUSB module provides Python with easy access to the host
machine’s Universal Serial Bus (USB) system.

Until 0.4 version, PyUSB used to be a thin wrapper aroung libusb.
With 1.0 version, things changed considerably. Now PyUSB is an
API rich, backend neutral Python USB module easy to use.

As with most Python modules, PyUSB’s documentation is based on Python
doc strings and can therefore be manipulated by tools such as pydoc.

You can also find a tutorial at: http://pyusb.sourceforge.net/docs/1.0/tutorial.html.

PyUSB is being developed and tested in Linux and Windows, but it should work
fine in any platform running Python >= 2.3, ctypes and at least one of the
builtin backends.

PyUSB supports libusb 0.1, libusb 1.0 and OpenUSB, but the user does not need
to worry about that, unless in some corner cases.

If you have any question about PyUSB, you can use the PyUSB mailing list
hosted in the SourceForge. In the PyUSB website (http://pyusb.sourceforge.net)
you can find instructions on how to subscribe to the mailing list.

Installing PyUSB on GNU/Linux Systems

These instructions are for Debian-based systems.  Instructions for
other flavors of GNU/Linux should be similar.

You will first need to install the following packages:

1) python (PyUSB is useless without it), version >= 2.3
2) At least one of the supported libraries (libusb 1.0, libusb 0.1 or OpenUSB)
3) If your Python version is < 2.5, you have to install ctypes as a separate package,
because these versions of Python does not ship it.

For example, the command

sudo apt-get install python libusb

should install all these packages on most Debian-based systems with
access to the proper package repositories.

Once the above packages are installed, you can install PyUSB
with the command

python setup.py install

run as root from within the same directory as this README file.

Installing PyUSB on Windows

Now that PyUSB is 100% written in Python, you install it on Windows
in the same way you do on Linux, but libusb for Windows is done
through libusb-win32 package (libusb 1.0 backend is in progress).
To install it you do as usual:

python setup.py install

Reporting bugs/Submitting patches

Some people have been sending me patches and reporting bugs directly
in my email. Please, do it through SourceForge tracker, I had
a hardtime tracking their names to put them in the acknowledgments file. 😉

PS: this README file was based on the great Josh Lifton’s one… ^_^

import usb.core
import usb.util

# find our device
dev = usb.core.find(idVendor=0xfffe, idProduct=0x0001)

# was it found?
if dev is None:
    raise ValueError('Device not found')

# set the active configuration. With no arguments, the first
# configuration will be the active one

# get an endpoint instance
ep = usb.util.find_descriptor(
        dev.get_interface_altsetting(),   # first interface
        # match the first OUT endpoint
        custom_match = \
            lambda e: \
                usb.util.endpoint_direction(e.bEndpointAddress) == \

assert ep is not None

# write the data

Voir aussi

  • http://docs.python.org/library/ctypes.html (‘ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python’)
  • http://pypi.python.org/pypi/jaraco.windows/1.8 (‘jaraco.windows aims to provide a pure-python interface to Windows APIs using ctypes. This package is not designed to be exhaustive, but rather to supply interfaces as they are needed by the contributors.’)

Posted in 2010, ctypes, Operating Systems, python, USB | Tagué: , | Leave a Comment »

Traitement d’images avec python: python-magic, pyexiv2, PIL, ReportLab, Motmot, Pythonmagick, magickpy, APLpy

Posted by patrick sur août 10, 2009

Source: Python magazine, june 2009, volume 3, issue 6,  « Image processing with python » by Eugen Winterberger.

This library wrap the FreeImage library functions so a python developer can easy drive FreeImage .dll/.so
For do it I use ctypes and the FreeImage library

Posted in 2009, python, Traitement d'images | Tagué: , , , , , , , , , , , , | Leave a Comment »

Java : « Will Open-Sourcing Java Remove Competetive Corporate-Think? » de Bruce Eckel

Posted by patrick sur décembre 19, 2008

  • http://www.artima.com/weblogs/viewpost.jsp?thread=244996 (« Will Open-Sourcing Java Remove Competetive Corporate-Think? The problem with public corporations is that, no matter what they say, « maximize quarterly profits » is the real mantra…Java was created in a company run by a hyper-competitive CEO and the whole culture around it has been competitive. Many decisions were made without the consumer (programmers) in mind, but with Sun’s apparent best interests in mind…Early on, the « write once, run everywhere » chant started and that justified everything. If you want to talk to something OS-specific, you can use JNI, right? What a nightmare. And the joke became « write once, test everywhere »…Sun, as is its habit, never learns from its mistakes. Sometime in the last couple of years, rumor has it that Adobe apparently extended the possibility of working with Sun so that Flex would integrate seamlessly with Java. This would seem like a best-of-all-worlds situation, to pal up with the folks who have been specializing in UI programming. Instead, that seems to be around the time that JavaFX was started (which even poached Flex’s « Fx »). The classic « not invented here » response is the standard reply of corporate competitive-think…Open-sourcing Java is not going to make it an open-source project. The culture that has built up around Java for over ten years is not going to change just by moving to a new license…A truly open-source programming language does not have shareholders to serve. It can only serve its actual customers, the programmers who are consuming the language. For example, Python has always been about « what do you want to do today? » If you want to create a cross-platform app, no problem. And if you want to talk directly to the OS, that’s been made as easy as possible. This makes sense because Python is an « enabling » language — it’s about helping you do what you need to do, rather than telling you what you can and can’t do (these comments also apply to Ruby and other enabling languages, but I only occasionally tinker with Ruby so I can’t speak authoritatively about it).Python, after thrashing around with many different approaches, also solved the « interfacing to native code » problem. In Python 2.5, ctypes were added. Now you only have to say « there’s the DLL, connect to it as efficiently as possible » and it does it. And amazingly, the DLLs are the only things that are different on different platforms; your Python code can be the same. That’s the right solution to the problem. (When Jython 2.5 comes out, I’ve heard it will have ctypes working so that may be the reasonable alternative to JNI)…Java won’t die. But the adoption of new Java versions and features is going to continue to slow. People have been bitten too many times. Java lost its status of being a leader awhile ago, and it’s now a legacy language — it’s just taking awhile for everyone to realize it…Full disclosure: My consulting contract (mostly speaking, some free-form writing) with Adobe expired around last May (and yes, Adobe is a full-on corporation with all of the problems that implies — but according to rumor it was Adobe that suggested the liaison with Sun); I decided on my own that Flex is a good solution for UIs, both before and after that contract. Also, James Ward and I just published our coauthored book (written under our own steam, not part of the Adobe contract) First Steps in Flex. I’m working on an open-source book on Python 3. And before you say I’ve gone totally anti-Java, I also organize The Java Posse Roundup with The Java Posse (so think « tough love »).« )


  • http://wiki.debian.org/Java
  • http://en.wikipedia.org/wiki/IcedTea (‘IcedTea is a software development and integration project launched by Red Hat in June 2007.[1] The goal is to make the OpenJDK software which Sun Microsystems released as free software in 2007 usable without requiring any other software that is not free software. For Red Hat, this would make it possible to add OpenJDK to the Fedora Linux distribution, as well as other distributions. This goal has been met, and a version of IcedTea based on OpenJDK was packaged with Fedora 8 in November 2007. April 2008 saw the first release[2] of a new variant, IcedTea6 which is based on Sun’s build drops of OpenJDK6, a fork of the OpenJDK with the goal of being compatible with the existing JDK6. This was released in Ubuntu and Fedora in May 2008. The IcedTea package in these distributions has been renamed to OpenJDK using the OpenJDK trademark notice‘)
  • http://packages.debian.org/sid/java-package (‘This program currently works with the following Java(TM) 2 Runtime Environments and Development Kits:
     * Sun Microsystems(TM) 1.4, 5 and 6 Standard Edition
     * IBM(TM) 1.3, 1.4, 5 and 6 Standard Edition
     * Blackdown Java-Linux 1.3 and 1.4 Standard Edition')

Posted in 2008, java, jython, python | Tagué: , , , , | Leave a Comment »