Skip to content

Instantly share code, notes, and snippets.

@nuric
Created July 17, 2020 13:00
Show Gist options
  • Save nuric/e9df5e38c34c804ec6c298ffca7e2d9b to your computer and use it in GitHub Desktop.
Save nuric/e9df5e38c34c804ec6c298ffca7e2d9b to your computer and use it in GitHub Desktop.
"""Configuration library for experiments."""
from typing import Dict, Any
import logging
import pprint
import sys
import argparse
logger = logging.getLogger(__name__)
parser = argparse.ArgumentParser(description=__doc__, fromfile_prefix_chars="@")
config: Dict[str, Any] = {}
def add_parser(title: str, description: str = ""):
"""Create a new context for arguments and return a handle."""
return parser.add_argument_group(title, description)
def parse(save_fname: str = "") -> Dict[str, Any]:
"""Parse given arguments."""
config.update(vars(parser.parse_args()))
logging.info("Parsed %i arguments.", len(config))
# Save passed arguments
if save_fname:
with open(save_fname, "w") as fout:
fout.write("\n".join(sys.argv[1:]))
logging.info("Saving arguments to %s.", save_fname)
return config
def print_config():
"""Print the current config to stdout."""
pprint.pprint(config)
"""Some data loading module."""
from typing import List
import random
import configlib
from configlib import config as C
# Configuration arguments
parser = configlib.add_parser("Dataset config")
parser.add_argument("--data_length", default=4, type=int, help="Length of random list.")
def load_data() -> List[int]:
"""Load some random data."""
data = list(range(C["data_length"]))
random.shuffle(data)
return data
"""Some training script that uses data module."""
import configlib
from configlib import config as C
import data
# Configuration arguments
parser = configlib.add_parser("Train config")
parser.add_argument("--debug", action="store_true", help="Enable debug mode.")
def train():
"""Main training function."""
if C["debug"]:
print("Debugging mode enabled.")
print("Example dataset:")
print(data.load_data())
if __name__ == "__main__":
configlib.parse("last_arguments.txt")
print("Running with configuration:")
configlib.print_config()
train()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment