On Mon, Jul 15, 2013 at 8:05 PM, Cédric Krier <cedric.kr...@b2ck.com> wrote:

> On 15/07/13 19:34 +0530, Sharoon Thomas wrote:
> > On Mon, Jul 15, 2013 at 6:19 PM, Cédric Krier <cedric.kr...@b2ck.com>
> wrote:
> >
> > > On 15/07/13 10:25 +0300, Giedrius Slavinskas wrote:
> > > 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)
> >
> >
> > Nice, perhaps we could implement other magic methods in Measure class
> > making comparisons and operators also work ? More like the python
> > package Udo mentioned.
>
>
> But what will be the result of:
>
>     Measure(10, gr) + Measure(1, kg)
>
> Or we must restrict operators to measures with the same unit.


Restricting operations to similar units is an option, but that would
only affect when we try to emulate Measure instances as numeric
types [1]. We could still implement the comparison operators even
with dissimilar units [2]  but of the same category.


[1]
http://docs.python.org/2/reference/datamodel.html#emulating-numeric-types
[2] http://docs.python.org/2/reference/datamodel.html#object.__lt__

Thanks,

-- 
--
Sharoon Thomas
Openlabs Technologies & Consulting (P) Limited

w: http://www.openlabs.co.in
m: +1 813.793.6736 (OPEN) Extn. 200
t: @sharoonthomas

- We CARE for our customers

Reply via email to