Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Enum (tuple):
- __values = {}
- def __getattr__(self, attr):
- if attr in self.values.values():
- return 2**self.values.values().index(attr)
- else:
- raise Exception('called enum-object has no the attr called'
- +' \"'+attr+'\" and has no the value named \"'+attr+'\"')
- def __getitem__(self, key):
- if key in self.values.keys():
- return self.values[key]
- else:
- return self.__getattr__(key)
- def __iter__(self):
- for item in self.values.items():
- yield item
- def __new__(cls, *iterable): # итератор двойных итераторов
- id=1
- for it in iterable:
- Enum.__values[id]=it
- id <<= 1
- return tuple.__new__(cls, Enum.__values.keys())
- def __init__(self, *keys): # итератор двойных итераторов
- self.values = Enum.__values.copy()
- Enum.__values = {}
- super(Enum, self).__init__(keys)
- def __str__(self):
- return super(Enum, self).__repr__()
- def __repr__(self):
- r = 0
- for i in self.values:
- r |= i
- return str(r)
- def main():
- e = Enum('first','dfdf','rt')
- print e[1]
- print e
- print ''
- for i in e:
- print i
- print ''
- s = Enum('second')
- print s[1]
- print e['first']
- print e.first
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement