vietnamrest.blogg.se

Python enum
Python enum









#PYTHON ENUM CODE#

You can make your code less repetitive and more readable by giving your enum members more descriptive names: from enum import EnumĮURO_PER_MEGAWATT_HOUR = "Eur/MWh", "price"ĭEGREES_CENTIGRADE = "degC", "temperature" Give your enum members more descriptive names!Ĭurrently, you have duplication of data in your enumeration: each member's name is the same as the first item of its value, the only difference being that the members all have all-uppercase names, while not all members have all-uppercase symbols. You should refactor your _init_ and _str_ methods to the following: from enum import Enumĭef _init_(self, symbol: str, domain: str) -> None: Moreover, it's unclear why you have default values in your _init_ signature, given that each Unit member must (and does) have a two-item tuple as its value. It should know nothing about how you intend to use it in your wider code base.) Your Unit class should, ideally, make sense as an abstraction in its own terms, without reference to any external functions or classes. ( col looks suspiciously like you've named this attribute according to how you intend to use your Unit enumeration in your pandas dataframe. The names for Unit's attributes should reflect this, rather than txt and col, both of which are opaque to anyone else reading your code. The structure of your data is clear: all members of the Unit enumeration have values that are 2-item tuples, the first item of which is the unit's symbol, and the second item of which is the unit's domain. Use more descriptive names for your attributes. However, I think there are nonetheless several aspects I might do differently. In my opinion, using Python's enum module for this kind of task is highly idiomatic - I think you've chosen the best way to express your code's intent here. More generally, is this a valid use case of Enums? It seems sensible to me, but it's going far beyond just being a list of integers as used in other languages.Put it in a and replace references to it by self.relations()? Is there a better way to do this? I'd prefer to keep in inside the class, but I can't put it at the class's root as it'd be interpreted as an enum value. Raise NotImplementedError("This division is not defined.") Raise NotImplementedError("This multiplication is not defined.") If (mul1, mul2) = (self, other) or (mul1, mul2) = (other, self): Relations = ((Unit.MWH, Unit.MW, Unit.H), (Unit.EUR, Unit.MWH, Unit.EURMWH)) class Unit(Enum):ĭef _init_(self, txt: str = "", col: str = ""):

python enum

Also, it allows for automatic calculation of the unit of a calculation, e.g., that the result of 40 megawatts during 5 hours is a value in MWh. I'm using Python's Enum to define the physical units in which a value is expressed.Įventually, I want to add them as an attribute to my pandas.Series and pandas.DataFrame instances, so that this information does not need to be stored in the variable name or in comments in the code.









Python enum