[Python-ideas] Set starting point for itertools.product()
storchaka at gmail.com
Thu Oct 25 06:22:00 EDT 2018
25.10.18 09:31, Ronie Martinez пише:
> Here is an example:
> import itertools
> import time
> def main():
> datetime_odometer = itertools.product(
> range(2018,10_000),# year
> range(1,13),# month
> range(1,31),# days
> range(0,24),# hours
> range(0,60),# minutes
> range(0,60)# seconds
> datetime_of_interest = (2050,6,15,10,5,0)
> for iin datetime_odometer:
> if i == datetime_of_interest:# target start time
> if __name__ =='__main__':
> start = time.time()
> duration = time.time() - start
> print(duration,'seconds')# 91.9426908493042 seconds
> It took 92 seconds to get to the target start time. It does not only
> apply to datetimes but for other purposes that uses "odometer-like"
> I don't have any propose solution for now, but I guess adding this
> feature within itertools will come in handy.
Thank you for clarification. Now I understand your idea.
For datetimes it is better to use the datetime classes:
delta = timedelta(microseconds=1)
dt = datetime(2050,6,15,10,5,0)
dt += delta
Note that in your example you missed 31th days, but iterate 29th and
See also the calendar module which provides date range iterators
(although not with microsecond precision).
Currently for general "odometer-like" patterns you can use the
undocumented __setstate__ method of itertools.product. But this is on
your own risk.
More information about the Python-ideas