Skip to main content

Share global variables across modules

Project description

intermodule

This library makes it possible to share global variables across modules.

Installation

pip install intermodule

Api Reference

class intermodule.SharedGlobal

Instances of this class manage a global variable and keep it synchronized across modules. You should never instantiate this class directly. Instead, use set_global and get_global functions. This class inherits a variant of ObjectProxy from wrapt. Along with all augmented assignment operators, this class also overloads the normal assignment operator (=) thanks to assign-overload. Underlying global variable can be any object and the resulting instance of this class will act like that object in every way. If you use this class, you need to call intermodule.patch_and_reload_module(). You can also access this function from assign_overload module. You can find documentation about this function in assign-overload.

SharedGlobal.set_global(name, value)

Creates a new SharedGlobal instance with the given name and value but doesn't return it. Instead, assigns it to the calling module. Lastly, assigns it to every module which has a global variable whose name is name and whose value is a SharedGlobal instance.

SharedGlobal.get_global(name)

Search through every module for a already defined global variable named name. If found, check if it is a SharedGlobal instance. If it is, assign its value to the calling module. The value won't be returned. If no module defines a variable named name or if its value is not a SharedGlobal instance, raise an error.

Example

# module1.py
import intermodule

intermodule.SharedGlobal.set_global("x", 10)

def main():
    global x
    import module2
    module2.func1()
    print(x) # prints 20
    x = 30
    module2.func2()

if intermodule.patch_and_reload_module():
    main()
# module2.py
import intermodule

intermodule.SharedGlobal.get_global("x")

def func1():
    global x
    x = 20

def func2():
    print(x) # prints 30

if intermodule.patch_and_reload_module():
    pass

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

intermodule-1.0.0.post1.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

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

intermodule-1.0.0.post1-py2.py3-none-any.whl (3.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file intermodule-1.0.0.post1.tar.gz.

File metadata

  • Download URL: intermodule-1.0.0.post1.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for intermodule-1.0.0.post1.tar.gz
Algorithm Hash digest
SHA256 37d27fad6b059677cc00e452009f453d8f02dda466e146566e858d4749ee5ffe
MD5 dd535971cb7ea7839d2997a220f503eb
BLAKE2b-256 6a4b2cdbfcff3e0760adc2dfc64ebde8329fa8dc2bf4821178e5cd58a961c274

See more details on using hashes here.

File details

Details for the file intermodule-1.0.0.post1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for intermodule-1.0.0.post1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b1beadeb2f5d57f522683020f1badc9e4d85ed1f5e7973fda6096bbce3efda4f
MD5 ddceca83f1b71e80944c4618790f212f
BLAKE2b-256 4ad7e22a53857cdab4621420545d03185a59ff1dc01aabeecd88d030a844e924

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