[Zope-dev] PythonLibraries Product

Evan Simpson evan at 4-am.com
Mon Sep 8 15:43:01 EDT 2003


I'm thinking seriously about writing a Product to provide collections of 
Python functions defined by a single source text -- PythonLibraries. 
This would *not* be the same as Zope 3's persistent modules, although it 
would provide some of the same benefits.

Here's the README.txt:

Python Libraries

   The Python Libraries Product provides support for collections of
   restricted Python code.  A Python Library is similar to a Folder
   full of Python-based Scripts, except that the functions in the
   Library are more like ordinary Python functions than Scripts, and
   a single persistent global variable namespace is shared among the
   functions in the Library.

   A Library obeys the same security restrictions, and has access to
   the same set of modules, as a Script.  Libraries are *not* very
   much like Python modules or packages, except in that they both are
   convenient places to keep groups of functions.  You cannot import
   a Library, define Python classes in one, or use the 'print' statement
   outside of function definitions.

   When changes to a Library are saved, its source code is executed.
   This process, known as "Library initialization", also occurs
   whenever the Library is loaded into memory from the ZODB.  Global
   variables defined during initialization are divided into three
   groups, depending on the kind of value to which they refer:
   functions, simple data, and modules.  There is one additional
   global variable, named 'Library', that is defined both during
   initialization and function evaluation, and which cannot be deleted
   or rebound.  It refers to the Library object itself.

   Functions are made available to other Zope code as Library attributes.
   A function 'do_it' contained in the Library located at '/foo/myLib'
   may be called by a TALES expression such as "here/foo/myLib/do_it"
   or the Python expression "context.foo.myLib.do_it()".  Function
   names that conflict with methods of the Library object are syntax
   errors.  A Library's functions are not published by default, which
   means that they cannot be accessed by URL through ZPublisher.  There
   is a Library method 'pl_publish()' that can be used to explicitly
   publish a function.

   Simple data includes Python built-in types such as numbers,
   strings, lists, and dictionaries.  Variables with simple data are
   persistent, and their value is retained even when the Library is
   changed or reloaded, although the execution of the code can remove
   or overwrite them.  These variables are not visible to other Zope
   objects.

   Modules are imported Python modules.  These are not visible to
   other Zope objects, and do not persist -- they are re-imported
   each time the Library is changed or loaded.

   Unlike Scripts, Libraries do not have a Bindings tab.  In order to
   access context objects, such as the Library's container, the root
   Zope object, or the authenticated user, functions must use a global
   variable created by a call to the 'pl_bind()' method of the Library.
   This method takes two arguments: a variable name, and a TALES
   expression.  The TALES expression will be evaluated the first time
   that the variable is used in each call to a Library function, and
   the value will be cached for subsequent uses within the same call.
   For example, examine the following snippet of Library code:

     Library.pl_bind('user', 'user')
     Library.pl_bind('thingy', 'here/thingy | nothing')
     def f():
         if user.has_role('Authenticated') and thingy is not None:
             print user, thingy
         return printed

   When the function 'f' is called, the 'user' and 'thingy' variables
   will be evaluated in its first line, and the values will be reused
   in the second line, if the condition is true.  If 'f' is called
   again, in the same request or not, the bound variables will be
   re-evaluated.  Bound variables are not available during Library
   initialization, since they are unlikely to evaluate meaningfully
   when a Library is loaded from the ZODB.




More information about the Zope-Dev mailing list