Coverage for cosmolayer / parser / utils.py: 85%

13 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-11 14:25 +0000

1import re 

2 

3import pandas as pd 

4 

5 

6def parse_table( 

7 contents: str, 

8 row_regex: re.Pattern[str], 

9 section_regex: re.Pattern[str], 

10 schema: dict[str, type], 

11) -> pd.DataFrame: 

12 section_match = section_regex.search(contents) 

13 if not section_match: 

14 raise ValueError("Could not parse table information.") 

15 rows = [ 

16 { 

17 title: converter(value) 

18 for (title, converter), value in zip( 

19 schema.items(), row_match.groups(), strict=True 

20 ) 

21 } 

22 for row_match in row_regex.finditer(section_match.group(1)) 

23 ] 

24 return pd.DataFrame(rows, columns=schema.keys()) 

25 

26 

27def parse_value(contents: str, regex: re.Pattern[str]) -> float: 

28 match = regex.search(contents) 

29 if not match: 

30 raise ValueError("Could not parse value.") 

31 return float(match.group(1))