Table of Contents

Class: Enumeration ./src/peak/model/enumerations.py

An enumeration type

Defining an enumeration lets you specify a set of acceptable values of some other primitive type, each with its own name and representation. You can think of Python 2.3's new boolean type as an enumeration whose values are the integers 0 and 1, with names of False and True. PEAK model.Enumeration classes work similarly. For example:

        >>> class ThreeWay(model.Enumeration):
                Yes   = model.enum(1)
                No    = model.enum(0)
                Maybe = model.enum(-1)

        >>> print ThreeWay.Yes
        ThreeWay.Yes

        >>> print `ThreeWay.Yes.name`
        'Yes'

        >>> if ThreeWay.Yes == 1: print "yes!"
        yes!

        >>> print ThreeWay['No']
        ThreeWay.No

        >>> if 'Maybe' in ThreeWay: print "maybe!"
        maybe!

        >>> [e for e in ThreeWay]
        [ThreeWay.Maybe, ThreeWay.No, ThreeWay.Yes]

The above class will have attributes Yes, No, and Maybe, each of which is ThreeWay instance that hashes and compares equal to the specified literal (1,0, or -1). The str() and repr() of these instances is the enumeration class and instance names, separated by ..

Note: Enumeration values may be of any type. See the docs on model.enum(), model.enums() and model.enumDict() for various ways to specify the literals of an enumeration.

Enumeration Class Methods

Notice that the example ThreeWay class itself offers some useful methods, such as __getitem__, get, __contains__, and __iter__. The first three methods work as if the class were a dictionary whose keys were all the names and values of the enumeration instances, mapped to the instances themselves. __iter__ iterates over all the instances sorted in order of their values. All of these methods are available from any model.Enumeration subclass you define.

Subclassing and Instantiating Enumerations

It is not necessary to specifically create an instance of an enumeration class. The mere act of defining an enumeration class causes the instances to come into existence, and be available as attributes of the class. There is only ever a fixed number of instances of an enumeration class, and even if you try to create a new instance, you will just get back one of the original ones.

You can, however, subclass an enumeration class to add more values:

            >>> class FourWay(ThreeWay):
                    Dunno = model.enum("wha?")

            >>> FourWay.No  # values are inherited, but not the instances
            FourWay.No

            >>> if FourWay.Yes == ThreeWay.Yes: print "equal!"
            equal!

            >>> if FourWay.Yes is not ThreeWay.Yes: print "but not identical!"
            but not identical!

The subclass will inherit all the same literals as its superclass, but will have new instances encapsulating them. Thus, FourWay.Yes is equal to, but not the same object as, ThreeWay.Yes. The subclass and superclass instances will repr() with their respective class names, even though they will hash and compare the same.

Base Classes   
PrimitiveType
HashAndCompare
Methods   
__init__
__new__
__reduce__
__repr__
mdl_fromString
  __init__ 
__init__ ( self,  name )

  __new__ 
__new__ ( klass,  nameOrValue )

  __reduce__ 
__reduce__ ( self )

  __repr__ 
__repr__ ( self )

  mdl_fromString 
mdl_fromString ( klass,  value )

Return an enumeration instance for string value


Table of Contents

This document was automatically generated on Mon Aug 10 01:11:12 2020 by HappyDoc version 2.1