[Python-modules-commits] r11501 - in packages/python-ctypeslib/trunk/debian (examples.Debian)
mrbeige-guest at users.alioth.debian.org
mrbeige-guest at users.alioth.debian.org
Tue Feb 2 16:56:08 UTC 2010
Date: Tuesday, February 2, 2010 @ 16:56:02
Author: mrbeige-guest
Revision: 11501
Update examples.Debian
Modified:
packages/python-ctypeslib/trunk/debian/examples.Debian
Modified: packages/python-ctypeslib/trunk/debian/examples.Debian
===================================================================
--- packages/python-ctypeslib/trunk/debian/examples.Debian 2010-02-02 16:12:00 UTC (rev 11500)
+++ packages/python-ctypeslib/trunk/debian/examples.Debian 2010-02-02 16:56:02 UTC (rev 11501)
@@ -24,36 +24,62 @@
#
# When you pass the -l argument, it will scan the libary for
# executable symbols. If it finds them, it will automatically set up
-# to run, using ctypes, from that library.
+# to run, using ctypes, from that library. In this example, math_c.py
+# becomes a module which automatically interface to libm
h2xml /usr/include/math.h -c -o math_c.xml
xml2py math_c.xml -o math_c.py -l /lib/libm.so.6
+# In math_c.py:
+#
+# | _libraries['/lib/libm.so.6'] = CDLL('/lib/libm.so.6')
+# |
+# | log10 = _libraries['/lib/libm.so.6'].log10
+# | log10.restype = c_double
+# | log10.argtypes = [c_double]
+#
+# which is an automatic ctypes interface to log10 in libm
+# To use it:
+python2.6
+>>> import math_c
+>>> math_c.log10(100)
+2.0
+
+
########################################
-
-# Let's work on time.h:
+# Let's play with time.h (works on python2.5):
#
-# We get a lot of stuff, for example, the definition of the C structure
-# "struct tm" is automatically created, for example:
+# We get a lot of stuff, for example, the ctypes definition of the C
+# structure "struct tm" is automatically created, for example:
#
-# class tm(Structure):
-# pass
-# tm._fields_ = [
-# ('tm_sec', c_int),
-# ('tm_min', c_int),
-# ('tm_hour', c_int),
-# ('tm_mday', c_int),
-# ('tm_mon', c_int),
-# ('tm_year', c_int),
-# ('tm_wday', c_int),
-# ('tm_yday', c_int),
-# ('tm_isdst', c_int),
-# ('tm_gmtoff', c_long),
-# ('tm_zone', STRING),
+# | class tm(Structure):
+# | pass
+# | tm._fields_ = [
+# | ('tm_sec', c_int),
+# | ('tm_min', c_int),
+# | ('tm_hour', c_int),
+# | ('tm_mday', c_int),
+# | ('tm_mon', c_int),
+# | ('tm_year', c_int),
+# | ('tm_wday', c_int),
+# | ('tm_yday', c_int),
+# | ('tm_isdst', c_int),
+# | ('tm_gmtoff', c_long),
+# | ('tm_zone', STRING),
+# | ]
+#
+# We also have various functions defined:
+#
+# | _libraries['/lib/libc.so.6'] = CDLL('/lib/libc.so.6')
+# |
+# | strptime = _libraries['/lib/libc.so.6'].strptime
+# | strptime.restype = STRING
+# | strptime.argtypes = [STRING, STRING, POINTER(tm)]
+
# We use '-l /lib/libc.so.6', so that we automaticall set up to run
# defined functions from libc.so.6, using ctypes
@@ -63,11 +89,14 @@
man strptime
python
>>> import time_c
-# Initialize a `struct tm` object to use as the return values of our
-# function call.
+# Initialize a `struct tm` object to use as the return values of our
+# function call.
>>> tm = time_c.tm()
+# Call strptime, with two strings and a pointer to the struct_tm. Read
+# the manual page on strptime to understand the return value.
>>> time_c.strptime("Dec 19, 1985", "%b %d, %Y", tm)
''
+# And our data structure is filled in:
>>> tm.tm_yday
352
>>> tm.tm_year
More information about the Python-modules-commits
mailing list