Skip to content

zzlib.cls module

zzlib.cls

zzlib.cls.CachedDelayed

Bases: Delayed[T]

Used to store a function and all its arguments for later calls, with result cache.

zzlib.cls.CachedDelayedTuple

Used to represent each results from a function call tuple result.

zzlib.cls.DecoMeta

Bases: type

A metaclass that automatically decorates all methods with a specified decorator.

If a class defines __deco__ as a function, this metaclass will apply that decorator to all method functions defined in the class.

The decorator is applied during class creation and affects only methods defined directly in the class, not inherited methods.

Example
class MyClass(metaclass=DecoMeta):
    __deco__ = some_decorator  # Decorator to apply to all methods

    def method1(self):  # Will be decorated with some_decorator
        pass

    def method2(self):  # Will also be decorated
        pass

zzlib.cls.Def = object.__new__(_DefaultType) module-attribute

A singleton object representing a default value, distinct from None.

zzlib.cls.Delayed

Bases: Generic[T]

Used to store a function and all its arguments for later calls.

Example
# Store a function call for later execution
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

d = Delayed(greet)("Alice", greeting="Hi")
# Function not executed yet

result = d.trigger()  # Now executes the function
assert result == "Hi, Alice!"

trigger(**kw)

Execute the stored function with stored arguments and additional keyword arguments.

Parameters:

Name Type Description Default
**kw

Additional keyword arguments that will be merged with stored ones.

{}

Returns:

Type Description
T

The result of calling the stored function.

zzlib.cls.FactoryProxy

Bases: Delayed, ProxyBase

Stores a function representation. When the value is retrieved, the function will be executed and its return value will be returned.

Example
l = [1]
listlen = FactoryProxy(lambda: len(l))
assert listlen == 1
l.append(1)
assert listlen == 2

zzlib.cls.Lazy

Bases: FactoryProxy

A class that delays function execution and caches the result.

Similar to FactoryProxy but caches the result after first execution. Subsequent accesses will return the cached value instead of re-executing the function. The cached value can be reset using reset().

Example
def expensive_func(i):
    # Some expensive computation
    return i ** 2

lazy_val = Lazy(expensive_func)(i)
result1 = lazy_val  # Function executes and caches result
result2 = lazy_val  # Returns cached result without re-executing

is_failed property

Returns True if lazy function execution failed.

is_loaded property

Returns True if lazy function is executed and result is cached.

load(force=False)

Execute lazy function and cache result.

Parameters:

Name Type Description Default
force bool

If True, re-execute even if already cached.

False

zzlib.cls.LazyLoad

Bases: FactoryProxy

A class that delays function execution until load() is called.

This class allows deferring execution of functions until explicitly loaded. The function result is cached after first load.

Example
def expensive_func(i):
    # Some expensive computation
    return i ** 2

lazy_obj = LazyLoad(expensive_func)(i)  # No computation yet
result = lazy_obj.load()                # Triggers computation

is_loaded property

Check if the lazy object has been loaded.

load(force=False)

Load the lazy object by triggering computation.

Parameters:

Name Type Description Default
force bool

If True, force recomputation even if already loaded. Default is False.

False

zzlib.cls.LazyTuple

A class that lazily executes functions returning tuples. Each tuple element is lazily evaluated separately.

This allows deferring execution of functions that return multiple values, with function being executed when any output is accessed.

Example
def expensive_func(i):
    # Some expensive computation
    return i ** 2

lazy_1, lazy_2 = LazyTuple(2)(expensive_func)(i)
first = lazy_1 + 1  # Triggers expensive_func
second = lazy_2 + 1 # Triggers expensive_func

zzlib.cls.Patcher

A class that allows upgrading existing objects by adding methods and properties while preserving the original C fingerprint. This enables compatibility in scenarios where the exact type matters.

The upgrade process executes the __upgrade__() method which can initialize new attributes similar to __init__(). The original object can be converted to the upgraded class type.

Note: Patched objects are not pickle-compatible and must be re-patched after unpickling, including in multiprocessing scenarios.

Example
class MyList(List, _c.Patcher):
    def __upgrade__(self):
        self.val = 1

    def method(self):
        print(self.val)

a = []
a = MyList(a)  # Upgrade list object
a.method()     # Access new method

zzlib.cls.Proxy

Bases: ProxyBase

A variable container for passing references.

Example
# Example 1
a = Proxy(1)
assert a == 1

# Example 2
plus_1 = lambda x: x = x + 1
a = Proxy(1)
plus_1(a)
assert a == 2

zzlib.cls.ProxyBase

A proxy class that delegates attribute access to its __subject__ unless explicitly overridden.

Attributes listed in __noproxy__ will not be proxied to __subject__. By default, functions and properties are not proxied. All attributes must be listed in __noproxy__.

The proxy behaves identically to direct access of the underlying __subject__ for all non-overridden attributes.

Example
class Config(ProxyBase):
    __noproxy__ = ("conf_file",)

    def __init__(self, conf_file=None):
        self.conf_file = conf_file

    @property
    def __subject__(self):
        return self.conf_file.read()

zzlib.cls.Singleton

Bases: type

A metaclass to create a singleton.