Skip to main content

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)

To Do

  • Add support for module-level swizzling
  • Swizzle for method args (swizzle+partial)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swizzle-0.1.4.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swizzle-0.1.4-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file swizzle-0.1.4.tar.gz.

File metadata

  • Download URL: swizzle-0.1.4.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for swizzle-0.1.4.tar.gz
Algorithm Hash digest
SHA256 88146105bf72d37ab9154746311c9b5d68e498be7986c1b12634236d2a9a554d
MD5 8ad16030557f3d10f8b43f19503436e3
BLAKE2b-256 792611cdcfaece9c4c7c81c9c77f9dc86552e171911d3289cc2fa359e12c6105

See more details on using hashes here.

File details

Details for the file swizzle-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: swizzle-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for swizzle-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c0a6ff1781386bc91bbc31524d489c9504dec3322ce6700e362ac24ef9c56530
MD5 24a27fa7c9335152731e696282b07552
BLAKE2b-256 59ce02fd142ea72e4dcf1b6c60d6e7ded72598bccce27e6dd1cddc5f8a4f0bbd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page