|
1 | 1 | #!/usr/bin/env python
|
2 | 2 |
|
3 | 3 | import hydra
|
4 |
| - |
5 | 4 | from omegaconf import OmegaConf
|
6 | 5 | import logging
|
7 | 6 | import numpy as np
|
8 | 7 | import sys
|
9 | 8 |
|
10 |
| -LOG = logging.getLogger(sys.argv[0]) |
11 | 9 |
|
12 | 10 | @hydra.main(version_base=None, config_path='conf/', config_name='config')
|
13 | 11 | def gen_rand(cfg):
|
14 | 12 | if cfg.verbose:
|
15 | 13 | print(OmegaConf.to_yaml(cfg), file=sys.stderr)
|
| 14 | + logger = logging.getLogger(__name__) |
16 | 15 | if cfg.n <= 0:
|
17 |
| - LOG.error(f'negative number to generate {cfg.n}') |
18 |
| - return 1 |
19 |
| - LOG.info(f'generating {cfg.n} random numbers') |
20 |
| - LOG.info(f'using {cfg.distr.name} distribution') |
| 16 | + logger.error(f'negative number to generate {cfg.n}') |
| 17 | + sys.exit(1) |
| 18 | + logger.info(f'generating {cfg.n} random numbers') |
| 19 | + logger.info(f'using {cfg.distr.name} distribution') |
21 | 20 | if cfg.distr.name == 'gauss':
|
22 |
| - LOG.info(f'mu={cfg.distr.mu}, sigma={cfg.distr.sigma}') |
| 21 | + logger.info(f'mu={cfg.distr.mu}, sigma={cfg.distr.sigma}') |
23 | 22 | if cfg.distr.sigma <= 0:
|
24 |
| - LOG.error(f'negative standard deviation {cfg.distr.sigma}') |
25 |
| - return 1 |
| 23 | + logger.error(f'negative standard deviation {cfg.distr.sigma}') |
| 24 | + sys.exit(1) |
26 | 25 | numbers = np.random.normal(loc=cfg.distr.mu, scale=cfg.distr.sigma,
|
27 | 26 | size=(cfg.n,))
|
28 | 27 | elif cfg.distr.name == 'uniform':
|
29 |
| - LOG.info(f'a={cfg.distr.a}, b={cfg.distr.b}') |
| 28 | + logger.info(f'a={cfg.distr.a}, b={cfg.distr.b}') |
30 | 29 | if cfg.distr.a >= cfg.distr.b:
|
31 |
| - LOG.warning(f'lower bound exceed upper bound, ' |
| 30 | + logger.warning(f'lower bound exceed upper bound, ' |
32 | 31 | f'{cfg.distr.a} >= {cfg.distr.b}')
|
33 | 32 | numbers = np.random.uniform(cfg.distr.a, cfg.distr.b, size=(cfg.n,))
|
34 |
| - LOG.info('starting output') |
| 33 | + logger.info('starting output') |
| 34 | + out = open(cfg.file, 'w') if cfg.file else sys.stdout |
35 | 35 | for number in numbers:
|
36 |
| - print(number) |
37 |
| - LOG.info('output done') |
| 36 | + print(number, file=out) |
| 37 | + logger.info('output done') |
38 | 38 | return 0
|
39 | 39 |
|
40 | 40 | if __name__ == '__main__':
|
41 |
| - status = gen_rand() |
42 |
| - sys.exit(status) |
| 41 | + gen_rand() |
0 commit comments