Skip to main content

Global accessors for things like settings. This could be used across applications and modules to share data.

Project description

Global accessors for things like settings. This could be used across applications and modules to share data.

This utility is to help easily share values across multiple modules and libraries without having to pass values around.

Example

Simple example.

# settings.py
import pyglobal

pyglobal.set('abc', 123)
pyglobal.default('qwerty', '')
pyglobal.set('Hello', 'World!')

pyglobal.default('SECRET_KEY', '!!!CHANGE!!!')  # Warning: Any library using pyglobal could then look for this value.

pyglobal.set('DATABASE_URL', 'http://')  # Warning: Any library using pyglobal could then look for this value.

Then use the settings in another part of the project

# main.py
import pyglobal
import settings  # Set pyglobal values before using in other modules

import db

app = App(SECRET_KEY=pyglobal.get('SECRET_KEY'))

The sub module db.py doesn’t have to know anything about settings.py and can be nested down packages.

# db.py
import pyglobal

db = Database(pyglobal.get('DATABASE_URL'))

Avoiding clashes with other projects

Avoid clashes and add some small amount of security to your app with a scope

# settings.py
import pyglobal
import uuid

app_scope = 'MYAPP=' + str(uuid.uuid4())

def scope_get(key, default=None):
    return pyglobal.get(key, default, app_scope)

def scope_set(key, value)
    return pyglobal.set(key, value, app_scope)

pyglobal.scope_get = scope_get
pyglobal.scope_set = scope_set

pyglobal.set('SECRET_KEY', '!! Change !!', scope=app_scope)
scope_set('DATABASE_URL', 'http://')

Once again you can use in other files, but you would also need the scope to access.

# main.py
import pyglobal
import settings

secret = pyglobal.scope_get('SECRET_KEY')
db_url = pyglobal.get('DATABASE_URL', scope=settings.app_scope)

At some point code can always be disassembled, so this isn’t really secure. All scopes are is another level of obfuscation and a way to avoid global names from clashing with other projects.

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

pyglobal-0.0.1.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distributions

pyglobal-0.0.1-cp38-cp38-win_amd64.whl (20.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

pyglobal-0.0.1-cp38-cp38-win32.whl (26.1 kB view hashes)

Uploaded CPython 3.8 Windows x86

pyglobal-0.0.1-cp37-cp37m-win_amd64.whl (32.0 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

pyglobal-0.0.1-cp37-cp37m-win32.whl (37.3 kB view hashes)

Uploaded CPython 3.7m Windows x86

pyglobal-0.0.1-cp36-cp36m-win_amd64.whl (43.3 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

pyglobal-0.0.1-cp36-cp36m-win32.whl (48.6 kB view hashes)

Uploaded CPython 3.6m Windows x86

pyglobal-0.0.1-cp34-cp34m-win_amd64.whl (52.6 kB view hashes)

Uploaded CPython 3.4m Windows x86-64

pyglobal-0.0.1-cp34-cp34m-win32.whl (52.6 kB view hashes)

Uploaded CPython 3.4m Windows x86

Supported by

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