swizzle 0.1.1__py3-none-any.whl → 0.1.3__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 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.1"
5
+ __version__ = "0.1.3"
7
6
 
8
- def _swizzle(func):
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
- 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)
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,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: swizzle
3
- Version: 0.1.1
4
- Summary: Transforms a string representation of a Python literal into the corresponding Python object.
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, 3, 1)
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: (6,)
119
- print(Test.yz) # Output: (5,)
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=Y3Z1mQdZbcjFE612dzwlqMby_qtJtSf68NgmylPPHGw,2684
2
+ swizzle-0.1.3.dist-info/METADATA,sha256=ueORk9NK-YFClvH7EvL7nTFwmXUaaNHkFUZfafMPw6c,2908
3
+ swizzle-0.1.3.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
4
+ swizzle-0.1.3.dist-info/top_level.txt,sha256=XFSQti81x2zM0zAMCY1YD0lqB1eSg5my9BB03uFgCic,8
5
+ swizzle-0.1.3.dist-info/RECORD,,
@@ -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,,