defaultcontext
Tiny util for creating `tensorflow`_-like context managers for default instances of classes.
Installation
You can install defaultcontext using pip:
pip install defaultcontext
Usage
The library provides the with_default_context class decorator which simply does two things:
Adds static method Class.get_default() which returns the default object in the current context block.
Adds method instance.as_default() to the class, which manages a context within which the instance becomes default
This is useful for creating psuedo-global objects that can be accessed from any code executed within a given context block without passing such objects around.
This idea is inspired by Graph and Session classes from Google’s tensorflow.
Basic usage:
from defaultcontext import with_default_context
@with_default_context
class Environment:
def __init__(self, name):
self.name = name
def __str__(self):
return 'Environment %s' % self.name
with Environment(name='A').as_default():
print(Environment.get_default()) # A
with Environment(name='B').as_default():
print(Environment.get_default()) # B
print(Environment.get_default()) # None
If with_default_context was called without parameters the global default value of a class will be None. The global default can be added using global_default_factory:
def make_default_env():
return Environment(name='default')
@with_default_context(global_default_factory=make_default_env)
class Environment:
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
Environment.get_default() # default
with Environment(name='custom').as_default():
print(Environment.get_default()) # custom
Environment.get_default() # default
Alternatively, if the class can be constructed without arguments, global default can be set to Class() by setting use_empty_init to True:
@with_default_context(use_empty_init=True)
class Environment:
def __init__(self, name='default'):
self.name = name
def __str__(self):
return self.name
Environment.get_default() # default
with Environment(name='custom').as_default():
print(Environment.get_default()) # custom
Environment.get_default() # default
Metadata
Release files for defaultcontext 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| defaultcontext-1.1.1.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| defaultcontext-1.1.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 18.5 kB
Release files / defaultcontext-1.1.1.tar.gz
| Download URL | defaultcontext-1.1.1.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d8b0eee5ff4d0065fea4f51a7ca0dcf2711d2e71ff5f83d29d8bf8804a33c3eb
|
|
BLAKE2b-256 checksum How to use checksums |
f4273703ce9e86cc7d959fbc152c55ad95a0fe48fddbff49405a2b7d225859f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / defaultcontext-1.1.1-py2.py3-none-any.whl
| Download URL | defaultcontext-1.1.1-py2.py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
931762c2a5c3333adb20d18cca60a646020a4b40dace1d04009f01c26a29e18e
|
|
BLAKE2b-256 checksum How to use checksums |
4b14dba64ac445f3de0a62a7cc515302085bcbea7b182944582648c2feafbb0a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |