halib 0.1.19__py3-none-any.whl → 0.1.21__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
halib/__init__.py CHANGED
@@ -15,12 +15,18 @@ __all__ = [
15
15
  "console",
16
16
  "console_log",
17
17
  "ConsoleLog",
18
- "re"
18
+ "re",
19
+ "now_str",
20
+ "omegaconf",
21
+ "OmegaConf",
22
+ "DictConfig",
23
+ "load_yaml",
19
24
  ]
20
25
 
21
26
  import numpy as np
22
27
  import pandas as pd
23
28
  from .filetype import *
29
+ from .filetype.yamlfile import load_yaml
24
30
  from .sys import cmd
25
31
  from .sys import filesys as fs
26
32
 
@@ -35,14 +41,23 @@ from tqdm import tqdm
35
41
  import matplotlib.pyplot as plt
36
42
  import re
37
43
  import arrow
44
+ import omegaconf
45
+ from omegaconf import OmegaConf
46
+ from omegaconf.dictconfig import DictConfig
38
47
 
39
48
  console = Console()
40
49
 
50
+
41
51
  def now_str(sep_date_time="."):
42
- assert sep_date_time in [".", "_", "-"], "sep_date_time must be one of '.', '_', or '-'"
52
+ assert sep_date_time in [
53
+ ".",
54
+ "_",
55
+ "-",
56
+ ], "sep_date_time must be one of '.', '_', or '-'"
43
57
  now_string = arrow.now().format(f"YYYYMMDD{sep_date_time}HHmmss")
44
58
  return now_string
45
59
 
60
+
46
61
  def norm_str(in_str):
47
62
  # Replace one or more whitespace characters with a single underscore
48
63
  norm_string = re.sub(r"\s+", "_", in_str)
@@ -50,6 +65,7 @@ def norm_str(in_str):
50
65
  norm_string = norm_string.strip()
51
66
  return norm_string
52
67
 
68
+
53
69
  def console_rule(msg, do_norm_msg=True, is_end_tag=False):
54
70
  msg = norm_str(msg) if do_norm_msg else msg
55
71
  if is_end_tag:
@@ -57,6 +73,7 @@ def console_rule(msg, do_norm_msg=True, is_end_tag=False):
57
73
  else:
58
74
  console.rule(f"<{msg}>")
59
75
 
76
+
60
77
  def console_log(func):
61
78
  def wrapper(*args, **kwargs):
62
79
  console_rule(func.__name__)
@@ -66,6 +83,7 @@ def console_log(func):
66
83
 
67
84
  return wrapper
68
85
 
86
+
69
87
  class ConsoleLog:
70
88
  def __init__(self, message):
71
89
  self.message = message
@@ -0,0 +1,65 @@
1
+ import time
2
+ import networkx as nx
3
+ from rich import inspect
4
+ from rich.pretty import pprint
5
+ from omegaconf import OmegaConf
6
+ from rich.console import Console
7
+ from argparse import ArgumentParser
8
+
9
+ console = Console()
10
+
11
+ def _load_yaml_recursively(
12
+ yaml_file, yaml_files=[], share_nx_graph=nx.DiGraph(), log_info=False
13
+ ):
14
+ conf = OmegaConf.load(yaml_file)
15
+ yaml_files.append(yaml_file)
16
+ if "__base__" in conf:
17
+ parent = conf["__base__"]
18
+ if isinstance(parent, str):
19
+ parent = [parent]
20
+ for p in parent:
21
+ edge = (yaml_file, p)
22
+ share_nx_graph.add_edge(*edge)
23
+ for cycle in nx.simple_cycles(share_nx_graph):
24
+ assert False, f"Cyclic dependency detected: {cycle}"
25
+ # update conf with parent; BY loading parent and merging with conf (the child)
26
+ conf = OmegaConf.merge(
27
+ _load_yaml_recursively(p, yaml_files, share_nx_graph), conf
28
+ )
29
+ if log_info:
30
+ console.rule()
31
+ console.print(f"current yaml_file: {yaml_file}")
32
+ inspect(yaml_files)
33
+ pprint(OmegaConf.to_container(conf, resolve=True))
34
+ time.sleep(1)
35
+ return conf
36
+
37
+
38
+ def load_yaml(yaml_file, to_dict=False, log_info=False):
39
+ yaml_files = []
40
+ share_nx_graph = nx.DiGraph()
41
+ omgconf = _load_yaml_recursively(yaml_file, yaml_files=yaml_files, share_nx_graph=share_nx_graph, log_info=log_info)
42
+
43
+ if to_dict:
44
+ return OmegaConf.to_container(omgconf, resolve=True)
45
+ else:
46
+ return omgconf
47
+
48
+
49
+ def parse_args():
50
+ parser = ArgumentParser(
51
+ description="desc text")
52
+ parser.add_argument(
53
+ "-cfg", "--cfg", type=str, help="cfg file", default="cfg__default.yaml")
54
+ return parser.parse_args()
55
+
56
+
57
+ def main():
58
+ args = parse_args()
59
+ cfg_file = args.cfg
60
+ cfg = load_yaml(cfg_file, to_dict=True)
61
+ console.rule()
62
+ pprint(cfg)
63
+
64
+ if __name__ == "__main__":
65
+ main()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: halib
3
- Version: 0.1.19
3
+ Version: 0.1.21
4
4
  Summary: Small library for common tasks
