util.py 819 B

123456789101112131415161718192021222324252627282930313233343536
  1. import re
  2. from typing import Dict
  3. from isoduration.constants import PERIOD_PREFIX, TIME_PREFIX, WEEK_PREFIX
  4. from isoduration.parser.exceptions import OutOfDesignators
  5. def is_period(ch: str) -> bool:
  6. return ch == PERIOD_PREFIX
  7. def is_time(ch: str) -> bool:
  8. return ch == TIME_PREFIX
  9. def is_week(ch: str) -> bool:
  10. return ch == WEEK_PREFIX
  11. def is_number(ch: str) -> bool:
  12. return bool(re.match(r"[+\-0-9.,eE]", ch))
  13. def is_letter(ch: str) -> bool:
  14. return ch.isalpha() and ch.lower() != "e"
  15. def parse_designator(designators: Dict[str, str], target: str) -> str:
  16. while True:
  17. try:
  18. key, value = designators.popitem(last=False) # type: ignore
  19. except KeyError as exc:
  20. raise OutOfDesignators from exc
  21. if key == target:
  22. return value