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 be autocached 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_, *, strong=None, autocache=True)
dict_
can be a dict or an iterable of (key, value) pairs and will be used to initialize BDict
.
strong
defaults to None and configures whether BDict stores a strong reference to the instance. False
will cause
bdict to use only weak references but not work when the original instance dissapears. True
forces a strong reference
but might cause a cyclic reference if autocache
is used. By default (recommended), BDict will attempt to use a weak reference
and if it fails to bind the cache (due to the instance having __slots__
or autocache=False
) it will use a strong reference.
autocache
configures whether to cache the BDict on the instance. Caching results in faster access but does not work if __slots__
are defined, and will prevent changes made on the class'es BDict from appearing.
If an object is defined using __slots__
, I highly suggest adding __weakref__
to the slots.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bdict-0.1.0b3.tar.gz
.
File metadata
- Download URL: bdict-0.1.0b3.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 796a08666943153503ba9968f0e2d4613265bb68cc55f4fa5e7dc4b95896d3d6 |
|
MD5 | 20e22373d7e3f255cc1fe006ea2969f3 |
|
BLAKE2b-256 | 13b2944edb5fadd191b500e67cda1b4f584a8a82491de5a2c938c135d92ff64e |
File details
Details for the file bdict-0.1.0b3-py3-none-any.whl
.
File metadata
- Download URL: bdict-0.1.0b3-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 542c31022f3abd4509a3fe0b47973d8245505666a4e1302e2abc92336d269da4 |
|
MD5 | df76f72fadddca90cba6a60e88c703bd |
|
BLAKE2b-256 | f5e7d4c98cc1d1d232a07fb3505e94d922ab0d48b4b02676139de5a4eab74440 |