swizzle 0.1.1__py3-none-any.whl → 0.1.2__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.
- swizzle/__init__.py +37 -27
- {swizzle-0.1.1.dist-info → swizzle-0.1.2.dist-info}/METADATA +4 -4
- swizzle-0.1.2.dist-info/RECORD +5 -0
- swizzle-0.1.1.dist-info/RECORD +0 -5
- {swizzle-0.1.1.dist-info → swizzle-0.1.2.dist-info}/WHEEL +0 -0
- {swizzle-0.1.1.dist-info → swizzle-0.1.2.dist-info}/top_level.txt +0 -0
swizzle/__init__.py
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
from functools import wraps
|
2
2
|
import sys
|
3
3
|
import types
|
4
|
-
import inspect
|
5
4
|
|
6
|
-
__version__ = "0.1.
|
5
|
+
__version__ = "0.1.2"
|
7
6
|
|
8
|
-
def
|
7
|
+
def _split(s, sep):
|
8
|
+
if sep == '':
|
9
|
+
return list(s)
|
10
|
+
else:
|
11
|
+
return s.split(sep)
|
12
|
+
|
13
|
+
def _swizzle(func, sep = None):
|
9
14
|
def _getattr(obj, name, default=object()):
|
10
15
|
try:
|
11
16
|
return func(obj, name)
|
@@ -15,29 +20,37 @@ def _swizzle(func):
|
|
15
20
|
@wraps(func)
|
16
21
|
def _swizzle_attributes(obj, name):
|
17
22
|
"""Find attributes of an object that match substrings of a given name."""
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
23
|
+
try:
|
24
|
+
return func(obj, name)
|
25
|
+
except AttributeError:
|
26
|
+
pass
|
27
|
+
if sep is not None:
|
28
|
+
names = _split(name, sep)
|
29
|
+
found_attributes = [func(obj, n) for n in names]
|
30
|
+
else:
|
31
|
+
found_attributes = []
|
32
|
+
sentinel = object()
|
33
|
+
i = 0
|
34
|
+
while i < len(name):
|
35
|
+
match_found = False
|
36
|
+
for j in range(len(name), i, -1):
|
37
|
+
substr = name[i:j]
|
38
|
+
attr = _getattr(obj, substr, sentinel)
|
39
|
+
if attr is not sentinel:
|
40
|
+
found_attributes.append(attr)
|
41
|
+
i = j # Move index to end of the matched substring
|
42
|
+
match_found = True
|
43
|
+
break
|
44
|
+
if not match_found:
|
45
|
+
raise AttributeError(f"No matching attribute found for substring: {name[i:]}")
|
46
|
+
return tuple(found_attributes)
|
34
47
|
return _swizzle_attributes
|
35
48
|
|
36
|
-
def swizzle(cls=None, meta = False):
|
49
|
+
def swizzle(cls=None, meta = False, sep = None):
|
37
50
|
def decorator(cls):
|
38
51
|
# Decorate the class's __getattr__ or __getattribute__
|
39
52
|
cls_fn = cls.__getattr__ if hasattr(cls, '__getattr__') else cls.__getattribute__
|
40
|
-
setattr(cls, cls_fn.__name__, _swizzle(cls_fn))
|
53
|
+
setattr(cls, cls_fn.__name__, _swizzle(cls_fn, sep))
|
41
54
|
|
42
55
|
# Handle the meta class
|
43
56
|
if meta:
|
@@ -47,7 +60,7 @@ def swizzle(cls=None, meta = False):
|
|
47
60
|
meta_cls = SwizzleType
|
48
61
|
cls = meta_cls(cls.__name__, cls.__bases__, dict(cls.__dict__))
|
49
62
|
meta_fn = meta_cls.__getattr__ if hasattr(meta_cls, '__getattr__') else meta_cls.__getattribute__
|
50
|
-
setattr(meta_cls, meta_fn.__name__, _swizzle(meta_fn))
|
63
|
+
setattr(meta_cls, meta_fn.__name__, _swizzle(meta_fn, sep))
|
51
64
|
return cls
|
52
65
|
|
53
66
|
if cls is None:
|
@@ -55,16 +68,13 @@ def swizzle(cls=None, meta = False):
|
|
55
68
|
else:
|
56
69
|
return decorator(cls)
|
57
70
|
|
58
|
-
|
59
|
-
|
60
|
-
# make swizzle a callable module
|
61
71
|
class Swizzle(types.ModuleType):
|
62
72
|
def __init__(self):
|
63
73
|
types.ModuleType.__init__(self, __name__)
|
64
74
|
self.__dict__.update(sys.modules[__name__].__dict__)
|
65
75
|
|
66
|
-
def __call__(self, cls=None, meta=False):
|
67
|
-
return swizzle(cls, meta)
|
76
|
+
def __call__(self, cls=None, meta=False, sep = None):
|
77
|
+
return swizzle(cls, meta, sep)
|
68
78
|
|
69
79
|
sys.modules[__name__] = Swizzle()
|
70
80
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: swizzle
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.2
|
4
4
|
Summary: Transforms a string representation of a Python literal into the corresponding Python object.
|
5
5
|
Home-page: https://github.com/janthmueller/swizzle
|
6
6
|
Author: Jan T. Müller
|
@@ -77,7 +77,7 @@ class XYZ(IntEnum):
|
|
77
77
|
Z = 3
|
78
78
|
|
79
79
|
# Test the swizzle
|
80
|
-
print(XYZ.yxz) # Output: (2
|
80
|
+
print(XYZ.yxz) # Output: (<XYZ.Y: 2>, <XYZ.X: 1>, <XYZ.Z: 3>)
|
81
81
|
```
|
82
82
|
Setting the `meta` argument to `True` in the swizzle decorator extends the `getattr` behavior of the metaclass, enabling attribute swizzling directly on the class itself.
|
83
83
|
|
@@ -115,8 +115,8 @@ class Test:
|
|
115
115
|
xyz = 7
|
116
116
|
|
117
117
|
# Test the swizzle
|
118
|
-
print(Test.xz) # Output:
|
119
|
-
print(Test.yz) # Output:
|
118
|
+
print(Test.xz) # Output: 6
|
119
|
+
print(Test.yz) # Output: 5
|
120
120
|
print(Test.xyyz) # Output: (4, 5)
|
121
121
|
print(Test.xyzx) # Output: (7, 1)
|
122
122
|
```
|
@@ -0,0 +1,5 @@
|
|
1
|
+
swizzle/__init__.py,sha256=xWLQSOHFJ3eK_I5tnY5LLybU6IbsPocwz-3LmEunDVw,2684
|
2
|
+
swizzle-0.1.2.dist-info/METADATA,sha256=fkh4lJnUzFmgHWaZWp5q5JRNHO7puyjPxvAjS578Fkk,2890
|
3
|
+
swizzle-0.1.2.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
4
|
+
swizzle-0.1.2.dist-info/top_level.txt,sha256=XFSQti81x2zM0zAMCY1YD0lqB1eSg5my9BB03uFgCic,8
|
5
|
+
swizzle-0.1.2.dist-info/RECORD,,
|
swizzle-0.1.1.dist-info/RECORD
DELETED
@@ -1,5 +0,0 @@
|
|
1
|
-
swizzle/__init__.py,sha256=kJNzlAdcopVTPxSaWMVTMgbXThStQE4_Bbepgr_6cTk,2281
|
2
|
-
swizzle-0.1.1.dist-info/METADATA,sha256=60kRVo_hVRzN-fJtatPm8EWBTEgRAJmQgJOdVrREZ24,2869
|
3
|
-
swizzle-0.1.1.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
4
|
-
swizzle-0.1.1.dist-info/top_level.txt,sha256=XFSQti81x2zM0zAMCY1YD0lqB1eSg5my9BB03uFgCic,8
|
5
|
-
swizzle-0.1.1.dist-info/RECORD,,
|
File without changes
|
File without changes
|