Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

pypm install dm.reuse

How to install dm.reuse

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install dm.reuse
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.1 Available View build log
1.0 Available View build log
Windows (64-bit)
1.1 Available View build log
1.0 Available View build log
Mac OS X (10.5+)
1.1 Available View build log
1.0 Available View build log
Linux (32-bit)
1.1 Available View build log
1.0 Available View build log
Linux (64-bit)
1.1 Available View build log
1.0 Available View build log
BSD (see "dm/reuse/LICENSE.txt", for details)
Lastest release
version 1.1 on Jan 5th, 2011

Utilities to reuse (slightly modified) objects in new contexts.

Currently, there is a single utility: rebindFunction. It allows to reuse the code of a function while changing name, globals, default arguments, properties and/or names used.

Lets look at a trivial example. Function f accesses global variables i and j.


>>> i = 1; j = 2
>>> def f(): return i, j
>>> f()
(1, 2)

We want to derive a new function g which binds i to -1:

>>> from dm.reuse import rebindFunction
>>> g=rebindFunction(f, i=-1)
>>> g()
(-1, 2)

We can specify the rebinds not only via keyword arguments but via a dictionary as well:

>>> g=rebindFunction(f, dict(i=-1, j=-2))
>>> g()
(-1, -2)

Usually, the function name is taken over from the original function, but it can be changed:

>>> f.func_name
>>> g.func_name
>>> g=rebindFunction(f, dict(i=-1, j=-2), funcName='g')
>>> g.func_name
>>> g()
(-1, -2)

The originals function docstring is taken over, too -- unless overridden:

>>> f.func_doc = 'some documentation'
>>> g=rebindFunction(f, dict(i=-1, j=-2))
>>> f.__doc__ is g.__doc__
>>> g=rebindFunction(f, dict(i=-1, j=-2), funcDoc='some new documentation')
>>> g.__doc__
'some new documentation'

Default values for arguments can be added, removed or changed. Unknown arguments are recognized:

>>> def f(a1, a2=2): return a1, a2
>>> g=rebindFunction(f, argRebindDir=dict(a1=1))
>>> g()
(1, 2)
>>> from dm.reuse import REQUIRED
>>> g=rebindFunction(f, argRebindDir=dict(a2=REQUIRED))
>>> g(1)
Traceback (most recent call last):
TypeError: f() takes exactly 2 arguments (1 given)
>>> g=rebindFunction(f, argRebindDir=dict(a2=10))
>>> g(1)
(1, 10)
>>> g=rebindFunction(f, argRebindDir=dict(a3=10))
Traceback (most recent call last):
ValueError: unknown arguments in `argRebindDir`: a3

Finally, function properties can be rebound with propRebindDir. We are careful, to give the new function a separate new property dict.

>>> f.prop='p'
>>> g=rebindFunction(f)
>>> g.prop
>>> g=rebindFunction(f, propRebindDir=dict(prop='P', prop2='p2'))
>>> g.prop, g.prop2
('P', 'p2')
>>> f.__dict__
{'prop': 'p'}

Occationally, functions use local imports which are not adequate in the new context. In order to provide control over them, names used inside the function code can be changed.

>>> def f(a): import codecs; return codecs, a
>>> g=rebindFunction(f, nameRebindDir=dict(codecs='urllib'))
>>> r = g(1)
>>> r[0].__name__, r[1]
('urllib', 1)


1.1 nameRebindDir support added

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.