Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: jmp Newsgroups: comp.lang.python Subject: Re: Enum questions. Date: Wed, 13 Apr 2016 13:59:09 +0200 Lines: 94 Message-ID: References: <570E1B98.4080904@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de J1I7iNv2bmjw3a0/7S/72gwnRwwVSPEI9Ico9Qw6dRGw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; '*args):': 0.09; '@property': 0.09; '[1]:': 0.09; 'enum': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; '2.7': 0.13; 'useful,': 0.13; 'def': 0.13; 'value.': 0.15; '"while': 0.16; '2):': 0.16; '@classmethod': 0.16; 'cls': 0.16; 'lambda': 0.16; 'missing?': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'recipes': 0.16; 'subclassing': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'doc': 0.22; 'seems': 0.23; 'import': 0.24; 'examples': 0.24; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'specifically': 0.28; 'skip:u 20': 0.28; 'values': 0.28; 'directly,': 0.29; 'print': 0.30; 'creating': 0.30; 'code': 0.30; 'certain': 0.31; 'run': 0.33; 'class': 0.33; 'except': 0.34; 'next': 0.35; 'could': 0.35; 'important.': 0.35; 'expected': 0.35; 'step': 0.36; 'but': 0.36; 'skip:i 20': 0.36; 'possible': 0.36; 'cases': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'received:org': 0.37; 'missing': 0.37; 'rather': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'hope': 0.61; 'received:194': 0.61; 'charset:windows-1252': 0.62; 'skip:n 10': 0.62; 'different': 0.63; 'between': 0.65; 'here': 0.66; 'useful.': 0.72; 'miss': 0.77; 'pardon': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: paris.sequans.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: <570E1B98.4080904@rece.vub.ac.be> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <570E1B98.4080904@rece.vub.ac.be> Xref: csiph.com comp.lang.python:106936 On 04/13/2016 12:12 PM, Antoon Pardon wrote: > I have been looking at the enum documentation and it > seems enums are missing two features I rather find > important. > > 1) Given an Enum value, someway to get the next/previous > one > > 2) Given two Enum values, iterate over the values between > them. > > Did I miss those in the documentation or are they really > missing? > From the doc : "While Enum and IntEnum are expected to cover the majority of use-cases, they cannot cover them all. Here are recipes for some different types of enumerations that can be used directly, or as examples for creating one’s own." I would disagree with you when you state that the features you mentioned are important. They could be useful, in certain cases but most of your code would better be agnostic to the enum value. Now it is possible that you specifically work with a system where those features would be really useful. As mentioned by the documentation, subclassing Enum is possible: tested with python 2.7 from enum import IntEnum class UltimateEnum(IntEnum): @classmethod def range(cls, *args): enumToVal = lambda e: e.value if isinstance(e, cls) else e return (i for i in cls if i.value in range(*map(enumToVal, args))) @property def next(self): it = iter(self.__class__) try: for e in it: if e is self: return next(it) except StopIteration: return None return None class Foo(UltimateEnum): A = 1 B = 4 C = 9 D = 28 print "first to C:" for f in Foo.range(Foo.C): print f print "B to D :" for f in Foo.range(Foo.B, Foo.D): print f print "A to D+1 with step 2 : " for f in Foo.range(Foo.A, Foo.D.value+1, 2): print f print "next property" print Foo.A.next print Foo.C.next print Foo.D.next In [1]: run test.py first to C: Foo.A Foo.B B to D : Foo.B Foo.C A to D+1 with step 2 : Foo.A Foo.C next property Foo.B Foo.D None Hope it helps, jm