swizzle 2.2.0__py3-none-any.whl → 2.3.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.
swizzle/__init__.py CHANGED
@@ -14,7 +14,7 @@ except ImportError:
14
14
 
15
15
  _type = builtins.type
16
16
 
17
- __version__ = "2.2.0"
17
+ __version__ = "2.3.0"
18
18
 
19
19
  MISSING = object()
20
20
 
@@ -43,7 +43,7 @@ def swizzledtuple(typename, field_names, *, rename=False, defaults=None, module=
43
43
  to the order given in `field_names`.
44
44
  sep (str, optional): A separator string that customizes the structure of attribute
45
45
  access. If provided, this sep allows attributes to be accessed by combining field
46
- names with the sep in between them. Defaults to no sep.
46
+ names with the sep in between them. Defaults to None.
47
47
 
48
48
  Returns:
49
49
  type: A new subclass of `tuple` with named fields and customized attribute access.
@@ -196,6 +196,40 @@ def swizzledtuple(typename, field_names, *, rename=False, defaults=None, module=
196
196
  def __getattribute__(self, attr_name):
197
197
  return super(_tuple, self).__getattribute__(attr_name)
198
198
 
199
+ # def __getitem__(self, index):
200
+ # a_names = arrange_names[index]
201
+ # _sep = '' if sep is None else sep
202
+ # return getattr(self, _sep.join(a_names))
203
+
204
+ def __getitem__(self, index):
205
+ if not isinstance(index, slice):
206
+ return _tuple.__getitem__(self, index)
207
+
208
+ selected_indices = arrange_indices[index]
209
+ selected_values = _tuple.__getitem__(self, index)
210
+
211
+ seen = set()
212
+ filtered = [
213
+ (i, v, field_names[i])
214
+ for i, v in zip(selected_indices, selected_values)
215
+ if not (i in seen or seen.add(i))
216
+ ]
217
+
218
+ if filtered:
219
+ _, filtered_values, filtered_names = zip(*filtered)
220
+ else:
221
+ filtered_values, filtered_names = (), ()
222
+
223
+ return swizzledtuple(
224
+ typename,
225
+ filtered_names,
226
+ rename=rename,
227
+ defaults=filtered_values,
228
+ module=module,
229
+ arrange_names=arrange_names[index],
230
+ sep=sep
231
+ )()
232
+
199
233
 
200
234
 
201
235
  for method in (
@@ -206,6 +240,7 @@ def swizzledtuple(typename, field_names, *, rename=False, defaults=None, module=
206
240
  _asdict,
207
241
  __getnewargs__,
208
242
  __getattribute__,
243
+ __getitem__
209
244
  ):
210
245
  method.__qualname__ = f'{typename}.{method.__name__}'
211
246
 
@@ -220,7 +255,8 @@ def swizzledtuple(typename, field_names, *, rename=False, defaults=None, module=
220
255
  '__repr__': __repr__,
221
256
  '_asdict': _asdict,
222
257
  '__getnewargs__': __getnewargs__,
223
- '__getattribute__': __getattribute__
258
+ '__getattribute__': __getattribute__,
259
+ '__getitem__': __getitem__
224
260
  }
225
261
  seen = set()
226
262
  for index, name in enumerate(arrange_names):
@@ -0,0 +1,195 @@
1
+ Metadata-Version: 2.4
2
+ Name: swizzle
3
+ Version: 2.3.0
4
+ Summary: Swizzle enables the retrieval of multiple attributes, similar to swizzling in computer graphics.
5
+ Home-page: https://github.com/janthmueller/swizzle
6
+ Author: Jan T. Müller
7
+ Author-email: mail@jantmueller.com
8
+ License: MIT
9
+ Project-URL: Documentation, https://github.com/janthmueller/swizzle/blob/main/README.md
10
+ Project-URL: Source, https://github.com/janthmueller/swizzle
11
+ Project-URL: Tracker, https://github.com/janthmueller/swizzle/issues
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Requires-Python: >=3.6
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Dynamic: author
21
+ Dynamic: author-email
22
+ Dynamic: classifier
23
+ Dynamic: description
24
+ Dynamic: description-content-type
25
+ Dynamic: home-page
26
+ Dynamic: license
27
+ Dynamic: license-file
28
+ Dynamic: project-url
29
+ Dynamic: requires-python
30
+ Dynamic: summary
31
+
32
+ # Swizzle
33
+
34
+ [![PyPI Latest Release](https://img.shields.io/pypi/v/swizzle.svg)](https://pypi.org/project/swizzle/)
35
+ [![Pepy Total Downloads](https://img.shields.io/pepy/dt/swizzle)](https://pepy.tech/project/swizzle)
36
+ [![GitHub License](https://img.shields.io/github/license/janthmueller/swizzle)](https://github.com/janthmueller/swizzle/blob/main/LICENSE)
37
+
38
+ ## Introduction
39
+
40
+ **Swizzle** is a Python package that enhances attribute access, allowing for flexible retrieval of multiple attributes based on specified arrangements of their names.
41
+
42
+ Managing object attributes efficiently can sometimes become cumbersome, especially when you need to access multiple attributes in various combinations. Swizzle simplifies this process by extending Python's attribute access mechanisms, enabling you to access attributes in any order or combination without explicitly referencing the instance every time or defining new methods for each combination.
43
+
44
+ ## Features
45
+
46
+ - **Dynamic Attribute Access**: Retrieve multiple attributes in any specified arrangement.
47
+ - **Reduced Boilerplate**: Eliminate redundant code by avoiding multiple getter methods.
48
+ - **Integration with Existing Classes**: Works seamlessly with regular classes, `dataclass`, and even `Enum` types.
49
+ - **Customizable Matching**: Control attribute matching behavior with parameters like `sep`.
50
+
51
+ ## Installation
52
+
53
+ ### From PyPI
54
+
55
+ Install Swizzle via pip:
56
+
57
+ ```bash
58
+ pip install swizzle
59
+ ```
60
+
61
+ ### From GitHub
62
+
63
+ Install the latest version directly from GitHub:
64
+
65
+ ```bash
66
+ pip install git+https://github.com/janthmueller/swizzle.git
67
+ ```
68
+
69
+ ## Getting Started
70
+
71
+ ### Basic Usage with the `@swizzle` Decorator
72
+
73
+ Apply the `@swizzle` decorator to your class:
74
+
75
+ ```python
76
+ import swizzle
77
+
78
+ @swizzle
79
+ class Vector:
80
+ def __init__(self, x, y, z):
81
+ self.x = x
82
+ self.y = y
83
+ self.z = z
84
+
85
+ v = Vector(1, 2, 3)
86
+
87
+ # Access attributes in different orders
88
+ print(v.yzx) # Output: Vector(y=2, z=3, x=1)
89
+ ```
90
+
91
+ ### Using Swizzle with `dataclass`
92
+
93
+ Swizzle integrates smoothly with Python's `dataclass`:
94
+
95
+ ```python
96
+ import swizzle
97
+ from dataclasses import dataclass
98
+
99
+ @swizzle
100
+ @dataclass
101
+ class Point:
102
+ x: int
103
+ y: int
104
+ z: int
105
+
106
+ p = Point(1, 2, 3)
107
+
108
+ print(p.zxy) # Output: Point(z=3, x=1, y=2)
109
+ ```
110
+
111
+ ### Swizzling Enums with `meta=True`
112
+
113
+ Enable attribute swizzling directly on the class by setting `meta=True`:
114
+
115
+ ```python
116
+ import swizzle
117
+ from enum import IntEnum
118
+
119
+ @swizzle(meta=True)
120
+ class Axis(IntEnum):
121
+ X = 1
122
+ Y = 2
123
+ Z = 3
124
+
125
+ print(Axis.YXZ) # Output: Axis(Y=<Axis.Y: 2>, X=<Axis.X: 1>, Z=<Axis.Z: 3>)
126
+ ```
127
+
128
+ ## Advanced Usage
129
+
130
+ ### Swizzled Named Tuples with `swizzledtuple`
131
+
132
+ Create swizzled named tuples inspired by `namedtuple`:
133
+
134
+ ```python
135
+ from swizzle import swizzledtuple
136
+
137
+ Vector = swizzledtuple('Vector', 'x y z')
138
+
139
+ v = Vector(1, 2, 3)
140
+
141
+ print(v.yzx) # Output: Vector(y=2, z=3, x=1)
142
+ print(v.yzx.xxzyzz) # Output: Vector(x=1, x=1, z=3, y=2, z=3, z=3)
143
+ ```
144
+
145
+ ### Custom Separators
146
+
147
+ Attributes are matched from left to right, starting with the longest substring match. This behavior can be controlled by the `sep` argument in the swizzle decorator:
148
+
149
+ ```python
150
+ import swizzle
151
+
152
+ @swizzle(meta=True, sep='_')
153
+ class BoundingBox:
154
+ x = 10
155
+ y = 20
156
+ w = 100
157
+ h = 200
158
+
159
+ print(BoundingBox.x_y_w_h) # Output: BoundingBox(x=10, y=20, w=100, h=200)
160
+ ```
161
+
162
+ ## Understanding Swizzling
163
+
164
+ Swizzling allows:
165
+
166
+ - **Rearrangement**: Access attributes in any order (e.g., `v.yzx`).
167
+ - **Duplication**: Access the same attribute multiple times (e.g., `v.xxy`).
168
+ - **Dynamic Composition**: Create new instances with desired attribute arrangements.
169
+
170
+ ## Benefits
171
+
172
+ - **Efficient Attribute Management**: Simplify complex attribute combinations.
173
+ - **Dynamic Access Patterns**: No need for predefined attribute combinations.
174
+ - **Cleaner Codebase**: Reduce redundancy and improve maintainability.
175
+ - **Enhanced Readability**: Code becomes more expressive and intuitive.
176
+
177
+ ## Conclusion
178
+
179
+ Swizzle is a powerful tool that streamlines attribute management in Python. It offers a flexible and efficient way to access and manipulate object attributes, making your code cleaner and more maintainable.
180
+
181
+ ## License
182
+
183
+ This project is licensed under the terms of the MIT license. See the [LICENSE](https://github.com/janthmueller/swizzle/blob/main/LICENSE) file for details.
184
+
185
+ ## Contributions
186
+
187
+ Contributions are welcome! Feel free to submit a Pull Request or open an Issue on GitHub.
188
+
189
+ ## Acknowledgments
190
+
191
+ Inspired by swizzling in graphics programming, Swizzle brings similar flexibility to Python attribute management.
192
+
193
+ ---
194
+
195
+ Give Swizzle a try and see how it can simplify your Python projects!
@@ -0,0 +1,6 @@
1
+ swizzle/__init__.py,sha256=oEH3oUXQiz3nl6yf0tkFyHkiK43iPNeXhP9qSsUd5yw,16406
2
+ swizzle-2.3.0.dist-info/licenses/LICENSE,sha256=WDAegKWtl3rZUiN-SQ2FEQQwEFxlM_jEKQyJRJawJXo,1070
3
+ swizzle-2.3.0.dist-info/METADATA,sha256=-eXIDaSiwrN82ROSZmJF5UDRScA5aNUQF-T8y_BaXA0,5684
4
+ swizzle-2.3.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
5
+ swizzle-2.3.0.dist-info/top_level.txt,sha256=XFSQti81x2zM0zAMCY1YD0lqB1eSg5my9BB03uFgCic,8
6
+ swizzle-2.3.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.2.0)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,128 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: swizzle
3
- Version: 2.2.0
4
- Summary: Swizzle enables the retrieval of multiple attributes, similar to swizzling in computer graphics.
5
- Home-page: https://github.com/janthmueller/swizzle
6
- Author: Jan T. Müller
7
- Author-email: mail@jantmueller.com
8
- License: MIT
9
- Project-URL: Documentation, https://github.com/janthmueller/swizzle/blob/main/README.md
10
- Project-URL: Source, https://github.com/janthmueller/swizzle
11
- Project-URL: Tracker, https://github.com/janthmueller/swizzle/issues
12
- Classifier: Intended Audience :: Developers
13
- Classifier: Topic :: Software Development :: Libraries :: Python Modules
14
- Classifier: License :: OSI Approved :: MIT License
15
- Classifier: Operating System :: OS Independent
16
- Classifier: Programming Language :: Python :: 3 :: Only
17
- Requires-Python: >=3.6
18
- Description-Content-Type: text/markdown
19
- License-File: LICENSE
20
-
21
- # Swizzle
22
- [![PyPI Latest Release](https://img.shields.io/pypi/v/swizzle.svg)](https://pypi.org/project/swizzle/)
23
- [![Pepy Total Downlods](https://img.shields.io/pepy/dt/swizzle)](https://pepy.tech/project/swizzle)
24
- [![GitHub License](https://img.shields.io/github/license/janthmueller/swizzle)](https://github.com/janthmueller/swizzle/blob/main/LICENSE)
25
- [![GitHub Stars](https://img.shields.io/github/stars/janthmueller/swizzle.svg)](https://github.com/janthmueller/swizzle/stargazers)
26
-
27
- **Swizzle** for Python enhances attribute lookup methods to facilitate dynamic and flexible retrieval of multiple attributes based on specified arrangements of their names.
28
- > **Update v2:**
29
- > Introducing `swizzledtuple` , a new function that allows you to create swizzled named tuples. This feature is inspired by the `namedtuple` function from the [collections module](https://docs.python.org/3/library/collections.html#collections.namedtuple) and provides a concise way to define swizzled tuples.
30
- > ```python
31
- > from swizzle import swizzledtuple
32
- >
33
- > Vector = swizzledtuple('Vector', 'x y z', arrange_names = "y z x x")
34
- >
35
- > # Test the swizzle
36
- > v = Vector(1, 2, 3)
37
- > print(v) # Output: Vector(y=2, z=3, x=1, x=1)
38
- > print(v.yzx) # Output: Vector(y = 2, z = 3, x = 1)
39
- > print(v.yzx.xxzyzz) # Output: Vector(x=1, x=1, z=3, y=2, z=3, z=3)
40
- >```
41
-
42
- ### Swizzle Decorator:
43
-
44
- ```python
45
- import swizzle
46
-
47
- @swizzle
48
- class Vector:
49
- def __init__(self, x, y, z):
50
- self.x = x
51
- self.y = y
52
- self.z = z
53
-
54
- # Test the swizzle
55
- print(Vector(1, 2, 3).yzx) # Output: Vector(y = 2, z = 3, x = 1)
56
- ```
57
-
58
-
59
- ## Installation
60
- ### From PyPI
61
- ```bash
62
- pip install swizzle
63
- ```
64
- ### From GitHub
65
- ```bash
66
- pip install git+https://github.com/janthmueller/swizzle.git
67
- ```
68
-
69
- ## Further Examples
70
-
71
- ### Using `swizzle` with `dataclass`
72
-
73
- ```python
74
- import swizzle
75
- from dataclasses import dataclass
76
-
77
- @swizzle
78
- @dataclass
79
- class Vector:
80
- x: int
81
- y: int
82
- z: int
83
-
84
- # Test the swizzle
85
- print(Vector(1, 2, 3).yzx) # Output: Vector(y = 2, z = 3, x = 1)
86
- ```
87
-
88
- ### Using `swizzle` with `IntEnum`
89
-
90
- ```python
91
- import swizzle
92
- from enum import IntEnum
93
-
94
- @swizzle(meta=True)
95
- class Vector(IntEnum):
96
- X = 1
97
- Y = 2
98
- Z = 3
99
-
100
- # Test the swizzle
101
- print(Vector.YXZ) # Output: Vector(Y=<Vector.Y: 2>, X=<Vector.X: 1>, Z=<Vector.Z: 3>)
102
- ```
103
- 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.
104
-
105
-
106
- ### Sequential matching
107
- Attributes are matched from left to right, starting with the longest substring match. This behavior can be controlled by the `sep` argument in the swizzle decorator.
108
- ```python
109
- import swizzle
110
-
111
- @swizzle(meta=True)
112
- class Vector:
113
- x = 1
114
- y = 2
115
- z = 3
116
- xy = 4
117
- yz = 5
118
- xz = 6
119
- xyz = 7
120
-
121
- # Test the swizzle
122
- print(Vector.xz) # Output: 6
123
- print(Vector.yz) # Output: 5
124
- print(Vector.xyyz) # Output: Vector(xy=4, yz=5)
125
- print(Vector.xyzx) # Output: Vector(xyz=7, x=1)
126
- ```
127
-
128
-
@@ -1,6 +0,0 @@
1
- swizzle/__init__.py,sha256=tdMYMDJVpTR3TGGyvQz5r6Qm48uzwdpfxt8EnmqTFbU,15304
2
- swizzle-2.2.0.dist-info/LICENSE,sha256=WDAegKWtl3rZUiN-SQ2FEQQwEFxlM_jEKQyJRJawJXo,1070
3
- swizzle-2.2.0.dist-info/METADATA,sha256=5GpqF5HP7Zlj3HGy-3z0r4Z79U10ODitqRJ5iwoQ4t4,3819
4
- swizzle-2.2.0.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
5
- swizzle-2.2.0.dist-info/top_level.txt,sha256=XFSQti81x2zM0zAMCY1YD0lqB1eSg5my9BB03uFgCic,8
6
- swizzle-2.2.0.dist-info/RECORD,,