Skip to content

Commit

Permalink
Refactor parsing functions: rename _convert_string_part to _convert_p…
Browse files Browse the repository at this point in the history
…art and simplify logic for handling special characters
  • Loading branch information
arthur-debert committed Dec 5, 2024
1 parent db0b642 commit 725e9ea
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
4 changes: 2 additions & 2 deletions rangy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .builtins import register_builtins
from .rangy import Rangy, _parse
from .distribute import distribute
from .parse import parse_range, _normalize_to_sequence, _convert_string_part
from .parse import parse_range, _normalize_to_sequence, _convert_part

register_builtins()
__all__ = ["ANY", "AT_LEAST_ONE", "EXACT", "RANGE", "COUNT_TYPES", "INFINITY", "ANY_CHAR", "AT_LEAST_ONE_CHAR", "SPECIAL_CHARS", "Converter", "ConverterRegistry", "Rangy", "_parse", "distribute", "parse_range", "_normalize_to_sequence", "_convert_string_part"]
__all__ = ["ANY", "AT_LEAST_ONE", "EXACT", "RANGE", "COUNT_TYPES", "INFINITY", "ANY_CHAR", "AT_LEAST_ONE_CHAR", "SPECIAL_CHARS", "Converter", "ConverterRegistry", "Rangy", "_parse", "distribute", "parse_range", "_normalize_to_sequence", "_convert_part"]
22 changes: 3 additions & 19 deletions rangy/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def _normalize_to_sequence(range_input):
raise ParseRangeError(f"Unsupported range input type: {type(range_input)}")


def _convert_string_part(part):
if part in SPECIAL_CHARS.keys():
def _convert_part(part):
if part in SPECIAL_CHARS.values():
return part
for converter in ConverterRegistry():
try:
Expand All @@ -101,24 +101,8 @@ def parse_range(range_input):

start, end = _normalize_to_sequence(range_input)


try:
if start == '*':
parsed_start = 0
elif start == '+':
parsed_start = 1
elif not isinstance(start, str):
converter = ConverterRegistry.get(start)
parsed_start = converter(start)
else:
parsed_start = _convert_string_part(start)

if not isinstance(end, str):
converter = ConverterRegistry.get(end)
parsed_end = converter(end)
else:
parsed_end = _convert_string_part(end)
return parsed_start, parsed_end
return _convert_part(start), _convert_part(end)

except (KeyError, ValueError, TypeError) as e:
raise ParseRangeError(f"Error parsing range: {e}") from e

0 comments on commit 725e9ea

Please sign in to comment.