* Cédric Krier [2013-07-15 14:49 +0200]:
On 15/07/13 10:25 +0300, Giedrius Slavinskas wrote:
2013/7/15 Guillem Barba Domingo <guillemba...@gmail.com>

>
> El 15/07/2013 2:16, "Oscar Alvarez" <oscar.alvarez.mont...@gmail.com> 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)

And we could use __mult__ on the product.uom so that multiplying one
by a scalar would create a Measure.

So in the end we would have something like:

    (line.quantity * line.unit).convert_to(kg)

So appart from the useless use of slots and the namedtuple I find it
better than my previous proposition.

--
Nicolas Évrard

B2CK SPRL
4, rue de Rotterdam
4000 Liège
Belgium
Tel: +32 472 54 46 59
E-mail/Jabber: nicolas.evr...@b2ck.com
Website: http://www.b2ck.com/

Attachment: signature.asc
Description: Digital signature

Reply via email to