Skip to main content

Python auto-binding dict

Project description

bdict

A library allowing you to create an auto method-binding dict.

Ever dreamed of writing clean dicts pointing keys to methods? Fear no more!

Mainly used for event handlers, a binding dict consists of a mapping between any events or keys, to their appropriate handler functions within a class. Upon key lookup, the dict will bind the appropriate function to the instance of the class.

For an example:

class Server:
    """Typical server with a small mapping between event handlers and functions"""
    def __init__(self, name):
        self.name = name

    def on_connect(self, remote_host):
        print(self.name, remote_host)

    def on_connect(self, remote_host):
        print(self.name, remote_host)

    handlers = BDict({NewConnectionEvent: on_connect,
                      DisconnectonEvent: on_disconnect})

>>> s = Server("myserver")
>>> s.handlers[NewConnectionEvent]("1.2.3.4")
myserver 1.2.3.4

As you can see, after accessing the handlers dict, and upon key lookup, the dict bound the handler functions to the instance.

BDict also works with classmethods in a clean and fashioned way:

class MyClass:
    """Typical server with a small mapping between event handlers and functions"""            
    @classmethod
    def class_handle(cls):
        print(cls.__name__)

    handlers = BDict({"class_handle": class_handle})

>>> MyClass.handlers["class_handle"]
<bound method MyClass.class_handle of <class '__main__.MyClass'>>
>>> MyClass.handlers["class_handle"]()
MyClass

>>> inst = MyClass()
>>> inst.handlers["class_handle"]
<bound method MyClass.class_handle of <class '__main__.MyClass'>>
>>> inst.handlers["class_handle"]()
MyClass

Upon accessing the BDict through an instance, the BDict will create an internal instance data container on the BDict, allowing you to modify it's dictionary and not affect other instances!

>>> inst.handlers[123] = 456
>>> inst.handlers[123]
456
>>> inst2 = MyClass()
>>> inst2.handlers[123]
Traceback (most recent call last):
  ...
KeyError: 123

Usage:

BDict(dict_)

dict_ can be a dict or an iterable of (key, value) pairs and will be used to initialize BDict.

The class BDict is used on must support weak referencing (the vast majority of custom objects do). If you use __slots__, you are able to do this by adding __weakref__ to the slots.

BDict.autobind(key, value)

Allows adding an autobinding entry in the dict (regular additions will not autobind). key is the key for access while value is the function that will be autobound.

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

bdict-0.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

bdict-0.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file bdict-0.1.0.tar.gz.

File metadata

  • Download URL: bdict-0.1.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for bdict-0.1.0.tar.gz
Algorithm Hash digest
SHA256 720fd713d34d057813eddf1834255459248d90f15dd5b64bd97db449206c2821
MD5 05f7ab18af4cd50661a8de0abb6bd0af
BLAKE2b-256 9c3bae00dcaad306488276bae8240ce84432a34acbe9ee14bda451d0fb19f88d

See more details on using hashes here.

File details

Details for the file bdict-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bdict-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7997d1b9779b46750e64ce32e5bd597aa1ec5e0fc817be909a3b98da1f3659ed
MD5 f9b74120e9d725d69b513186c3733a14
BLAKE2b-256 68e6125bfae2e93b128265070e7363b02b55b9c053554125429d11649197baaf

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