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 Downloads GitHub License

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 or defining new methods for each combination.

Features

  • Dynamic Attribute Access: Retrieve multiple attributes in any specified arrangement.
  • Reduced Boilerplate: Eliminate redundant code by avoiding multiple getter methods.
  • Integration with Existing Classes: Works seamlessly with regular classes, dataclass, and even Enum types.
  • Customizable Matching: Control attribute matching behavior with parameters like sep.

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

Advanced Usage

Swizzled Named Tuples with swizzledtuple

Create swizzled named tuples inspired by namedtuple:

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)

Custom Separators

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, sep='_')
class BoundingBox:
    x = 10
    y = 20
    w = 100
    h = 200

print(BoundingBox.x_y_w_h)  # Output: BoundingBox(x=10, y=20, w=100, h=200)

Understanding Swizzling

Swizzling allows:

  • Rearrangement: Access attributes in any order (e.g., v.yzx).
  • Duplication: Access the same attribute multiple times (e.g., v.xxy).
  • Dynamic Composition: Create new instances with desired attribute arrangements.

Benefits

  • Efficient Attribute Management: Simplify complex attribute combinations.
  • Dynamic Access Patterns: No need for predefined attribute combinations.
  • Cleaner Codebase: Reduce redundancy and improve maintainability.
  • Enhanced Readability: Code becomes more expressive and intuitive.

Conclusion

Swizzle is a powerful tool that streamlines attribute management in Python. It offers a flexible and efficient way to access and manipulate object attributes, making your code cleaner and more maintainable.

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.

Acknowledgments

Inspired by swizzling in graphics programming, Swizzle brings similar flexibility to Python attribute management.


Give Swizzle a try and see how it can simplify your Python projects!

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.1.tar.gz (9.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.2.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: swizzle-2.2.1.tar.gz
  • Upload date:
  • Size: 9.8 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.1.tar.gz
Algorithm Hash digest
SHA256 2cde503e22ff54f7693291df124bf30b3f7da2511ee196206b012e4d40012b2d
MD5 092fa301493a27e5ffbd122f7e2c4e35
BLAKE2b-256 3eecf5082df621ea55c54ba47360a48b0fe5bf1871eba5bb3e17019307c145bf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swizzle-2.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 54733b18e1d00607443631558e6812017914f3a28c52656265c5afea9401549f
MD5 7b34821bae614975a9e65bb7aa0510d9
BLAKE2b-256 b085bd835a6a0306d863374c16de8aaf778f634990333bbdb79f7546fe0574e5

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