Easy plugin for open-close class objects
Project description
open-close-mixin
About
Easy pluggable Mixin (and decorators) for objects that may benefit from open-close dynamics.
Installing
You can simply install it using pip as follows:
$ pip install open-close-mixin
Usage
You may import an OpenCloseMixin
along some other decorators from the package root and use them further on to create your own class with open and close dynamics!
>>> from open_close_mixin import OpenCloseMixin, always
>>> class Foo(OpenCloseMixin):
>>> # only while open, that's default behaviour
>>> def run_when_open(self):
>>> print("The instance is open so I was able to run")
>>>
>>> @always
>>> def run_always(self):
>>> print("I am ALWAYS able to run, whether the instance is open or not")
>>>
>>> f = Foo()
>>> f.open()
>>> f.run_always()
I am ALWAYS able to run, whether the instance is open or not
>>>
>>> f.run_when_open()
The instance is open so I was able to run
>>>
>>> f.close()
>>> f.run_always()
I am ALWAYS able to run, whether the instance is open or not
>>>
>>> f.run_when_open()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/alberto/Projects/personal/open-close-mixin/open_close_mixin/decorators.py", line 27, in wrapper
raise exception from None
ValueError: The instance is not open and the method "Foo.run_when_open" cannot run under such condition.
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
open-close-mixin-1.1.2.tar.gz
(4.1 kB
view hashes)
Built Distribution
Close
Hashes for open_close_mixin-1.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9ccd199918251a458ceb1bb52f21e1f717fa02eae1e5a7981ed4fa31d222db8 |
|
MD5 | 3d3e82574c3253a4c0c9ce43c9653eb6 |
|
BLAKE2b-256 | 9ad5c2575636707e6bedb36fc04a756f4e7fcf21e35a5cb17872638464ae6bb7 |