On 15/07/13 10:25 +0300, Giedrius Slavinskas wrote: > 2013/7/15 Guillem Barba Domingo <[email protected]> > > > > > El 15/07/2013 2:16, "Oscar Alvarez" <[email protected]> va > > escriure: > > > > > > > > El 14/07/13 17:20, Cédric Krier escribió: > > > > > >> Hi, > > >> > > >> I would like to submit this poll for this change: > > >> > > >> http://codereview.tryton.org/969002/#msg9 > > >> > > >> > > >> kg.convert(1000, gr) == 1 > > > > The above one is more logical and more object-oriented approach as you > provide the quantity (units and units of measurement) to the method convert. > > It would be even more logical if we would have quantity as object and we > could pass it as a single argument: > > class Quantity: > def __init__(self, units, uom): > self.units = units > self.uom = uom > > Unfortunatily, there is no such a infrastructure on tryton's orm, so the > best we can do is to pass the quantity by two arguments.
So let's introduce it.
class Measure(namedtuple('Measure', ['quantity', 'unit'])):
__slots__ = ()
def convert_to(self, uom, round=True):
Uom = Pool().get('product.uom')
return Measure(Uom.compute_qty(self.unit, self.quantity, uom,
round=round), uom)
Which will make:
Measure(1000, gr).convert_to(kg) == Measure(1, kg)
--
Cédric Krier
B2CK SPRL
Rue de Rotterdam, 4
4000 Liège
Belgium
Tel: +32 472 54 46 59
Email/Jabber: [email protected]
Website: http://www.b2ck.com/
pgpFBXwX5yVt3.pgp
Description: PGP signature
