swizzle 0.1.0__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 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.0"
5
+ __version__ = "0.1.2"
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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: swizzle
3
- Version: 0.1.0
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
@@ -35,6 +35,11 @@ print(Vector(1, 2, 3).yzx) # Output: (2, 3, 1)
35
35
  ```
36
36
 
37
37
  ## Installation
38
+ ### From PyPI
39
+ ```bash
40
+ pip install swizzle
41
+ ```
42
+ ### From GitHub
38
43
  ```bash
39
44
  pip install git+https://github.com/janthmueller/swizzle.git
40
45
  ```
@@ -72,7 +77,7 @@ class XYZ(IntEnum):
72
77
  Z = 3
73
78
 
74
79
  # Test the swizzle
75
- print(XYZ.yxz) # Output: (2, 3, 1)
80
+ print(XYZ.yxz) # Output: (<XYZ.Y: 2>, <XYZ.X: 1>, <XYZ.Z: 3>)
76
81
  ```
77
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.
78
83
 
@@ -110,8 +115,8 @@ class Test:
110
115
  xyz = 7
111
116
 
112
117
  # Test the swizzle
113
- print(Test.xz) # Output: (6,)
114
- print(Test.yz) # Output: (5,)
118
+ print(Test.xz) # Output: 6
119
+ print(Test.yz) # Output: 5
115
120
  print(Test.xyyz) # Output: (4, 5)
116
121
  print(Test.xyzx) # Output: (7, 1)
117
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,,
@@ -1,5 +0,0 @@
1
- swizzle/__init__.py,sha256=6xrT6tZERBQWGNPvWofOiSnVGzq-0-nRWbAjt9-czJY,2281
2
- swizzle-0.1.0.dist-info/METADATA,sha256=f4MejVS5TgwsY0hroO6wR7gWIJjLl1Guzx4K_zhm8j4,2807
3
- swizzle-0.1.0.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
4
- swizzle-0.1.0.dist-info/top_level.txt,sha256=XFSQti81x2zM0zAMCY1YD0lqB1eSg5my9BB03uFgCic,8
5
- swizzle-0.1.0.dist-info/RECORD,,