swizzle

Swizzle

PyPI Latest Release Pepy Total Downloads GitHub License

Overview

Swizzle makes it easy to get or set multiple attributes of an object at once, using simple attribute syntax. It works with regular classes, dataclass, Enum, and more.

It provides convenient multi-attribute access. You can grab, combine, or assign attributes in any order or combination.


Installation

From PyPI

pip install swizzle

From GitHub

pip install git+https://github.com/janthmueller/swizzle.git

Getting Started

Basic Usage with the @swizzle Decorator

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)
print(v.yzx)  # Output: Vector(y=2, z=3, x=1)

Swizzled Setters

@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)
v.zyx = 9, 8, 7
print(v.zyx)  # Output: Vector(z=9, y=8, x=7)

Tip: Using __slots__ can be a good practice when setter=True, as it helps prevent accidentally creating new attributes if names are mistyped.

Custom Separators

For objects with multiple fields, combining attribute names without a separator can become hard to read. You can define a separator to make expressions clearer:

import swizzle

@swizzle(sep='_')
class Person:
    def __init__(self, name, age, city, country):
        self.name = name
        self.age = age
        self.city = city
        self.country = country

p = Person("Jane", 30, "Berlin", "Germany")

# Access multiple attributes clearly using underscores
print(p.name_age_city_country)  
# Output: Person(name='Jane', age=30, city='Berlin', country='Germany')

Without a separator, p.nameagecitycountry is harder to read. Using sep='_' keeps your attribute combinations clear and expressive.

Swizzled Named Tuples

Inspired by namedtuple, swizzledtuple is the default output type for swizzled attributes.

from swizzle import swizzledtuple

Vector = swizzledtuple('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)

You can also change the type of the returned object by passing the type argument to the @swizzle decorator. You could return a plain tuple or list if you prefer.

Using Swizzle with dataclass

from dataclasses import dataclass
import swizzle

@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

from enum import IntEnum
import swizzle

@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>)

Setting meta=True enables swizzling on class attributes.


Documentation and Advanced Usage

For more advanced features, custom settings, and examples, see the full documentation: Swizzle Docs


Feedback and Use Cases

Swizzle was built to explore flexible attribute manipulation in Python. Feedback and suggestions are welcome. I would love to hear:

  • Interesting use cases you discover
  • Ideas for improvements or additional features

Feel free to open an issue or PR if you try it out.


License

MIT License. See LICENSE

Visit original content creator repository https://github.com/janthmueller/swizzle

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *