Skip to main content

misc tools for configs, logs

Project description

hao

configurations, logs and others.

install

pip install hao

precondition

The folder contained any of the following files (searched in this very order) will be treated as project root path.

  • requirements.txt
  • VERSION
  • conf
  • setup.py
  • .idea
  • .git

If your project structure does NOT conform to this, it will not work as expected.

features

config

It will try to load YAML config file from conf folder

.                               # project root
├── conf
│   ├── config-{env}.yml        # if `export env=abc`, will raise error if not found
│   ├── config-{hhostname}.yml  # try to load this file, then the default `config.yml`
│   └── config.yml              # the default config file that should always exist
├── requirements.txt            # every project should have this file
├── VERSION                     # hao.versions.get_version() will try to read this file
├── .git

In following order:

if os.environ.get("env") is not None:
    try_to_load(f'config-{env}.yml', fallback='config.yml')                   # echo $env
else:
    try_to_load(f'config-{socket.gethostname()}.yml', fallback='config.yml')  # echo hostname

Say you have the following content in your config file:

es:
  default:
    host: 172.23.3.3
    port: 9200
    indices:
      - news
      - papers

The get the configured values in your code:

import hao
es_host = hao.config.get('es.default.host')          # str
es_port = hao.config.get('es.default.port')          # int
indices = hao.config.get('es.default.indices')       # list
...

logs

Set the logger level in config.yml

e.g.

logging:
  __main__: DEBUG
  transformers: WARNING
  lightning: INFO
  pytorch_lightning: INFO
  elasticsearch: WARNING
  tests: DEBUG
  root: INFO                        # root level

If you want to change the log format:

logger:
  format: "%(asctime)s %(levelname)-7s %(name)s:%(lineno)-4d - %(message)s"

Declear your logger

import hao
LOGGER = hao.logs.get_logger(__name__)

namespaces

from hao.namespaces import from_args, attr

@from_args
#@from_args(adds=Trainer.add_argparse_args)
class TrainConf(Namespace):
    root_path_checkpoints = attr(str, default=hao.paths.get_path('data/checkpoints/'))
    dataset_train = attr(str, default='train.txt')
    dataset_val = attr(str, default='val.txt')
    dataset_test = attr(str, default='test.txt')
    batch_size = attr(int, default=128, key='train.batch_size')                          # key means try to load from config.yml by the key
    task = attr(str, choices=('ner', 'nmt'), default='ner')
    seed = attr(int)
    epochs = attr(int, default=5)

Where attr is a wrapper for argpars.add_argument()

Usage 1: overwrite the default value from command line

python -m your_module --task=nmt

Usage 2: overwrite the default value from constructor

train_conf = TrainConf(task='nmt')

Value lookup order:

  • command line
  • constructor
  • config yml if key specified in attr
  • default if specified in attr

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

hao-3.1.3.tar.gz (83.5 kB view details)

Uploaded Source

Built Distribution

hao-3.1.3-py3-none-any.whl (83.1 kB view details)

Uploaded Python 3

File details

Details for the file hao-3.1.3.tar.gz.

File metadata

  • Download URL: hao-3.1.3.tar.gz
  • Upload date:
  • Size: 83.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.7.9

File hashes

Hashes for hao-3.1.3.tar.gz
Algorithm Hash digest
SHA256 49f2a72421ce02ebfb2cd68fcea41cde0cedfc7cddc8712351e1836921e978ef
MD5 35d40e77ec51b785756a0025a777d7ce
BLAKE2b-256 194d2d8df0d40140ea44ef44ab414a76b8e169cafe2b252df93e4c97f7872214

See more details on using hashes here.

File details

Details for the file hao-3.1.3-py3-none-any.whl.

File metadata

  • Download URL: hao-3.1.3-py3-none-any.whl
  • Upload date:
  • Size: 83.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.7.9

File hashes

Hashes for hao-3.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 341935f44847b61785a502b0a4f1ac75ff04ea1a9feec4e00bf13623848f958c
MD5 467eeebd978795431bcaa3770db01a20
BLAKE2b-256 df826a307b00db26cb79f6a86cf4e0412f0f0f699e22d700f4dd5d3874a7deb4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page