A package for managing JSON-based global variables by file context.
Project description
Global Variable Management Script
This Python script provides functionality to manage global variables stored in a JSON file. The script allows you to set, get, remove, and view global variables associated with a specific file path. The global variables are stored in a file named glovar.json.
Overview
The script includes functions to:
- Set and get global variables.
- Configure the path for the JSON file and the variables path.
- Read from and write to a JSON file (
glovar.json) that stores global variables.
Installation
pip install glovar
Variables
var_path
Path to the file or string name to which global variables are bound. Default is
Change it using var_file(file_path) function to change path.
data_path
Path to directory where json file will be create and store. Default is package directory.
Change it using data(directory) function to change directory.
Functions
var_file(file_path=os.getcwd())
Set the path for the current user context.
Args:
file_path(str): The path to the file. Defaults to the directory of the caller frame.
data(directory=os.getcwd())
Set the path for the glovar.json file.
Args:
directory(str): The directory where theglovar.jsonfile is located. Defaults to the directory of the caller frame.
read_globals()
Read and return the contents of the glovar.json file as a dictionary.
Returns:
dict: The contents ofglovar.json, or an empty dictionary if the file does not exist.
set(name, value=None)
Set a global variable in the glovar.json file for the current var_file.
Args:
name(str): The name of the global variable.value: The value to set for the global variable. Defaults toNone.
get(name)
Retrieve a global variable from the glovar.json file for the current var_file.
Args:
name(str): The name of the global variable to retrieve.
Returns:
- The value of the global variable, or
Noneif the variable does not exist.
remove(name)
Remove a global variable from the glovar.json file for the current var_file.
Args:
name(str): The name of the global variable to remove.
Returns:
dict: The updated dictionary of global variables for the current var_file.
peek()
Peek at the current global variables for the current var_file.
Returns:
dict: The dictionary of global variables for the current var_file.
Usage
-
Link variables to file:
glovar.var_file("path/to/your/file")
do not execute this function to link to an executable file.
-
Set the JSON file path:
glovar.data("path/to/your/directory")
do not execute this function to link to an executable file's directory.
-
Set a global variable:
glovar.set("variable_name", "value")
-
Get a global variable:
value = glovar.get("variable_name")
-
Remove a global variable:
globals = glovar.remove("variable_name")
-
Peek at current global variables:
current_globals = glovar.peek()
Examples
Example 1: Setting and Getting a Global Variable without change file and data
Example1.py
import glovar
glovar.set("my_variable", "my_value")
value = glovar.get("my_variable")
print("Value:", value) # Output: Value: my_value
This script create glovar.json in package directory
Example 2: link file variables to other file
Example2.py
import glovar
glovar.var_file("full/path/to/Example1.py")
value = glovar.get("my_variable")
print("Value:", value) # Output: Value: my_value
Due to link Example1.py file to Example2.py using glovar.file("full/path/to/Example1.py") we can see my_variable from Example1.py
Example 3: link file variables to directory
You can link variables to directory instead of files
Example1.py
import glovar
glovar.var_file() # leave empty to link to file directory
glovar.set("my_variable", "my_value")
value = glovar.get("my_variable")
print("Value:", value) # Output: Value: my_value
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file glovar-0.2.2.tar.gz.
File metadata
- Download URL: glovar-0.2.2.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3be321f0d2f1b43c1441af5905b3f34b6b09fa1311389fe5673b9bba5bad947f
|
|
| MD5 |
6bb964a08f3317c12d0175e6d7903288
|
|
| BLAKE2b-256 |
a49f1d41e3df429b34c72d3a9aa24b0105a060a39dd429074e18b69bb42fb2e4
|
File details
Details for the file glovar-0.2.2-py3-none-any.whl.
File metadata
- Download URL: glovar-0.2.2-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f20135cd5c5202cad97fea2cfd97ccc24a7f2fc056835c9436ee470936b98f2
|
|
| MD5 |
7258b5ae19491707b47f377157f8499d
|
|
| BLAKE2b-256 |
1640d8e0c7ed27bae4bec991e52a22febc47248c5fa4bbc49cfbce7cccd61387
|