Skip to content

Commit 8ca42d6

Browse files
committed
Fix logging
1 parent 119d626 commit 8ca42d6

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

source-code/hydra/gen_rand.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
11
#!/usr/bin/env python
22

33
import hydra
4-
54
from omegaconf import OmegaConf
65
import logging
76
import numpy as np
87
import sys
98

10-
LOG = logging.getLogger(sys.argv[0])
119

1210
@hydra.main(version_base=None, config_path='conf/', config_name='config')
1311
def gen_rand(cfg):
1412
if cfg.verbose:
1513
print(OmegaConf.to_yaml(cfg), file=sys.stderr)
14+
logger = logging.getLogger(__name__)
1615
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')
2120
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}')
2322
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)
2625
numbers = np.random.normal(loc=cfg.distr.mu, scale=cfg.distr.sigma,
2726
size=(cfg.n,))
2827
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}')
3029
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, '
3231
f'{cfg.distr.a} >= {cfg.distr.b}')
3332
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
3535
for number in numbers:
36-
print(number)
37-
LOG.info('output done')
36+
print(number, file=out)
37+
logger.info('output done')
3838
return 0
3939

4040
if __name__ == '__main__':
41-
status = gen_rand()
42-
sys.exit(status)
41+
gen_rand()

0 commit comments

Comments
 (0)