5
5
  Author: Hoang Van Ha
6
6
  Author-email: hoangvanhauit@gmail.com
@@ -16,9 +16,15 @@ Requires-Dist: rich
16
16
  Requires-Dist: tqdm
17
17
  Requires-Dist: tabulate
18
18
  Requires-Dist: loguru
19
+ Requires-Dist: omegaconf
20
+ Requires-Dist: networkx
19
21
 
20
22
  Helper package for coding and automation
21
23
 
24
+ **Version 0.1.21**
25
+
26
+ + using `networkx` and `omegaconf` to allow yaml file inheritance and override
27
+ ---
22
28
  **Version 0.1.15**
23
29
 
24
30
  + `__init__.py`: add common logging library; also `console_log` decorator to log function (start and end)
@@ -1,4 +1,4 @@
1
- halib/__init__.py,sha256=r6lmaJf9c7Yyb7bc7WfxYecz0mEMB20SF7huwOEgt4g,2050
1
+ halib/__init__.py,sha256=6E1oqNe2lqLUSGcWZXMZskNtPxZk42FwCOc4c2q4Ou4,2323
2
2
  halib/csvfile.py,sha256=Eoeni0NIbNG3mB5ESWAvNwhJxOjmCaPd1qqYRHImbvk,1567
3
3
  halib/filesys.py,sha256=r1SftGKM7nyw6QbY5UmcueZLkXEIleSzhui7dQsosPw,2907
4
4
  halib/gdrive.py,sha256=-dx8hbknor1stIXhAzCnCfOHAPWm9a9z7L0epsOBHjA,6274
@@ -15,6 +15,7 @@ halib/filetype/csvfile.py,sha256=mXqJYcEKyRDLbdkgy68yceJEPhfLV-L2bz5ljrOKwSM,418
15
15
  halib/filetype/jsonfile.py,sha256=9LBdM7LV9QgJA1bzJRkq69qpWOP22HDXPGirqXTgSCw,480
16
16
  halib/filetype/textfile.py,sha256=QtuI5PdLxu4hAqSeafr3S8vCXwtvgipWV4Nkl7AzDYM,399
17
17
  halib/filetype/videofile.py,sha256=n4lRKhQH8uRxVrhvdw_NUfrnChocflv3LfGlGya0WUs,4761
18
+ halib/filetype/yamlfile.py,sha256=CxiOz2pgFBkP_V8gmPpFWeXyJ3uug-na42m4ydrWtYU,1993
18
19
  halib/online/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
19
20
  halib/online/gdrive.py,sha256=tiVCJpJKci0wQ7H9iKti0K3AxsTp_q8e9uBSxPOBB6s,7794
20
21
  halib/online/gdrive_mkdir.py,sha256=Ur9-J1uzaM9qgpWF-d6md3gjkrFdPiMNLmbJtpQjXDI,1496
@@ -23,8 +24,8 @@ halib/online/projectmake.py,sha256=r9vPID23o_GG7TJy32PIUMvjtl2f-M9nlYJq63GL6ZA,4
23
24
  halib/sys/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
25
  halib/sys/cmd.py,sha256=b2x7JPcNnFjLGheIESVYvqAb-w2UwBM1PAwYxMZ5YjA,228
25
26
  halib/sys/filesys.py,sha256=ERpnELLDKJoTIIKf-AajgkY62nID4qmqmX5TkE95APU,2931
26
- halib-0.1.19.dist-info/LICENSE.txt,sha256=qZssdna4aETiR8znYsShUjidu-U4jUT9Q-EWNlZ9yBQ,1100
27
- halib-0.1.19.dist-info/METADATA,sha256=XNYBNT58HTKUCBL4RH0sw3zzj86Yr3AqaVkAuutUuEM,1620
28
- halib-0.1.19.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
29
- halib-0.1.19.dist-info/top_level.txt,sha256=7AD6PLaQTreE0Fn44mdZsoHBe_Zdd7GUmjsWPyQ7I-k,6
30
- halib-0.1.19.dist-info/RECORD,,
27
+ halib-0.1.21.dist-info/LICENSE.txt,sha256=qZssdna4aETiR8znYsShUjidu-U4jUT9Q-EWNlZ9yBQ,1100
28
+ halib-0.1.21.dist-info/METADATA,sha256=cFTD9w4Ai5PDO8RQSrnpyvPcHX3Yju25TZtf2vSLpdI,1772
29
+ halib-0.1.21.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
30
+ halib-0.1.21.dist-info/top_level.txt,sha256=7AD6PLaQTreE0Fn44mdZsoHBe_Zdd7GUmjsWPyQ7I-k,6
31
+ halib-0.1.21.dist-info/RECORD,,
File without changes