Skip to main content

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

Project description

Swizzle

PyPI Latest Release Pepy Total Downlods GitHub License 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 sep 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.2.0.tar.gz (8.1 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.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for swizzle-2.2.0.tar.gz
Algorithm Hash digest
SHA256 161747aaa5f7aef00b67d23ae15c55c20a583d1d6e5f6e85705328c87ba91786
MD5 fab9578e3b5224ea716b59049e841eeb
BLAKE2b-256 19144f78bdf54e2c0c87d730774ee486c5abe092a982528f126a9820c91fa2b9

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for swizzle-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3266da73489ccf2c2c0b86d162bd3464b57eed2a8ebde617db70872b5fd8579
MD5 7d377bdc2a18b2b023399cb147d1b1a4
BLAKE2b-256 d360eddeb6bf93b69ecb8acfe55ab655cda994e73ff5971dfc6bfe1f3f914266

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