Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

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.

Files for bdict, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size bdict-0.1.0-py3-none-any.whl (4.4 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size bdict-0.1.0.tar.gz (3.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page