From cf7f70be8007992c3a541646aa7a2c0b9f5ad9de Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 05:04:50 +0000 Subject: [PATCH 1/4] Added _print method to PrettyTime class for pretty-printing time --- prettytime.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/prettytime.py b/prettytime.py index e192b74..890e223 100644 --- a/prettytime.py +++ b/prettytime.py @@ -110,6 +110,10 @@ def __init__(self, num=None): self.num = None self.today = datetime.datetime.today() + def _print(self): + time_units = [attr for attr in dir(self) if attr in self.TIME_LIST and getattr(self, attr) != 0] + return ', '.join(f'{getattr(self, unit)} {unit}' for unit in time_units) + def _make(self, timedict): return PrettyDelta(**timedict) From c7add6650bf0e1afd12a7caaa8c191eede056904 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 05:05:39 +0000 Subject: [PATCH 2/4] Added documentation for the ._print() method --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 0ce417b..4fd0f81 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,18 @@ datetime.date(2015, 2, 5) True ``` +## Pretty-Printing Time + +The `._print()` method can be used to get a string representation of the PrettyTime object in a human-readable format. This method returns a string that lists the time units (seconds, minutes, hours, days, weeks, months, years) that are not zero in the PrettyTime object. + +Example: + +```python +>>> from prettytime import t +>>> print(t(3).days._print()) +'3 days' +``` + ## Planned changes: + Add [`django-pretty-times`](https://pypi.python.org/pypi/django-pretty-times/0.1.0)-like functionality to allow pretty printing as well From df525c25458f38f89e53b2f29fa0262b97ab78e5 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 05:09:20 +0000 Subject: [PATCH 3/4] Updated _print() method to compare current time with PrettyTime object time --- prettytime.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/prettytime.py b/prettytime.py index 890e223..521f0e7 100644 --- a/prettytime.py +++ b/prettytime.py @@ -111,8 +111,12 @@ def __init__(self, num=None): self.today = datetime.datetime.today() def _print(self): - time_units = [attr for attr in dir(self) if attr in self.TIME_LIST and getattr(self, attr) != 0] - return ', '.join(f'{getattr(self, unit)} {unit}' for unit in time_units) + if datetime.datetime.today() < self.today: + return str(self.num) + " " + self.attr + " from now" + else: + return str(self.num) + " " + self.attr + " ago" + else: + return str(self.num) + " " + self.attr + " ago" def _make(self, timedict): return PrettyDelta(**timedict) From 8ec200a1693f6f40bcadd560e2b99d2c79a1925c Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 05:12:13 +0000 Subject: [PATCH 4/4] Removed duplicate line and added case for 'today' in _print() function --- prettytime.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prettytime.py b/prettytime.py index 521f0e7..f34780f 100644 --- a/prettytime.py +++ b/prettytime.py @@ -113,8 +113,8 @@ def __init__(self, num=None): def _print(self): if datetime.datetime.today() < self.today: return str(self.num) + " " + self.attr + " from now" - else: - return str(self.num) + " " + self.attr + " ago" + elif datetime.datetime.today() == self.today: + return "today" else: return str(self.num) + " " + self.attr + " ago"