class BoatHelper(mincepy.TypeHelper): TYPE_ID = uuid.UUID('4d82b67a-dbcb-4388-b20e-8542c70491d1') TYPE = Boat # Describe how to store the properties make = mincepy.field() length = mincepy.field() owner = mincepy.field(ref=True)
class Person(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('d60ca740-9fa6-4002-83f6-e4c91403e41b') name = mincepy.field() age = mincepy.field() car = mincepy.field(ref=True) def __init__(self, name, age, car=None): super().__init__() self.name = name self.age = age self.car = car
class Car(mincepy.ConvenientSavable): TYPE_ID = bson.ObjectId('5e075d6244572f823ed93274') colour = mincepy.field() make = mincepy.field() def __init__(self, make='ferrari', colour='red'): super().__init__() self.make = make self.colour = colour def __str__(self) -> str: return f'{self.colour} {self.make}'
class Person(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('d60ca740-9fa6-4002-83f6-e4c91403e41b') name = mincepy.field() age = mincepy.field() car = mincepy.field(ref=True) def __init__(self, name, age, car=None): super().__init__() self.name = name self.age = age self.car = car def __repr__(self): return f'{self.__class__.__name__}({repr(self.name)}, {repr(self.age)}, {repr(self.car)})'
class V1(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('8b1620f6-dd6d-4d39-b8b1-4433dc2a54df') ref = mincepy.field() def __init__(self, obj): super().__init__() self.ref = obj
class StoreByValue(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('40377bfc-901c-48bb-a85c-1dd692cddcae') ref = mincepy.field() def __init__(self, ref): super().__init__() self.ref = ref
class File(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('8d645bb8-4657-455b-8b61-8613bc8a0acf') path = mincepy.field() def __init__(self, path): super().__init__() self.path = path
class Person(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('692429b6-a08b-489a-aa09-6eb3174b6405') car = mincepy.field(ref=True) # Save the car by reference def __init__(self, car): super().__init__() self.car = car
class CarV0(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('297808e4-9bc7-4f0a-9f8d-850a5f558663') colour = mincepy.field() make = mincepy.field() def __init__(self, colour: str, make: str): super().__init__() self.colour = colour self.make = make def save_instance_state(self, saver: mincepy.Saver): super().save_instance_state(saver) # Here, I decide to store as an array return [self.colour, self.make] def load_instance_state(self, saved_state, loader: mincepy.Loader): self.colour = saved_state[0] self.make = saved_state[1]
class CarV1(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('297808e4-9bc7-4f0a-9f8d-850a5f558663') colour = mincepy.field() make = mincepy.field() class V0toV1(mincepy.ObjectMigration): VERSION = 1 @classmethod def upgrade(cls, saved_state, loader: 'mincepy.Loader') -> dict: return dict(colour=saved_state[0], make=saved_state[1]) # Set the migration LATEST_MIGRATION = V0toV1 def __init__(self, colour: str, make: str): super().__init__() self.colour = colour self.make = make
class V3(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('40377bfc-901c-48bb-a85c-1dd692cddcae') ref = mincepy.field(ref=True) description = mincepy.field() class Migration(mincepy.ObjectMigration): VERSION = 2 PREVIOUS = StoreByRef.ToRefMigration @classmethod def upgrade(cls, saved_state, loader: 'mincepy.Loader'): saved_state['description'] = None LATEST_MIGRATION = Migration def __init__(self, ref): super().__init__() self.ref = ref self.description = None
class Powerboat(Boat): TYPE_ID = uuid.UUID('924ef5b2-ce20-40b0-8c98-4da470f6c2c3') horsepower = mincepy.field() def __init__(self, make: str, length: float, horsepower: float, owner: testing.Person = None): super().__init__(make, length, owner) self.horsepower = horsepower
class Garage(mincepy.ConvenientSavable): TYPE_ID = bson.ObjectId('5e07b40a44572f823ed9327b') car = mincepy.field() def __init__(self, car=None): super().__init__() self.car = car def save_instance_state(self, saver: mincepy.Saver): state = super().save_instance_state(saver) return state
class V3(mincepy.ConvenientSavable): TYPE_ID = StoreByRef.TYPE_ID ref = mincepy.field(ref=True) description = mincepy.field() class Migration(mincepy.ObjectMigration): VERSION = 2 PREVIOUS = StoreByRef.ToRefMigration @classmethod def upgrade(cls, saved_state, loader: 'mincepy.Loader'): saved_state['description'] = None return saved_state LATEST_MIGRATION = Migration def __init__(self, ref): super().__init__() self.ref = ref self.description = None
class CarDelegate(mincepy.SimpleSavable): TYPE_ID = uuid.UUID('c0148a43-c0c0-4d2b-9262-ed1c8c6ab2fc') car = mincepy.field() def __init__(self, car): super().__init__() self.car = car def save_instance_state(self, saver): return {'car': self.car} def load_instance_state(self, saved_state, loader): self.car = saved_state['car']
class V2(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('8b1620f6-dd6d-4d39-b8b1-4433dc2a54df') ref = mincepy.field(ref=True) class V1toV2(mincepy.ObjectMigration): VERSION = 1 @classmethod def upgrade(cls, saved_state, loader: 'mincepy.Loader'): # Create a reference to the live car object saved_state['ref'] = mincepy.ObjRef(car) return saved_state LATEST_MIGRATION = V1toV2
class CarV2(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('297808e4-9bc7-4f0a-9f8d-850a5f558663') colour = mincepy.field() make = mincepy.field() reg = mincepy.field() # New attribute class V1toV2(mincepy.ObjectMigration): VERSION = 2 PREVIOUS = CarV1.V0toV1 @classmethod def upgrade(cls, saved_state, loader: 'mincepy.Loader') -> dict: # Augment the saved state saved_state['reg'] = 'unknown' return saved_state # Set the migration LATEST_MIGRATION = V1toV2 def __init__(self, colour: str, make: str, reg=None): super().__init__() self.colour = colour self.make = make self.reg = reg
class A(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('a50f21bc-899e-445f-baf7-0a1a373e51fc') migrations = mincepy.field() class Migration(mincepy.ObjectMigration): VERSION = 11 @classmethod def upgrade(cls, saved_state, loader: 'mincepy.Loader'): saved_state['migrations'].append('A V11') LATEST_MIGRATION = Migration def __init__(self): super().__init__() self.migrations = []
class StoreByRef(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('40377bfc-901c-48bb-a85c-1dd692cddcae') ref = mincepy.field(ref=True) class ToRefMigration(mincepy.ObjectMigration): VERSION = 1 @classmethod def upgrade(cls, saved_state, loader: 'mincepy.Loader') -> dict: # Replace the value stored version with a reference saved_state['ref'] = mincepy.ObjRef(saved_state['ref']) return saved_state # Changed my mind, want to store by value now LATEST_MIGRATION = ToRefMigration def __init__(self, ref): super().__init__() self.ref = ref
class Cycle(mincepy.ConvenientSavable): TYPE_ID = uuid.UUID('600fb6ae-684c-4f8e-bed3-47ae06739d29') _ref = mincepy.field() def __init__(self, ref=None): super().__init__() self._ref = mincepy.ObjRef(ref) @property def ref(self): return self._ref() @ref.setter def ref(self, value): self._ref = mincepy.ObjRef(value) def __eq__(self, other): return self.ref is other.ref def yield_hashables(self, hasher): yield from hasher.yield_hashables(id(self.ref))
class Image(fields.WithFields): width = fields.field() height = fields.field() _fmt = mincepy.field(store_as='format') def __init__(self, width: int, height: int, fmt: str, creator=None): super().__init__() self.width = width self.height = height self._fmt = fmt self._creator = creator def area(self): return self.width * self.height @mincepy.field(store_as='_creator') def creator(self): return self._creator @creator.setter def creator(self, value): self._creator = value
class PowerboatHelper(BoatHelper): TYPE_ID = uuid.UUID('924ef5b2-ce20-40b0-8c98-4da470f6c2c3') TYPE = Powerboat horsepower = mincepy.field()
class BirdHelper(mincepy.TypeHelper): TYPE = Bird TYPE_ID = uuid.UUID('5cc59e03-ea5d-43ff-8814-3b6f2e22cd76') specie = mincepy.field()