Swizzle enables the retrieval of multiple attributes, similar to swizzling in computer graphics.
Project description
Swizzle
Swizzle for Python enhances attribute lookup methods to facilitate dynamic and flexible retrieval of multiple attributes based on specified arrangements of their names.
Update v2: Introducing
swizzledtuple, a new function that allows you to create swizzled named tuples. This feature is inspired by thenamedtuplefunction from the collections module and provides a concise way to define swizzled tuples.from swizzle import swizzledtuple Vector = swizzledtuple('Vector', 'x y z', arrange_names = "y z x x") # Test the swizzle v = Vector(1, 2, 3) print(v) # Output: Vector(y=2, z=3, x=1, x=1) print(v.yzx) # Output: Vector(y = 2, z = 3, x = 1) print(v.yzx.xxzyzz) # Output: Vector(x=1, x=1, z=3, y=2, z=3, z=3)
Swizzle Decorator:
import swizzle
@swizzle
class Vector:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
# Test the swizzle
print(Vector(1, 2, 3).yzx) # Output: Vector(y = 2, z = 3, x = 1)
Installation
From PyPI
pip install swizzle
From GitHub
pip install git+https://github.com/janthmueller/swizzle.git
Further Examples
Using swizzle with dataclass
import swizzle
from dataclasses import dataclass
@swizzle
@dataclass
class Vector:
x: int
y: int
z: int
# Test the swizzle
print(Vector(1, 2, 3).yzx) # Output: Vector(y = 2, z = 3, x = 1)
Using swizzle with IntEnum
import swizzle
from enum import IntEnum
@swizzle(meta=True)
class Vector(IntEnum):
X = 1
Y = 2
Z = 3
# Test the swizzle
print(Vector.YXZ) # Output: Vector(Y=<Vector.Y: 2>, X=<Vector.X: 1>, Z=<Vector.Z: 3>)
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.
Sequential matching
Attributes are matched from left to right, starting with the longest substring match. This behavior can be controlled by the seperator argument in the swizzle decorator.
import swizzle
@swizzle(meta=True)
class Vector:
x = 1
y = 2
z = 3
xy = 4
yz = 5
xz = 6
xyz = 7
# Test the swizzle
print(Vector.xz) # Output: 6
print(Vector.yz) # Output: 5
print(Vector.xyyz) # Output: Vector(xy=4, yz=5)
print(Vector.xyzx) # Output: Vector(xyz=7, x=1)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swizzle-2.1.1.tar.gz.
File metadata
- Download URL: swizzle-2.1.1.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ad16cd5bc65da5449b2b8c7b6706d74c8ffbaee1c9a01c95a74e638708c0401
|
|
| MD5 |
b741eb6faf6d7c605a307073094e01c2
|
|
| BLAKE2b-256 |
b8a1fc6d1f735f1ced671bd50f5b6de6fec211ef34df332ad3dc5a58c4cf5833
|
File details
Details for the file swizzle-2.1.1-py3-none-any.whl.
File metadata
- Download URL: swizzle-2.1.1-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54f4e9b23ee48f828f44046601b67c0d18db6cf482c1a6e399070b04ab70bd14
|
|
| MD5 |
7d8bdfe7e853dd0dbe3913357a738656
|
|
| BLAKE2b-256 |
5a186c89e86cf547f72b117bd4e431dcb286ac0b8ea1f35a17f2fa88e7bd8a92
|