Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Nov 25, 2023
1 parent 1749ac4 commit e8818d8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 62 deletions.
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ with the same identifier:
>>> def shouty(string):
... return string.upper()
...
>>> parse('{:shouty} world', 'hello world', dict(shouty=shouty))
>>> parse('{:shouty} world', 'hello world', {"shouty": shouty})
<Result ('HELLO',) {}>
If the type converter has the optional ``pattern`` attribute, it is used as
Expand All @@ -350,9 +350,9 @@ regular expression for better pattern matching (instead of the default one):
>>> def parse_number(text):
... return int(text)
>>> parse_number.pattern = r'\d+'
>>> parse('Answer: {number:Number}', 'Answer: 42', dict(Number=parse_number))
>>> parse('Answer: {number:Number}', 'Answer: 42', {"Number": parse_number})
<Result () {'number': 42}>
>>> _ = parse('Answer: {:Number}', 'Answer: Alice', dict(Number=parse_number))
>>> _ = parse('Answer: {:Number}', 'Answer: Alice', {"Number": parse_number})
>>> assert _ is None, "MISMATCH"
You can also use the ``with_pattern(pattern)`` decorator to add this
Expand All @@ -364,7 +364,7 @@ information to a type converter function:
>>> @with_pattern(r'\d+')
... def parse_number(text):
... return int(text)
>>> parse('Answer: {number:Number}', 'Answer: 42', dict(Number=parse_number))
>>> parse('Answer: {number:Number}', 'Answer: 42', {"Number": parse_number})
<Result () {'number': 42}>
A more complete example of a custom type might be:
Expand All @@ -390,7 +390,7 @@ in the ``with_pattern()`` decorator:
>>> @with_pattern(r'((\d+))', regex_group_count=2)
... def parse_number2(text):
... return int(text)
>>> parse('Answer: {:Number2} {:Number2}', 'Answer: 42 43', dict(Number2=parse_number2))
>>> parse('Answer: {:Number2} {:Number2}', 'Answer: 42 43', {"Number2": parse_number2})
<Result (42, 43) {}>
Otherwise, this may cause parsing problems with unnamed/fixed parameters.
Expand Down
52 changes: 26 additions & 26 deletions parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,31 @@ def __eq__(self, other):
return self._name == other._name and self._offset == other._offset


MONTHS_MAP = dict(
Jan=1,
January=1,
Feb=2,
February=2,
Mar=3,
March=3,
Apr=4,
April=4,
May=5,
Jun=6,
June=6,
Jul=7,
July=7,
Aug=8,
August=8,
Sep=9,
September=9,
Oct=10,
October=10,
Nov=11,
November=11,
Dec=12,
December=12,
)
MONTHS_MAP = {
"Jan": 1,
"January": 1,
"Feb": 2,
"February": 2,
"Mar": 3,
"March": 3,
"Apr": 4,
"April": 4,
"May": 5,
"Jun": 6,
"June": 6,
"Jul": 7,
"July": 7,
"Aug": 8,
"August": 8,
"Sep": 9,
"September": 9,
"Oct": 10,
"October": 10,
"Nov": 11,
"November": 11,
"Dec": 12,
"December": 12,
}
DAYS_PAT = r"(Mon|Tue|Wed|Thu|Fri|Sat|Sun)"
MONTHS_PAT = r"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"
ALL_MONTHS_PAT = r"(%s)" % "|".join(MONTHS_MAP)
Expand Down Expand Up @@ -585,7 +585,7 @@ def evaluate_result(self, m):
named_fields[korig] = value

# now figure the match spans
spans = dict((n, m.span(name_map[n])) for n in named_fields)
spans = {n: m.span(name_map[n]) for n in named_fields}
spans.update((i, m.span(n + 1)) for i, n in enumerate(self._fixed_fields))

# and that's our result
Expand Down
4 changes: 2 additions & 2 deletions tests/test_bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ def parse_number(text):
return int(text)

# -- CASE: Use named (OK)
type_map = dict(Name=parse_word_and_covert_to_uppercase, Number=parse_number)
type_map = {"Name": parse_word_and_covert_to_uppercase, "Number": parse_number}
r = parse.parse(
"Hello {name:Name} {number:Number}", "Hello Alice 42", extra_types=type_map
)
assert r.named == dict(name="ALICE", number=42)
assert r.named == {"name": "ALICE", "number": 42}

