Last active
March 15, 2020 23:24
-
-
Save frarteaga/5dbaa605a1e494b81c665a9d33496b40 to your computer and use it in GitHub Desktop.
Codes for the post https://binary-coffee.dev/post/refactorizando-codigo-duplicado-y-poder-ejecutar-pruebas-facilmente
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from datetime import date | |
from datetime import timedelta | |
def IsDate(value): | |
return type(value) is date | |
class DateRange: | |
""" | |
Iterator over every date in a range [date_a, date_b). If a predicate is defined | |
then iterate only over those dates thats evaluate filter(date) as True. Examples: | |
>>> list(DateRange(datetime.date(2019, 12, 30), datetime.date(2020, 1, 3))) | |
[datetime.date(2019, 12, 30), datetime.date(2019, 12, 31), datetime.date(2020, 1, 1), datetime.date(2020, 1, 2)] | |
>>> list(DateRange(datetime.date(2019, 12, 30), datetime.date(2020, 1, 3), lambda x: x.day % 2 == 0)) | |
[datetime.date(2019, 12, 30), datetime.date(2020, 1, 2)] | |
>>> IsPal = lambda date: str(date).replace('-', '') == str(date).replace('-', '')[: : -1] | |
>>> list(DateRange(datetime.date(2020, 1, 1), datetime.date(2020, 12, 31), IsPal)) | |
[datetime.date(2020, 2, 2)] | |
""" | |
__oneDay = timedelta(days=1) | |
def __init__(self, date_a, date_b, filter=None): | |
pass | |
if not IsDate(date_a) or not IsDate(date_b): | |
raise ValueError("Arguments must be of type datetime.date") | |
if date_a > date_b: | |
raise ValueError("The first date must be the same or before the second date") | |
self.__A = date_a | |
self.__B = date_b | |
self.__filter = filter | |
def __iter__(self): | |
return self | |
def __next__(self): | |
while not self._filter(self.__A): | |
self.__checkif_must_stop() | |
self.__A += DateRange.__oneDay | |
result = self.__A | |
self.__checkif_must_stop() | |
self.__A += DateRange.__oneDay | |
return result | |
def __checkif_must_stop(self): | |
if self.__A >= self.__B: | |
raise StopIteration() | |
def _filter(self, date): | |
if self.__filter is None: | |
return True | |
return self.__filter(date) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from datetime import date | |
from datetime import timedelta | |
__oneDay = timedelta(days=1) | |
def DateRange(date_a, date_b, filter=None): | |
""" | |
Iterator over every date in a range [date_a, date_b). If a predicate is defined | |
then iterate only over those dates thats evaluate filter(date) as True. Examples: | |
>>> list(DateRange(datetime.date(2019, 12, 30), datetime.date(2020, 1, 3))) | |
[datetime.date(2019, 12, 30), datetime.date(2019, 12, 31), datetime.date(2020, 1, 1), datetime.date(2020, 1, 2)] | |
>>> list(DateRange(datetime.date(2019, 12, 30), datetime.date(2020, 1, 3), lambda x: x.day % 2 == 0)) | |
[datetime.date(2019, 12, 30), datetime.date(2020, 1, 2)] | |
>>> IsPal = lambda date: str(date).replace('-', '') == str(date).replace('-', '')[: : -1] | |
>>> list(DateRange(datetime.date(2020, 1, 1), datetime.date(2020, 12, 31), IsPal)) | |
[datetime.date(2020, 2, 2)] | |
""" | |
_filter = lambda date: True | |
if filter is not None: | |
_filter = filter | |
while date_a < date_b: | |
if _filter(date_a): | |
yield date_a | |
date_a += __oneDay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://binary-coffee.dev/post/fechas-palindromos | |
from datetime import date | |
from datetime import timedelta | |
oneDay = timedelta(days=1) | |
def IsPalindromoDate(date): | |
strDate = str(date).replace('-', '') | |
return strDate == strDate[: : -1] | |
print('Past palindromes:') | |
while str(currentDate) != '1000-01-01': | |
if IsPalindromoDate(currentDate): | |
print(currentDate) | |
currentDate -= oneDay | |
currentDate = date.today() + oneDay | |
print('Future palindromes:') | |
while str(currentDate) != '2999-12-31': | |
if IsPalindromoDate(currentDate): | |
print(currentDate) | |
currentDate += oneDay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date | |
# Rename or make a copy of DateRange_iterator.py or DateRange_yield.py to the file DateRange.py | |
from DateRange import DateRange | |
def IsPalindromoDate(date): | |
strDate = str(date).replace('-', '') | |
return strDate == strDate[: : -1] | |
print('Past palindromes:') | |
for pd in DateRange(date(1000, 1, 1), date.today(), IsPalindromoDate): | |
print(pd) | |
print('Future palindromes:') | |
for pd in DateRange(date.today(), date(2999, 12, 31), IsPalindromoDate): | |
print(pd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment