Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51593 > unrolled thread

OrderedEnum examples

Started byBas van der Wulp <Bas.vdWulp@gmail.com>
First post2013-07-30 20:18 +0200
Last post2013-07-31 00:30 +0200
Articles 6 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  OrderedEnum examples Bas van der Wulp <Bas.vdWulp@gmail.com> - 2013-07-30 20:18 +0200
    Re: OrderedEnum examples Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-30 12:58 -0600
    Re: OrderedEnum examples Ethan Furman <ethan@stoneleaf.us> - 2013-07-30 11:38 -0700
    Re: OrderedEnum examples Ethan Furman <ethan@stoneleaf.us> - 2013-07-30 12:16 -0700
    Re: OrderedEnum examples Ethan Furman <ethan@stoneleaf.us> - 2013-07-30 12:30 -0700
      Re: OrderedEnum examples Bas van der Wulp <Bas.vdWulp@gmail.com> - 2013-07-31 00:30 +0200

#51593 — OrderedEnum examples

FromBas van der Wulp <Bas.vdWulp@gmail.com>
Date2013-07-30 20:18 +0200
SubjectOrderedEnum examples
Message-ID<51f8036c$0$2995$6d4158fb@reader.xsnews.nl>
Using the enum34 0.9.13 package from PyPi in Python 2.7.3, the examples 
for OrderedEnum seem to be broken.

The example in the package documentation reads:

class OrderedEnum(Enum):
     def __ge__(self, other):
         if self.__class__ is other.__class__:
             return self._value >= other._value
         return NotImplemented
     def __gt__(self, other):
         if self.__class__ is other.__class__:
             return self._value > other._value
         return NotImplemented
     def __le__(self, other):
         if self.__class__ is other.__class__:
             return self._value <= other._value
         return NotImplemented
     def __lt__(self, other):
         if self.__class__ is other.__class__:
             return self._value < other._value
         return NotImplemented

class Grade(OrderedEnum):
     __ordered__ = 'A B C D F'
     A = 5
     B = 4
     C = 3
     D = 2
     F = 1

Grade.C < Grade.A

to which Python replies with:

Traceback (most recent call last):
   File "test.py", line 35, in <module>
     print Grade.C < Grade.A
   File "test.py", line 23, in __lt__
     return self._value < other._value
AttributeError: 'Grade' object has no attribute '_value'

Also, in the example in the Python 3.4 library documentation (section 
8.17.2) has the __ordered__ attribute removed (presumably because, in 
contrast to Python 2.x, Python 3 will respect the order of attribute 
definition). This example gives the same ValueErrror when using the 
enum34 package in Python 2.7.3. It is the same example, after all.

Replacing each occurrence of self._value with either self._value_ or 
self.value in the examples seems to make them work as expected.

Are both examples incorrect, or not intended to work in Python 2.x?

--
S. van der Wulp

[toc] | [next] | [standalone]


#51596

FromIan Kelly <ian.g.kelly@gmail.com>
Date2013-07-30 12:58 -0600
Message-ID<mailman.5335.1375210751.3114.python-list@python.org>
In reply to#51593
On Tue, Jul 30, 2013 at 12:18 PM, Bas van der Wulp <Bas.vdWulp@gmail.com> wrote:
> Replacing each occurrence of self._value with either self._value_ or
> self.value in the examples seems to make them work as expected.
>
> Are both examples incorrect, or not intended to work in Python 2.x?

The _value attribute was renamed _value_ in:

http://hg.python.org/cpython/rev/511c4daac102

It looks like the example wasn't updated to match.  You should
probably just use self.value here since the name of the private
attribute is an implementation detail.

[toc] | [prev] | [next] | [standalone]


#51597

FromEthan Furman <ethan@stoneleaf.us>
Date2013-07-30 11:38 -0700
Message-ID<mailman.5336.1375210974.3114.python-list@python.org>
In reply to#51593
On 07/30/2013 11:18 AM, Bas van der Wulp wrote:
> Using the enum34 0.9.13 package from PyPi in Python 2.7.3, the examples for OrderedEnum seem to be broken.

Thanks for catching that, I'll get it fixed asap.


> Also, in the example in the Python 3.4 library documentation (section 8.17.2) has the __ordered__ attribute removed
> (presumably because, in contrast to Python 2.x, Python 3 will respect the order of attribute definition).

Correct.  In 3.4 __ordered__ never came into being as it was not necessary.  I added that purely so that 2.x could be 
ordered if desired.


--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#51599

FromEthan Furman <ethan@stoneleaf.us>
Date2013-07-30 12:16 -0700
Message-ID<mailman.5337.1375211805.3114.python-list@python.org>
In reply to#51593
On 07/30/2013 11:38 AM, Ethan Furman wrote:
>
> Thanks for catching that, I'll get it fixed asap.

Latest code is on PyPI.

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#51600

FromEthan Furman <ethan@stoneleaf.us>
Date2013-07-30 12:30 -0700
Message-ID<mailman.5338.1375213962.3114.python-list@python.org>
In reply to#51593
On 07/30/2013 11:58 AM, Ian Kelly wrote:
> On Tue, Jul 30, 2013 at 12:18 PM, Bas van der Wulp <Bas.vdWulp@gmail.com> wrote:
>> Replacing each occurrence of self._value with either self._value_ or
>> self.value in the examples seems to make them work as expected.
>>
>> Are both examples incorrect, or not intended to work in Python 2.x?
>
> The _value attribute was renamed _value_ in:
>
> http://hg.python.org/cpython/rev/511c4daac102
>
> It looks like the example wasn't updated to match.  You should
> probably just use self.value here since the name of the private
> attribute is an implementation detail.

In `__new__` it has to be `_value_`, but in the other methods `.value` works fine.  Updated the 3.4 example with `.value`.

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#51612

FromBas van der Wulp <Bas.vdWulp@gmail.com>
Date2013-07-31 00:30 +0200
Message-ID<51f83e89$0$21818$6d4158fb@reader.xsnews.nl>
In reply to#51600
On 30-7-2013 21:30, Ethan Furman wrote:
> On 07/30/2013 11:58 AM, Ian Kelly wrote:
>> On Tue, Jul 30, 2013 at 12:18 PM, Bas van der Wulp
>> <Bas.vdWulp@gmail.com> wrote:
>>> Replacing each occurrence of self._value with either self._value_ or
>>> self.value in the examples seems to make them work as expected.
>>>
>>> Are both examples incorrect, or not intended to work in Python 2.x?
>>
>> The _value attribute was renamed _value_ in:
>>
>> http://hg.python.org/cpython/rev/511c4daac102
>>
>> It looks like the example wasn't updated to match.  You should
>> probably just use self.value here since the name of the private
>> attribute is an implementation detail.
>
> In `__new__` it has to be `_value_`, but in the other methods `.value`
> works fine.  Updated the 3.4 example with `.value`.
>
> --
> ~Ethan~

That was quick! Thanks Ethan and Ian.

Regards,
Bas

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web