Skip to main content

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.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plone.alterego-1.1.2.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plone.alterego-1.1.2-py2-none-any.whl (7.4 kB view details)

Uploaded Python 2

File details

Details for the file plone.alterego-1.1.2.tar.gz.

File metadata

  • Download URL: plone.alterego-1.1.2.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.15

File hashes

Hashes for plone.alterego-1.1.2.tar.gz
Algorithm Hash digest
SHA256 5f405edc22680318fe41880f6ef70ba12baaa3180ceca8394caf9f02bc600a77
MD5 cddc7323c373c4add7412f7006064bdc
BLAKE2b-256 4dd449c6dd42e43570e1134256235401d689fd71880cae8cead20ccd19e11c66

See more details on using hashes here.

File details

Details for the file plone.alterego-1.1.2-py2-none-any.whl.

File metadata

  • Download URL: plone.alterego-1.1.2-py2-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.15

File hashes

Hashes for plone.alterego-1.1.2-py2-none-any.whl
Algorithm Hash digest
SHA256 5f49a47008314dc2c7a4b3f699c46698bec66db57448dcf784e34c4c7f734c3b
MD5 a213e58a7d046a246e0cbcfbe49ddfb3
BLAKE2b-256 b4185d9eab71d105ff54b7e41b60e35638c12ed2797c62e407739bb5c8ceaf11

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page