Skip to main content

Parse shell env variables to python dict

Project description

Hyperparse

A simple and powerful parser for shell environment variables and command line arguments.

Installation

pip install hyperparse

Quick Start

from hyperparse import parse_string

config = parse_string('lr=1e-4,batch=32,model={name:bert,layers:12}')

print(config.lr)           # 0.0001
print(config.batch)        # 32
print(config.model.name)   # 'bert'

Supported Types

Type Example Result
Integer a=1 1
Negative a=-1 -1
Float a=3.14 3.14
Scientific a=1e-5 0.00001
Boolean a=True True
None a=None or a None
String a=hello 'hello'
List a=[1,2,3] [1, 2, 3]
Nested List a=[[1,2],[3,4]] [[1, 2], [3, 4]]
Dict a={x:1,y:2} {'x': 1, 'y': 2}
Nested a={x:[1,2],y:{z:3}} {'x': [1, 2], 'y': {'z': 3}}

Attribute Access

Parse results support both dict-style and attribute-style access:

config = parse_string('a=1,b={x:2,y:3}')

# Dict style
config['a']      # 1
config['b']['x'] # 2

# Attribute style
config.a         # 1
config.b.x       # 2

Check Key Existence

config = parse_string('a=1,b=2,c=3')

# Check if all keys exist
config.hasall('a', 'b')      # True
config.hasall('a', 'x')      # False

# Check if any key exists
config.hasany('a', 'x')      # True
config.hasany('x', 'y')      # False

Environment Variables

export usermode="lr=1e-4,batch=32,epochs=10"
python main.py
from hyperparse import parse, reset_hyper

usermode, _ = parse("usermode")
print(usermode.lr)     # 0.0001
print(usermode.batch)  # 32

Argparse Integration

from hyperparse import parse, reset_hyper
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--lr', type=float, default=0.01)
parser.add_argument('--batch', type=int, default=16)
args = parser.parse_args()

usermode, _ = parse("usermode")
reset_hyper(usermode, args)

print(args.lr)     # 0.0001 (overridden by env)
print(args.batch)  # 32 (overridden by env)

Local Variables

from hyperparse import parse, reset_hyper

lr = 0.01
batch = 16

usermode, _ = parse("usermode")
reset_hyper(usermode)

print(lr)     # 0.0001
print(batch)  # 32

API Reference

parse_string(s)

Parse a string into an AttrDict.

parse(name)

Parse from environment variable or command line argument.

reset_hyper(hyper, pargs=None)

Update argparse namespace or local variables with parsed values.

AttrDict

Dict subclass with attribute access and helper methods:

  • .hasall(*keys) - True if all keys exist
  • .hasany(*keys) - True if any key exists

License

MIT

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

hyperparse-0.0.9.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

hyperparse-0.0.9-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file hyperparse-0.0.9.tar.gz.

File metadata

  • Download URL: hyperparse-0.0.9.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for hyperparse-0.0.9.tar.gz
Algorithm Hash digest
SHA256 3cc56c278b91d0f62558799b4351c13aa284f791cd42bf04930157bbb99fede6
MD5 a891518c75d61c2c0ed0ebb1a67137ad
BLAKE2b-256 421481aaeba5a6a433b5673befedb20701c2e3f2930e305d13ac0a9bab68ae9a

See more details on using hashes here.

File details

Details for the file hyperparse-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: hyperparse-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for hyperparse-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d73b162ebff51ba4f485bea7c5fd90c4be598dc5f972d1724082eeaa175ba715
MD5 6f0d98ef88a9e8c6c5aedeb9f01ea390
BLAKE2b-256 0dc4920ec255e3bdd75419c3374149b05ed7d0da705bd05816df05ecbee4e04c

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