Create pseudo-singleton key based objects like those generated by logging.getLogger
Project description
keyed-classes
Create pseudo-singleton key based objects like those generated by logging.getLogger
>>> from keyed_classes import KeyedClass
>>> class MyClass(KeyedClass)
... def __init__(self, key):
... self.key = key
...
>>> instance_one = MyClass('this is a key')
>>> instance_two = MyClass('this is a key')
>>> instance_three = MyClass('this is a different key')
>>> instance_one is instance_two
True
>>> instance_one is instance_three
False
Note: Some thought needs to be put into the __init__
method of a KeyedClass
. If attempting to create a new instance may or may not just return a previously created instance instead, what does that mean for code in __init__
, and instance variables (and whatever else) created there-in?
This is getting a bit implementation detail-y, but in every time you try to create a new instance the code in __init__
will be run, self
either referring to the newly created instance if the key did not previously exist, and referring to the original instance created from the key if it has been used before.
If you'd like to avoid subclassing KeyedClass
but still want something like this for whatever reason, there is also a metaclass version keyed_classes.KeyedClassMeta
, but I would recommend against using it, as it's not quite as friendly as keyed_classes.KeyedClass
.
>>> from keyed_classes import KeyedClassMeta
>>> class MyClass(metaclass=KeyedClassMeta)
... def __init__(self, key):
... self.key = key
...
>>> instance_one = MyClass('this is a key')
>>> instance_two = MyClass('this is a key')
>>> instance_three = MyClass('this is a different key')
>>> instance_one is instance_two
True
>>> instance_one is instance_three
False
Note: The behavior is not exactly the same between the two implementations. keyed_classes.KeyedClassMeta
classes actually do create a new instance every time one is requested (and the __init__
method is run on this new instance), but this object is essentially just thrown away before it gets back to the user and the originally created object is returned back to the user.
Why do I want this?
You probably don't!
Generally this is a bit of a cludgy solution, it is effectivly just cleverly using (and hiding from the user) global state, which we all know is something approximating the root of all evil.
I came up with this as a solution to the issue of mutually exclusive groups of options in Click
, which does not support that feature itself.
import click
from keyed_classes import KeyedClass
class MutuallyExclusiveOptionGroup(KeyedClass):
def __init__(self, key):
self.key = key
self.tripped = False
def __call__(self, ctx, param, value):
if value is None:
return
if self.tripped:
op, _ = self.option
raise click.BadParameter(f"'{op.name}' already specified, only one '{self.key}' option allowed in one invocation.")
else:
self.tripped = True
self.option = (param, value)
return value
@click.command()
@click.option("--op1", callback=MutuallyExclusiveOptionGroup('method'))
@click.option("--op2", callback=MutuallyExclusiveOptionGroup('method'))
...
def main(op1, op2, ...):
...
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 keyed-classes-0.1.0.tar.gz
.
File metadata
- Download URL: keyed-classes-0.1.0.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.4 CPython/3.7.1 Linux/4.14.78-1-MANJARO
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 777e309bc74c2b82bf532bb6713d73874f896ecfa84181f5aec925b688326efc |
|
MD5 | bbc917c4fbc86063513c51ae5ea6475a |
|
BLAKE2b-256 | 2960b02f0b21cfd2dfb13299c1f86f893e97ed04237793765487ac0af967544f |
File details
Details for the file keyed_classes-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: keyed_classes-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 2.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.4 CPython/3.7.1 Linux/4.14.78-1-MANJARO
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25fe8944f7b2a168ed53617491db4dadef97634a71b7b463c62dda344a573b3a |
|
MD5 | cce918a2c726bb77c30285868c74bf2c |
|
BLAKE2b-256 | 7b2c50ce24714067c7153cfd7a50f06ea195dc2bd850f9c1d1acc773decc4080 |