# -*- coding: utf-8 -*- import yaml import datetime class Figure(object): def __init__(self, value, unit): self.value = value self.unit = unit def get_value(self): return self._value def set_value(self, value): self._value = value """ use property() instead of decorator to enable overriding """ value = property(get_value, set_value) def get_unit(self): return self._unit def set_unit(self, unit): assert type(unit) is unicode self._unit = unit """ use property() instead of decorator to enable overriding """ unit = property(get_unit, set_unit) class Distance(Figure): def __init__(self, value, unit): assert type(value) is float super(Distance, self).__init__(value, unit) @property def metres(self): if self.unit == 'km': return self.value * 1000 else: raise Exception() class Sum(Figure): def __init__(self, value, currency): super(Sum, self).__init__(value, currency) def get_value(self): return super(Sum, self).get_value() def set_value(self, value): assert type(value) is float super(Sum, self).set_value(value) """ use property() instead of decorator to enable overriding """ value = property(get_value, set_value) @property def currency(self): return self.unit def get_unit(self): return super(Sum, self).get_unit() def set_unit(self, currency): if currency == u'€': currency = u'EUR' assert type(currency) is unicode assert currency in [u'EUR'] super(Sum, self).set_unit(currency) """ use property() instead of decorator to enable overriding """ unit = property(get_unit, set_unit) class Discount(object): def __init__( self, name = None, amount = None, ): assert type(name) is unicode self.name = name assert type(amount) is Sum assert amount.value >= 0 self.amount = amount def dict_repr(self): return { 'name': self.name, 'value': self.amount.value, 'value_currency': self.amount.currency, } yaml.SafeDumper.add_representer(Discount, lambda dumper, discount: dumper.represent_dict(discount.dict_repr())) class Order(object): def __init__(self, platform, order_id, order_date, customer_id = None): assert type(platform) is unicode self.platform = platform assert type(order_id) is unicode self.order_id = order_id if type(order_date) is datetime.datetime: order_date = order_date.date() assert type(order_date) is datetime.date self.order_date = order_date assert customer_id is None or type(customer_id) is unicode self.customer_id = customer_id self.items = [] self.discounts = [] def dict_repr(self): return {k: v for (k, v) in { 'articles': self.items, 'customer_id': self.customer_id, 'discounts': self.discounts, 'order_date': self.order_date.strftime('%Y-%m-%d'), 'order_id': self.order_id, 'platform': self.platform, }.items() if v is not None} yaml.SafeDumper.add_representer(Order, lambda dumper, order: dumper.represent_dict(order.dict_repr())) class Item(object): def __init__( self, name = None, price_brutto = None, ): assert type(name) is unicode self.name = name assert type(price_brutto) is Sum self.price_brutto = price_brutto def dict_repr(self): return { 'name': self.name, 'price_brutto': self.price_brutto.value, 'price_brutto_currency': self.price_brutto.currency, } yaml.SafeDumper.add_representer(Item, lambda dumper, item: dumper.represent_dict(item.dict_repr())) class Article(Item): def __init__( self, quantity = None, authors = [], state = None, reseller = None, shipper = None, **kwargs ): super(Article, self).__init__(**kwargs) assert type(quantity) is int self.quantity = quantity assert type(authors) is list self.authors = authors assert state is None or type(state) is unicode self.state = state assert reseller is None or type(reseller) is unicode self.reseller = reseller assert shipper is None or type(shipper) is unicode self.shipper = shipper self.delivery_date = None def dict_repr(self): attr = super(Article, self).dict_repr() attr.update({ 'delivery_date': self.delivery_date, 'quantity': self.quantity, 'reseller': self.reseller, 'shipper': self.shipper, 'state': self.state, }) if len(self.authors) > 0: attr['authors'] = self.authors return attr yaml.SafeDumper.add_representer(Article, lambda dumper, article: dumper.represent_dict(article.dict_repr())) class Transportation(Item): def __init__( self, departure_point = None, destination_point = None, distance = None, route_map = None, **kwargs ): super(Transportation, self).__init__(**kwargs) assert type(departure_point) is unicode self.departure_point = departure_point assert type(destination_point) is unicode self.destination_point = destination_point assert distance is None or type(distance) is Distance self.distance = distance assert route_map is None or type(route_map) is str self.route_map = route_map def dict_repr(self): attr = super(Transportation, self).dict_repr() attr.update({ 'departure_point': self.departure_point, 'destination_point': self.destination_point, 'distance_metres': self.distance.metres if self.distance else None, 'route_map': self.route_map, }) return attr yaml.SafeDumper.add_representer(Transportation, lambda dumper, transportation: dumper.represent_dict(transportation.dict_repr())) class TaxiRide(Transportation): def __init__(self, name = None, driver = None, arrival_time = None, departure_time = None, **kwargs): if name is None: name = u'Taxi Ride' super(TaxiRide, self).__init__(name = name, **kwargs) assert type(driver) is unicode self.driver = driver assert arrival_time is None or type(arrival_time) is datetime.datetime self.arrival_time = arrival_time assert departure_time is None or type(departure_time) is datetime.datetime self.departure_time = departure_time def dict_repr(self): attr = super(TaxiRide, self).dict_repr() attr.update({ 'arrival_time': self.arrival_time.strftime('%Y-%m-%d %H:%M') if self.arrival_time else None, 'departure_time': self.departure_time.strftime('%Y-%m-%d %H:%M') if self.departure_time else None, 'driver': self.driver, }) return attr yaml.SafeDumper.add_representer(TaxiRide, lambda dumper, taxi_ride: dumper.represent_dict(taxi_ride.dict_repr()))