DecoratorsToolKit 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- Decorators/classes/builder.py +61 -0
- Decorators/classes/interface.py +15 -0
- Decorators/classes/noChange.py +9 -0
- Decorators/classes/singleton.py +18 -0
- Decorators/functions/fallback.py +11 -0
- Decorators/functions/maxLen.py +12 -0
- Decorators/functions/minLen.py +13 -0
- Decorators/functions/retry.py +16 -0
- Decorators/functions/validateType.py +32 -0
- Decorators/variables/final.py +17 -0
- Decorators/variables/nonNull.py +24 -0
- decoratorstoolkit-1.0.0.dist-info/METADATA +4 -0
- decoratorstoolkit-1.0.0.dist-info/RECORD +15 -0
- decoratorstoolkit-1.0.0.dist-info/WHEEL +5 -0
- decoratorstoolkit-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from enum import Enum
|
|
3
|
+
|
|
4
|
+
def Builder():
|
|
5
|
+
|
|
6
|
+
def decorator(cls):
|
|
7
|
+
|
|
8
|
+
parameters = inspect.signature(cls.__init__)
|
|
9
|
+
|
|
10
|
+
def builder(data: dict):
|
|
11
|
+
|
|
12
|
+
args = {}
|
|
13
|
+
ID = None
|
|
14
|
+
|
|
15
|
+
for index, item in data.items():
|
|
16
|
+
|
|
17
|
+
for name, parameter in parameters.parameters.items():
|
|
18
|
+
if name == "self":
|
|
19
|
+
continue
|
|
20
|
+
|
|
21
|
+
if index == "ID":
|
|
22
|
+
|
|
23
|
+
if hasattr(cls, "_ID"):
|
|
24
|
+
ID = max(cls._ID, item)
|
|
25
|
+
|
|
26
|
+
if index == name:
|
|
27
|
+
|
|
28
|
+
if isinstance(item, list):
|
|
29
|
+
if all(isinstance(element, dict) for element in item):
|
|
30
|
+
builded = []
|
|
31
|
+
|
|
32
|
+
for item_data in item:
|
|
33
|
+
|
|
34
|
+
if callable(getattr(parameter.annotation, "builder", None)):
|
|
35
|
+
newBuild = parameter.annotation.builder(item_data)
|
|
36
|
+
builded.append(newBuild)
|
|
37
|
+
|
|
38
|
+
else:
|
|
39
|
+
builded.append(item_data)
|
|
40
|
+
|
|
41
|
+
args[name] = builded
|
|
42
|
+
|
|
43
|
+
elif issubclass(parameter.annotation, Enum):
|
|
44
|
+
args[name] = parameter.annotation(item)
|
|
45
|
+
|
|
46
|
+
else:
|
|
47
|
+
if callable(getattr(parameter.annotation, "builder", None)):
|
|
48
|
+
args[name] = parameter.annotation.builder(item)
|
|
49
|
+
|
|
50
|
+
else:
|
|
51
|
+
args[name] = item
|
|
52
|
+
|
|
53
|
+
instance = cls(**args)
|
|
54
|
+
if ID is not None:
|
|
55
|
+
instance.ID = ID
|
|
56
|
+
|
|
57
|
+
return instance
|
|
58
|
+
cls.builder = builder
|
|
59
|
+
return cls
|
|
60
|
+
|
|
61
|
+
return decorator
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
def Interface(requireds: list[str]):
|
|
2
|
+
def decorator(cls):
|
|
3
|
+
if len(requireds) == 0:
|
|
4
|
+
raise ValueError("Les pré-requis ne doivent pas être nulles")
|
|
5
|
+
|
|
6
|
+
for required in requireds:
|
|
7
|
+
|
|
8
|
+
if callable(getattr(cls, required, None)):
|
|
9
|
+
continue
|
|
10
|
+
|
|
11
|
+
else:
|
|
12
|
+
raise ValueError(f"La méthode {required} n'est pas présente")
|
|
13
|
+
|
|
14
|
+
return cls
|
|
15
|
+
return decorator
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
def Singleton():
|
|
2
|
+
|
|
3
|
+
def decorator(cls):
|
|
4
|
+
|
|
5
|
+
original_new = cls.__new__
|
|
6
|
+
|
|
7
|
+
def singleton_new(cls, *args, **kwargs):
|
|
8
|
+
|
|
9
|
+
if not hasattr(cls, "_instance"):
|
|
10
|
+
cls._instance = original_new(cls)
|
|
11
|
+
|
|
12
|
+
return cls._instance
|
|
13
|
+
|
|
14
|
+
cls.__new__ = singleton_new
|
|
15
|
+
|
|
16
|
+
return cls
|
|
17
|
+
|
|
18
|
+
return decorator
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
def MaxLen(maxLen: int):
|
|
2
|
+
|
|
3
|
+
def decorator(function: callable):
|
|
4
|
+
def verify(*args, **kwargs):
|
|
5
|
+
if len(args[0]) > maxLen:
|
|
6
|
+
raise ValueError("La valeur maximal est dépassée")
|
|
7
|
+
|
|
8
|
+
else:
|
|
9
|
+
return function(*args, **kwargs)
|
|
10
|
+
|
|
11
|
+
return verify
|
|
12
|
+
return decorator
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
def MinLen(minLen: int):
|
|
2
|
+
|
|
3
|
+
def decorator(function: callable):
|
|
4
|
+
def verify(*args, **kwargs):
|
|
5
|
+
|
|
6
|
+
if len(args[0]) < minLen:
|
|
7
|
+
raise ValueError("La valeur minimal n'est pas atteinte")
|
|
8
|
+
|
|
9
|
+
else:
|
|
10
|
+
return function(*args, **kwargs)
|
|
11
|
+
|
|
12
|
+
return verify
|
|
13
|
+
return decorator
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
def Retry(cathed_exception: Exception, amount: int=1):
|
|
3
|
+
def decorator(function: callable):
|
|
4
|
+
def execute(*args, **kwargs):
|
|
5
|
+
tries = 1
|
|
6
|
+
while tries <= amount:
|
|
7
|
+
try:
|
|
8
|
+
return function(*args, **kwargs)
|
|
9
|
+
except cathed_exception:
|
|
10
|
+
tries += 1
|
|
11
|
+
|
|
12
|
+
raise TimeoutError("Le nombre de tentative de la fonction est dépassé")
|
|
13
|
+
|
|
14
|
+
return execute
|
|
15
|
+
return decorator
|
|
16
|
+
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
def ValidateType():
|
|
5
|
+
def decorator(function: callable):
|
|
6
|
+
signature = inspect.signature(function)
|
|
7
|
+
|
|
8
|
+
def validate(*args, **kwargs):
|
|
9
|
+
|
|
10
|
+
bound = signature.bind(*args, **kwargs)
|
|
11
|
+
|
|
12
|
+
for name, value in bound.arguments.items():
|
|
13
|
+
|
|
14
|
+
if name in ("self", "cls"):
|
|
15
|
+
continue
|
|
16
|
+
|
|
17
|
+
expected = signature.parameters[name].annotation
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
if expected is inspect.Parameter.empty or expected is Any:
|
|
21
|
+
continue
|
|
22
|
+
|
|
23
|
+
if isinstance(value, expected):
|
|
24
|
+
continue
|
|
25
|
+
|
|
26
|
+
else:
|
|
27
|
+
raise TypeError(f"Le type de {name} doit être {expected}")
|
|
28
|
+
|
|
29
|
+
return function(*args, **kwargs)
|
|
30
|
+
|
|
31
|
+
return validate
|
|
32
|
+
return decorator
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
class Final:
|
|
4
|
+
|
|
5
|
+
def __init__(self, value: Any):
|
|
6
|
+
|
|
7
|
+
self.value = value
|
|
8
|
+
|
|
9
|
+
def __set__(self, instance, value):
|
|
10
|
+
|
|
11
|
+
raise ValueError("Ne peut pas changer un Final")
|
|
12
|
+
|
|
13
|
+
def __get__(self, instance, owner):
|
|
14
|
+
if instance is None:
|
|
15
|
+
return self
|
|
16
|
+
|
|
17
|
+
return getattr(instance, self.value, None)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class NonNull:
|
|
2
|
+
|
|
3
|
+
def __init__(self):
|
|
4
|
+
self.name = None
|
|
5
|
+
|
|
6
|
+
def __set_name__(self, owner, name):
|
|
7
|
+
self.name = name
|
|
8
|
+
|
|
9
|
+
def __get__(self, instance, owner):
|
|
10
|
+
if instance is None:
|
|
11
|
+
return self
|
|
12
|
+
|
|
13
|
+
return instance.__dict__.get(self.name)
|
|
14
|
+
|
|
15
|
+
def __set__(self, instance, value):
|
|
16
|
+
try:
|
|
17
|
+
if value is None or len(value) == 0:
|
|
18
|
+
raise ValueError("La valeur ne doit pas être Null")
|
|
19
|
+
|
|
20
|
+
except TypeError:
|
|
21
|
+
if value is None:
|
|
22
|
+
raise ValueError("La valeur ne doit pas être Null")
|
|
23
|
+
|
|
24
|
+
instance.__dict__[self.name] = value
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Decorators/classes/builder.py,sha256=r10Y-T-lBUtRnbsfnYZq7l2Qs_WBTnQgMs86wCNZzGY,2038
|
|
2
|
+
Decorators/classes/interface.py,sha256=E0JFrNbR2s_-Myuu__aEVenBfTC31sM1dmVG7iRyvXo,439
|
|
3
|
+
Decorators/classes/noChange.py,sha256=gOK6q7zNRVTuD_EQMRJVeJ6ZsXs7nZ477UdVtvfTnkc,118
|
|
4
|
+
Decorators/classes/singleton.py,sha256=xWHSSXn1lS18__ksmTHae7Ar6B9Ags4nUjPVyyU7ksA,354
|
|
5
|
+
Decorators/functions/fallback.py,sha256=RU9slD2gNGr8qiK_iT6plkb4pcEx1TdbaAJAX-7fSTU,248
|
|
6
|
+
Decorators/functions/maxLen.py,sha256=8zaebsuVytZx0P-CrV7A8kTIYPc1tCi_56QQvc1ZqBM,331
|
|
7
|
+
Decorators/functions/minLen.py,sha256=6en7iH29tH9APuVkIde7eU0xvYnZrbiJ3x5ljzec-XM,337
|
|
8
|
+
Decorators/functions/retry.py,sha256=6cNCtyFz4MDAAK7qhDDmngjgdhsZSig1vpmTa5ufNJg,499
|
|
9
|
+
Decorators/functions/validateType.py,sha256=fI8qWwzWGj2SMwbtzAwi66QESPNiOaOhQc9qk3IjP7E,845
|
|
10
|
+
Decorators/variables/final.py,sha256=kHruaeXLBZMgB3YLlkZhEFLjcjS9lOuXAsBgbAZnwfY,362
|
|
11
|
+
Decorators/variables/nonNull.py,sha256=0a8DUIakTnroaEdd1o8Ej7qOkk9baNG8KItgRv2g6-Q,649
|
|
12
|
+
decoratorstoolkit-1.0.0.dist-info/METADATA,sha256=UKuto557t2mRsuu36h-5YYqyIioJATryK1J_sHZzLq8,104
|
|
13
|
+
decoratorstoolkit-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
14
|
+
decoratorstoolkit-1.0.0.dist-info/top_level.txt,sha256=4xi8Km7rU8QlMe7M8yJtF86Of0YP6nrhh7wymb2qz2A,11
|
|
15
|
+
decoratorstoolkit-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Decorators
|