Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (testpypi.python.org).
Help us improve Python packaging - Donate today!

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

Project Description

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

Installation

You can install defaultcontext using pip:

pip install defaultcontext

Usage

Basic:

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
Release History

Release History

This version
History Node

1.0.1

History Node

1.0.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
defaultcontext-1.0.1.tar.gz (7.6 kB) Copy SHA256 Checksum SHA256 Source May 26, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting