forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
57 lines (40 loc) · 1.64 KB
/
base.py
File metadata and controls
57 lines (40 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from collections.abc import Mapping, MutableMapping
class Singleton(type):
"""A metaclass that creates a Singleton base class when called."""
_inst = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._inst:
cls._inst[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._inst[cls]
class InscDict(MutableMapping):
"""Improved version of the header dictionary from
`requests.structures.CaseInsensitiveDict`."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __getitem__(self, key):
return self.__dict__[key.lower()][-1]
def __setitem__(self, key, value):
# NOTE: Use the lowercased key for lookups, but store the actual key
# alongside the value
self.__dict__[key.lower()] = (key, value)
def __delitem__(self, key):
del self.__dict__[key.lower()]
def __len__(self):
return len(self.__dict__)
def __iter__(self):
return iter(key for key, val in self.__dict__.values())
def __str__(self):
return f"<InscDict {self.__dict__}>"
def __eq__(self, other):
if not isinstance(other, Mapping):
raise TypeError
# Compare insensitively
return self.loweritems() == InscDict(other).loweritems()
def lowerkeys(self):
"""Like `keys`, but with all lowercase keys."""
return self.__dict__.keys()
def loweritems(self):
"""Like `items`, but with all lowercase keys."""
return ((lowerkey, val) for lowerkey, (key, val) in self.__dict__.items())
def copy(self):
return InscDict(self.__dict__.values())