<s> """<STR_LIT>""" <EOL> import datetime <EOL> from google . appengine . ext import testbed <EOL> from ndb . model import * <EOL> class Flexidate ( object ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , date , fuzz = <NUM_LIT:1> ) : <EOL> obj = super ( Flexidate , cls ) . __new__ ( cls ) <EOL> assert isinstance ( date , datetime . date ) , repr ( date ) <EOL> assert not isinstance ( date , datetime . datetime ) , repr ( date ) <EOL> assert isinstance ( fuzz , ( int , long ) ) , repr ( fuzz ) <EOL> assert fuzz >= <NUM_LIT:1> , repr ( fuzz ) <EOL> obj . __date = date <EOL> obj . __fuzz = fuzz <EOL> return obj <EOL> def __repr__ ( self ) : <EOL> if self . __fuzz == <NUM_LIT:1> : <EOL> return '<STR_LIT>' % ( self . __date . year , self . __date . month , <EOL> self . __date . day ) <EOL> else : <EOL> return '<STR_LIT>' % ( self . __date . year , self . __date . month , <EOL> self . __date . day , self . __fuzz ) <EOL> @ property <EOL> def start ( self ) : <EOL> return self . __date <EOL> @ property <EOL> def fuzz ( self ) : <EOL> return self . __fuzz <EOL> def end ( self ) : <EOL> return self . __date + datetime . timedelta ( days = self . __fuzz - <NUM_LIT:1> ) <EOL> def __eq__ ( self , other ) : <EOL> if not isinstance ( other , Flexidate ) : <EOL> return NotImplemented <EOL> return self . __date == other . __date and self . __fuzz == other . __fuzz <EOL> def __ne__ ( self , other ) : <EOL> eq = self . __eq__ ( other ) <EOL> if isinstance ( eq , bool ) : <EOL> eq = not eq <EOL> return eq <EOL> class CustomProperty ( StructuredProperty ) : <EOL> """<STR_LIT>""" <EOL> MISSING = object ( ) <EOL> def __init__ ( self , <EOL> construct , <EOL> attributes , <EOL> name = None , <EOL> repeated = False , <EOL> indexed = True , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> assert construct is not None <EOL> assert attributes is not None <EOL> self . _construct = construct <EOL> if isinstance ( attributes , dict ) : <EOL> pass <EOL> elif isinstance ( attributes , ( list , tuple , set , frozenset ) ) : <EOL> attributes = dict ( zip ( attributes , attributes ) ) <EOL> else : <EOL> assert False , type ( attributes ) <EOL> self . _attrmap = { } <EOL> classdict = { } <EOL> for key , value in attributes . iteritems ( ) : <EOL> if isinstance ( value , basestring ) : <EOL> prop = GenericProperty ( ) <EOL> self . _attrmap [ key ] = ( prop , None ) <EOL> classdict [ key ] = prop <EOL> elif isinstance ( value , Property ) : <EOL> self . _attrmap [ key ] = ( value , None ) <EOL> classdict [ key ] = value <EOL> elif isinstance ( value , tuple ) : <EOL> assert len ( value ) == <NUM_LIT:2> , repr ( value ) <EOL> prop , func = value <EOL> assert isinstance ( prop , Property ) , repr ( prop ) <EOL> assert callable ( func ) , repr ( func ) <EOL> self . _attrmap [ key ] = value <EOL> classdict [ key ] = prop <EOL> else : <EOL> assert False , repr ( value ) <EOL> modelclass = MetaModel ( '<STR_LIT>' , ( Model , ) , classdict ) <EOL> super ( CustomProperty , self ) . __init__ ( modelclass , <EOL> name , <EOL> repeated = repeated , <EOL> indexed = indexed , <EOL> ) <EOL> def _to_base_type ( self , value ) : <EOL> assert not isinstance ( value , self . _modelclass ) , repr ( value ) <EOL> if not isinstance ( value , self . _modelclass ) : <EOL> newvalue = self . _modelclass ( ) <EOL> for attrname , ( prop , func ) in self . _attrmap . iteritems ( ) : <EOL> if func is None : <EOL> attrval = getattr ( value , attrname , self . MISSING ) <EOL> else : <EOL> attrval = func ( value ) <EOL> if attrval is not self . MISSING : <EOL> setattr ( newvalue , attrname , attrval ) <EOL> value = newvalue <EOL> return value <EOL> def _from_base_type ( self , value ) : <EOL> assert isinstance ( value , self . _modelclass ) , repr ( value ) <EOL> if isinstance ( value , self . _modelclass ) : <EOL> value = self . _construct ( value ) <EOL> return value <EOL> class FlexidateProperty ( CustomProperty ) : <EOL> def __init__ ( self , name = None , repeated = False , indexed = True ) : <EOL> return super ( FlexidateProperty , self ) . __init__ ( <EOL> construct = lambda ent : Flexidate ( ent . start , ent . fuzz ) , <EOL> attributes = { '<STR_LIT:start>' : DateProperty ( ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:end>' : ( DateProperty ( ) , lambda fd : fd . end ( ) ) } , <EOL> name = name , <EOL> repeated = repeated , <EOL> indexed = indexed , <EOL> ) <EOL> def __repr__ ( self ) : <EOL> return ( '<STR_LIT>' % <EOL> ( self . _name , self . _repeated , self . _indexed ) ) <EOL> def _validate ( self , value ) : <EOL> if not isinstance ( value , Flexidate ) : <EOL> raise TypeError ( '<STR_LIT>' % ( value , ) ) <EOL> class Actor ( Model ) : <EOL> name = StringProperty ( ) <EOL> born = FlexidateProperty ( ) <EOL> events = FlexidateProperty ( repeated = True ) <EOL> def main ( ) : <EOL> tb = testbed . Testbed ( ) <EOL> tb . activate ( ) <EOL> tb . init_datastore_v3_stub ( ) <EOL> tb . init_memcache_stub ( ) <EOL> print Actor . name <EOL> print Actor . born <EOL> print Actor . born . start <EOL> print Actor . born . fuzz <EOL> print Actor . born . end <EOL> a = Actor ( name = '<STR_LIT>' ) <EOL> a . born = Flexidate ( datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) , <NUM_LIT> ) <EOL> print '<STR_LIT>' , a <EOL> pb = a . _to_pb ( ) <EOL> b = Actor . _from_pb ( pb ) <EOL> b . key = None <EOL> print '<STR_LIT>' , b <EOL> assert a == b , ( a , b ) <EOL> a . put ( ) <EOL> b . name = '<STR_LIT>' <EOL> b . born = Flexidate ( datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) , <NUM_LIT> ) <EOL> b . events = [ a . born , b . born ] <EOL> b . put ( ) <EOL> print b <EOL> q = Actor . query ( Actor . born . start == datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> print '<STR_LIT>' , q <EOL> for i , res in enumerate ( q ) : <EOL> print '<STR_LIT>' % ( i , res ) <EOL> q = Actor . query ( Actor . born == Flexidate ( datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) , <NUM_LIT> ) ) <EOL> print '<STR_LIT>' , q <EOL> for i , res in enumerate ( q ) : <EOL> print '<STR_LIT>' % ( i , res ) <EOL> q = Actor . query ( Actor . born . fuzz >= <NUM_LIT> ) <EOL> print '<STR_LIT>' , q <EOL> for i , res in enumerate ( q ) : <EOL> print '<STR_LIT>' % ( i , res ) <EOL> q = Actor . query ( Actor . events . fuzz == <NUM_LIT> ) <EOL> print '<STR_LIT>' , q <EOL> for i , res in enumerate ( q ) : <EOL> print '<STR_LIT>' % ( i , res ) <EOL> tb . deactivate ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
