swizzle 0.1.1__tar.gz → 0.1.3__tar.gz
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-0.1.1 → swizzle-0.1.3}/PKG-INFO +5 -5
- {swizzle-0.1.1 → swizzle-0.1.3}/README.md +3 -3
- {swizzle-0.1.1 → swizzle-0.1.3}/setup.py +2 -3
- swizzle-0.1.3/swizzle/__init__.py +80 -0
- {swizzle-0.1.1 → swizzle-0.1.3}/swizzle.egg-info/PKG-INFO +5 -5
- swizzle-0.1.1/swizzle/__init__.py +0 -70
- {swizzle-0.1.1 → swizzle-0.1.3}/setup.cfg +0 -0
- {swizzle-0.1.1 → swizzle-0.1.3}/swizzle.egg-info/SOURCES.txt +0 -0
- {swizzle-0.1.1 → swizzle-0.1.3}/swizzle.egg-info/dependency_links.txt +0 -0
- {swizzle-0.1.1 → swizzle-0.1.3}/swizzle.egg-info/top_level.txt +0 -0
@@ -1,7 +1,7 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: swizzle
|
3
|
-
Version: 0.1.
|
4
|
-
Summary:
|
3
|
+
Version: 0.1.3
|
4
|
+
Summary: The Swizzle Decorator enables the retrieval of multiple attributes, similar to swizzling in computer graphics.
|
5
5
|
Home-page: https://github.com/janthmueller/swizzle
|
6
6
|
Author: Jan T. Müller
|
7
7
|
Author-email: mail@jantmueller.com
|
@@ -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
|
```
|
@@ -58,7 +58,7 @@ class XYZ(IntEnum):
|
|
58
58
|
Z = 3
|
59
59
|
|
60
60
|
# Test the swizzle
|
61
|
-
print(XYZ.yxz) # Output: (2
|
61
|
+
print(XYZ.yxz) # Output: (<XYZ.Y: 2>, <XYZ.X: 1>, <XYZ.Z: 3>)
|
62
62
|
```
|
63
63
|
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.
|
64
64
|
|
@@ -96,8 +96,8 @@ class Test:
|
|
96
96
|
xyz = 7
|
97
97
|
|
98
98
|
# Test the swizzle
|
99
|
-
print(Test.xz) # Output:
|
100
|
-
print(Test.yz) # Output:
|
99
|
+
print(Test.xz) # Output: 6
|
100
|
+
print(Test.yz) # Output: 5
|
101
101
|
print(Test.xyyz) # Output: (4, 5)
|
102
102
|
print(Test.xyzx) # Output: (7, 1)
|
103
103
|
```
|
@@ -17,7 +17,7 @@ setup(
|
|
17
17
|
packages=find_packages(exclude=["tests"]),
|
18
18
|
author="Jan T. Müller",
|
19
19
|
author_email="mail@jantmueller.com",
|
20
|
-
description="
|
20
|
+
description="The Swizzle Decorator enables the retrieval of multiple attributes, similar to swizzling in computer graphics.",
|
21
21
|
long_description=long_description,
|
22
22
|
long_description_content_type="text/markdown",
|
23
23
|
url="https://github.com/janthmueller/swizzle",
|
@@ -40,5 +40,4 @@ setup(
|
|
40
40
|
|
41
41
|
# python setup.py sdist bdist_wheel
|
42
42
|
# twine check dist/*
|
43
|
-
#
|
44
|
-
# twine upload dist/*
|
43
|
+
# twine upload dist/* -> insert token
|
@@ -0,0 +1,80 @@
|
|
1
|
+
from functools import wraps
|
2
|
+
import sys
|
3
|
+
import types
|
4
|
+
|
5
|
+
__version__ = "0.1.3"
|
6
|
+
|
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):
|
14
|
+
def _getattr(obj, name, default=object()):
|
15
|
+
try:
|
16
|
+
return func(obj, name)
|
17
|
+
except AttributeError:
|
18
|
+
return default
|
19
|
+
|
20
|
+
@wraps(func)
|
21
|
+
def _swizzle_attributes(obj, name):
|
22
|
+
"""Find attributes of an object that match substrings of a given name."""
|
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)
|
47
|
+
return _swizzle_attributes
|
48
|
+
|
49
|
+
def swizzle(cls=None, meta = False, sep = None):
|
50
|
+
def decorator(cls):
|
51
|
+
# Decorate the class's __getattr__ or __getattribute__
|
52
|
+
cls_fn = cls.__getattr__ if hasattr(cls, '__getattr__') else cls.__getattribute__
|
53
|
+
setattr(cls, cls_fn.__name__, _swizzle(cls_fn, sep))
|
54
|
+
|
55
|
+
# Handle the meta class
|
56
|
+
if meta:
|
57
|
+
meta_cls = type(cls)
|
58
|
+
if meta_cls == type:
|
59
|
+
class SwizzleType(meta_cls): pass
|
60
|
+
meta_cls = SwizzleType
|
61
|
+
cls = meta_cls(cls.__name__, cls.__bases__, dict(cls.__dict__))
|
62
|
+
meta_fn = meta_cls.__getattr__ if hasattr(meta_cls, '__getattr__') else meta_cls.__getattribute__
|
63
|
+
setattr(meta_cls, meta_fn.__name__, _swizzle(meta_fn, sep))
|
64
|
+
return cls
|
65
|
+
|
66
|
+
if cls is None:
|
67
|
+
return decorator
|
68
|
+
else:
|
69
|
+
return decorator(cls)
|
70
|
+
|
71
|
+
class Swizzle(types.ModuleType):
|
72
|
+
def __init__(self):
|
73
|
+
types.ModuleType.__init__(self, __name__)
|
74
|
+
self.__dict__.update(sys.modules[__name__].__dict__)
|
75
|
+
|
76
|
+
def __call__(self, cls=None, meta=False, sep = None):
|
77
|
+
return swizzle(cls, meta, sep)
|
78
|
+
|
79
|
+
sys.modules[__name__] = Swizzle()
|
80
|
+
|
@@ -1,7 +1,7 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: swizzle
|
3
|
-
Version: 0.1.
|
4
|
-
Summary:
|
3
|
+
Version: 0.1.3
|
4
|
+
Summary: The Swizzle Decorator enables the retrieval of multiple attributes, similar to swizzling in computer graphics.
|
5
5
|
Home-page: https://github.com/janthmueller/swizzle
|
6
6
|
Author: Jan T. Müller
|
7
7
|
Author-email: mail@jantmueller.com
|
@@ -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
|
```
|
@@ -1,70 +0,0 @@
|
|
1
|
-
from functools import wraps
|
2
|
-
import sys
|
3
|
-
import types
|
4
|
-
import inspect
|
5
|
-
|
6
|
-
__version__ = "0.1.1"
|
7
|
-
|
8
|
-
def _swizzle(func):
|
9
|
-
def _getattr(obj, name, default=object()):
|
10
|
-
try:
|
11
|
-
return func(obj, name)
|
12
|
-
except AttributeError:
|
13
|
-
return default
|
14
|
-
|
15
|
-
@wraps(func)
|
16
|
-
def _swizzle_attributes(obj, name):
|
17
|
-
"""Find attributes of an object that match substrings of a given name."""
|
18
|
-
found_attributes = []
|
19
|
-
sentinel = object()
|
20
|
-
i = 0
|
21
|
-
while i < len(name):
|
22
|
-
match_found = False
|
23
|
-
for j in range(len(name), i, -1):
|
24
|
-
substr = name[i:j]
|
25
|
-
attr = _getattr(obj, substr, sentinel)
|
26
|
-
if attr is not sentinel:
|
27
|
-
found_attributes.append(attr)
|
28
|
-
i = j # Move index to end of the matched substring
|
29
|
-
match_found = True
|
30
|
-
break
|
31
|
-
if not match_found:
|
32
|
-
raise AttributeError(f"No matching attribute found for substring: {name[i:]}")
|
33
|
-
return tuple(found_attributes)
|
34
|
-
return _swizzle_attributes
|
35
|
-
|
36
|
-
def swizzle(cls=None, meta = False):
|
37
|
-
def decorator(cls):
|
38
|
-
# Decorate the class's __getattr__ or __getattribute__
|
39
|
-
cls_fn = cls.__getattr__ if hasattr(cls, '__getattr__') else cls.__getattribute__
|
40
|
-
setattr(cls, cls_fn.__name__, _swizzle(cls_fn))
|
41
|
-
|
42
|
-
# Handle the meta class
|
43
|
-
if meta:
|
44
|
-
meta_cls = type(cls)
|
45
|
-
if meta_cls == type:
|
46
|
-
class SwizzleType(meta_cls): pass
|
47
|
-
meta_cls = SwizzleType
|
48
|
-
cls = meta_cls(cls.__name__, cls.__bases__, dict(cls.__dict__))
|
49
|
-
meta_fn = meta_cls.__getattr__ if hasattr(meta_cls, '__getattr__') else meta_cls.__getattribute__
|
50
|
-
setattr(meta_cls, meta_fn.__name__, _swizzle(meta_fn))
|
51
|
-
return cls
|
52
|
-
|
53
|
-
if cls is None:
|
54
|
-
return decorator
|
55
|
-
else:
|
56
|
-
return decorator(cls)
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
# make swizzle a callable module
|
61
|
-
class Swizzle(types.ModuleType):
|
62
|
-
def __init__(self):
|
63
|
-
types.ModuleType.__init__(self, __name__)
|
64
|
-
self.__dict__.update(sys.modules[__name__].__dict__)
|
65
|
-
|
66
|
-
def __call__(self, cls=None, meta=False):
|
67
|
-
return swizzle(cls, meta)
|
68
|
-
|
69
|
-
sys.modules[__name__] = Swizzle()
|
70
|
-
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|