The Swizzle Decorator enables the retrieval of multiple attributes, similar to swizzling in computer graphics.
Project description
Swizzle Decorator
The Swizzle Decorator for Python enhances attribute lookup methods (__getattr__ or __getattribute__) to facilitate dynamic and flexible retrieval of multiple attributes based on specified arrangements of their names. This concept is reminiscent of swizzling in computer graphics, where it allows efficient access to components of vectors or coordinates in various orders:
import swizzle
@swizzle
class Vector:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
print(Vector(1, 2, 3).yzx) # Output: (2, 3, 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 XYZ:
x: int
y: int
z: int
# Test the swizzle
xyz = XYZ(1, 2, 3)
print(xyz.yzx) # Output: (2, 3, 1)
Using swizzle with IntEnum
import swizzle
from enum import IntEnum
@swizzle(meta=True)
class XYZ(IntEnum):
X = 1
Y = 2
Z = 3
# Test the swizzle
print(XYZ.yxz) # Output: (<XYZ.Y: 2>, <XYZ.X: 1>, <XYZ.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.
Using swizzle with NamedTuple
import swizzle
from typing import NamedTuple
@swizzle
class XYZ(NamedTuple):
x: int
y: int
z: int
# Test the swizzle
xyz = XYZ(1, 2, 3)
print(xyz.yzx) # Output: (2, 3, 1)
Sequential matching
Attributes are matched from left to right, starting with the longest substring match.
import swizzle
@swizzle(meta=True)
class Test:
x = 1
y = 2
z = 3
xy = 4
yz = 5
xz = 6
xyz = 7
# Test the swizzle
print(Test.xz) # Output: 6
print(Test.yz) # Output: 5
print(Test.xyyz) # Output: (4, 5)
print(Test.xyzx) # Output: (7, 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-0.1.3.tar.gz.
File metadata
- Download URL: swizzle-0.1.3.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d8817315e21577b1fe3f7977d0bf92e83ab4a47b610ca748c1198fd60a91f49
|
|
| MD5 |
ce9d781bc36359d71be17a59a22aa918
|
|
| BLAKE2b-256 |
75a0df3985f8dc6d790cbcd3721b253e5177d047d4df652a4a402986c7b9c1d2
|
File details
Details for the file swizzle-0.1.3-py3-none-any.whl.
File metadata
- Download URL: swizzle-0.1.3-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2f254a529d8e923d7d2615ea89d891b673c51a7f6fe31a2fe95307e47b65a31
|
|
| MD5 |
64f1cf84eb83ef96dcb39e0b941efc23
|
|
| BLAKE2b-256 |
907d18c575173eac47143c7ba46660f76de6b5bac23a69a075a16d8a9a5739b3
|