Swizzle
Introduction
Swizzle is a Python package that enhances attribute access, allowing for flexible retrieval of multiple attributes based on specified arrangements of their names.
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.
Features
- Dynamic Attribute Access: Retrieve multiple attributes in any specified arrangement.
- Integration with Existing Classes: Works seamlessly with regular classes,
dataclass, and evenEnumtypes. - Swizzled Setters (New!): Optionally enable attribute assignment with swizzling syntax (e.g.,
vec.xyz = 1,2,3).
Installation
From PyPI
Install Swizzle via pip:
pip install swizzle
From GitHub
Install the latest version directly from GitHub:
pip install git+https://github.com/janthmueller/swizzle.git
Getting Started
Basic Usage with the @swizzle Decorator
Apply the @swizzle decorator to your class:
import swizzle
@swizzle
class Vector:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
v = Vector(1, 2, 3)
# Access attributes in different orders
print(v.yzx) # Output: Vector(y=2, z=3, x=1)
Using Swizzle with dataclass
Swizzle integrates smoothly with Python's dataclass:
import swizzle
from dataclasses import dataclass
@swizzle
@dataclass
class Point:
x: int
y: int
z: int
p = Point(1, 2, 3)
print(p.zxy) # Output: Point(z=3, x=1, y=2)
Swizzling Enums with meta=True
Enable attribute swizzling directly on the class by setting meta=True:
import swizzle
from enum import IntEnum
@swizzle(meta=True)
class Axis(IntEnum):
X = 1
Y = 2
Z = 3
print(Axis.YXZ) # Output: Axis(Y=<Axis.Y: 2>, X=<Axis.X: 1>, Z=<Axis.Z: 3>)
Swizzled Setters (New!)
Starting with the latest version, Swizzle supports setting multiple attributes at once using swizzled assignment syntax:
import swizzle
@swizzle(setter=True)
class Vector:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
v = Vector(1, 2, 3)
# Set multiple attributes in swizzled order
v.zyx = 9, 8, 7
# Access the attributes with swizzle syntax
print(v.zyx) # Output: Vector(z=9, y=8, x=7)
Advanced Usage
Swizzled Named Tuples with swizzledtuple
Create swizzled named tuples inspired by namedtuple:
from swizzle import swizzledtuple
Vector = swizzledtuple('Vector', 'x y z') # Equivalent to swizzle.t('Vector', 'x y z')
v = Vector(1, 2, 3)
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)
Custom Separators for Clearer Access
You can customize the separator used between attribute names in swizzle expressions to make them more readable—especially when combining many fields. Use the sep argument in the @swizzle decorator:
import swizzle
@swizzle(sep='_')
class Vector:
def __init__(self, x, y, z, w):
self.x = x
self.y = y
self.z = z
self.w = w
v = Vector(1, 2, 3, 4)
print(v.x_y_z_w) # Output: Vector(x=1, y=2, z=3, w=4)
This helps visually separate attribute names, making swizzled expressions more readable and less error-prone in complex cases.
License
This project is licensed under the terms of the MIT license. See the LICENSE file for details.
Contributions
Contributions are welcome! Feel free to submit a Pull Request or open an Issue on GitHub.
Metadata
Release files for swizzle 2.6.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| swizzle-2.6.1.tar.gz | 13.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| swizzle-2.6.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.1 kB
Release files / swizzle-2.6.1.tar.gz
| Download URL | swizzle-2.6.1.tar.gz |
|---|---|
| Size | 13.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
11b9c5795076c7acaf260bc742e0b25dd095da079745e07b6b4e0a02f828fb2e
|
|
BLAKE2b-256 checksum How to use checksums |
7bce33e5714220219c9fcfbecf465f50f940c0a8bbbeed6ff8b69a5e63905ffe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Release files / swizzle-2.6.1-py3-none-any.whl
| Download URL | swizzle-2.6.1-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d7dd52a22facabfd01355fc6e4abe5af9683aec126908af01f7d042591190fb6
|
|
BLAKE2b-256 checksum How to use checksums |
7bdcadd7d5c3bbfdb26b29138be90fa48d1b2a1a087e9dce0e6364285c4a4ef9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Security for swizzle 2.6.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.