Menu

Post image 1
Post image 2
1 / 2
0

Emergent Abstractions

DEV Community·Aaron Maxwell·25 days ago
#xNQexpxC
Reading 0:00
15s threshold

Here's an interesting class, from a program I wrote: from datetime import ( date , MINYEAR , MAXYEAR , timedelta , ) class DateInterval : BEGINNING_OF_TIME = date ( MINYEAR , 1 , 1 ) END_OF_TIME = date ( MAXYEAR , 12 , 31 ) def __init__ ( self , start = None , end = None ): if start is None : start = self . BEGINNING_OF_TIME if end is None : end = self . END_OF_TIME if start > end : raise ValueError ( f " Start date { start } must not be after end date { end } " ) self . start = start self . end = end @classmethod def all ( cls ): return cls ( cls . BEGINNING_OF_TIME , cls . END_OF_TIME ) def __contains__ ( self , when ): return self . start <= when <= self . end def __iter__ ( self ): num_days = 1 + ( self . end - self . start ). days for offset in range ( num_days ): yield self .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More