I needed enums for a Python project where the enums would be part of an API that I was defining for use by end-users. My requirements for an enum implementation were:
- Allow definition of a named class representing the set of allowable values. (I needed a class, not an instance, since I wanted to be able to specify the enum class as the parameter type for API functions that expected a class.) The enum classes should be derived from a common superclass (so I could test if a type was an enum type).
- The allowable values would be represented by instances of the enum class that would be automatically created at definition time. Each instance would have a name and a value (i.e. would be a name/value pair).
- The values should not be restricted to be integers (e.g. the values could be floats). But the values should be immutable objects (e.g. not lists)).
- Each enum class can be queried to get the list of allowable names and values.
- Each enum class can be queried to find the name/value pair instance given the name.
- It shouldn't be possible to modify the value of an existing name/value pair.
- Allow definition of alternative names for existing enum values. (e.g. so that the name can be changed in a future revision of the API while maintaining backward compatibility for users' source code) but these alternative names should not show up when the list of allowable values is queried.
(It is published under the MIT licence.)
To define an enum class, you use the 'Enum' function from the above 'enum' module. This function expects its first argument to be a string specifying the name of the enum class you are defining. The subsequent arguments should be keyword arguments specifying the names and values for this enum class. The 'Enum' function returns a class object (which is why its name is capitalized). For example, to define an enum class named 'Colours', you could do this:
from enum import Enum >>> Colours = Enum('Colours', red=1, green=2, blue=3)
After that, there would be instances Colours.red, Colours.green, Colours.blue representing the possible colour values. To get the value of the 'red' instance, you would use 'Colours.red.value' (which is 1). To get the 'red' instance given a colour string in the variable 'col', you would use 'Colours.get(col)' or 'Colours(col)'. (If that string variable contained anything other than "red", "green" or "blue", you would get an exception.)
Some more examples of use will show the other facilities of this enum implementation:
from enum import Enum >>> Colours = Enum('Colours', red=1, green=2, blue=3) >>> Colours.green.name 'green' >>> Colours.green.value 2 >>> Colours.names() ['red', 'green', 'blue'] >>> Colours.get('blue').value 3 >>> Colours.getByValue(3).name 'blue' >>> Colours('blue').value 3 >>> Colours.addAlias('bleu', 'blue') >>> Colours.names() ['red', 'green', 'blue'] >>> Colours.bleu.value 3 >>> [x.name for x in Colours.objects()] ['red', 'green', 'blue'] >>> listOfNames = ['red', 'blue'] >>> [x.name for x in Colours.listOfNamesToListOfEnums(listOfNames)] ['red', 'blue'] >>> commaSepStr = "red, blue" >>> [x.name for x in Colours.commaSepStrToEnumList(commaSepStr)] ['red', 'blue'] >>> Numbers = Enum('Numbers', pi=3.1415926, e=2.71828) >>> isinstance(Numbers, type) True >>> Numbers.names() ['e', 'pi'] >>> round(Numbers.e.value, 3) 2.718 >>> x = Numbers.pi >>> x is Numbers('pi') True >>> Numbers('phi') Traceback (most recent call last): ... ValueError: 'phi' is not a valid enum name (Must be one of: ['e', 'pi']) >>> Numbers.e.value = 42 Traceback (most recent call last): ... TypeError: can't modify an enum
Great Article
ReplyDeleteB.Tech Final Year Projects for CSE in Python
FInal Year Project Centers in Chennai
Python Training in Chennai
Python Training in Chennai
rastgele görüntülü konuşma - kredi hesaplama - instagram video indir - instagram takipçi satın al - instagram takipçi satın al - tiktok takipçi satın al - instagram takipçi satın al - instagram beğeni satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - instagram beğeni satın al - instagram beğeni satın al - polen filtresi - google haritalara yer ekleme - btcturk güvenilir mi - binance hesap açma - kuşadası kiralık villa - tiktok izlenme satın al - instagram takipçi satın al - sms onay - paribu sahibi - binance sahibi - btcturk sahibi - paribu ne zaman kuruldu - binance ne zaman kuruldu - btcturk ne zaman kuruldu - youtube izlenme satın al - torrent oyun - google haritalara yer ekleme - altyapısız internet - bedava internet - no deposit bonus forex - erkek spor ayakkabı - webturkey.net - karfiltre.com - tiktok jeton hilesi - tiktok beğeni satın al - microsoft word indir - misli indir
ReplyDeleteaşk kitapları
ReplyDeleteyoutube abone satın al
cami avizesi
cami avizeleri
avize cami
no deposit bonus forex 2021
takipçi satın al
takipçi satın al
takipçi satın al
takipcialdim.com/tiktok-takipci-satin-al/
instagram beğeni satın al
instagram beğeni satın al
btcturk
tiktok izlenme satın al
sms onay
youtube izlenme satın al
no deposit bonus forex 2021
tiktok jeton hilesi
tiktok beğeni satın al
binance
takipçi satın al
uc satın al
sms onay
sms onay
tiktok takipçi satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
instagram beğeni satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
takipcialdim.com/instagram-begeni-satin-al/
perde modelleri
instagram takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
betboo
marsbahis
sultanbet
film izle - sex hikayeleri - sex hikayesi - erotik hikaye -
ReplyDeleteankara escort - bornova escort - alsancak escort - çeşme escort - izmir escort - smm panel - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - haber - instagram takipçi hilesi - instagram takipçi satın al - izmir evden eve nakliyat - seocu - instagram takipçi hilesi - instagram takipçi satın al - izmir escort - takipçi satın al - instagram takipçi satın al - tiktok takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takibi bırakanlar - buca escort -
karşıyaka escort
takipçi satın al
ReplyDeleteinstagram takipçi satın al
https://www.takipcikenti.com
marsbahis
ReplyDeletebetboo
sultanbet
marsbahis
betboo
sultanbet
thanks admin good article HDE Bilişim
ReplyDeleteAlışveriş
Compo Expert
Multitek
Seokoloji
Vezir Sosyal Medya
Adak
Maltepe Adak
thx sir all posting so good i love u for all info
ReplyDeleteVenüsbet Venüsbet Grandbetting Grandbetting Hiltonbet Betnano Marsbahis Meritking Asyabahis
Your support is greatly appreciated. دانلود آهنگ رضا بهرام عاشقی ممنوع
ReplyDeleteخرید نهال گردو
ReplyDeletetiktok jeton hilesi
ReplyDeletetiktok jeton hilesi
binance referans kimliği
gate güvenilir mi
tiktok jeton hilesi
paribu
btcturk
bitcoin nasıl alınır
yurtdışı kargo
instagram takipçi satın al
ReplyDelete