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.
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.
pip install glovar
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.
Path to directory where json file will be create and store. Default is package directory.
Change it using data(directory) function to change directory.
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.
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 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 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.
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 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 at the current global variables for the current var_file.
Returns:
dict: The dictionary of global variables for the current var_file.
-
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()
Example1.py
import glovar
glovar.set("my_variable", "my_value")
value = glovar.get("my_variable")
print("Value:", value) # Output: Value: my_valueThis script create glovar.json in package directory
Example2.py
import glovar
glovar.var_file("full/path/to/Example1.py")
value = glovar.get("my_variable")
print("Value:", value) # Output: Value: my_valueDue to link Example1.py file to Example2.py using glovar.file("full/path/to/Example1.py") we can see my_variable from Example1.py
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