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.
Release files for bdict 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| bdict-0.1.0.tar.gz | 3.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bdict-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.3 kB
Release files / bdict-0.1.0.tar.gz
| Download URL | bdict-0.1.0.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
720fd713d34d057813eddf1834255459248d90f15dd5b64bd97db449206c2821
|
|
BLAKE2b-256 checksum How to use checksums |
9c3bae00dcaad306488276bae8240ce84432a34acbe9ee14bda451d0fb19f88d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / bdict-0.1.0-py3-none-any.whl
| Download URL | bdict-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7997d1b9779b46750e64ce32e5bd597aa1ec5e0fc817be909a3b98da1f3659ed
|
|
BLAKE2b-256 checksum How to use checksums |
68e6125bfae2e93b128265070e7363b02b55b9c053554125429d11649197baaf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |