From: I V on
On Wed, 02 Jun 2010 05:17:11 -0700, B.V. wrote:
> But trying to be open to other languages, the server implements also an
> XMLRPC interface (and also a JSONRPC-like interface). That's the key
> point: Decimal is python specific. So in an application, you can't rely
> on the value received from a client, because depending on the protocol,
> the type of the value is different. So the idea was to create a class
> that can behave like a Decimal or a float depending on the context, and
> set xmlrpclib.Unmarshaller.dispatch["double"] to a function that return
> a Float instance.

Looking at the Tryton docs, it seems that it already supports field types
that can't be directly represented in XMLRPC or JSON, like BigInteger or
Selection. How are these serialized over the non-python RPC mechanisms?
Could you not do the same for Decimals?

> A contributor filed an issue on the bug tracker (https://
> bugs.tryton.org/roundup/issue1575) and because he's a nice guy (ok it's
> a friend of mine), he made a patch proposal (http://
> codereview.appspot.com/1387041). The end of the story is in the comments
> of the proposal.
>
>
>> > But we also need to do:
>> > isinstance(Float('1'), float) == True isinstance(Float('1'), Decimal)
>> > == True
>>
>> Can you explain why you need this?
>
> It's a requirement of the project leader.
>
>
>> Should isinstance(Float('1.1'), float) and isinstance(Float('1.1'),
>> Decimal) also both be true, or would only one of those be true?  (And
>> by the way, what value would Float('1.1') have?  float('1.1') and
>> Decimal('1.1') are different values.)
>
> I think they both should be True, for '1', '1.1', '0', '0.1', ... For
> the value, I would say that it depends of the definition of the field
> (fields.Float or fields.Numeric).
>
>
>
>> I don't think your approach can succeed;  I'd suggest just subclassing
>> 'object' and abandoning the 'isinstance' requirements.  Or perhaps
>> creating a subclass of Decimal that interacts nicely with floats.  You
>> might also want to investigate the numbers ABC, though that's new in
>> Python 2.6.
>
> First, Float implementation was a subclass of Decimal that works with
> floats, and solves many (maybe all) problems. But as you may read in the
> comments of the patch proposal, it seems to be not enough.
>
> B.

From: Nathan Rice on
My apologies if someone already mentioned this and I missed it but...

class.__instancecheck__(self, instance) - Return true if instance
should be considered a (direct or indirect) instance of class. If
defined, called to implement isinstance(instance, class).

class.__subclasscheck__(self, subclass) - Return true if subclass
should be considered a (direct or indirect) subclass of class. If
defined, called to implement issubclass(subclass, class).

Nathan

On Wed, Jun 2, 2010 at 4:24 AM, B.V. <bv.tryton(a)gmail.com> wrote:
> Hi,
>
> In order to solve some issues due to operations between Decimal and
> float, we wanted to implement a class that inherits from both float
> and Decimal.
>
> Typically, we wrote:
> class Float(Decimal, float):
> ...
>
> This can not be achieved because of a TypeError exception (with
> message "multiple bases have instance lay-out conflict").
>
> With a class that inherits from Decimal, with overridden __add__,
> __mul__, .... , we succeed to solve operations issues.
>
> But we also need to do:
> isinstance(Float('1'), float) == True
> isinstance(Float('1'), Decimal) == True
> which is, AFAIK, only possible with Float(Decimal, float).
>
> Is there a workaround ?
>
> We are developping with python version 2.5 and 2.6.
>
> Thanks for your help.
>
> B.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
From: Steven D'Aprano on
On Wed, 02 Jun 2010 17:17:11 -0400, Nathan Rice wrote:

> My apologies if someone already mentioned this and I missed it but...
>
> class.__instancecheck__(self, instance) - Return true if instance should
> be considered a (direct or indirect) instance of class. If defined,
> called to implement isinstance(instance, class).
>
> class.__subclasscheck__(self, subclass) - Return true if subclass should
> be considered a (direct or indirect) subclass of class. If defined,
> called to implement issubclass(subclass, class).

The original poster needs to support Python 2.5 and 2.6, but
__instancecheck__ and __subclasscheck__ are only supported in 2.6 or
higher, so this doesn't help.



--
Steven
From: Chris Rebert on
On Wed, Jun 2, 2010 at 4:11 PM, Steven D'Aprano
<steve-REMOVE-THIS(a)cybersource.com.au> wrote:
> On Wed, 02 Jun 2010 17:17:11 -0400, Nathan Rice wrote:
>> My apologies if someone already mentioned this and I missed it but...
>>
>> class.__instancecheck__(self, instance) - Return true if instance should
>> be considered a (direct or indirect) instance of class. If defined,
>> called to implement isinstance(instance, class).
>>
>> class.__subclasscheck__(self, subclass) - Return true if subclass should
>> be considered a (direct or indirect) subclass of class. If defined,
>> called to implement issubclass(subclass, class).
>
> The original poster needs to support Python 2.5 and 2.6, but
> __instancecheck__ and __subclasscheck__ are only supported in 2.6 or
> higher, so this doesn't help.

Even in 2.6+, good luck trying to define new methods on class `type`
(the metaclass of float and Decimal).

Cheers,
Chris
--
http://blog.rebertia.com
From: B.V. on
On Jun 3, 2:00 am, Chris Rebert <c...(a)rebertia.com> wrote:
> On Wed, Jun 2, 2010 at 4:11 PM, Steven D'Aprano
>
> <steve-REMOVE-T...(a)cybersource.com.au> wrote:
> > On Wed, 02 Jun 2010 17:17:11 -0400, Nathan Rice wrote:
> >> My apologies if someone already mentioned this and I missed it but...
>
> >> class.__instancecheck__(self, instance) - Return true if instance should
> >> be considered a (direct or indirect) instance of class. If defined,
> >> called to implement isinstance(instance, class).
>
> >> class.__subclasscheck__(self, subclass) - Return true if subclass should
> >> be considered a (direct or indirect) subclass of class. If defined,
> >> called to implement issubclass(subclass, class).
>
> > The original poster needs to support Python 2.5 and 2.6, but
> > __instancecheck__ and __subclasscheck__ are only supported in 2.6 or
> > higher, so this doesn't help.
>
> Even in 2.6+, good luck trying to define new methods on class `type`
> (the metaclass of float and Decimal).
>
> Cheers,
> Chris
> --http://blog.rebertia.com

You mean it! I have spent two hours trying it, with no success. I
think we manage to find another solution to our problem.
Thank you for helping.

B.