Registry / serialization / zope-size

zope-size

JSON →
library6.0pypypi✓ verified 84d ago

Zope.size provides interfaces and a simple adapter pattern to determine the 'size' of an object in a generic way, typically for display or aggregation purposes. It is part of the Zope Foundation ecosystem and is currently at version 6.0, receiving updates as part of the broader Zope component set to maintain Python compatibility and address minor issues.

pip install zope.size
INSTALL
IMPORT
SIG · ZOPE-SIZE
Z
zope-size
serializationpythonv6.0
Install
2.2s avg
Import
29ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.031s · 19.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.2s · import 0.027s · 21MB
22MB installed
● package 22MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ISized
from zope.size.interfaces import ISized
get_size
from zope.size import get_size

This quickstart demonstrates how to define an object, create an adapter that implements `zope.size.interfaces.ISized` for that object's interface, register the adapter using `zope.component.provideAdapter`, and then retrieve the object's size using the `zope.size.get_size` utility function. It highlights the crucial step of adapter registration.

import zope.interface import zope.component from zope.size.interfaces import ISized from zope.size import get_size # 1. Define an interface for our object class IMyObject(zope.interface.Interface): """An object with some data.""" # 2. Define a class implementing the interface @zope.interface.implementer(IMyObject) class MyObject: def __init__(self, items): self.items = items # 3. Define an adapter that provides ISized for IMyObject @zope.component.adapter(IMyObject) @zope.interface.implementer(ISized) class MyObjectSizer: def __init__(self, context): self.context = context @property def size(self): return len(self.context.items) # 4. Create an instance of our object my_list_obj = MyObject([1, 2, 3, 4, 5]) my_dict_obj = MyObject({'a': 1, 'b': 2}) # 5. Register the adapter (typically done at application startup) zope.component.provideAdapter(MyObjectSizer) # 6. Use get_size to retrieve the size list_size = get_size(my_list_obj) dict_size = get_size(my_dict_obj) # Print the results print(f"Size of list object: {list_size}") print(f"Size of dictionary object: {dict_size}") # Example of an object without a registered adapter class UnsizedObject: pass unsized_obj = UnsizedObject() try: get_size(unsized_obj) except zope.component.interfaces.ComponentLookupError as e: print(f"\nError for unsized object (expected): {e}")
Debug
Known issues
breakingVersion 6.0 dropped support for Python 3.8. Ensure your project is running on Python 3.9 or newer.
fix
Upgrade your Python environment to 3.9, 3.10, 3.11, or 3.12. If you must use Python 3.8, pin `zope.size<6.0`.
affects: 6.0 and later
gotchaZope.size relies heavily on `zope.component` for adapter lookup. If no adapter is registered for a given object's type/interface to provide `ISized`, `get_size` will raise a `ComponentLookupError`.
fix
Ensure that you have called `zope.component.provideAdapter(YourSizerClass)` for every type you expect to get the size of. This is typically done once at application startup.
affects: All versions
gotchaThe `ISized` interface requires a `size` property (not a method). Implementing it as a method instead of a property will lead to `AttributeError` when `get_size` tries to access it, or incorrect behavior.
fix
In your adapter implementing `ISized`, ensure `size` is decorated with `@property`:
```python
@property
def size(self):
    return ...
```
affects: All versions
Errors
Common errors & fixes
zope.component.interfaces.ComponentLookupError: No registered adapter for (...) for zope.size.interfaces.ISized
The `get_size` function could not find an adapter that implements `ISized` for the type of object you passed.
fix
You must register an adapter using `zope.component.provideAdapter(YourSizerClass)` before calling `get_size` for objects of that type. Ensure your adapter correctly implements `ISized` and is registered for the correct interface/type.
AttributeError: 'YourSizerClass' object has no attribute 'size'
Your adapter class implementing `ISized` defines `size` as a regular method or is missing it entirely, instead of a property.
fix
Ensure the `size` attribute in your adapter is defined as a property using the `@property` decorator:
```python
@zope.interface.implementer(ISized)
class YourSizer:
    def __init__(self, context):
        self.context = context

    @property
    def size(self):
        return len(self.context.data)
```
TypeError: object of type 'YourObject' has no len()
You are likely attempting to use `len()` directly on an object for which `zope.size` is intended, but `len()` is not defined for that object. You should be using `get_size()` with an appropriate adapter.
fix
Instead of `len(my_object)`, ensure you have an `ISized` adapter registered for `my_object`'s type/interface, and then use `get_size(my_object)`.
Upgrade
Version history
6.0latest on PyPI · released Sep 12, 2025
Audit
Dependencies
zope.interfacerequiredCore dependency for defining and implementing interfaces used by zope.size.
zope.componentrequiredRequired for registering and looking up adapters that provide size information.
Agent activity
42 hits · last 30 days
node
37
OpenAI (training)
1
Resources
zope-size — pip install zope-size · libregistry