Skip to main content

Swizzle enables the retrieval of multiple attributes, similar to swizzling in computer graphics.

Project description

Swizzle

PyPI version Downloads License: MIT GitHub Stars

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 the namedtuple function 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


Download files

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

Source Distribution

swizzle-2.1.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

swizzle-2.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for swizzle-2.1.0.tar.gz
Algorithm Hash digest
SHA256 a32a2b0b2b91f15322bf4f8dce1654f3c34bc52e531a0c202370c66100f7e8ab
MD5 f1901ec198d1190af409d1d8fff23aa1
BLAKE2b-256 9981014bd6da1d67ae169dd7f43362b67bc1654eff694fe0eb40c891f2c4cfab

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swizzle-2.1.0-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

Hashes for swizzle-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6328773d661829e3fa1ebe170b579a6f10a77974317231f632f57bfb350d36f
MD5 35707400477ec9df4c91accd1f2deb29
BLAKE2b-256 0aa0e820a4facf6071cc8366eb8e5bc2bc70a63ad03ddac5ae83530df12cf531

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