Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
devel:languages:python:misc
python-dm.reuse
python-dm.reuse.spec
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File python-dm.reuse.spec of Package python-dm.reuse
# # spec file for package python-dm.reuse # # Copyright (c) 2013 SUSE LINUX Products GmbH, Nuernberg, Germany. # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed # upon. The license for this file, and modifications and additions to the # file, is the same license as for the pristine package itself (unless the # license for the pristine package is not an Open Source License, in which # case the license is the MIT License). An "Open Source License" is a # license that conforms to the Open Source Definition (Version 1.9) # published by the Open Source Initiative. # Please submit bugfixes or comments via http://bugs.opensuse.org/ # Name: python-dm.reuse Version: 1.1 Release: 0 Summary: Support for object reuse with slight modifications License: BSD-2-Clause Group: Development/Languages/Python Url: https://pypi.python.org/pypi/dm.reuse Source: https://pypi.python.org/packages/source/d/dm.reuse/dm.reuse-%{version}.tar.gz BuildRequires: python-devel BuildRequires: python-setuptools BuildRoot: %{_tmppath}/%{name}-%{version}-build %if 0%{?suse_version} && 0%{?suse_version} <= 1110 %{!?python_sitelib: %global python_sitelib %(python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()")} %else BuildArch: noarch %endif %description dm.reuse ======== 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`. Examples -------- >>> 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 'f' >>> g.func_name 'f' >>> g=rebindFunction(f, dict(i=-1, j=-2), funcName='g') >>> g.func_name 'g' >>> 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__ True >>> 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 'p' >>> 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) History ------- 1.1 ``nameRebindDir`` support added %prep %setup -q -n dm.reuse-%{version} %build python setup.py build %install python setup.py install --prefix=%{_prefix} --root=%{buildroot} %files %defattr(-,root,root,-) %{python_sitelib}/* %changelog
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor