Last active
July 24, 2016 19:17
-
-
Save ekaitz-zarraga/cd41480a169c47458934e5bf71605f67 to your computer and use it in GitHub Desktop.
Python iterator tests
This file contains hidden or 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
""" | |
Copyright 2016 Ekaitz Zarraga <[email protected]> | |
This work is free. You can redistribute it and/or modify it under the | |
terms of the Do What The Fuck You Want To Public License, Version | |
as published by Sam Hocevar. See http://www.wtfpl.net/ for more details. | |
""" | |
from datetime import timedelta | |
from datetime import date | |
class dateFileSystemIterator: | |
""" | |
Stupid but nice class to iterate over YYYY/MM/DD filesystems or similar. | |
""" | |
def __init__( self, start = date.today(), end = date.today(), days_step = 1, separator = '/'): | |
self.start = start | |
self.current = start | |
self.end = end | |
self.separator = separator | |
self.step = timedelta( days = days_step ) | |
def __iter__( self ): | |
return self | |
def __next__( self ): # Python3 compatibility | |
return self.next() | |
def next( self ): | |
if self.current >= self.end: | |
raise StopIteration | |
else: | |
self.current += self.step | |
datestring = self.current - self.step | |
datestring = datestring.strftime("%Y"+self.separator+"%m"+self.separator+"%d") | |
return datestring | |
def __str__( self ): | |
out = self.current - self.step | |
tostring = lambda x: x.strftime("%Y"+self.separator+"%m"+self.separator+"%d") | |
return "<dateFileSystemIterator: <Current: " + tostring(self.current) + ">" \ | |
+ ",<Start: " + tostring(self.start) + ">" \ | |
+ ",<End: " + tostring(self.end) + ">" \ | |
+ ",<Step: " + str(self.step) + ">" | |
def __repr__( self ): | |
return self.__str__() | |
if __name__ == '__main__': | |
it=dateFileSystemIterator(start = date.today() - timedelta(days=30)) | |
print it | |
for i in it: | |
print i |
This file contains hidden or 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
""" | |
Copyright 2016 Ekaitz Zarraga <[email protected]> | |
This work is free. You can redistribute it and/or modify it under the | |
terms of the Do What The Fuck You Want To Public License, Version | |
as published by Sam Hocevar. See http://www.wtfpl.net/ for more details. | |
""" | |
""" | |
Yield style version. Similar but shorter | |
""" | |
from datetime import datetime, timedelta | |
def iterate_dates( date_start, date_end=datetime.today(), separator='/'): | |
date = date_start | |
while date < date_end: | |
yield date.strftime('%Y'+separator+'%m'+separator+'%d') | |
date += timedelta(days=1) | |
if __name__ == '__main__': | |
for a in iterate_dates( datetime.today() - timedelta(days=10) ): | |
print a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment