Skip to content

Commit

Permalink
uglify code with black
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Nov 22, 2023
1 parent acdfeb0 commit 5cc7111
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ To run the tests locally:
Changelog
---------

- 1.20.0 Added support for strptime codes (thanks @bendichter)
- 1.19.1 Added support for sign specifiers in number formats (thanks @anntzer)
- 1.19.0 Added slice access to fixed results (thanks @jonathangjertsen).
Also corrected matching of *full string* vs. *full line* (thanks @giladreti)
Expand Down
20 changes: 15 additions & 5 deletions parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ def date_convert(

dt_format_to_regex = {symbol: "[0-9]{2}" for symbol in "ymdIMSUW"}
dt_format_to_regex.update({"-" + symbol: "[0-9]{1,2}" for symbol in "ymdIMS"})

dt_format_to_regex.update(
{
"a": "(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)",
Expand All @@ -291,7 +290,9 @@ def date_convert(
)

# Compile a regular expression pattern that matches any date/time format symbol.
dt_format_symbols_re = re.compile('|'.join(re.escape("%{}".format(k)) for k in dt_format_to_regex.keys()))
dt_format_symbols_re = re.compile(
"|".join(re.escape("%{}".format(k)) for k in dt_format_to_regex)
)


def get_regex_for_datetime_format(format_):
Expand All @@ -305,7 +306,9 @@ def get_regex_for_datetime_format(format_):
str: A regex pattern corresponding to the datetime format string.
"""
# Replace all format symbols with their regex patterns.
return dt_format_symbols_re.sub(lambda m: dt_format_to_regex[m.group(0)[1:]], format_)
return dt_format_symbols_re.sub(
lambda m: dt_format_to_regex[m.group(0)[1:]], format_
)


class TooManyFields(ValueError):
Expand Down Expand Up @@ -363,7 +366,12 @@ def extract_format(format, extra_types):

# the rest is the type, if present
type = format
if type and type not in ALLOWED_TYPES and type not in extra_types and not any("%" + x in type for x in "YyHIMSf"):
if (
type
and type not in ALLOWED_TYPES
and type not in extra_types
and not any("%" + x in type for x in "YyHIMSf")
):
raise ValueError("format spec %r not recognised" % type)

return locals()
Expand Down Expand Up @@ -711,7 +719,9 @@ def _handle_field(self, field):
if "%y" in type or "%Y" in type:
self._type_conversions[group] = lambda x, _: datetime.strptime(x, type)
else:
self._type_conversions[group] = lambda x, _: datetime.strptime(x, type).time()
self._type_conversions[group] = lambda x, _: datetime.strptime(
x, type
).time()
elif type == "ti":
s = r"(\d{4}-\d\d-\d\d)((\s+|T)%s)?(Z|\s*[-+]\d\d:?\d\d)?" % TIME_PAT
n = self._group_index
Expand Down
28 changes: 17 additions & 11 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,26 +445,26 @@ def test_two_datetimes(self):
self.assertEqual(r[1], datetime(2012, 8, 1))

def test_flexible_datetimes(self):
r = parse.parse('a {:%Y-%m-%d} b', "a 1997-07-16 b")
r = parse.parse("a {:%Y-%m-%d} b", "a 1997-07-16 b")
self.assertEqual(len(r.fixed), 1)
self.assertEqual(r[0], datetime(1997, 7, 16))

r = parse.parse('a {:%Y-%b-%d} b', "a 1997-Feb-16 b")
r = parse.parse("a {:%Y-%b-%d} b", "a 1997-Feb-16 b")
self.assertEqual(len(r.fixed), 1)
self.assertEqual(r[0], datetime(1997, 2, 16))

r = parse.parse('a {:%Y-%b-%d} {:d} b', "a 1997-Feb-16 8 b")
r = parse.parse("a {:%Y-%b-%d} {:d} b", "a 1997-Feb-16 8 b")
self.assertEqual(len(r.fixed), 2)
self.assertEqual(r[0], datetime(1997, 2, 16))

r = parse.parse('a {my_date:%Y-%b-%d} {num:d} b', "a 1997-Feb-16 8 b")
r = parse.parse("a {my_date:%Y-%b-%d} {num:d} b", "a 1997-Feb-16 8 b")
self.assertEqual((r.named["my_date"]), datetime(1997, 2, 16))
self.assertEqual((r.named["num"]), 8)

r = parse.parse('a {:%Y-%B-%d} b', "a 1997-February-16 b")
r = parse.parse("a {:%Y-%B-%d} b", "a 1997-February-16 b")
self.assertEqual(r[0], datetime(1997, 2, 16))

r = parse.parse('a {:%Y%m%d} b', "a 19970716 b")
r = parse.parse("a {:%Y%m%d} b", "a 19970716 b")
self.assertEqual(r[0], datetime(1997, 7, 16))

def test_flexible_datetime_with_colon(self):
Expand All @@ -474,25 +474,31 @@ def test_flexible_datetime_with_colon(self):
@unittest.skipIf(sys.version_info[0] < 3, "Python 3+ required for timezone support")
def test_flexible_datetime_with_timezone(self):
from datetime import timezone

r = parse.parse("{dt:%Y-%m-%d %H:%M:%S %z}", "2023-11-21 13:23:27 +0000")
self.assertEqual(r.named["dt"], datetime(2023, 11, 21, 13, 23, 27, tzinfo=timezone.utc))
self.assertEqual(
r.named["dt"], datetime(2023, 11, 21, 13, 23, 27, tzinfo=timezone.utc)
)

@unittest.skipIf(sys.version_info[0] < 3, "Python 3+ required for timezone support")
def test_flexible_datetime_with_timezone_that_has_colons(self):
from datetime import timezone

r = parse.parse("{dt:%Y-%m-%d %H:%M:%S %z}", "2023-11-21 13:23:27 +00:00:00")
self.assertEqual(r.named["dt"], datetime(2023, 11, 21, 13, 23, 27, tzinfo=timezone.utc))
self.assertEqual(
r.named["dt"], datetime(2023, 11, 21, 13, 23, 27, tzinfo=timezone.utc)
)

def test_flexible_time(self):
r = parse.parse('a {time:%H:%M:%S} b', "a 13:23:27 b")
r = parse.parse("a {time:%H:%M:%S} b", "a 13:23:27 b")
self.assertEqual(r.named["time"], time(13, 23, 27))

def test_flexible_time_no_hour(self):
r = parse.parse('a {time:%M:%S} b', "a 23:27 b")
r = parse.parse("a {time:%M:%S} b", "a 23:27 b")
self.assertEqual(r.named["time"], time(0, 23, 27))

def test_flexible_time_ms(self):
r = parse.parse('a {time:%M:%S:%f} b', "a 23:27:123456 b")
r = parse.parse("a {time:%M:%S:%f} b", "a 23:27:123456 b")
self.assertEqual(r.named["time"], time(0, 23, 27, 123456))

def test_datetimes(self):
Expand Down

0 comments on commit 5cc7111

Please sign in to comment.