# -- CASE: Use unnamed/fixed (problematic)
r = parse.parse("Hello {:Name} {:Number}", "Hello Alice 42", extra_types=type_map)
Expand Down
16 changes: 8 additions & 8 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ def test_custom_type():
r = parse.parse(
"{:shouty} {:spam}",
"hello world",
dict(shouty=lambda s: s.upper(), spam=lambda s: "".join(reversed(s))),
{"shouty": lambda s: s.upper(), "spam": lambda s: "".join(reversed(s))},
)
assert r.fixed == ("HELLO", "dlrow")
r = parse.parse("{:d}", "12", dict(d=lambda s: int(s) * 2))
r = parse.parse("{:d}", "12", {"d": lambda s: int(s) * 2})
assert r.fixed == (24,)
r = parse.parse("{:d}", "12")
assert r.fixed == (12,)
Expand Down Expand Up @@ -180,25 +180,25 @@ def test_mixed():
# pull a fixed and named values out of string
r = parse.parse("hello {} {name} {} {spam}", "hello world and other beings")
assert r.fixed == ("world", "other")
assert r.named == dict(name="and", spam="beings")
assert r.named == {"name": "and", "spam": "beings"}


def test_named_typed():
# pull a named, typed values out of string
r = parse.parse("hello {number:d} {things}", "hello 12 people")
assert r.named == dict(number=12, things="people")
assert r.named == {"number": 12, "things": "people"}
r = parse.parse("hello {number:w} {things}", "hello 12 people")
assert r.named == dict(number="12", things="people")
assert r.named == {"number": "12", "things": "people"}


def test_named_aligned_typed():
# pull a named, typed values out of string
r = parse.parse("hello {number:<d} {things}", "hello 12 people")
assert r.named == dict(number=12, things="people")
assert r.named == {"number": 12, "things": "people"}
r = parse.parse("hello {number:>d} {things}", "hello 12 people")
assert r.named == dict(number=12, things="people")
assert r.named == {"number": 12, "things": "people"}
r = parse.parse("hello {number:^d} {things}", "hello 12 people")
assert r.named == dict(number=12, things="people")
assert r.named == {"number": 12, "things": "people"}


def test_multiline():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_parsetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def parse_yesno(text):


def test_with_pattern():
ab_vals = dict(a=1, b=2)
ab_vals = {"a": 1, "b": 2}

@parse.with_pattern(r"[ab]")
def ab(text):
Expand All @@ -96,7 +96,7 @@ def parse_unit(text):
def parse_number(text):
return int(text)

type_converters = dict(Number=parse_number, Unit=parse_unit)
type_converters = {"Number": parse_number, "Unit": parse_unit}
# -- CASE: Unnamed-params (affected)
parser = parse.Parser("test {:Unit}-{:Number}", type_converters)
assert_fixed_match(parser, "test meter-10", ("meter", 10))
Expand Down Expand Up @@ -132,15 +132,15 @@ def parse_number(text):
]
for bad_regex_group_count, error_class in BAD_REGEX_GROUP_COUNTS_AND_ERRORS:
parse_unit.regex_group_count = bad_regex_group_count # -- OVERRIDE-HERE
type_converters = dict(Number=parse_number, Unit=parse_unit)
type_converters = {"Number": parse_number, "Unit": parse_unit}
parser = parse.Parser("test {:Unit}-{:Number}", type_converters)
with pytest.raises(error_class):
parser.parse("test meter-10")


def test_with_pattern_and_regex_group_count_is_none():
# -- CORNER-CASE: Increase code-coverage.
data_values = dict(a=1, b=2)
data_values = {"a": 1, "b": 2}

@parse.with_pattern(r"[ab]")
def parse_data(text):
Expand Down
34 changes: 17 additions & 17 deletions tests/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ def _(fmt, matches):
assert d.get(k) == matches[k]

for t in "%obxegfdDwWsS":
_(t, dict(type=t))
_("10" + t, dict(type=t, width="10"))
_("05d", dict(type="d", width="5", zero=True))
_("<", dict(align="<"))
_(".<", dict(align="<", fill="."))
_(">", dict(align=">"))
_(".>", dict(align=">", fill="."))
_("^", dict(align="^"))
_(".^", dict(align="^", fill="."))
_("x=d", dict(type="d", align="=", fill="x"))
_("d", dict(type="d"))
_("ti", dict(type="ti"))
_("spam", dict(type="spam"))

_(".^010d", dict(type="d", width="10", align="^", fill=".", zero=True))
_(".2f", dict(type="f", precision="2"))
_("10.2f", dict(type="f", width="10", precision="2"))
_(t, {"type": t})
_("10" + t, {"type": t, "width": "10"})
_("05d", {"type": "d", "width": "5", "zero": True})
_("<", {"align": "<"})
_(".<", {"align": "<", "fill": "."})
_(">", {"align": ">"})
_(".>", {"align": ">", "fill": "."})
_("^", {"align": "^"})
_(".^", {"align": "^", "fill": "."})
_("x=d", {"type": "d", "align": "=", "fill": "x"})
_("d", {"type": "d"})
_("ti", {"type": "ti"})
_("spam", {"type": "spam"})

_(".^010d", {"type": "d", "width": "10", "align": "^", "fill": ".", "zero": True})
_(".2f", {"type": "f", "precision": "2"})
_("10.2f", {"type": "f", "width": "10", "precision": "2"})


def test_dot_separated_fields():
Expand Down

0 comments on commit e8818d8

Please sign in to comment.