An interpreter for lazy configs in Python
Project description
This repository contains an interpreter for config files written in a subset of Python’s grammar. It aims to combine Python’s beautiful syntax, lazy initialization and a much simpler import machinery.
Features overview
Lazy scopes
The scopes inside config files are lazy, this means that any declared value will be calculated only when requested. This allows the user to define various memory-consuming values in a single config.
Because the scopes are lazy, the following code is ambiguous:
# which value to use?
x = 1
x = 2
To avoid ambiguity, each name can be used only once in a config. As a consequence some syntactic structures (e.g. for loops) are not supported.
Statements
Only three Python statements are supported: value definitions, function definitions and imports.
Value definitions
Values are declared using the = operator followed by any valid Python expression.
Here are some examples:
file_path = "/some/path"
num_folds = 5
kernel_sizes = [3, 3, 5]
some_value = apply_to_input(lambda x: x ** 2)
odd_squares = [i ** 2 for i in range(10) if i % 2 == 1]
Function definitions
You can define functions inside configs. A function body consists of several value or function definitions or assertions followed by a return statement:
def normalize(x, y, z):
length = sqrt(x ** 2 + y ** 2 + z ** 2)
assert length > 0
return x / length, y / length, z / length
def adder(f):
def wrapper(x):
return f(x) + 1
return wrapper
@adder
def f(x):
return x
def check_call(seq):
assert seq, seq
return f(seq[0])
Imports
You can import from other libraries inside config files just like in regular Python:
from numpy import prod as product
from numpy import sum, random
import pandas as pd
import tqdm
import math.radians
You can also import from config files, located relative to the main config (in this case from ./some_config.config, ./folder/dataset.config and ../upper/another.config):
from .some_config import *
from ..upper.another import *
from .folder.dataset import DataSet as D
Note, that you can use starred imports (e.g. from a import *) only when importing from another config.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file lazycon-0.0.1.tar.gz
.
File metadata
- Download URL: lazycon-0.0.1.tar.gz
- Upload date:
- Size: 17.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.0.post20201103 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 936462b7c15c6d24797cc75748ad2ec69ef2363d7924f30ca00711a8015df997 |
|
MD5 | 50e46cd7477623decd47eb7c9350d989 |
|
BLAKE2b-256 | c0d1d45e1809074a6a451cc983f7b9993f791bf1fcef574d6ba4585f2ae303b7 |