Skip to main content

Tiny util for creating tensorflow-like context managers for default instances of classes

Project description

https://badge.fury.io/py/defaultcontext.svg https://travis-ci.org/bogdan-kulynych/defaultcontext.svg?branch=master

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

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

defaultcontext-1.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

defaultcontext-1.1.0-py2.py3-none-any.whl (10.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file defaultcontext-1.1.0.tar.gz.

File metadata

File hashes

Hashes for defaultcontext-1.1.0.tar.gz
Algorithm Hash digest
SHA256 7d5ac1317f8d2ada91a756d39fb019d84b3cd942e836c34b63e27b1732808817
MD5 7b29f9065747103c66000cc1f6dab117
BLAKE2b-256 a7f853657dce455c00f352836b529e3f774d3fa3d20be5ce91808a4cadc510e2

See more details on using hashes here.

File details

Details for the file defaultcontext-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for defaultcontext-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d7c34186b5dc22548aca6ea3dd5f2abd155e962854af4f4749838131fabbcc21
MD5 8a9fcbf5ba9851a47565d0531989282b
BLAKE2b-256 4d4dd50d6e43f8125bee0a6bf2265e381cb8856f04794c4a1a961bb385c96b9a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page