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.

Files for pyglobal, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size pyglobal-0.0.1-cp34-cp34m-win32.whl (52.6 kB) File type Wheel Python version cp34 Upload date Hashes View
Filename, size pyglobal-0.0.1-cp34-cp34m-win_amd64.whl (52.6 kB) File type Wheel Python version cp34 Upload date Hashes View
Filename, size pyglobal-0.0.1-cp36-cp36m-win32.whl (48.6 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size pyglobal-0.0.1-cp36-cp36m-win_amd64.whl (43.3 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size pyglobal-0.0.1-cp37-cp37m-win32.whl (37.3 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size pyglobal-0.0.1-cp37-cp37m-win_amd64.whl (32.0 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size pyglobal-0.0.1-cp38-cp38-win32.whl (26.1 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size pyglobal-0.0.1-cp38-cp38-win_amd64.whl (20.7 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size pyglobal-0.0.1.tar.gz (7.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page