data-manipulation-utilities 0.1.1__py3-none-any.whl → 0.1.3__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.
@@ -1,111 +0,0 @@
1
- '''
2
- Script meant to read information from files in
3
-
4
- https://gitlab.cern.ch/lhcb-datapkg/Gen/DecFiles
5
-
6
- and store it in current project as data
7
- '''
8
- import os
9
- import re
10
- import glob
11
- from dataclasses import dataclass
12
- from importlib.resources import files
13
-
14
- import tqdm
15
- import yaml
16
- from dmu.logging.log_store import LogStore
17
-
18
- log=LogStore.add_logger('dmu_scripts:physics:update_decinfo')
19
- # ------------------------------
20
- @dataclass
21
- class Data:
22
- '''
23
- Class used to store shared data
24
- '''
25
- dec_path : str
26
- regex : str = r'#[\s]*[a-zA-Z]+:[\s]*(.*)'
27
- # ------------------------------
28
- def _setup() -> None:
29
- if 'DECPATH' not in os.environ:
30
- raise ValueError('DECPATH, path to root of DecFiles, not found')
31
-
32
- Data.dec_path = os.environ['DECPATH']
33
- # ------------------------------
34
- def _line_from_list(file_path : str, contains : str, l_line : list[str]) -> str:
35
- l_value = [ line for line in l_line if contains in line ]
36
-
37
- if len(l_value) == 0:
38
- log.warning(f'Could not extract {contains} line in: {file_path}')
39
- return 'not_found'
40
-
41
- return l_value[0]
42
- # ------------------------------
43
- def _val_from_line(file_path : str, line : str) -> str:
44
- if line == 'not_found':
45
- return line
46
-
47
- mtch = re.match(Data.regex, line)
48
- if not mtch:
49
- log.warning(f'Cannot extract value from \"{line}\" in file {file_path}')
50
- return 'not_found'
51
-
52
- value = mtch.group(1)
53
- value = value.replace(' ', '')
54
-
55
- return value
56
- # ------------------------------
57
- def _get_evt_name(file_path : str) -> tuple[str,str]:
58
- with open(file_path, encoding='utf-8') as ifile:
59
- l_line = ifile.read().splitlines()
60
-
61
- evt_line = _line_from_list(file_path, 'EventType', l_line)
62
- nam_line = _line_from_list(file_path, 'NickName' , l_line)
63
-
64
- evt_type = _val_from_line(file_path, evt_line)
65
- nickname = _val_from_line(file_path, nam_line)
66
-
67
- return evt_type, nickname
68
- # ------------------------------
69
- def _read_info() -> dict[str,str]:
70
- dec_file_wc = f'{Data.dec_path}/dkfiles/*.dec'
71
- l_dec_file = glob.glob(dec_file_wc)
72
- nfiles = len(l_dec_file)
73
- if nfiles == 0:
74
- raise ValueError(f'No dec file foudn in {dec_file_wc}')
75
-
76
- log.info(f'Found {nfiles} decay files')
77
-
78
- l_evt_name = [ _get_evt_name(file_path) for file_path in tqdm.tqdm(l_dec_file, ascii=' -') ]
79
- d_evt_name = _dict_from_tup_list(l_evt_name)
80
-
81
- return d_evt_name
82
- # ------------------------------
83
- def _dict_from_tup_list(l_evt_name : list[tuple[str,str]]) -> dict[str,str]:
84
- d_res = {}
85
- for key, val in l_evt_name:
86
- if key in d_res:
87
- old_val = d_res[key]
88
- log.warning(f'Key {key} with value {old_val} already found, overriding with {val}')
89
-
90
- d_res[key] = val
91
-
92
- return d_res
93
- # ------------------------------
94
- def _dump_info(d_evt_name : dict[str,str]) -> None:
95
- yaml_path = files('dmu_data').joinpath('physics/evt_name.yaml')
96
- yaml_path = str(yaml_path)
97
-
98
- log.info(f'Saving to: {yaml_path}')
99
- with open(yaml_path, 'w', encoding='utf-8') as ofile:
100
- yaml.dump(d_evt_name, ofile)
101
- # ------------------------------
102
- def main():
103
- '''
104
- Script starts here
105
- '''
106
- _setup()
107
- d_evt_name = _read_info()
108
- _dump_info(d_evt_name)
109
- # ------------------------------
110
- if __name__ == '__main__':
111
- main()