Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64
Apache
: 172.26.7.228 | : 3.146.176.112
Cant Read [ /etc/named.conf ]
5.6.40-24+ubuntu18.04.1+deb.sury.org+1
www-data
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
HASH IDENTIFIER
README
+ Create Folder
+ Create File
/
snap /
core /
17200 /
usr /
lib /
python3.5 /
[ HOME SHELL ]
Name
Size
Permission
Action
__pycache__
[ DIR ]
drwxr-xr-x
asyncio
[ DIR ]
drwxr-xr-x
collections
[ DIR ]
drwxr-xr-x
concurrent
[ DIR ]
drwxr-xr-x
ctypes
[ DIR ]
drwxr-xr-x
curses
[ DIR ]
drwxr-xr-x
dbm
[ DIR ]
drwxr-xr-x
distutils
[ DIR ]
drwxr-xr-x
email
[ DIR ]
drwxr-xr-x
encodings
[ DIR ]
drwxr-xr-x
html
[ DIR ]
drwxr-xr-x
http
[ DIR ]
drwxr-xr-x
idlelib
[ DIR ]
drwxr-xr-x
importlib
[ DIR ]
drwxr-xr-x
json
[ DIR ]
drwxr-xr-x
lib-dynload
[ DIR ]
drwxr-xr-x
lib2to3
[ DIR ]
drwxr-xr-x
logging
[ DIR ]
drwxr-xr-x
multiprocessing
[ DIR ]
drwxr-xr-x
plat-x86_64-linux-gnu
[ DIR ]
drwxr-xr-x
pydoc_data
[ DIR ]
drwxr-xr-x
sqlite3
[ DIR ]
drwxr-xr-x
test
[ DIR ]
drwxr-xr-x
tkinter
[ DIR ]
drwxr-xr-x
unittest
[ DIR ]
drwxr-xr-x
urllib
[ DIR ]
drwxr-xr-x
venv
[ DIR ]
drwxr-xr-x
wsgiref
[ DIR ]
drwxr-xr-x
xml
[ DIR ]
drwxr-xr-x
xmlrpc
[ DIR ]
drwxr-xr-x
LICENSE.txt
12.47
KB
-rw-r--r--
__future__.py
4.73
KB
-rw-r--r--
__phello__.foo.py
64
B
-rw-r--r--
_bootlocale.py
1.27
KB
-rw-r--r--
_collections_abc.py
24.13
KB
-rw-r--r--
_compat_pickle.py
8.36
KB
-rw-r--r--
_compression.py
5.21
KB
-rw-r--r--
_dummy_thread.py
5
KB
-rw-r--r--
_markupbase.py
14.26
KB
-rw-r--r--
_osx_support.py
18.66
KB
-rw-r--r--
_pydecimal.py
222.46
KB
-rw-r--r--
_pyio.py
85.91
KB
-rw-r--r--
_sitebuiltins.py
3.04
KB
-rw-r--r--
_strptime.py
21.67
KB
-rw-r--r--
_sysconfigdata.py
126
B
-rw-r--r--
_threading_local.py
7.24
KB
-rw-r--r--
_weakrefset.py
5.57
KB
-rw-r--r--
abc.py
8.43
KB
-rw-r--r--
aifc.py
30.84
KB
-rw-r--r--
antigravity.py
475
B
-rw-r--r--
argparse.py
88.21
KB
-rw-r--r--
ast.py
11.72
KB
-rw-r--r--
asynchat.py
11.69
KB
-rw-r--r--
asyncore.py
19.63
KB
-rw-r--r--
base64.py
19.96
KB
-rwxr-xr-x
bdb.py
22.81
KB
-rw-r--r--
binhex.py
13.63
KB
-rw-r--r--
bisect.py
2.53
KB
-rw-r--r--
bz2.py
12.13
KB
-rw-r--r--
cProfile.py
5.19
KB
-rwxr-xr-x
calendar.py
22.4
KB
-rw-r--r--
cgi.py
35.19
KB
-rwxr-xr-x
cgitb.py
11.74
KB
-rw-r--r--
chunk.py
5.3
KB
-rw-r--r--
cmd.py
14.51
KB
-rw-r--r--
code.py
9.88
KB
-rw-r--r--
codecs.py
35.38
KB
-rw-r--r--
codeop.py
5.85
KB
-rw-r--r--
colorsys.py
3.97
KB
-rw-r--r--
compileall.py
11.42
KB
-rw-r--r--
configparser.py
52.2
KB
-rw-r--r--
contextlib.py
11.97
KB
-rw-r--r--
copy.py
8.74
KB
-rw-r--r--
copyreg.py
6.67
KB
-rw-r--r--
crypt.py
1.83
KB
-rw-r--r--
csv.py
15.75
KB
-rw-r--r--
datetime.py
74.12
KB
-rw-r--r--
decimal.py
320
B
-rw-r--r--
difflib.py
82.23
KB
-rw-r--r--
dis.py
16.94
KB
-rw-r--r--
doctest.py
101.6
KB
-rw-r--r--
dummy_threading.py
2.75
KB
-rw-r--r--
enum.py
21.71
KB
-rw-r--r--
filecmp.py
9.6
KB
-rw-r--r--
fileinput.py
13.92
KB
-rw-r--r--
fnmatch.py
3.09
KB
-rw-r--r--
formatter.py
14.79
KB
-rw-r--r--
fractions.py
23.82
KB
-rw-r--r--
ftplib.py
34.36
KB
-rw-r--r--
functools.py
28.1
KB
-rw-r--r--
genericpath.py
4.26
KB
-rw-r--r--
getopt.py
7.31
KB
-rw-r--r--
getpass.py
5.91
KB
-rw-r--r--
gettext.py
18.18
KB
-rw-r--r--
glob.py
4.95
KB
-rw-r--r--
gzip.py
19.79
KB
-rw-r--r--
hashlib.py
7.79
KB
-rw-r--r--
heapq.py
22.39
KB
-rw-r--r--
hmac.py
4.94
KB
-rw-r--r--
imaplib.py
50.75
KB
-rw-r--r--
imghdr.py
3.67
KB
-rw-r--r--
imp.py
10.38
KB
-rw-r--r--
inspect.py
110.66
KB
-rw-r--r--
io.py
3.32
KB
-rw-r--r--
ipaddress.py
73.83
KB
-rw-r--r--
keyword.py
2.16
KB
-rwxr-xr-x
linecache.py
5.19
KB
-rw-r--r--
locale.py
72.97
KB
-rw-r--r--
lzma.py
12.62
KB
-rw-r--r--
macpath.py
5.77
KB
-rw-r--r--
macurl2path.py
2.67
KB
-rw-r--r--
mailbox.py
76.58
KB
-rw-r--r--
mailcap.py
8.21
KB
-rw-r--r--
mimetypes.py
20.36
KB
-rw-r--r--
modulefinder.py
22.54
KB
-rw-r--r--
netrc.py
5.61
KB
-rw-r--r--
nntplib.py
42.07
KB
-rw-r--r--
ntpath.py
22.26
KB
-rw-r--r--
nturl2path.py
2.39
KB
-rw-r--r--
numbers.py
10
KB
-rw-r--r--
opcode.py
5.75
KB
-rw-r--r--
operator.py
10.61
KB
-rw-r--r--
optparse.py
58.93
KB
-rw-r--r--
os.py
36.1
KB
-rw-r--r--
pathlib.py
45.89
KB
-rw-r--r--
pdb.py
59.56
KB
-rwxr-xr-x
pickle.py
54.54
KB
-rw-r--r--
pickletools.py
89.61
KB
-rw-r--r--
pipes.py
8.71
KB
-rw-r--r--
pkgutil.py
20.71
KB
-rw-r--r--
platform.py
47.91
KB
-rwxr-xr-x
plistlib.py
32
KB
-rw-r--r--
poplib.py
14.17
KB
-rw-r--r--
posixpath.py
14.53
KB
-rw-r--r--
pprint.py
20.37
KB
-rw-r--r--
profile.py
21.5
KB
-rwxr-xr-x
pstats.py
25.7
KB
-rw-r--r--
pty.py
4.65
KB
-rw-r--r--
py_compile.py
7.01
KB
-rw-r--r--
pyclbr.py
13.25
KB
-rw-r--r--
pydoc.py
100.38
KB
-rwxr-xr-x
queue.py
8.57
KB
-rw-r--r--
quopri.py
7.08
KB
-rwxr-xr-x
random.py
25.48
KB
-rw-r--r--
re.py
15.14
KB
-rw-r--r--
reprlib.py
5.21
KB
-rw-r--r--
rlcompleter.py
6.16
KB
-rw-r--r--
runpy.py
11.13
KB
-rw-r--r--
sched.py
6.07
KB
-rw-r--r--
selectors.py
18.72
KB
-rw-r--r--
shelve.py
8.33
KB
-rw-r--r--
shlex.py
11.15
KB
-rw-r--r--
shutil.py
38.93
KB
-rw-r--r--
signal.py
2.07
KB
-rw-r--r--
site.py
21.74
KB
-rw-r--r--
smtpd.py
34.53
KB
-rwxr-xr-x
smtplib.py
42.57
KB
-rwxr-xr-x
sndhdr.py
6.27
KB
-rw-r--r--
socket.py
26.46
KB
-rw-r--r--
socketserver.py
24.09
KB
-rw-r--r--
sre_compile.py
17.98
KB
-rw-r--r--
sre_constants.py
6.3
KB
-rw-r--r--
sre_parse.py
34.14
KB
-rw-r--r--
ssl.py
42.54
KB
-rw-r--r--
stat.py
4.92
KB
-rw-r--r--
statistics.py
19.08
KB
-rw-r--r--
string.py
11.58
KB
-rw-r--r--
stringprep.py
12.61
KB
-rw-r--r--
struct.py
257
B
-rw-r--r--
subprocess.py
66.8
KB
-rw-r--r--
sunau.py
17.67
KB
-rw-r--r--
symbol.py
2.04
KB
-rwxr-xr-x
symtable.py
7.02
KB
-rw-r--r--
sysconfig.py
24.07
KB
-rw-r--r--
tabnanny.py
11.13
KB
-rwxr-xr-x
tarfile.py
90.83
KB
-rwxr-xr-x
telnetlib.py
22.48
KB
-rw-r--r--
tempfile.py
30.98
KB
-rw-r--r--
textwrap.py
19.19
KB
-rw-r--r--
this.py
1003
B
-rw-r--r--
threading.py
47.77
KB
-rw-r--r--
timeit.py
12.1
KB
-rwxr-xr-x
token.py
3
KB
-rw-r--r--
tokenize.py
27.14
KB
-rw-r--r--
trace.py
30.8
KB
-rwxr-xr-x
traceback.py
21.67
KB
-rw-r--r--
tracemalloc.py
15.27
KB
-rw-r--r--
tty.py
879
B
-rw-r--r--
turtle.py
140.14
KB
-rw-r--r--
types.py
8.59
KB
-rw-r--r--
typing.py
58.12
KB
-rw-r--r--
uu.py
6.59
KB
-rwxr-xr-x
uuid.py
22.72
KB
-rw-r--r--
warnings.py
15.48
KB
-rw-r--r--
wave.py
17.27
KB
-rw-r--r--
weakref.py
18.95
KB
-rw-r--r--
webbrowser.py
20.92
KB
-rwxr-xr-x
xdrlib.py
5.77
KB
-rw-r--r--
zipapp.py
6.99
KB
-rw-r--r--
zipfile.py
71.95
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : enum.py
import sys from collections import OrderedDict from types import MappingProxyType, DynamicClassAttribute __all__ = ['Enum', 'IntEnum', 'unique'] def _is_descriptor(obj): """Returns True if obj is a descriptor, False otherwise.""" return ( hasattr(obj, '__get__') or hasattr(obj, '__set__') or hasattr(obj, '__delete__')) def _is_dunder(name): """Returns True if a __dunder__ name, False otherwise.""" return (name[:2] == name[-2:] == '__' and name[2:3] != '_' and name[-3:-2] != '_' and len(name) > 4) def _is_sunder(name): """Returns True if a _sunder_ name, False otherwise.""" return (name[0] == name[-1] == '_' and name[1:2] != '_' and name[-2:-1] != '_' and len(name) > 2) def _make_class_unpicklable(cls): """Make the given class un-picklable.""" def _break_on_call_reduce(self, proto): raise TypeError('%r cannot be pickled' % self) cls.__reduce_ex__ = _break_on_call_reduce cls.__module__ = '<unknown>' class _EnumDict(dict): """Track enum member order and ensure member names are not reused. EnumMeta will use the names found in self._member_names as the enumeration member names. """ def __init__(self): super().__init__() self._member_names = [] def __setitem__(self, key, value): """Changes anything not dundered or not a descriptor. If an enum member name is used twice, an error is raised; duplicate values are not checked for. Single underscore (sunder) names are reserved. """ if _is_sunder(key): raise ValueError('_names_ are reserved for future Enum use') elif _is_dunder(key): pass elif key in self._member_names: # descriptor overwriting an enum? raise TypeError('Attempted to reuse key: %r' % key) elif not _is_descriptor(value): if key in self: # enum overwriting a descriptor? raise TypeError('Key already defined as: %r' % self[key]) self._member_names.append(key) super().__setitem__(key, value) # Dummy value for Enum as EnumMeta explicitly checks for it, but of course # until EnumMeta finishes running the first time the Enum class doesn't exist. # This is also why there are checks in EnumMeta like `if Enum is not None` Enum = None class EnumMeta(type): """Metaclass for Enum""" @classmethod def __prepare__(metacls, cls, bases): return _EnumDict() def __new__(metacls, cls, bases, classdict): # an Enum class is final once enumeration items have been defined; it # cannot be mixed with other types (int, float, etc.) if it has an # inherited __new__ unless a new __new__ is defined (or the resulting # class will fail). member_type, first_enum = metacls._get_mixins_(bases) __new__, save_new, use_args = metacls._find_new_(classdict, member_type, first_enum) # save enum items into separate mapping so they don't get baked into # the new class members = {k: classdict[k] for k in classdict._member_names} for name in classdict._member_names: del classdict[name] # check for illegal enum names (any others?) invalid_names = set(members) & {'mro', } if invalid_names: raise ValueError('Invalid enum member name: {0}'.format( ','.join(invalid_names))) # create a default docstring if one has not been provided if '__doc__' not in classdict: classdict['__doc__'] = 'An enumeration.' # create our new Enum type enum_class = super().__new__(metacls, cls, bases, classdict) enum_class._member_names_ = [] # names in definition order enum_class._member_map_ = OrderedDict() # name->value map enum_class._member_type_ = member_type # save attributes from super classes so we know if we can take # the shortcut of storing members in the class dict base_attributes = {a for b in enum_class.mro() for a in b.__dict__} # Reverse value->name map for hashable values. enum_class._value2member_map_ = {} # If a custom type is mixed into the Enum, and it does not know how # to pickle itself, pickle.dumps will succeed but pickle.loads will # fail. Rather than have the error show up later and possibly far # from the source, sabotage the pickle protocol for this class so # that pickle.dumps also fails. # # However, if the new class implements its own __reduce_ex__, do not # sabotage -- it's on them to make sure it works correctly. We use # __reduce_ex__ instead of any of the others as it is preferred by # pickle over __reduce__, and it handles all pickle protocols. if '__reduce_ex__' not in classdict: if member_type is not object: methods = ('__getnewargs_ex__', '__getnewargs__', '__reduce_ex__', '__reduce__') if not any(m in member_type.__dict__ for m in methods): _make_class_unpicklable(enum_class) # instantiate them, checking for duplicates as we go # we instantiate first instead of checking for duplicates first in case # a custom __new__ is doing something funky with the values -- such as # auto-numbering ;) for member_name in classdict._member_names: value = members[member_name] if not isinstance(value, tuple): args = (value, ) else: args = value if member_type is tuple: # special case for tuple enums args = (args, ) # wrap it one more time if not use_args: enum_member = __new__(enum_class) if not hasattr(enum_member, '_value_'): enum_member._value_ = value else: enum_member = __new__(enum_class, *args) if not hasattr(enum_member, '_value_'): enum_member._value_ = member_type(*args) value = enum_member._value_ enum_member._name_ = member_name enum_member.__objclass__ = enum_class enum_member.__init__(*args) # If another member with the same value was already defined, the # new member becomes an alias to the existing one. for name, canonical_member in enum_class._member_map_.items(): if canonical_member._value_ == enum_member._value_: enum_member = canonical_member break else: # Aliases don't appear in member names (only in __members__). enum_class._member_names_.append(member_name) # performance boost for any member that would not shadow # a DynamicClassAttribute if member_name not in base_attributes: setattr(enum_class, member_name, enum_member) # now add to _member_map_ enum_class._member_map_[member_name] = enum_member try: # This may fail if value is not hashable. We can't add the value # to the map, and by-value lookups for this value will be # linear. enum_class._value2member_map_[value] = enum_member except TypeError: pass # double check that repr and friends are not the mixin's or various # things break (such as pickle) for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'): class_method = getattr(enum_class, name) obj_method = getattr(member_type, name, None) enum_method = getattr(first_enum, name, None) if obj_method is not None and obj_method is class_method: setattr(enum_class, name, enum_method) # replace any other __new__ with our own (as long as Enum is not None, # anyway) -- again, this is to support pickle if Enum is not None: # if the user defined their own __new__, save it before it gets # clobbered in case they subclass later if save_new: enum_class.__new_member__ = __new__ enum_class.__new__ = Enum.__new__ return enum_class def __bool__(self): """ classes/types should always be True. """ return True def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1): """Either returns an existing member, or creates a new enum class. This method is used both when an enum class is given a value to match to an enumeration member (i.e. Color(3)) and for the functional API (i.e. Color = Enum('Color', names='red green blue')). When used for the functional API: `value` will be the name of the new class. `names` should be either a string of white-space/comma delimited names (values will start at `start`), or an iterator/mapping of name, value pairs. `module` should be set to the module this class is being created in; if it is not set, an attempt to find that module will be made, but if it fails the class will not be picklable. `qualname` should be set to the actual location this class can be found at in its module; by default it is set to the global scope. If this is not correct, unpickling will fail in some circumstances. `type`, if set, will be mixed in as the first base class. """ if names is None: # simple value lookup return cls.__new__(cls, value) # otherwise, functional API: we're creating a new Enum type return cls._create_(value, names, module=module, qualname=qualname, type=type, start=start) def __contains__(cls, member): return isinstance(member, cls) and member._name_ in cls._member_map_ def __delattr__(cls, attr): # nicer error message when someone tries to delete an attribute # (see issue19025). if attr in cls._member_map_: raise AttributeError( "%s: cannot delete Enum member." % cls.__name__) super().__delattr__(attr) def __dir__(self): return (['__class__', '__doc__', '__members__', '__module__'] + self._member_names_) def __getattr__(cls, name): """Return the enum member matching `name` We use __getattr__ instead of descriptors or inserting into the enum class' __dict__ in order to support `name` and `value` being both properties for enum members (which live in the class' __dict__) and enum members themselves. """ if _is_dunder(name): raise AttributeError(name) try: return cls._member_map_[name] except KeyError: raise AttributeError(name) from None def __getitem__(cls, name): return cls._member_map_[name] def __iter__(cls): return (cls._member_map_[name] for name in cls._member_names_) def __len__(cls): return len(cls._member_names_) @property def __members__(cls): """Returns a mapping of member name->value. This mapping lists all enum members, including aliases. Note that this is a read-only view of the internal mapping. """ return MappingProxyType(cls._member_map_) def __repr__(cls): return "<enum %r>" % cls.__name__ def __reversed__(cls): return (cls._member_map_[name] for name in reversed(cls._member_names_)) def __setattr__(cls, name, value): """Block attempts to reassign Enum members. A simple assignment to the class namespace only changes one of the several possible ways to get an Enum member from the Enum class, resulting in an inconsistent Enumeration. """ member_map = cls.__dict__.get('_member_map_', {}) if name in member_map: raise AttributeError('Cannot reassign members.') super().__setattr__(name, value) def _create_(cls, class_name, names=None, *, module=None, qualname=None, type=None, start=1): """Convenience method to create a new Enum class. `names` can be: * A string containing member names, separated either with spaces or commas. Values are incremented by 1 from `start`. * An iterable of member names. Values are incremented by 1 from `start`. * An iterable of (member name, value) pairs. * A mapping of member name -> value pairs. """ metacls = cls.__class__ bases = (cls, ) if type is None else (type, cls) classdict = metacls.__prepare__(class_name, bases) # special processing needed for names? if isinstance(names, str): names = names.replace(',', ' ').split() if isinstance(names, (tuple, list)) and isinstance(names[0], str): names = [(e, i) for (i, e) in enumerate(names, start)] # Here, names is either an iterable of (name, value) or a mapping. for item in names: if isinstance(item, str): member_name, member_value = item, names[item] else: member_name, member_value = item classdict[member_name] = member_value enum_class = metacls.__new__(metacls, class_name, bases, classdict) # TODO: replace the frame hack if a blessed way to know the calling # module is ever developed if module is None: try: module = sys._getframe(2).f_globals['__name__'] except (AttributeError, ValueError) as exc: pass if module is None: _make_class_unpicklable(enum_class) else: enum_class.__module__ = module if qualname is not None: enum_class.__qualname__ = qualname return enum_class @staticmethod def _get_mixins_(bases): """Returns the type for creating enum members, and the first inherited enum class. bases: the tuple of bases that was given to __new__ """ if not bases: return object, Enum # double check that we are not subclassing a class with existing # enumeration members; while we're at it, see if any other data # type has been mixed in so we can use the correct __new__ member_type = first_enum = None for base in bases: if (base is not Enum and issubclass(base, Enum) and base._member_names_): raise TypeError("Cannot extend enumerations") # base is now the last base in bases if not issubclass(base, Enum): raise TypeError("new enumerations must be created as " "`ClassName([mixin_type,] enum_type)`") # get correct mix-in type (either mix-in type of Enum subclass, or # first base if last base is Enum) if not issubclass(bases[0], Enum): member_type = bases[0] # first data type first_enum = bases[-1] # enum type else: for base in bases[0].__mro__: # most common: (IntEnum, int, Enum, object) # possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>, # <class 'int'>, <Enum 'Enum'>, # <class 'object'>) if issubclass(base, Enum): if first_enum is None: first_enum = base else: if member_type is None: member_type = base return member_type, first_enum @staticmethod def _find_new_(classdict, member_type, first_enum): """Returns the __new__ to be used for creating the enum members. classdict: the class dictionary given to __new__ member_type: the data type whose __new__ will be used by default first_enum: enumeration to check for an overriding __new__ """ # now find the correct __new__, checking to see of one was defined # by the user; also check earlier enum classes in case a __new__ was # saved as __new_member__ __new__ = classdict.get('__new__', None) # should __new__ be saved as __new_member__ later? save_new = __new__ is not None if __new__ is None: # check all possibles for __new_member__ before falling back to # __new__ for method in ('__new_member__', '__new__'): for possible in (member_type, first_enum): target = getattr(possible, method, None) if target not in { None, None.__new__, object.__new__, Enum.__new__, }: __new__ = target break if __new__ is not None: break else: __new__ = object.__new__ # if a non-object.__new__ is used then whatever value/tuple was # assigned to the enum member name will be passed to __new__ and to the # new enum member's __init__ if __new__ is object.__new__: use_args = False else: use_args = True return __new__, save_new, use_args class Enum(metaclass=EnumMeta): """Generic enumeration. Derive from this class to define new enumerations. """ def __new__(cls, value): # all enum instances are actually created during class construction # without calling this method; this method is called by the metaclass' # __call__ (i.e. Color(3) ), and by pickle if type(value) is cls: # For lookups like Color(Color.red) return value # by-value search for a matching enum member # see if it's in the reverse mapping (for hashable values) try: if value in cls._value2member_map_: return cls._value2member_map_[value] except TypeError: # not there, now do long search -- O(n) behavior for member in cls._member_map_.values(): if member._value_ == value: return member raise ValueError("%r is not a valid %s" % (value, cls.__name__)) def __repr__(self): return "<%s.%s: %r>" % ( self.__class__.__name__, self._name_, self._value_) def __str__(self): return "%s.%s" % (self.__class__.__name__, self._name_) def __dir__(self): added_behavior = [ m for cls in self.__class__.mro() for m in cls.__dict__ if m[0] != '_' and m not in self._member_map_ ] return (['__class__', '__doc__', '__module__'] + added_behavior) def __format__(self, format_spec): # mixed-in Enums should use the mixed-in type's __format__, otherwise # we can get strange results with the Enum name showing up instead of # the value # pure Enum branch if self._member_type_ is object: cls = str val = str(self) # mix-in branch else: cls = self._member_type_ val = self._value_ return cls.__format__(val, format_spec) def __hash__(self): return hash(self._name_) def __reduce_ex__(self, proto): return self.__class__, (self._value_, ) # DynamicClassAttribute is used to provide access to the `name` and # `value` properties of enum members while keeping some measure of # protection from modification, while still allowing for an enumeration # to have members named `name` and `value`. This works because enumeration # members are not set directly on the enum class -- __getattr__ is # used to look them up. @DynamicClassAttribute def name(self): """The name of the Enum member.""" return self._name_ @DynamicClassAttribute def value(self): """The value of the Enum member.""" return self._value_ @classmethod def _convert(cls, name, module, filter, source=None): """ Create a new Enum subclass that replaces a collection of global constants """ # convert all constants from source (or module) that pass filter() to # a new Enum called name, and export the enum and its members back to # module; # also, replace the __reduce_ex__ method so unpickling works in # previous Python versions module_globals = vars(sys.modules[module]) if source: source = vars(source) else: source = module_globals members = {name: value for name, value in source.items() if filter(name)} cls = cls(name, members, module=module) cls.__reduce_ex__ = _reduce_ex_by_name module_globals.update(cls.__members__) module_globals[name] = cls return cls class IntEnum(int, Enum): """Enum where members are also (and must be) ints""" def _reduce_ex_by_name(self, proto): return self.name def unique(enumeration): """Class decorator for enumerations ensuring unique member values.""" duplicates = [] for name, member in enumeration.__members__.items(): if name != member.name: duplicates.append((name, member.name)) if duplicates: alias_details = ', '.join( ["%s -> %s" % (alias, name) for (alias, name) in duplicates]) raise ValueError('duplicate values found in %r: %s' % (enumeration, alias_details)) return enumeration
Close