Parse shell env variables to python dict
Project description
Hyperparse : A Simple Shell Environment Variables Parser
Introduction
This script provides a way to parse environment variables and use them as hyperparameters in Python scripts.
The script parses a string that contains a comma-separated list of key-value pairs, where each key-value pair is separated by an equal sign (=). The keys should be strings, and the values can be integers, floats, lists of integers or floats, booleans, or None. The script can handle values that are enclosed in single or double quotes.
The script also provides a set_hyper function that allows you to set the variables in your Python script. You can pass in a dictionary of hyperparameters and a namespace or dictionary of arguments. The function will update the namespace or dictionary with the hyperparameters.
Usage
First set a variable in shell and run the python file main.py
.
export usermode="a=1,b,c=[1,2,3],d=4,e=3.2,f=itud,g=False"
python main.py
Parse variables
# main.py
from hyperparser import get_hyper
usermode = get_hyper("usermode")
print(usermode)
# {"a": 1, "b": None, "c": [1, 2, 3], "d": 4, "e": 3.2, "f": "itud"}
Reset argparse elements
# main.py
from hyperparse import get_hyper, set_hyper
usermode = get_hyper("usermode")
parser = argparse.ArgumentParser()
parser.add_argument('--a', type=int, default=5)
parser.add_argument('--f', type=str, default="hello")
args = parser.parse_args()
set_hyper(usermode, args)
print(args) # Namespace(a=1, f='itud')
Reset Local Variables
# main.py
from hyperparse import get_hyper, set_hyper
usermode = get_hyper("usermode")
a = 5
f = "stk"
set_hyper(usermode)
print(a) # 1
print(f) # itud
```# hyperparse
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
File details
Details for the file hyperparse-0.0.6.tar.gz
.
File metadata
- Download URL: hyperparse-0.0.6.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c9755573da39ff0cedaab19a8a525ec9d88f6342be90a87ea0a5eaf3973b81a |
|
MD5 | 04c4859794f5f43132e8729db663be1f |
|
BLAKE2b-256 | 9ddff95e208832755d999d419ec3407da0b1388e27fdae284df07dce09e75915 |
File details
Details for the file hyperparse-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: hyperparse-0.0.6-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0555a3546a354c9f0ce9dec67a44f8c8c22fa5d7e4326fdd08ff3a8d4542a591 |
|
MD5 | 9eccd4c1b14c7e667762058ac3c5c9d3 |
|
BLAKE2b-256 | b9775fa55430308e384bbfe74745b0b1d1ad1610a5c62c34149c8659888544c8 |