Skip to content

Commit 9999838

Browse files
wakemaster39Cameron Hurst
andauthored
fix: Perioid comparision with other classes and time delta and deprecation warnings (#427)
* fix: Perioid comparision with other classes and time delta * fix: cleanup deprecation warnings for regular expression escapes Co-authored-by: Cameron Hurst <cahurst@cisco.com>
1 parent 9356312 commit 9999838

File tree

7 files changed

+76
-64
lines changed

7 files changed

+76
-64
lines changed

pendulum/formatting/formatter.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@
1111
from pendulum.utils._compat import decode
1212

1313

14-
_MATCH_1 = "\d"
15-
_MATCH_2 = "\d\d"
16-
_MATCH_3 = "\d{3}"
17-
_MATCH_4 = "\d{4}"
18-
_MATCH_6 = "[+-]?\d{6}"
19-
_MATCH_1_TO_2 = "\d\d?"
20-
_MATCH_1_TO_2_LEFT_PAD = "[0-9 ]\d?"
21-
_MATCH_1_TO_3 = "\d{1,3}"
22-
_MATCH_1_TO_4 = "\d{1,4}"
23-
_MATCH_1_TO_6 = "[+-]?\d{1,6}"
24-
_MATCH_3_TO_4 = "\d{3}\d?"
25-
_MATCH_5_TO_6 = "\d{5}\d?"
26-
_MATCH_UNSIGNED = "\d+"
27-
_MATCH_SIGNED = "[+-]?\d+"
28-
_MATCH_OFFSET = "[Zz]|[+-]\d\d:?\d\d"
29-
_MATCH_SHORT_OFFSET = "[Zz]|[+-]\d\d(?::?\d\d)?"
30-
_MATCH_TIMESTAMP = "[+-]?\d+(\.\d{1,6})?"
14+
_MATCH_1 = r"\d"
15+
_MATCH_2 = r"\d\d"
16+
_MATCH_3 = r"\d{3}"
17+
_MATCH_4 = r"\d{4}"
18+
_MATCH_6 = r"[+-]?\d{6}"
19+
_MATCH_1_TO_2 = r"\d\d?"
20+
_MATCH_1_TO_2_LEFT_PAD = r"[0-9 ]\d?"
21+
_MATCH_1_TO_3 = r"\d{1,3}"
22+
_MATCH_1_TO_4 = r"\d{1,4}"
23+
_MATCH_1_TO_6 = r"[+-]?\d{1,6}"
24+
_MATCH_3_TO_4 = r"\d{3}\d?"
25+
_MATCH_5_TO_6 = r"\d{5}\d?"
26+
_MATCH_UNSIGNED = r"\d+"
27+
_MATCH_SIGNED = r"[+-]?\d+"
28+
_MATCH_OFFSET = r"[Zz]|[+-]\d\d:?\d\d"
29+
_MATCH_SHORT_OFFSET = r"[Zz]|[+-]\d\d(?::?\d\d)?"
30+
_MATCH_TIMESTAMP = r"[+-]?\d+(\.\d{1,6})?"
3131
_MATCH_WORD = (
3232
"(?i)[0-9]*"
3333
"['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+"
34-
"|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}"
34+
r"|[\u0600-\u06FF/]+(\s*?[\u0600-\u06FF]+){1,2}"
3535
)
3636
_MATCH_TIMEZONE = "[A-za-z0-9-+]+(/[A-Za-z0-9-+_]+)?"
3737

3838

3939
class Formatter:
4040

4141
_TOKENS = (
42-
"\[([^\[]*)\]|\\\(.)|"
42+
r"\[([^\[]*)\]|\\(.)|"
4343
"("
4444
"Mo|MM?M?M?"
4545
"|Do|DDDo|DD?D?D?|ddd?d?|do?"
@@ -67,7 +67,7 @@ class Formatter:
6767
"Mo": None,
6868
"DDDo": None,
6969
"Do": lambda locale: tuple(
70-
"\d+{}".format(o) for o in locale.get("custom.ordinal").values()
70+
r"\d+{}".format(o) for o in locale.get("custom.ordinal").values()
7171
),
7272
"dddd": "days.wide",
7373
"ddd": "days.abbreviated",
@@ -615,7 +615,7 @@ def _get_parsed_locale_value(
615615
unit = "month"
616616
match = "months.abbreviated"
617617
elif token == "Do":
618-
parsed["day"] = int(re.match("(\d+)", value).group(1))
618+
parsed["day"] = int(re.match(r"(\d+)", value).group(1))
619619

620620
return
621621
elif token == "dddd":

pendulum/parsing/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@
2424
"^"
2525
"(?P<date>"
2626
" (?P<classic>" # Classic date (YYYY-MM-DD)
27-
" (?P<year>\d{4})" # Year
27+
r" (?P<year>\d{4})" # Year
2828
" (?P<monthday>"
29-
" (?P<monthsep>[/:])?(?P<month>\d{2})" # Month (optional)
30-
" ((?P<daysep>[/:])?(?P<day>\d{2}))" # Day (optional)
29+
r" (?P<monthsep>[/:])?(?P<month>\d{2})" # Month (optional)
30+
r" ((?P<daysep>[/:])?(?P<day>\d{2}))" # Day (optional)
3131
" )?"
3232
" )"
3333
")?"
3434
# Time (optional)
3535
"(?P<time>"
36-
" (?P<timesep>\ )?" # Separator (space)
37-
" (?P<hour>\d{1,2}):(?P<minute>\d{1,2})?(?::(?P<second>\d{1,2}))?" # HH:mm:ss (optional mm and ss)
36+
r" (?P<timesep>\ )?" # Separator (space)
37+
r" (?P<hour>\d{1,2}):(?P<minute>\d{1,2})?(?::(?P<second>\d{1,2}))?" # HH:mm:ss (optional mm and ss)
3838
# Subsecond part (optional)
3939
" (?P<subsecondsection>"
4040
" (?:[.|,])" # Subsecond separator (optional)
41-
" (?P<subsecond>\d{1,9})" # Subsecond
41+
r" (?P<subsecond>\d{1,9})" # Subsecond
4242
" )?"
4343
")?"
4444
"$",

pendulum/parsing/iso8601.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,34 @@
2020
"^"
2121
"(?P<date>"
2222
" (?P<classic>" # Classic date (YYYY-MM-DD) or ordinal (YYYY-DDD)
23-
" (?P<year>\d{4})" # Year
23+
r" (?P<year>\d{4})" # Year
2424
" (?P<monthday>"
25-
" (?P<monthsep>-)?(?P<month>\d{2})" # Month (optional)
26-
" ((?P<daysep>-)?(?P<day>\d{1,2}))?" # Day (optional)
25+
r" (?P<monthsep>-)?(?P<month>\d{2})" # Month (optional)
26+
r" ((?P<daysep>-)?(?P<day>\d{1,2}))?" # Day (optional)
2727
" )?"
2828
" )"
2929
" |"
3030
" (?P<isocalendar>" # Calendar date (2016-W05 or 2016-W05-5)
31-
" (?P<isoyear>\d{4})" # Year
31+
r" (?P<isoyear>\d{4})" # Year
3232
" (?P<weeksep>-)?" # Separator (optional)
3333
" W" # W separator
34-
" (?P<isoweek>\d{2})" # Week number
34+
r" (?P<isoweek>\d{2})" # Week number
3535
" (?P<weekdaysep>-)?" # Separator (optional)
36-
" (?P<isoweekday>\d)?" # Weekday (optional)
36+
r" (?P<isoweekday>\d)?" # Weekday (optional)
3737
" )"
3838
")?"
3939
# Time (optional)
4040
"(?P<time>"
41-
" (?P<timesep>[T\ ])?" # Separator (T or space)
42-
" (?P<hour>\d{1,2})(?P<minsep>:)?(?P<minute>\d{1,2})?(?P<secsep>:)?(?P<second>\d{1,2})?" # HH:mm:ss (optional mm and ss)
41+
r" (?P<timesep>[T\ ])?" # Separator (T or space)
42+
r" (?P<hour>\d{1,2})(?P<minsep>:)?(?P<minute>\d{1,2})?(?P<secsep>:)?(?P<second>\d{1,2})?" # HH:mm:ss (optional mm and ss)
4343
# Subsecond part (optional)
4444
" (?P<subsecondsection>"
4545
" (?:[.,])" # Subsecond separator (optional)
46-
" (?P<subsecond>\d{1,9})" # Subsecond
46+
r" (?P<subsecond>\d{1,9})" # Subsecond
4747
" )?"
4848
# Timezone offset
4949
" (?P<tz>"
50-
" (?:[-+])\d{2}:?(?:\d{2})?|Z" # Offset (+HH:mm or +HHmm or +HH or Z)
50+
r" (?:[-+])\d{2}:?(?:\d{2})?|Z" # Offset (+HH:mm or +HHmm or +HH or Z)
5151
" )?"
5252
")?"
5353
"$",
@@ -59,18 +59,18 @@
5959
"^P" # Duration P indicator
6060
# Years, months and days (optional)
6161
"(?P<w>"
62-
" (?P<weeks>\d+(?:[.,]\d+)?W)"
62+
r" (?P<weeks>\d+(?:[.,]\d+)?W)"
6363
")?"
6464
"(?P<ymd>"
65-
" (?P<years>\d+(?:[.,]\d+)?Y)?"
66-
" (?P<months>\d+(?:[.,]\d+)?M)?"
67-
" (?P<days>\d+(?:[.,]\d+)?D)?"
65+
r" (?P<years>\d+(?:[.,]\d+)?Y)?"
66+
r" (?P<months>\d+(?:[.,]\d+)?M)?"
67+
r" (?P<days>\d+(?:[.,]\d+)?D)?"
6868
")?"
6969
"(?P<hms>"
7070
" (?P<timesep>T)" # Separator (T)
71-
" (?P<hours>\d+(?:[.,]\d+)?H)?"
72-
" (?P<minutes>\d+(?:[.,]\d+)?M)?"
73-
" (?P<seconds>\d+(?:[.,]\d+)?S)?"
71+
r" (?P<hours>\d+(?:[.,]\d+)?H)?"
72+
r" (?P<minutes>\d+(?:[.,]\d+)?M)?"
73+
r" (?P<seconds>\d+(?:[.,]\d+)?S)?"
7474
")?"
7575
"$",
7676
re.VERBOSE,

pendulum/period.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,11 @@ def __hash__(self):
372372
return hash((self.start, self.end, self._absolute))
373373

374374
def __eq__(self, other):
375-
return (self.start, self.end, self._absolute) == (
376-
other.start,
377-
other.end,
378-
other._absolute,
379-
)
375+
if isinstance(other, Period):
376+
return (self.start, self.end, self._absolute) == (
377+
other.start,
378+
other.end,
379+
other._absolute,
380+
)
381+
else:
382+
return self.as_interval() == other

pendulum/tz/local_timezone.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ def _get_unix_timezone(_root="/"): # type: (str) -> Timezone
177177
# OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and
178178
# Gentoo has a TIMEZONE setting in /etc/conf.d/clock
179179
# We look through these files for a timezone:
180-
zone_re = re.compile('\s*ZONE\s*=\s*"')
181-
timezone_re = re.compile('\s*TIMEZONE\s*=\s*"')
180+
zone_re = re.compile(r'\s*ZONE\s*=\s*"')
181+
timezone_re = re.compile(r'\s*TIMEZONE\s*=\s*"')
182182
end_re = re.compile('"')
183183

184184
for filename in ("etc/sysconfig/clock", "etc/conf.d/clock"):

pendulum/tz/zoneinfo/posix_timezone.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212

1313
_spec = re.compile(
1414
"^"
15-
"(?P<std_abbr><.*?>|[^-+,\d]{3,})"
16-
"(?P<std_offset>([+-])?(\d{1,2})(:\d{2}(:\d{2})?)?)"
17-
"(?P<dst_info>"
18-
" (?P<dst_abbr><.*?>|[^-+,\d]{3,})"
19-
" (?P<dst_offset>([+-])?(\d{1,2})(:\d{2}(:\d{2})?)?)?"
20-
")?"
21-
"(?:,(?P<rules>"
22-
" (?P<dst_start>"
23-
" (?:J\d+|\d+|M\d{1,2}.\d.[0-6])"
24-
" (?:/(?P<dst_start_offset>([+-])?(\d+)(:\d{2}(:\d{2})?)?))?"
15+
r"(?P<std_abbr><.*?>|[^-+,\d]{3,})"
16+
r"(?P<std_offset>([+-])?(\d{1,2})(:\d{2}(:\d{2})?)?)"
17+
r"(?P<dst_info>"
18+
r" (?P<dst_abbr><.*?>|[^-+,\d]{3,})"
19+
r" (?P<dst_offset>([+-])?(\d{1,2})(:\d{2}(:\d{2})?)?)?"
20+
r")?"
21+
r"(?:,(?P<rules>"
22+
r" (?P<dst_start>"
23+
r" (?:J\d+|\d+|M\d{1,2}.\d.[0-6])"
24+
r" (?:/(?P<dst_start_offset>([+-])?(\d+)(:\d{2}(:\d{2})?)?))?"
2525
" )"
2626
" ,"
27-
" (?P<dst_end>"
28-
" (?:J\d+|\d+|M\d{1,2}.\d.[0-6])"
29-
" (?:/(?P<dst_end_offset>([+-])?(\d+)(:\d{2}(:\d{2})?)?))?"
27+
r" (?P<dst_end>"
28+
r" (?:J\d+|\d+|M\d{1,2}.\d.[0-6])"
29+
r" (?:/(?P<dst_end_offset>([+-])?(\d+)(:\d{2}(:\d{2})?)?))?"
3030
" )"
3131
"))?"
3232
"$",

tests/period/test_behavior.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ def test_comparison_to_timedelta():
4040
period = dt2 - dt1
4141

4242
assert period < timedelta(days=4)
43+
44+
45+
def test_equality_to_timedelta():
46+
dt1 = pendulum.datetime(2016, 11, 18)
47+
dt2 = pendulum.datetime(2016, 11, 20)
48+
49+
period = dt2 - dt1
50+
51+
assert period == timedelta(days=2)

0 commit comments

Comments
 (0)