enhanced python interface

17 views
Skip to first unread message

JohnBender

unread,
Jul 3, 2009, 6:42:51 PM7/3/09
to Comedi: Linux Control and Measurement Device Interface
Inspired by WxPython, I decided it would be worth my while to clean up
Comedi's Python interface slightly. Specifically, I was tired of:

import comedi
dev = comedi.comedi_open ('/dev/comedi0')

because it's a bit redundant and non-Pythonic. I wrote a little script
(pythonify.py) to be run between SWIG compilation and the install,
which removes the extra "comedi_" bits. So now the commands would be:

import comedi
dev = comedi.open ('/dev/comedi0')

This script I wrote is fully backward-compatible, since it leaves the
old commands in place, as well. So it "can't possibly" break any
existing code, but it will enable more streamlined and happy Python
programming for the future.

I don't know how to speak Makefile, so I'm not sure how to integrate
it into the existing codebase. At the moment, if you're running 'make'
in the swig/python directory, you can run 'make && python pythonify.py
&& sudo make install' and it works perfectly. Presumably there's some
easy way to set up an installation hook to make it run as part of the
make process.

Hopefully somebody can put in a little effort to make this available
to the whole community; otherwise, I hope it's at least useful to a
few individuals out there. I will upload pythonify.py to the "Files"
section of this discussion group.

W. Trevor King

unread,
Jul 5, 2009, 11:18:01 AM7/5/09
to comed...@googlegroups.com
On Fri, Jul 03, 2009 at 03:42:51PM -0700, JohnBender wrote:

> I wrote a little script (pythonify.py) to be run between SWIG
> compilation and the install, which removes the extra "comedi_"
> bits. So now the commands would be:
>
> import comedi
> dev = comedi.open ('/dev/comedi0')

Ooh, nice :). This had always bugged me, but not enough to do
anything about it ;).

However, I ran into this:

$ python pythonify.py
Traceback (most recent call last):
File "pythonify.py", line 24, in <module>
comedi_new.write( "{1} = _comedi.{0}_{1}\n".format( match.group(1),
AttributeError: 'str' object has no attribute 'format'
$ python --version
Python 2.5.2

From
http://docs.python.org/library/stdtypes.html
str.format is new in Python 2.6.

Work around with this patch on your pythonify.py:

--- pythonify.py-orig 2009-07-05 11:09:05.000000000 -0400
+++ pythonify.py 2009-07-05 11:11:21.000000000 -0400
@@ -21,8 +21,14 @@
comedi_new.write( line )
match = pattern.search( line )
if match is not None and 'swig' not in match.group( 0 ):
- comedi_new.write( "{1} = _comedi.{0}_{1}\n".format( match.group(1),
- match.group(2) ) )
+ try:
+ new_string = "{1} = _comedi.{0}_{1}\n".format( match.group(1),
+ match.group(2) )
+ except AttributeError: # for compatability with python < 2.6
+ new_string = "%s = _comedi.%s_%s\n" % ( match.group(2),
+ match.group(1),
+ match.group(2) )
+ comedi_new.write(new_string)
comedi_new.close()
comedi_py.close()

> I don't know how to speak Makefile, so I'm not sure how to integrate
> it into the existing codebase. At the moment, if you're running 'make'
> in the swig/python directory, you can run 'make && python pythonify.py
> && sudo make install' and it works perfectly. Presumably there's some
> easy way to set up an installation hook to make it run as part of the
> make process.

All you need to change in swig/python/Makefile.am is:

--- Makefile.am-orig 2009-07-05 10:34:34.000000000 -0400
+++ Makefile.am 2009-07-05 10:35:40.000000000 -0400
@@ -14,8 +14,7 @@

pyexec_SCRIPTS = comedi.py

-EXTRA_DIST = README.txt setup.py test_comedi.py
+EXTRA_DIST = README.txt setup.py test_comedi.py pythonify.py

comedi_python_wrap.c comedi.py: $(srcdir)/../comedi.i
$(SWIG) -python -o comedi_python_wrap.c -I$(top_srcdir)/include $(srcdir)/../comedi.i
+ python pythonify.py


For other comedi-python people out there, I've written some python
wrappers to make digitial and analog IO simpler for my lab. I'm not
sure how well the simultaneous analog IO will transfer onto non
NI-DAQ-STC-based cards, but feel free to take a look:
http://www.physics.drexel.edu/~wking/rsrch/code/#pycomedi

Cheers,
Trevor/labrat

--
This email may be signed or encrypted with GPG (http://www.gnupg.org).
The GPG signature (if present) will be attached as 'signature.asc'.
For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy

My public key is at http://www.physics.drexel.edu/~wking/pubkey.txt

Ian Abbott

unread,
Jul 6, 2009, 6:36:07 AM7/6/09
to comed...@googlegroups.com
On 03/07/09 23:42, JohnBender wrote:
> Inspired by WxPython, I decided it would be worth my while to clean up
> Comedi's Python interface slightly. Specifically, I was tired of:
>
> import comedi
> dev = comedi.comedi_open ('/dev/comedi0')
>
> because it's a bit redundant and non-Pythonic. I wrote a little script
> (pythonify.py) to be run between SWIG compilation and the install,
> which removes the extra "comedi_" bits. So now the commands would be:
>
> import comedi
> dev = comedi.open ('/dev/comedi0')
>
> This script I wrote is fully backward-compatible, since it leaves the
> old commands in place, as well. So it "can't possibly" break any
> existing code, but it will enable more streamlined and happy Python
> programming for the future.

Sounds good! Can your script be placed under the LGPL so it can be
incorporated into the Comedilib CVS repository?

> I don't know how to speak Makefile, so I'm not sure how to integrate
> it into the existing codebase. At the moment, if you're running 'make'
> in the swig/python directory, you can run 'make && python pythonify.py
> && sudo make install' and it works perfectly. Presumably there's some
> easy way to set up an installation hook to make it run as part of the
> make process.
>
> Hopefully somebody can put in a little effort to make this available
> to the whole community; otherwise, I hope it's at least useful to a
> few individuals out there. I will upload pythonify.py to the "Files"
> section of this discussion group.

It looks like Trevor/labrat is on the case there.

--
-=( Ian Abbott @ MEV Ltd. E-mail: <abb...@mev.co.uk> )=-
-=( Tel: +44 (0)161 477 1898 FAX: +44 (0)161 718 3587 )=-

JohnBender

unread,
Jul 6, 2009, 8:36:36 AM7/6/09
to Comedi: Linux Control and Measurement Device Interface
> > This script I wrote is fully backward-compatible, since it leaves the
> > old commands in place, as well. So it "can't possibly" break any
> > existing code, but it will enable more streamlined and happy Python
> > programming for the future.
>
> Sounds good!  Can your script be placed under the LGPL so it can be
> incorporated into the Comedilib CVS repository?

Happily. And sorry about the Python versioning problem -- I'd written
it more to be forward-compatible than backward. (Though notice I
didn't use the with_statement...)
Reply all
Reply to author
Forward
0 new messages