Module ContentsThis module defines four enumeration classes that can be used to define uniquesets of names and values:,. It also defines one decorator, and onehelper,. EnumBase class for creating enumerated constants. See sectionfor an alternate construction syntax.
Delicious Library allows you to import, browse, and share all your books, movies, music, and video games with Delicious Library. Run your very own library from your home or office using our impossibly simple interface. Delicious Library's digital shelves act as a visual card-catalog. A scan of a barcode is all Delicious Library needs to add an item to your digital shelves, downloading tons of.
IntEnumBase class for creating enumerated constants that are alsosubclasses of. IntFlagBase class for creating enumerated constants that can be combined usingthe bitwise operators without losing their membership.members are also subclasses of. FlagBase class for creating enumerated constants that can be combined usingthe bitwise operations without losing their membership. Unique ( )Enum class decorator that ensures only one name is bound to any one value. AutoInstances are replaced with an appropriate value for Enum members. Initial value starts at 1. Favoritemood Mood.
Describe ('HAPPY', 3) str ( Mood. FUNKY ) 'my custom str! 1'The rules for what is allowed are as follows: names that start and end witha single underscore are reserved by enum and cannot be used; all otherattributes defined within an enumeration will become members of thisenumeration, with the exception of special methods (, etc.), descriptors (methods are also descriptors), andvariable names listed in ignore.Note: if your enumeration defines and/or thenwhatever value(s) were given to the enum member will be passed into thosemethods. See for an example. Animal = Enum ( 'Animal', 'ANT BEE CAT DOG' ) Animal Animal. ANT Animal. Value 1 list ( Animal ) , The semantics of this API resemble.
The firstargument of the call to is the name of the enumeration.The second argument is the source of enumeration member names. It can be awhitespace-separated string of names, a sequence of names, a sequence of2-tuples with key/value pairs, or a mapping (e.g. Dictionary) of names tovalues. The last two options enable assigning arbitrary values toenumerations; the others auto-assign increasing integers starting with 1 (usethe start parameter to specify a different starting value). Anew class derived from is returned. In other words, the aboveassignment to Animal is equivalent to.
class Animal ( Enum ). DOG = 4.The reason for defaulting to 1 as the starting number and not 0 isthat 0 is False in a boolean sense, but enum members all evaluateto True.Pickling enums created with the functional API can be tricky as frame stackimplementation details are used to try and figure out which module theenumeration is being created in (e.g. It will fail if you use a utilityfunction in separate module, and also may not work on IronPython or Jython).The solution is to specify the module name explicitly as follows. Class IntEnum ( int, Enum ): passThis demonstrates how similar derived enumerations can be defined; for examplea StrEnum that mixes in instead of.Some rules:.When subclassing, mix-in types must appear beforeitself in the sequence of bases, as in theexample above.While can have members of any type, once you mix in anadditional type, all the members must have values of that type, e.g.above. This restriction does not apply to mix-ins which onlyadd methods and don’t specify another data type such as or.When another data type is mixed in, the value attribute is not thesame as the enum member itself, although it is equivalent and will compareequal.%-style formatting:%s and%r call the class’sand respectively; other codes (such as%i or%h for IntEnum) treat the enum member as its mixed-in type.,and will use the mixed-intype’s. If the class’s oris desired, use the!s or!r format codes. class Coordinate ( bytes, Enum ).
Coordinate with binary codes that can be indexed by the int code. Def new ( cls, value, label, unit ). new ( clsvalue ).
value = value. Label = label.
PX = ( 0, 'P.X', 'km' ). PY = ( 1, 'P.Y', 'km' ). VX = ( 2, 'V.X', 'km/s' ). VY = ( 3, 'V.Y', 'km/s' ). print ( Coordinate 'PY' ) Coordinate.PY print ( Coordinate ( 3 )) Coordinate.VY. Omitting valuesIn many use-cases one doesn’t care what the actual value of an enumerationis.
There are several ways to define this type of simple enumeration:.use instances of for the value.use instances of as the value.use a descriptive string as the value.use a tuple as the value and a custom to replace thetuple with an valueUsing any of these methods signifies to the user that these values are notimportant, and also enables one to add, remove, or reorder members withouthaving to renumber the remaining members.Whichever method you choose, you should provide a that also hidesthe (unimportant) value. class OrderedEnum ( Enum ). Def ge ( self, other ). class is other.
Value = other. Return NotImplemented.
Def gt ( self, other ). class is other. Value other. Return NotImplemented. Def le ( self, other ).
class is other. Value class Grade ( OrderedEnum ). class DuplicateFreeEnum ( Enum ).
Def init ( self,. args ). If any ( self.
Value for e in cls ). E = cls ( self. Raise ValueError (.
'aliases not allowed in DuplicateFreeEnum:%r -%r '.% ( a, e )). class Color ( DuplicateFreeEnum ). Traceback (most recent call last). ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' - 'GREEN'. class Planet ( Enum ). MERCURY = ( 3.303e+23, 2.4397e6 ).
VENUS = ( 4.869e+24, 6.0518e6 ). EARTH = ( 5.976e+24, 6.37814e6 ). MARS = ( 6.421e+23, 3.3972e6 ). JUPITER = ( 1.9e+27, 7.1492e7 ). SATURN = ( 5.688e+26, 6.0268e7 ).
URANUS = ( 8.686e+25, 2.5559e7 ). NEPTUNE = ( 1.024e+26, 2.4746e7 ).
Def init ( self, mass, radius ). Mass = mass # in kilograms.
Radius = radius # in meters. Def surfacegravity ( self ).
# universal gravitational constant (m3 kg-1 s-2). G = 6.67300E-11. Return G.
self. Mass / ( self. Radius. self. Value (5.976e+24, 6378140.0) Planet.
Surfacegravity 9.37129. Supported sunder names.name – name of the member.value – value of the member; can be set / modified in new.missing – a lookup function used when a value is not found; may beoverridden.ignore – a list of names, either as a or a,that will not be transformed into members, and will be removed from the finalclass.order – used in Python 2/3 code to ensure member order is consistent(class attribute, removed during class creation).generatenextvalue – used by the and byto get an appropriate value for an enum member; may beoverridden.
Comments are closed.
|
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |