Skip to content

Commit

Permalink
fix: rename period to interval in repr and tests (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benji19967 authored Oct 24, 2023
1 parent 18f9c0e commit 4abc087
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 100 deletions.
12 changes: 6 additions & 6 deletions src/pendulum/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str:
:param locale: The locale to use. Defaults to current locale.
:param separator: The separator to use between each unit
"""
periods = [
intervals = [
("year", self.years),
("month", self.months),
("week", self.weeks),
Expand All @@ -252,13 +252,13 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str:
loaded_locale = pendulum.locale(locale)

parts = []
for period in periods:
unit, period_count = period
if abs(period_count) > 0:
for interval in intervals:
unit, interval_count = interval
if abs(interval_count) > 0:
translation = loaded_locale.translation(
f"units.{unit}.{loaded_locale.plural(abs(period_count))}"
f"units.{unit}.{loaded_locale.plural(abs(interval_count))}"
)
parts.append(translation.format(period_count))
parts.append(translation.format(interval_count))

if not parts:
count: int | str = 0
Expand Down
24 changes: 13 additions & 11 deletions src/pendulum/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class Interval(Duration):
"""
A period of time between two datetimes.
An interval of time between two datetimes.
"""

@overload
Expand Down Expand Up @@ -61,7 +61,9 @@ def __new__(
or not isinstance(start, datetime)
and isinstance(end, datetime)
):
raise ValueError("Both start and end of a Period must have the same type")
raise ValueError(
"Both start and end of an Interval must have the same type"
)

if (
isinstance(start, datetime)
Expand Down Expand Up @@ -234,13 +236,13 @@ def end(self) -> pendulum.DateTime | pendulum.Date | datetime | date:

def in_years(self) -> int:
"""
Gives the duration of the Period in full years.
Gives the duration of the Interval in full years.
"""
return self.years

def in_months(self) -> int:
"""
Gives the duration of the Period in full months.
Gives the duration of the Interval in full months.
"""
return self.years * MONTHS_PER_YEAR + self.months

Expand All @@ -267,7 +269,7 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str:
"""
from pendulum.locales.locale import Locale

periods = [
intervals = [
("year", self.years),
("month", self.months),
("week", self.weeks),
Expand All @@ -278,13 +280,13 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str:
]
loaded_locale: Locale = Locale.load(locale or pendulum.get_locale())
parts = []
for period in periods:
unit, period_count = period
if abs(period_count) > 0:
for interval in intervals:
unit, interval_count = interval
if abs(interval_count) > 0:
translation = loaded_locale.translation(
f"units.{unit}.{loaded_locale.plural(abs(period_count))}"
f"units.{unit}.{loaded_locale.plural(abs(interval_count))}"
)
parts.append(translation.format(period_count))
parts.append(translation.format(interval_count))

if not parts:
count: str | int = 0
Expand Down Expand Up @@ -382,7 +384,7 @@ def __abs__(self) -> Self:
return self.__class__(self.start, self.end, absolute=True)

def __repr__(self) -> str:
return f"<Period [{self._start} -> {self._end}]>"
return f"<Interval [{self._start} -> {self._end}]>"

def __str__(self) -> str:
return self.__repr__()
Expand Down
6 changes: 3 additions & 3 deletions tests/datetime/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ def test_add_interval():
assert_datetime(new, 2017, 3, 12, 11, 45)


def test_period_over_midnight_tz():
def test_interval_over_midnight_tz():
start = pendulum.datetime(2018, 2, 25, tz="Europe/Paris")
end = start.add(hours=1)
period = end - start
new_end = start + period
interval = end - start
new_end = start + interval

assert new_end == end
20 changes: 10 additions & 10 deletions tests/interval/test_add_subtract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@
def test_dst_add():
start = pendulum.datetime(2017, 3, 7, tz="America/Toronto")
end = start.add(days=6)
period = end - start
new_end = start + period
interval = end - start
new_end = start + interval

assert new_end == end


def test_dst_add_non_variable_units():
start = pendulum.datetime(2013, 3, 31, 1, 30, tz="Europe/Paris")
end = start.add(hours=1)
period = end - start
new_end = start + period
interval = end - start
new_end = start + interval

assert new_end == end


def test_dst_subtract():
start = pendulum.datetime(2017, 3, 7, tz="America/Toronto")
end = start.add(days=6)
period = end - start
new_start = end - period
interval = end - start
new_start = end - interval

assert new_start == start


def test_naive_subtract():
start = pendulum.naive(2013, 3, 31, 1, 30)
end = start.add(hours=1)
period = end - start
new_end = start + period
interval = end - start
new_end = start + interval

assert new_end == end

Expand All @@ -43,7 +43,7 @@ def test_negative_difference_subtract():
start = pendulum.datetime(2018, 5, 28, 12, 34, 56, 123456)
end = pendulum.datetime(2018, 1, 1)

period = end - start
new_end = start + period
interval = end - start
new_end = start + interval

assert new_end == end
18 changes: 9 additions & 9 deletions tests/interval/test_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ def test_comparison_to_timedelta():
dt1 = pendulum.datetime(2016, 11, 18)
dt2 = pendulum.datetime(2016, 11, 20)

period = dt2 - dt1
interval = dt2 - dt1

assert period < timedelta(days=4)
assert interval < timedelta(days=4)


def test_equality_to_timedelta():
dt1 = pendulum.datetime(2016, 11, 18)
dt2 = pendulum.datetime(2016, 11, 20)

period = dt2 - dt1
interval = dt2 - dt1

assert period == timedelta(days=2)
assert interval == timedelta(days=2)


def test_inequality():
dt1 = pendulum.datetime(2016, 11, 18)
dt2 = pendulum.datetime(2016, 11, 20)
dt3 = pendulum.datetime(2016, 11, 22)

period1 = dt2 - dt1
period2 = dt3 - dt2
period3 = dt3 - dt1
interval1 = dt2 - dt1
interval2 = dt3 - dt2
interval3 = dt3 - dt1

assert period1 != period2
assert period1 != period3
assert interval1 != interval2
assert interval1 != interval3
28 changes: 14 additions & 14 deletions tests/interval/test_construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ def test_accuracy():
def test_dst_transition():
start = pendulum.datetime(2017, 3, 7, tz="America/Toronto")
end = start.add(days=6)
period = end - start
interval = end - start

assert period.days == 5
assert period.seconds == 82800
assert interval.days == 5
assert interval.seconds == 82800

assert period.remaining_days == 6
assert period.hours == 0
assert period.remaining_seconds == 0
assert interval.remaining_days == 6
assert interval.hours == 0
assert interval.remaining_seconds == 0

assert period.in_days() == 6
assert period.in_hours() == 5 * 24 + 23
assert interval.in_days() == 6
assert interval.in_hours() == 5 * 24 + 23


def test_timedelta_behavior():
Expand All @@ -108,14 +108,14 @@ def test_timedelta_behavior():
def test_different_timezones_same_time():
dt1 = pendulum.datetime(2013, 3, 31, 1, 30, tz="Europe/Paris")
dt2 = pendulum.datetime(2013, 4, 1, 1, 30, tz="Europe/Paris")
period = dt2 - dt1
interval = dt2 - dt1

assert period.in_words() == "1 day"
assert period.in_hours() == 23
assert interval.in_words() == "1 day"
assert interval.in_hours() == 23

dt1 = pendulum.datetime(2013, 3, 31, 1, 30, tz="Europe/Paris")
dt2 = pendulum.datetime(2013, 4, 1, 1, 30, tz="America/Toronto")
period = dt2 - dt1
interval = dt2 - dt1

assert period.in_words() == "1 day 5 hours"
assert period.in_hours() == 29
assert interval.in_words() == "1 day 5 hours"
assert interval.in_hours() == 29
20 changes: 10 additions & 10 deletions tests/interval/test_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import pendulum


def test_periods_with_same_duration_and_different_dates():
def test_intervals_with_same_duration_and_different_dates():
day1 = pendulum.DateTime(2018, 1, 1)
day2 = pendulum.DateTime(2018, 1, 2)
day3 = pendulum.DateTime(2018, 1, 2)

period1 = day2 - day1
period2 = day3 - day2
interval1 = day2 - day1
interval2 = day3 - day2

assert period1 != period2
assert len({period1, period2}) == 2
assert interval1 != interval2
assert len({interval1, interval2}) == 2


def test_periods_with_same_dates():
period1 = pendulum.DateTime(2018, 1, 2) - pendulum.DateTime(2018, 1, 1)
period2 = pendulum.DateTime(2018, 1, 2) - pendulum.DateTime(2018, 1, 1)
def test_intervals_with_same_dates():
interval1 = pendulum.DateTime(2018, 1, 2) - pendulum.DateTime(2018, 1, 1)
interval2 = pendulum.DateTime(2018, 1, 2) - pendulum.DateTime(2018, 1, 1)

assert period1 == period2
assert len({period1, period2}) == 1
assert interval1 == interval2
assert len({interval1, interval2}) == 1
38 changes: 19 additions & 19 deletions tests/interval/test_in_words.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,66 @@

def test_week():
start_date = pendulum.datetime(2012, 1, 1)
period = pendulum.interval(start=start_date, end=start_date.add(weeks=1))
assert period.in_words() == "1 week"
interval = pendulum.interval(start=start_date, end=start_date.add(weeks=1))
assert interval.in_words() == "1 week"


def test_week_and_day():
start_date = pendulum.datetime(2012, 1, 1)
period = pendulum.interval(start=start_date, end=start_date.add(weeks=1, days=1))
assert period.in_words() == "1 week 1 day"
interval = pendulum.interval(start=start_date, end=start_date.add(weeks=1, days=1))
assert interval.in_words() == "1 week 1 day"


def test_all():
start_date = pendulum.datetime(2012, 1, 1)
period = pendulum.interval(
interval = pendulum.interval(
start=start_date,
end=start_date.add(years=1, months=1, days=1, seconds=1, microseconds=1),
)
assert period.in_words() == "1 year 1 month 1 day 1 second"
assert interval.in_words() == "1 year 1 month 1 day 1 second"


def test_in_french():
start_date = pendulum.datetime(2012, 1, 1)
period = pendulum.interval(
interval = pendulum.interval(
start=start_date,
end=start_date.add(years=1, months=1, days=1, seconds=1, microseconds=1),
)
assert period.in_words(locale="fr") == "1 an 1 mois 1 jour 1 seconde"
assert interval.in_words(locale="fr") == "1 an 1 mois 1 jour 1 seconde"


def test_singular_negative_values():
start_date = pendulum.datetime(2012, 1, 1)
period = pendulum.interval(start=start_date, end=start_date.subtract(days=1))
assert period.in_words() == "-1 day"
interval = pendulum.interval(start=start_date, end=start_date.subtract(days=1))
assert interval.in_words() == "-1 day"


def test_separator():
start_date = pendulum.datetime(2012, 1, 1)
period = pendulum.interval(
interval = pendulum.interval(
start=start_date,
end=start_date.add(years=1, months=1, days=1, seconds=1, microseconds=1),
)
assert period.in_words(separator=", ") == "1 year, 1 month, 1 day, 1 second"
assert interval.in_words(separator=", ") == "1 year, 1 month, 1 day, 1 second"


def test_subseconds():
start_date = pendulum.datetime(2012, 1, 1)
period = pendulum.interval(
interval = pendulum.interval(
start=start_date, end=start_date.add(microseconds=123456)
)
assert period.in_words() == "0.12 second"
assert interval.in_words() == "0.12 second"


def test_subseconds_with_seconds():
start_date = pendulum.datetime(2012, 1, 1)
period = pendulum.interval(
interval = pendulum.interval(
start=start_date, end=start_date.add(seconds=12, microseconds=123456)
)
assert period.in_words() == "12 seconds"
assert interval.in_words() == "12 seconds"


def test_zero_period():
def test_zero_interval():
start_date = pendulum.datetime(2012, 1, 1)
period = pendulum.interval(start=start_date, end=start_date)
assert period.in_words() == "0 microseconds"
interval = pendulum.interval(start=start_date, end=start_date)
assert interval.in_words() == "0 microseconds"
Loading

0 comments on commit 4abc087

Please sign in to comment.