<s> import abc <EOL> import datetime <EOL> from pyalgotrade . utils import dt <EOL> from pyalgotrade import bar <EOL> class TimeRange ( object ) : <EOL> __metaclass__ = abc . ABCMeta <EOL> @ abc . abstractmethod <EOL> def belongs ( self , dateTime ) : <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def getBeginning ( self ) : <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def getEnding ( self ) : <EOL> raise NotImplementedError ( ) <EOL> class IntraDayRange ( TimeRange ) : <EOL> def __init__ ( self , dateTime , frequency ) : <EOL> super ( IntraDayRange , self ) . __init__ ( ) <EOL> assert isinstance ( frequency , int ) <EOL> assert frequency > <NUM_LIT:1> <EOL> assert frequency < bar . Frequency . DAY <EOL> ts = int ( dt . datetime_to_timestamp ( dateTime ) ) <EOL> slot = int ( ts / frequency ) <EOL> slotTs = slot * frequency <EOL> self . __begin = dt . timestamp_to_datetime ( slotTs , not dt . datetime_is_naive ( dateTime ) ) <EOL> if not dt . datetime_is_naive ( dateTime ) : <EOL> self . __begin = dt . localize ( self . __begin , dateTime . tzinfo ) <EOL> self . __end = self . __begin + datetime . timedelta ( seconds = frequency ) <EOL> def belongs ( self , dateTime ) : <EOL> return dateTime >= self . __begin and dateTime < self . __end <EOL> def getBeginning ( self ) : <EOL> return self . __begin <EOL> def getEnding ( self ) : <EOL> return self . __end <EOL> class DayRange ( TimeRange ) : <EOL> def __init__ ( self , dateTime ) : <EOL> super ( DayRange , self ) . __init__ ( ) <EOL> self . __begin = datetime . datetime ( dateTime . year , dateTime . month , dateTime . day ) <EOL> if not dt . datetime_is_naive ( dateTime ) : <EOL> self . __begin = dt . localize ( self . __begin , dateTime . tzinfo ) <EOL> self . __end = self . __begin + datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> def belongs ( self , dateTime ) : <EOL> return dateTime >= self . __begin and dateTime < self . __end <EOL> def getBeginning ( self ) : <EOL> return self . __begin <EOL> def getEnding ( self ) : <EOL> return self . __end <EOL> class MonthRange ( TimeRange ) : <EOL> def __init__ ( self , dateTime ) : <EOL> super ( MonthRange , self ) . __init__ ( ) <EOL> self . __begin = datetime . datetime ( dateTime . year , dateTime . month , <NUM_LIT:1> ) <EOL> if dateTime . month == <NUM_LIT:12> : <EOL> self . __end = datetime . datetime ( dateTime . year + <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> else : <EOL> self . __end = datetime . datetime ( dateTime . year , dateTime . month + <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> if not dt . datetime_is_naive ( dateTime ) : <EOL> self . __begin = dt . localize ( self . __begin , dateTime . tzinfo ) <EOL> self . __end = dt . localize ( self . __end , dateTime . tzinfo ) <EOL> def belongs ( self , dateTime ) : <EOL> return dateTime >= self . __begin and dateTime < self . __end <EOL> def getBeginning ( self ) : <EOL> return self . __begin <EOL> def getEnding ( self ) : <EOL> return self . __end <EOL> def is_valid_frequency ( frequency ) : <EOL> assert ( isinstance ( frequency , int ) ) <EOL> assert ( frequency > <NUM_LIT:1> ) <EOL> if frequency < bar . Frequency . DAY : <EOL> ret = True <EOL> elif frequency == bar . Frequency . DAY : <EOL> ret = True <EOL> elif frequency == bar . Frequency . MONTH : <EOL> ret = True <EOL> else : <EOL> ret = False <EOL> return ret <EOL> def build_range ( dateTime , frequency ) : <EOL> assert ( isinstance ( frequency , int ) ) <EOL> assert ( frequency > <NUM_LIT:1> ) <EOL> if frequency < bar . Frequency . DAY : <EOL> ret = IntraDayRange ( dateTime , frequency ) <EOL> elif frequency == bar . Frequency . DAY : <EOL> ret = DayRange ( dateTime ) <EOL> elif frequency == bar . Frequency . MONTH : <EOL> ret = MonthRange ( dateTime ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return ret <EOL> class Grouper ( object ) : <EOL> __metaclass__ = abc . ABCMeta <EOL> def __init__ ( self , groupDateTime ) : <EOL> self . __groupDateTime = groupDateTime <EOL> def getDateTime ( self ) : <EOL> return self . __groupDateTime <EOL> @ abc . abstractmethod <EOL> def addValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def getGrouped ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) </s>
