Debian Codegraph @

src/isoduration/parser/util.py

raw symbols SCIP
1 import decimal
2 import re
3 from typing import Dict
4
5 from isoduration.constants import PERIOD_PREFIX, TIME_PREFIX, WEEK_PREFIX
6 from isoduration.parser.exceptions import OutOfDesignators
7
8
9 def is_period(ch: str) -> bool:
10 return ch == PERIOD_PREFIX
11
12
13 def is_time(ch: str) -> bool:
14 return ch == TIME_PREFIX
15
16
17 def is_week(ch: str) -> bool:
18 return ch == WEEK_PREFIX
19
20
21 def is_number(ch: str) -> bool:
22 return bool(re.match(r"[+\-0-9.,eE]", ch))
23
24
25 def is_letter(ch: str) -> bool:
26 return ch.isalpha() and ch.lower() != "e"
27
28
29 def parse_designator(designators: Dict[str, str], target: str) -> str:
30 while True:
31 try:
32 key, value = designators.popitem(last=False) # type: ignore
33 except KeyError as exc:
34 raise OutOfDesignators from exc
35
36 if key == target:
37 return value
38
39
40 def is_integer(number: decimal.Decimal) -> bool:
41 return number == number.to_integral_value()