Skip to main content

objwrap

Easy object wrapping in Python.

This package enables creation of transparent objects that wrap others already in memory. Basically, it offers a more sophisticated view over getattr that hand-rewires object methods, including magic methods (__add__, __len__ etc).

Quickstart

First install the latest version of the package with pip install --upgrade objwrap.

Custom classes to wrap attribute getters and magic methods.
from objwrap import Wrapper

class Notify(Wrapper):
    def __wrapattr__(self, obj, name):
        print(f"Accessing {name}")
        return super().__wrapattr__(obj, name)

x = Notify(1)
print(x+1)
# Accessing __add__
# 2
Wrappers to transform inputs to method calls.
from objwrap import ClosedWrapper, wrapped


class Notify(ClosedWrapper):
    def __after__(self, obj):
        return Notify(obj)  # this is the default behavior
    
    def __before__(self, method, args, kwargs):
        method, args, kwargs = super().__before__(method, args, kwargs)
        print(f"Calling {method.__name__} on {wrapped(self)} with args {args} and kwargs {kwargs}")
        return method, args, kwargs


x = Notify(1)
y = Notify(2)
z = x + y  # a Notify object
print(wrapped(z))

# Calling __add__ on 2 with args [2] and kwargs {}
# 3
Wrappers around dicts that call methods for their values.
from objwrap import ClosedWrapper, wrapped
from objwrap.closure import Pending

class RunOnValues(ClosedWrapper):
    def __init__(self, obj):
        super().__init__(obj)
        self.symbols = obj.keys()
    
    def __unknown__(self, obj, name, args, kwargs):
        _, args, kwargs = super().__before__(None, args, kwargs)  # convert other instances of RunOnValues to dicts
        assert isinstance(obj, dict)  # enforce the type
        pending = dict()
        for symbol in self.symbols:
            pending[symbol] = Pending(method=getattr(obj[symbol], name),
                                      args=[arg[symbol] for arg in args],
                                      kwargs={k: arg[symbol] for k, arg in kwargs.items()})
        return pending

x = RunOnValues({"a": 1, "b": 2})
y = RunOnValues({"a": 10, "b": 20})
z = x + y
print(wrapped(z))

# {'a': 11, 'b': 22}

Metadata

Release files for objwrap 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for objwrap 0.0.2
File Interpreter ABI Platform Actions
objwrap-0.0.2-py3-none-any.whl Python 3 none any Details for objwrap-0.0.2-py3-none-any.whl

Release files / objwrap-0.0.2-py3-none-any.whl

Download URL objwrap-0.0.2-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dbd324b9d60650061de83c05a049fbbb291f7a447fb21ad1aebbaf4cd2c1cc7f
BLAKE2b-256 checksum
How to use checksums
95b367536831e7839f9bff9a02e8c0fc06a4c8252862e2657699b87d70edbca0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.4

Security for objwrap 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page