# 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 the `glovar.json` file 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 of `glovar.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 to `None`.
### `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 `None` if 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
1. **Link variables to file:**
```python
glovar.var_file("path/to/your/file")
```
do not execute this function to link to an executable file.
2. **Set the JSON file path:**
```python
glovar.data("path/to/your/directory")
```
do not execute this function to link to an executable file's directory.
3. **Set a global variable:**
```python
glovar.set("variable_name", "value")
```
4. **Get a global variable:**
```python
value = glovar.get("variable_name")
```
5. **Remove a global variable:**
```python
globals = glovar.remove("variable_name")
```
6. **Peek at current global variables:**
```python
current_globals = glovar.peek()
```
## Examples
### Example 1: Setting and Getting a Global Variable without change file and data
Example1.py
```python
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
```python
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
```python
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
```
Raw data
{
"_id": null,
"home_page": "https://github.com/PaceGG/Python-Global-vars-lib",
"name": "glovar",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": null,
"author": "PaceGG",
"author_email": "yura3012004@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/a4/9f/1d41e3df429b34c72d3a9aa24b0105a060a39dd429074e18b69bb42fb2e4/glovar-0.2.2.tar.gz",
"platform": null,
"description": "# Global Variable Management Script\r\n\r\nThis 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`.\r\n\r\n## Overview\r\n\r\nThe script includes functions to:\r\n\r\n- Set and get global variables.\r\n- Configure the path for the JSON file and the variables path.\r\n- Read from and write to a JSON file (`glovar.json`) that stores global variables.\r\n\r\n## Installation\r\n\r\n`pip install glovar`\r\n\r\n## Variables\r\n\r\n### `var_path`\r\n\r\nPath to the file or string name to which global variables are bound. Default is\r\nChange it using `var_file(file_path)` function to change path.\r\n\r\n### `data_path`\r\n\r\nPath to directory where json file will be create and store. Default is package directory.\r\nChange it using `data(directory)` function to change directory.\r\n\r\n## Functions\r\n\r\n### `var_file(file_path=os.getcwd())`\r\n\r\nSet the path for the current user context.\r\n\r\n**Args:**\r\n\r\n- `file_path` (str): The path to the file. Defaults to the directory of the caller frame.\r\n\r\n### `data(directory=os.getcwd())`\r\n\r\nSet the path for the `glovar.json` file.\r\n\r\n**Args:**\r\n\r\n- `directory` (str): The directory where the `glovar.json` file is located. Defaults to the directory of the caller frame.\r\n\r\n### `read_globals()`\r\n\r\nRead and return the contents of the `glovar.json` file as a dictionary.\r\n\r\n**Returns:**\r\n\r\n- `dict`: The contents of `glovar.json`, or an empty dictionary if the file does not exist.\r\n\r\n### `set(name, value=None)`\r\n\r\nSet a global variable in the `glovar.json` file for the current var_file.\r\n\r\n**Args:**\r\n\r\n- `name` (str): The name of the global variable.\r\n- `value`: The value to set for the global variable. Defaults to `None`.\r\n\r\n### `get(name)`\r\n\r\nRetrieve a global variable from the `glovar.json` file for the current var_file.\r\n\r\n**Args:**\r\n\r\n- `name` (str): The name of the global variable to retrieve.\r\n\r\n**Returns:**\r\n\r\n- The value of the global variable, or `None` if the variable does not exist.\r\n\r\n### `remove(name)`\r\n\r\nRemove a global variable from the `glovar.json` file for the current var_file.\r\n\r\n**Args:**\r\n\r\n- `name` (str): The name of the global variable to remove.\r\n\r\n**Returns:**\r\n\r\n- `dict`: The updated dictionary of global variables for the current var_file.\r\n\r\n### `peek()`\r\n\r\nPeek at the current global variables for the current var_file.\r\n\r\n**Returns:**\r\n\r\n- `dict`: The dictionary of global variables for the current var_file.\r\n\r\n## Usage\r\n\r\n1. **Link variables to file:**\r\n\r\n ```python\r\n glovar.var_file(\"path/to/your/file\")\r\n ```\r\n\r\n do not execute this function to link to an executable file.\r\n\r\n2. **Set the JSON file path:**\r\n\r\n ```python\r\n glovar.data(\"path/to/your/directory\")\r\n ```\r\n\r\n do not execute this function to link to an executable file's directory.\r\n\r\n3. **Set a global variable:**\r\n\r\n ```python\r\n glovar.set(\"variable_name\", \"value\")\r\n ```\r\n\r\n4. **Get a global variable:**\r\n\r\n ```python\r\n value = glovar.get(\"variable_name\")\r\n ```\r\n\r\n5. **Remove a global variable:**\r\n\r\n ```python\r\n globals = glovar.remove(\"variable_name\")\r\n ```\r\n\r\n6. **Peek at current global variables:**\r\n ```python\r\n current_globals = glovar.peek()\r\n ```\r\n\r\n## Examples\r\n\r\n### Example 1: Setting and Getting a Global Variable without change file and data\r\n\r\nExample1.py\r\n\r\n```python\r\nimport glovar\r\n\r\nglovar.set(\"my_variable\", \"my_value\")\r\n\r\nvalue = glovar.get(\"my_variable\")\r\nprint(\"Value:\", value) # Output: Value: my_value\r\n```\r\n\r\nThis script create glovar.json in package directory\r\n\r\n### Example 2: link file variables to other file\r\n\r\nExample2.py\r\n\r\n```python\r\nimport glovar\r\n\r\nglovar.var_file(\"full/path/to/Example1.py\")\r\n\r\nvalue = glovar.get(\"my_variable\")\r\nprint(\"Value:\", value) # Output: Value: my_value\r\n```\r\n\r\nDue to link Example1.py file to Example2.py using `glovar.file(\"full/path/to/Example1.py\")` we can see my_variable from Example1.py\r\n\r\n### Example 3: link file variables to directory\r\n\r\nYou can link variables to directory instead of files\r\n\r\nExample1.py\r\n\r\n```python\r\nimport glovar\r\n\r\nglovar.var_file() # leave empty to link to file directory\r\n\r\nglovar.set(\"my_variable\", \"my_value\")\r\n\r\nvalue = glovar.get(\"my_variable\")\r\nprint(\"Value:\", value) # Output: Value: my_value\r\n```\r\n",
"bugtrack_url": null,
"license": null,
"summary": "A package for managing JSON-based global variables by file context.",
"version": "0.2.2",
"project_urls": {
"Homepage": "https://github.com/PaceGG/Python-Global-vars-lib"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1640d8e0c7ed27bae4bec991e52a22febc47248c5fa4bbc49cfbce7cccd61387",
"md5": "7258b5ae19491707b47f377157f8499d",
"sha256": "6f20135cd5c5202cad97fea2cfd97ccc24a7f2fc056835c9436ee470936b98f2"
},
"downloads": -1,
"filename": "glovar-0.2.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7258b5ae19491707b47f377157f8499d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 3932,
"upload_time": "2024-09-05T22:18:16",
"upload_time_iso_8601": "2024-09-05T22:18:16.567237Z",
"url": "https://files.pythonhosted.org/packages/16/40/d8e0c7ed27bae4bec991e52a22febc47248c5fa4bbc49cfbce7cccd61387/glovar-0.2.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a49f1d41e3df429b34c72d3a9aa24b0105a060a39dd429074e18b69bb42fb2e4",
"md5": "6bb964a08f3317c12d0175e6d7903288",
"sha256": "3be321f0d2f1b43c1441af5905b3f34b6b09fa1311389fe5673b9bba5bad947f"
},
"downloads": -1,
"filename": "glovar-0.2.2.tar.gz",
"has_sig": false,
"md5_digest": "6bb964a08f3317c12d0175e6d7903288",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 3433,
"upload_time": "2024-09-05T22:18:18",
"upload_time_iso_8601": "2024-09-05T22:18:18.793533Z",
"url": "https://files.pythonhosted.org/packages/a4/9f/1d41e3df429b34c72d3a9aa24b0105a060a39dd429074e18b69bb42fb2e4/glovar-0.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-05 22:18:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "PaceGG",
"github_project": "Python-Global-vars-lib",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "glovar"
}