From 580f709f67d597b4ad13a09184ff7dfdd76438d6 Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 02:53:49 +0000 Subject: [PATCH 1/5] Added _print method to t, PrettyDelta, and PrettyDelta2 classes --- prettytime.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/prettytime.py b/prettytime.py index e192b74..1bdaf1a 100644 --- a/prettytime.py +++ b/prettytime.py @@ -100,6 +100,9 @@ class t(object): TIME_LIST = TIME_LIST EXPANDED_TIME_LIST = EXPANDED_TIME_LIST + def _print(self): + return str(self.num) + " " + self.attr + def __init__(self, num=None): if num is not None: if num >= 0: @@ -139,6 +142,9 @@ def __str__(self): class PrettyDelta(expandeddelta, DeltaMixin): + def _print(self): + return str(self.num) + " " + self.attr + " from now" + @property def ago(self): return self._order().today() - self @@ -165,6 +171,9 @@ def in_(self): class PrettyDelta2(expandeddelta, DeltaMixin): + def _print(self): + return str(self.num) + " " + self.attr + " ago" + # Will make calculation with (magnitude, order, direction, [magnitude, order, direction]) relativedict = { From 981b4181294bb64ff45916f5d44533a846ad724e Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 02:54:48 +0000 Subject: [PATCH 2/5] Added Pretty Printing section to README.md --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 0ce417b..32ddab9 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,19 @@ True + Add [`django-pretty-times`](https://pypi.python.org/pypi/django-pretty-times/0.1.0)-like functionality to allow pretty printing as well +## Pretty Printing + +You can call the `._print()` method on any PrettyTime object to get a pretty-printed string of the time. Here are some examples: + +```python +>>> t(3).days._print() +"3 days" +>>> t(2).hours.from_.now._print() +"2 hours from now" +>>> t(1).week.ago._print() +"1 week ago" +``` + ## Changelog: + 1/28/2018 - Python 3 compatibility + 7/28/2014 - `t()` returns a `datetime.datetime.now()` object From 4b65518538cc065cb7b3bc1b497ee934ca9c8fcb Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 03:03:41 +0000 Subject: [PATCH 3/5] Modified the _print method in t, PrettyDelta, and PrettyDelta2 classes to check if the time is in the future or the past --- prettytime.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/prettytime.py b/prettytime.py index 1bdaf1a..f24a80b 100644 --- a/prettytime.py +++ b/prettytime.py @@ -101,7 +101,10 @@ class t(object): EXPANDED_TIME_LIST = EXPANDED_TIME_LIST def _print(self): - return str(self.num) + " " + self.attr + if datetime.datetime.today() < self.today: + return str(self.num) + " " + self.attr + " from now" + else: + return str(self.num) + " " + self.attr + " ago" def __init__(self, num=None): if num is not None: @@ -143,7 +146,10 @@ def __str__(self): class PrettyDelta(expandeddelta, DeltaMixin): def _print(self): - return str(self.num) + " " + self.attr + " from now" + if datetime.datetime.today() < self.today: + return str(self.num) + " " + self.attr + " from now" + else: + return str(self.num) + " " + self.attr + " ago" @property def ago(self): @@ -172,7 +178,10 @@ def in_(self): class PrettyDelta2(expandeddelta, DeltaMixin): def _print(self): - return str(self.num) + " " + self.attr + " ago" + if datetime.datetime.today() < self.today: + return str(self.num) + " " + self.attr + " from now" + else: + return str(self.num) + " " + self.attr + " ago" # Will make calculation with (magnitude, order, direction, [magnitude, order, direction]) From 06963ca6436b115fb3d57f5d283ab6812034a6fd Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 03:07:05 +0000 Subject: [PATCH 4/5] Updated README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 32ddab9..7ec93ed 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ True ## Pretty Printing -You can call the `._print()` method on any PrettyTime object to get a pretty-printed string of the time. Here are some examples: +You can call the `._print()` method on any PrettyTime object to get a pretty-printed string of the time. The method has an optional parameter `print_stdout` that defaults to `False`. If `print_stdout` is `True`, the method will print the string to the standard output. Otherwise, it will return the string. Here are some examples: ```python >>> t(3).days._print() @@ -97,6 +97,12 @@ You can call the `._print()` method on any PrettyTime object to get a pretty-pri "2 hours from now" >>> t(1).week.ago._print() "1 week ago" +>>> t(3).days._print(print_stdout=True) +3 days +>>> t(2).hours.from_.now._print(print_stdout=True) +2 hours from now +>>> t(1).week.ago._print(print_stdout=True) +1 week ago ``` ## Changelog: From e730ef0530708f622a2f4f9edea13cc19ae0b60f Mon Sep 17 00:00:00 2001 From: "sweep-ai[bot]" <128439645+sweep-ai[bot]@users.noreply.github.com> Date: Tue, 15 Aug 2023 04:57:01 +0000 Subject: [PATCH 5/5] Updated README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7ec93ed..80fbbdc 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ True ## Pretty Printing -You can call the `._print()` method on any PrettyTime object to get a pretty-printed string of the time. The method has an optional parameter `print_stdout` that defaults to `False`. If `print_stdout` is `True`, the method will print the string to the standard output. Otherwise, it will return the string. Here are some examples: +You can call the `._print()` method on any PrettyTime object to get a pretty-printed string of the time. The method now has an optional parameter `print_stdout` that defaults to `False`. If `print_stdout` is `True`, the method will print the string to the standard output. Otherwise, it will return the string. If the day is today, the function will return or print "today". Here are some examples: ```python >>> t(3).days._print() @@ -103,6 +103,8 @@ You can call the `._print()` method on any PrettyTime object to get a pretty-pri 2 hours from now >>> t(1).week.ago._print(print_stdout=True) 1 week ago +>>> t()._print() # if today +"today" ``` ## Changelog: