1
0

__init__.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # -*- coding: utf-8 -*-
  2. import yaml
  3. import datetime
  4. class Figure(object):
  5. def __init__(self, value, unit):
  6. self.value = value
  7. self.unit = unit
  8. def get_value(self):
  9. return self._value
  10. def set_value(self, value):
  11. self._value = value
  12. """ use property() instead of decorator to enable overriding """
  13. value = property(get_value, set_value)
  14. def get_unit(self):
  15. return self._unit
  16. def set_unit(self, unit):
  17. assert type(unit) is unicode
  18. self._unit = unit
  19. """ use property() instead of decorator to enable overriding """
  20. unit = property(get_unit, set_unit)
  21. class Distance(Figure):
  22. def __init__(self, value, unit):
  23. assert type(value) is float
  24. super(Distance, self).__init__(value, unit)
  25. @property
  26. def metres(self):
  27. if self.unit == 'km':
  28. return self.value * 1000
  29. else:
  30. raise Exception()
  31. class Sum(Figure):
  32. def __init__(self, value, currency):
  33. super(Sum, self).__init__(value, currency)
  34. def get_value(self):
  35. return super(Sum, self).get_value()
  36. def set_value(self, value):
  37. assert type(value) is float
  38. super(Sum, self).set_value(value)
  39. """ use property() instead of decorator to enable overriding """
  40. value = property(get_value, set_value)
  41. @property
  42. def currency(self):
  43. return self.unit
  44. def get_unit(self):
  45. return super(Sum, self).get_unit()
  46. def set_unit(self, currency):
  47. if currency == u'€':
  48. currency = u'EUR'
  49. assert type(currency) is unicode
  50. assert currency in [u'EUR']
  51. super(Sum, self).set_unit(currency)
  52. """ use property() instead of decorator to enable overriding """
  53. unit = property(get_unit, set_unit)
  54. class Discount(object):
  55. def __init__(
  56. self,
  57. name = None,
  58. amount = None,
  59. ):
  60. assert type(name) is unicode
  61. self.name = name
  62. assert type(amount) is Sum
  63. assert amount.value >= 0
  64. self.amount = amount
  65. def dict_repr(self):
  66. return {
  67. 'name': self.name,
  68. 'value': self.amount.value,
  69. 'value_currency': self.amount.currency,
  70. }
  71. yaml.SafeDumper.add_representer(Discount, lambda dumper, discount: dumper.represent_dict(discount.dict_repr()))
  72. class Order(object):
  73. def __init__(self, platform, order_id, order_date, customer_id = None):
  74. assert type(platform) is unicode
  75. self.platform = platform
  76. assert type(order_id) is unicode
  77. self.order_id = order_id
  78. if type(order_date) is datetime.datetime:
  79. order_date = order_date.date()
  80. assert type(order_date) is datetime.date
  81. self.order_date = order_date
  82. assert customer_id is None or type(customer_id) is unicode
  83. self.customer_id = customer_id
  84. self.items = []
  85. self.discounts = []
  86. def dict_repr(self):
  87. return {k: v for (k, v) in {
  88. 'articles': self.items,
  89. 'customer_id': self.customer_id,
  90. 'discounts': self.discounts,
  91. 'order_date': self.order_date.strftime('%Y-%m-%d'),
  92. 'order_id': self.order_id,
  93. 'platform': self.platform,
  94. }.items() if v is not None}
  95. yaml.SafeDumper.add_representer(Order, lambda dumper, order: dumper.represent_dict(order.dict_repr()))
  96. class Item(object):
  97. def __init__(
  98. self,
  99. name = None,
  100. price_brutto = None,
  101. ):
  102. assert type(name) is unicode
  103. self.name = name
  104. assert type(price_brutto) is Sum
  105. self.price_brutto = price_brutto
  106. def dict_repr(self):
  107. return {
  108. 'name': self.name,
  109. 'price_brutto': self.price_brutto.value,
  110. 'price_brutto_currency': self.price_brutto.currency,
  111. }
  112. yaml.SafeDumper.add_representer(Item, lambda dumper, item: dumper.represent_dict(item.dict_repr()))
  113. class Article(Item):
  114. def __init__(
  115. self,
  116. quantity = None,
  117. authors = [],
  118. state = None,
  119. reseller = None,
  120. shipper = None,
  121. **kwargs
  122. ):
  123. super(Article, self).__init__(**kwargs)
  124. assert type(quantity) is int
  125. self.quantity = quantity
  126. assert type(authors) is list
  127. self.authors = authors
  128. assert state is None or type(state) is unicode
  129. self.state = state
  130. assert reseller is None or type(reseller) is unicode
  131. self.reseller = reseller
  132. assert shipper is None or type(shipper) is unicode
  133. self.shipper = shipper
  134. self.delivery_date = None
  135. def dict_repr(self):
  136. attr = super(Article, self).dict_repr()
  137. attr.update({
  138. 'delivery_date': self.delivery_date,
  139. 'quantity': self.quantity,
  140. 'reseller': self.reseller,
  141. 'shipper': self.shipper,
  142. 'state': self.state,
  143. })
  144. if len(self.authors) > 0:
  145. attr['authors'] = self.authors
  146. return attr
  147. yaml.SafeDumper.add_representer(Article, lambda dumper, article: dumper.represent_dict(article.dict_repr()))
  148. class Transportation(Item):
  149. def __init__(
  150. self,
  151. departure_point = None,
  152. destination_point = None,
  153. distance = None,
  154. route_map = None,
  155. **kwargs
  156. ):
  157. super(Transportation, self).__init__(**kwargs)
  158. assert type(departure_point) is unicode
  159. self.departure_point = departure_point
  160. assert type(destination_point) is unicode
  161. self.destination_point = destination_point
  162. assert distance is None or type(distance) is Distance
  163. self.distance = distance
  164. assert route_map is None or type(route_map) is str
  165. self.route_map = route_map
  166. def dict_repr(self):
  167. attr = super(Transportation, self).dict_repr()
  168. attr.update({
  169. 'departure_point': self.departure_point,
  170. 'destination_point': self.destination_point,
  171. 'distance_metres': self.distance.metres if self.distance else None,
  172. 'route_map': self.route_map,
  173. })
  174. return attr
  175. yaml.SafeDumper.add_representer(Transportation, lambda dumper, transportation: dumper.represent_dict(transportation.dict_repr()))
  176. class TaxiRide(Transportation):
  177. def __init__(self, name = None, driver = None, arrival_time = None, departure_time = None, **kwargs):
  178. if name is None:
  179. name = u'Taxi Ride'
  180. super(TaxiRide, self).__init__(name = name, **kwargs)
  181. assert type(driver) is unicode
  182. self.driver = driver
  183. assert arrival_time is None or type(arrival_time) is datetime.datetime
  184. self.arrival_time = arrival_time
  185. assert departure_time is None or type(departure_time) is datetime.datetime
  186. self.departure_time = departure_time
  187. def dict_repr(self):
  188. attr = super(TaxiRide, self).dict_repr()
  189. attr.update({
  190. 'arrival_time': self.arrival_time.strftime('%Y-%m-%d %H:%M') if self.arrival_time else None,
  191. 'departure_time': self.departure_time.strftime('%Y-%m-%d %H:%M') if self.departure_time else None,
  192. 'driver': self.driver,
  193. })
  194. return attr
  195. yaml.SafeDumper.add_representer(TaxiRide, lambda dumper, taxi_ride: dumper.represent_dict(taxi_ride.dict_repr()))