[Tutor] definition of a library

Alexandre Ratti alex@gabuzomeu.net
Wed, 20 Mar 2002 18:48:46 +0100


Hi Erik,


At 12:01 20/03/2002 -0500, you wrote:
>Date: Wed, 20 Mar 2002 08:09:49 -0500
>Subject: Re: [Tutor] definition of a library
>From: Erik Price <erikprice@mac.com>

>On Tuesday, March 19, 2002, at 11:46  PM, dman wrote:
>
> > In python, libraries can be written in Python, C, or C++ (and possibly
> > some other language, but I haven't seen them).  If they are python,
> > then they are usually distributed in source form since the interpreter
> > will automatically byte-compile them.  If they are C or C++ then they
> > must be compiled into a "shared libary" (.so on *nix, .dll on 'doze).
>
>Since I'm not quite ready to learn C or C++ (maybe tomorrow :), I can
>just assume that if a C or C++ library is intended to be embedded within
>Python programs, then it has already been compiled appropriately?

Yes, a library is basically any kind of module you can call from your 
program. It can be pure python or it can be written in C and precompiled. 
Precompiled C libraries are platform-dependant: you need to get the correct 
version for your operating system.

I think these compiled libraries usually have a .pyd extension. For 
instance, see this CSV module:

<QUOTE>

The CSV module provides a fast CSV parser which can split and join CSV 
records which have been produced by Microsoft products such as Access and 
Excel. http://www.object-craft.com.au/projects/csv/

</QUOTE>

The main point is that both kind of libraries can be called and used in the 
same way by the Python programmer. Example code for the CSV module:

import csv
p = csv.parser()
for i in xrange(100000):
     p.parse('1,2,3,4,5,6')

>There's something about this that reminds me of my recent Apache
>compile, since it can use DSO modules without needing a recompile, but I
>don't really understand all of the details....
>
> > The interpreter knows how to load .py[co]? files itself, and locates
> > them via $PYTHONPATH and sys.path.  The C function dlopen() allows a
> > (C) program to link and load a .so at runtime.  Thus the interpreter
> > can load an extension module without being recompiled.
>
>Let me stop you for a minute for a clarification -- does this mean that
>any C programs need to be called with dlopen()?  I think I have seen a C
>module called "cpickle" that essentially operates as a faster "pickle".
>Has this been specially written so as not to require the dlopen()
>function, or is it because it's not a .so file?

I think that C extension modules need to be developped in a special way if 
there are meant to be used from Python. Then you just import them into your 
Python program, just as any module.

>Thanks dman for the useful knowledge!


Cheers.

Alexandre