Low level support for dynamic modules
Project description
Now you see it, it now you don’t!
This package defines a dynamic module type that lets you create objects in the dynamic module on demand.
Usage
To use this package, you should:
Identify an appropriate parent module where the dynamic module will live.
Ensure that plone.alterego.dynamic.create() is called with this module and a dynamic module name. Typically, you’d do this in the parent module itself, so that the dynamic module is instantiated as soon as the parent module is imported.
Register a named utility providing IDynamicObjectFactory. The name should be the same as the full dotted path to the dynamic module. This utility will be responsible for creating the objects that inhabit the dynamic module.
Example
For a more fully-featured example, see the alterego.txt doctest.
Let’s say we have a generic content class that should get a unique interface for each instance.
>>> from zope import interface >>> class IContent(interface.Interface): ... pass >>> class Content(object): ... interface.implements(IContent)>>> c1 = Content()
To create the unique interface, we will use a dynamic module. There is a helper method to make this easier. It takes a parent module and a name as arguments:
>>> from plone.alterego.dynamic import create >>> dynamic = create('plone.alterego.tests.dynamic')
We can now import this module:
>>> from plone.alterego.tests import dynamic
To make objects on demand, we’ll need to register a utility that can act as a factory.
>>> from plone.alterego.interfaces import IDynamicObjectFactory >>> from zope.interface.interface import InterfaceClass >>> class InterfaceOnDemand(object): ... interface.implements(IDynamicObjectFactory) ... ... def __call__(self, name, module): ... schema = InterfaceClass(name, (interface.Interface,), __module__=module.__name__) ... setattr(module, name, schema) ... return schema
This utility should have a name that corresponds to the full, dotted name to the dynamic module. This way, we can have different factories for different dynamic modules. We’d register this in ZCML like so:
<utility name="plone.alterego.tests.dynamic" provides="plone.alterego.interfaces.IDynamicObjectFactory" factory=".factory.InterfaceOnDemand" />
From this point forward, when we access an attribute of the dynamic module, the factory will be used:
>>> dynamic.IOne <InterfaceClass plone.alterego.tests.dynamic.IOne>
Note that so long as the setattr() call above is executed, the factory is called only once. That is, you’ll always get the same object each time you access a given attribute of the dynamic module.
Changelog
1.1.4 (2020-03-21)
Bug fixes:
Minor packaging updates. [various] (#1)
1.1.3 (2018-11-21)
Bug fixes:
Cleanup project level files (setup.py, .travis-ci.yml…) [maurits] [gforcada] (#2524)
Initialized towncrier. [gforcada] (#2548)
1.1.3 (unreleased)
1.1.2 (2018-11-21)
Bug fixes:
Update code to follow Plone styleguide. [gforcada]
1.1 (2016-11-01)
New features:
Add compatibility with Python 3. [datakurre]
1.0.1 (2016-08-11)
Fixes:
Use zope.interface decorator. [gforcada]
1.0 (2011-04-30)
Use doctest from the stdlib instead of from zope.testing [davisagli]
1.0a1 (2009-04-17)
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file plone.alterego-1.1.4.tar.gz
.
File metadata
- Download URL: plone.alterego-1.1.4.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/2.7.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf54076d624670815b289ed99bc57e559f434da443b12a24eb2328bdfa873192 |
|
MD5 | 48339e390addf5355841b89c4c1f59a9 |
|
BLAKE2b-256 | a462bcd95756a2dfb924c9a2d2de94137ca4d4f2e70a264c799d86974b832145 |
File details
Details for the file plone.alterego-1.1.4-py2.py3-none-any.whl
.
File metadata
- Download URL: plone.alterego-1.1.4-py2.py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/2.7.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2de95adf3d4097e4e1b644022432ba0a19b9334857b42552ad07e8bb72c46e3c |
|
MD5 | f1b586954aadb8b096c484f13c3652f2 |
|
BLAKE2b-256 | 1be3289b870b8db6f3c755a1c7c3ab40c703d356bc2be636f8e1d5ad8c3cb5f4 |