Skip to main content

Generate and resolve paths to to objects.

Project description

ObjectPath

This package contains two things:

  • the z3c.objpath.interfaces.IObjectPath interface.

  • some helper functions to construct (relative) object paths, in z3c.objpath.path.

The idea is that a particular application can implement a utility that fulfills the IObjectPath interface, so that it is possible to construct paths to objects in a uniform way. The implementation may be done with zope.traversing, but in some cases you want application-specific object paths. In this case, the functions in z3c.objpath.path might be useful.

We’ll have a simple item:

>>> class Item(object):
...   __name__ = None
...   __parent__ = None
...   def __repr__(self):
...     return '<Item %s>' % self.__name__

Let’s create a simple container-like object:

>>> class Container(Item):
...   def __init__(self):
...     self._d = {}
...   def __setitem__(self, name, obj):
...     self._d[name] = obj
...     obj.__name__ = name
...     obj.__parent__ = self
...   def __getitem__(self, name):
...     return self._d[name]
...   def __repr__(self):
...     return '<Container %s>' % self.__name__

Now let’s create a structure:

>>> root = Container()
>>> root.__name__ = 'root'
>>> data = root['data'] = Container()
>>> a = data['a'] = Container()
>>> b = data['b'] = Container()
>>> c = data['c'] = Item()
>>> d = a['d'] = Item()
>>> e = a['e'] = Container()
>>> f = e['f'] = Item()
>>> g = b['g'] = Item()

We will now exercise two functions, path and resolve, which are inverses of each other:

>>> from z3c.objpath import path, resolve

We can create a path to a from root:

>>> path(root, a)
'/root/data/a'

We can also resolve it again:

>>> resolve(root, '/root/data/a')
<Container a>

We can also create a path to a from data:

>>> path(data, a)
'/data/a'

And resolve it again:

>>> resolve(data, '/data/a')
<Container a>

We can make a deeper path:

>>> path(root, f)
'/root/data/a/e/f'

And resolve it:

>>> resolve(root, '/root/data/a/e/f')
<Item f>

The path ‘/’ leads to the root object:

>>> resolve(root, '/')
<Container root>

We get an error if we cannot construct a path:

>>> path(e, a)
Traceback (most recent call last):
 ...
ValueError: Cannot create path for <Container a>

We also get an error if we cannot resolve a path:

>>> resolve(root, '/root/data/a/f/e')
Traceback (most recent call last):
 ...
ValueError: Cannot resolve path /root/data/a/f/e

z3c.objpath changes

1.3 (2022-12-02)

  • Add support for Python 3.7, 3.8, 3.9, 3.10, 3.11.

1.2 (2018-06-14)

  • Add support for Python 3.5, 3.6, PyPy and PyPy3.

1.1 (2012-10-13)

  • Add name space declaration for z3c.

  • Added a test extra, as test require zope.testing.

1.0 (2008-04-15)

  • Initial public 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

z3c.objpath-1.3.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

z3c.objpath-1.3-py2.py3-none-any.whl (7.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file z3c.objpath-1.3.tar.gz.

File metadata

  • Download URL: z3c.objpath-1.3.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.15

File hashes

Hashes for z3c.objpath-1.3.tar.gz
Algorithm Hash digest
SHA256 a7c0d17c077ba7feb30fc8157e7ec8b25fd1892d15fb989480e17bf1f4529a12
MD5 6e15cd34669ebb6cec9fe11bd4387737
BLAKE2b-256 c9e25f28b64c2330284da8a9d24a710e42958d4b96bf797e4f2b090d1056bfa1

See more details on using hashes here.

File details

Details for the file z3c.objpath-1.3-py2.py3-none-any.whl.

File metadata

  • Download URL: z3c.objpath-1.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.15

File hashes

Hashes for z3c.objpath-1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d522a65598fcd9ea891ff900c08093fdcfebee7b42b6f136c9ff36bcee5912f1
MD5 c930e95e44d4632b25efa7b5b4cc81cb
BLAKE2b-256 5842c0038aa5334b071ea2d37b8b5b107d19be96d8f7d8536eb8bc74ed15e1a4

See more details on using hashes here.

Supported by

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