Can someone explain why enum-vs-string is being discussed as if it is an
either-or choice? Why not just call the enum class using the input so that
you can supply a string or enum?
NanChoice(nan_choice_input)
I understand this would not be a really great choice for a flags enum or
int enum, but for single-choice strings, it seems like a reasonable
approach to me.
In the example below from my own code I just call the enums using the input
strings so that whether I decide to pass in a string, or import and use an
actual enum object, it will work fine either way.
from enum import Enum
import pandas as pd
class LoadType(Enum):
dead = "D"
live = "L"
snow = "S"
wind = "W"
seismic = "E"
D = "D"
L = "L"
S = "S"
W = "W"
E = "E"
class RiskCategory(Enum):
"""Risk Category of Buildings and Other Structures for Flood,
Wind, Snow, Earthquake, and Ice Loads.
cf. Table 1.5-1
"""
I = "I"
II = "II"
III = "III"
IV = "IV"
_Table_1_pt_5_dsh_2 = pd.DataFrame({LoadType.snow: [0.80, 1.00, 1.10, 1.20],
LoadType.seismic: [1.00, 1.00, 1.25, 1.50]},
index=list(RiskCategory))
def importance_factor(load_type_input):
return lambda risk_input:
_Table_1_pt_5_dsh_2[LoadType(load_type_input)][RiskCategory(risk_input)]
Maybe this idea sucks? I don't have any way of knowing since nobody reads
my code other than future me (who happens to be somewhat dim), but in the
past, I have used enums like this in my own code mostly as 1. a
documentation feature so I can easily remember what the valid choices are,
and 2. an easy error catcher since you get a nice exception when you supply
an invalid string to the enum class:
>>> class MyEnum(Enum):
... a: "a"
... b: "b"
...
>>> MyEnum("c")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\program files\python39\lib\enum.py", line 384, in __call__
return cls.__new__(cls, value)
File "c:\program files\python39\lib\enum.py", line 702, in __new__
raise ve_exc
ValueError: 'c' is not a valid MyEnum
Could this approach be useful for handling nans in the stats module? Maybe
the overhead of calling the enum be considered too high or something?
---
Ricky.
"I've never met a Kentucky man who wasn't either thinking about going home
or actually going home." - Happy Chandler
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/ZH4CCDFTGZRCO6XBNMM354HOHLPNDUR5/
Code of Conduct: http://python.org/psf/codeofconduct/