Name | swizzle JSON |
Version |
2.5.1
JSON |
| download |
home_page | None |
Summary | Swizzle enables the retrieval of multiple attributes, similar to swizzling in computer graphics. |
upload_time | 2025-07-14 11:59:27 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
license | MIT |
keywords |
swizzle
attributes
graphics
development
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Swizzle
[](https://pypi.org/project/swizzle/)
[](https://pepy.tech/project/swizzle)
[](https://github.com/janthmueller/swizzle/blob/main/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**: One call is enough to access multiple attributes.
* **Integration with Existing Classes**: Works seamlessly with regular classes, `dataclass`, and even `Enum` types.
* **Swizzled Setters (New!)**: Optionally enable attribute assignment with swizzling syntax (e.g., `vec.xyz = 1,2,3`).
## Installation
### From PyPI
Install Swizzle via pip:
```bash
pip install swizzle
```
### From GitHub
Install the latest version directly from GitHub:
```bash
pip install git+https://github.com/janthmueller/swizzle.git
```
## Getting Started
### Basic Usage with the `@swizzle` Decorator
Apply the `@swizzle` decorator to your class:
```python
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`:
```python
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`:
```python
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>)
```
### Swizzled Setters (New!)
Starting with the latest version, Swizzle supports setting multiple attributes at once using swizzled assignment syntax:
```python
import swizzle
@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)
# Set multiple attributes in swizzled order
v.zyx = 9, 8, 7
# Access the attributes with swizzle syntax
print(v.zyx) # Output: Vector(z=9, y=8, x=7)
```
## Advanced Usage
### Swizzled Named Tuples with `swizzledtuple`
Create swizzled named tuples inspired by `namedtuple`:
```python
from swizzle import swizzledtuple
Vector = swizzledtuple('Vector', 'x y z') # Equivalent to swizzle.t('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 for Clearer Access
You can customize the separator used between attribute names in swizzle expressions to make them more readable—especially when combining many fields. Use the `sep` argument in the `@swizzle` decorator:
```python
import swizzle
@swizzle(sep='_')
class Vector:
def __init__(self, x, y, z, w):
self.x = x
self.y = y
self.z = z
self.w = w
v = Vector(1, 2, 3, 4)
print(v.x_y_z_w) # Output: Vector(x=1, y=2, z=3, w=4)
```
This helps visually separate attribute names, making swizzled expressions more readable and less error-prone in complex cases.
## 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.
* **Swizzled Setters**: Assign multiple attributes simultaneously in any order (e.g., `v.xyz = 1,2,3`).
## Benefits
* **Efficient Attribute Management**: Simplify complex attribute combinations.
* **Dynamic Access Patterns**: No need for predefined attribute combinations.
* **Cleaner Codebase**: Reduce redundancy and improve maintainability.
## License
This project is licensed under the terms of the MIT license. See the [LICENSE](https://github.com/janthmueller/swizzle/blob/main/LICENSE) file for details.
## Contributions
Contributions are welcome! Feel free to submit a Pull Request or open an Issue on GitHub.
Raw data
{
"_id": null,
"home_page": null,
"name": "swizzle",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "swizzle, attributes, graphics, development",
"author": null,
"author_email": "\"Jan T. M\u00fcller\" <mail@jantmueller.com>",
"download_url": "https://files.pythonhosted.org/packages/72/6b/6e4dc1d09d4e17610398147772a72cff6e17e56aa071f402b44c4c0dd722/swizzle-2.5.1.tar.gz",
"platform": null,
"description": "# Swizzle\n\n[](https://pypi.org/project/swizzle/)\n[](https://pepy.tech/project/swizzle)\n[](https://github.com/janthmueller/swizzle/blob/main/LICENSE)\n\n## Introduction\n\n**Swizzle** is a Python package that enhances attribute access, allowing for flexible retrieval of multiple attributes based on specified arrangements of their names.\n\nManaging 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.\n\n## Features\n\n* **Dynamic Attribute Access**: Retrieve multiple attributes in any specified arrangement.\n* **Reduced Boilerplate**: One call is enough to access multiple attributes.\n* **Integration with Existing Classes**: Works seamlessly with regular classes, `dataclass`, and even `Enum` types.\n* **Swizzled Setters (New!)**: Optionally enable attribute assignment with swizzling syntax (e.g., `vec.xyz = 1,2,3`).\n\n## Installation\n\n### From PyPI\n\nInstall Swizzle via pip:\n\n```bash\npip install swizzle\n```\n\n### From GitHub\n\nInstall the latest version directly from GitHub:\n\n```bash\npip install git+https://github.com/janthmueller/swizzle.git\n```\n\n## Getting Started\n\n### Basic Usage with the `@swizzle` Decorator\n\nApply the `@swizzle` decorator to your class:\n\n```python\nimport swizzle\n\n@swizzle\nclass Vector:\n def __init__(self, x, y, z):\n self.x = x\n self.y = y\n self.z = z\n\nv = Vector(1, 2, 3)\n\n# Access attributes in different orders\nprint(v.yzx) # Output: Vector(y=2, z=3, x=1)\n```\n\n### Using Swizzle with `dataclass`\n\nSwizzle integrates smoothly with Python's `dataclass`:\n\n```python\nimport swizzle\nfrom dataclasses import dataclass\n\n@swizzle\n@dataclass\nclass Point:\n x: int\n y: int\n z: int\n\np = Point(1, 2, 3)\n\nprint(p.zxy) # Output: Point(z=3, x=1, y=2)\n```\n\n### Swizzling Enums with `meta=True`\n\nEnable attribute swizzling directly on the class by setting `meta=True`:\n\n```python\nimport swizzle\nfrom enum import IntEnum\n\n@swizzle(meta=True)\nclass Axis(IntEnum):\n X = 1\n Y = 2\n Z = 3\n\nprint(Axis.YXZ) # Output: Axis(Y=<Axis.Y: 2>, X=<Axis.X: 1>, Z=<Axis.Z: 3>)\n```\n\n### Swizzled Setters (New!)\n\nStarting with the latest version, Swizzle supports setting multiple attributes at once using swizzled assignment syntax:\n\n```python\nimport swizzle\n\n@swizzle(setter=True)\nclass Vector:\n def __init__(self, x, y, z):\n self.x = x\n self.y = y\n self.z = z\n\nv = Vector(1, 2, 3)\n\n# Set multiple attributes in swizzled order\nv.zyx = 9, 8, 7\n\n# Access the attributes with swizzle syntax\nprint(v.zyx) # Output: Vector(z=9, y=8, x=7)\n```\n\n## Advanced Usage\n\n### Swizzled Named Tuples with `swizzledtuple`\n\nCreate swizzled named tuples inspired by `namedtuple`:\n\n```python\nfrom swizzle import swizzledtuple\n\nVector = swizzledtuple('Vector', 'x y z') # Equivalent to swizzle.t('Vector', 'x y z')\n\nv = Vector(1, 2, 3)\n\nprint(v.yzx) # Output: Vector(y=2, z=3, x=1)\nprint(v.yzx.xxzyzz) # Output: Vector(x=1, x=1, z=3, y=2, z=3, z=3)\n```\n\n### Custom Separators for Clearer Access\n\nYou can customize the separator used between attribute names in swizzle expressions to make them more readable\u2014especially when combining many fields. Use the `sep` argument in the `@swizzle` decorator:\n\n```python\nimport swizzle\n\n@swizzle(sep='_')\nclass Vector:\n def __init__(self, x, y, z, w):\n self.x = x\n self.y = y\n self.z = z\n self.w = w\n\nv = Vector(1, 2, 3, 4)\n\nprint(v.x_y_z_w) # Output: Vector(x=1, y=2, z=3, w=4)\n```\n\nThis helps visually separate attribute names, making swizzled expressions more readable and less error-prone in complex cases.\n\n## Understanding Swizzling\n\nSwizzling allows:\n\n* **Rearrangement**: Access attributes in any order (e.g., `v.yzx`).\n* **Duplication**: Access the same attribute multiple times (e.g., `v.xxy`).\n* **Dynamic Composition**: Create new instances with desired attribute arrangements.\n* **Swizzled Setters**: Assign multiple attributes simultaneously in any order (e.g., `v.xyz = 1,2,3`).\n\n## Benefits\n\n* **Efficient Attribute Management**: Simplify complex attribute combinations.\n* **Dynamic Access Patterns**: No need for predefined attribute combinations.\n* **Cleaner Codebase**: Reduce redundancy and improve maintainability.\n\n## License\n\nThis project is licensed under the terms of the MIT license. See the [LICENSE](https://github.com/janthmueller/swizzle/blob/main/LICENSE) file for details.\n\n## Contributions\n\nContributions are welcome! Feel free to submit a Pull Request or open an Issue on GitHub.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Swizzle enables the retrieval of multiple attributes, similar to swizzling in computer graphics.",
"version": "2.5.1",
"project_urls": {
"documentation": "https://janthmueller.github.io/swizzle/",
"homepage": "https://github.com/janthmueller/swizzle",
"source": "https://github.com/janthmueller/swizzle",
"tracker": "https://github.com/janthmueller/swizzle/issues"
},
"split_keywords": [
"swizzle",
" attributes",
" graphics",
" development"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "4bede7111cb0a431d8d6944c133c3a3c7cf29ba02ff015d99f12d3ed2a077190",
"md5": "729fe083c9f2d758da6593b2520001ee",
"sha256": "67b0c893a4c7c13a19d5ff11ad166f76683fd554755e5fb9c6f3a73efb0fdf03"
},
"downloads": -1,
"filename": "swizzle-2.5.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "729fe083c9f2d758da6593b2520001ee",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 11918,
"upload_time": "2025-07-14T11:59:26",
"upload_time_iso_8601": "2025-07-14T11:59:26.564333Z",
"url": "https://files.pythonhosted.org/packages/4b/ed/e7111cb0a431d8d6944c133c3a3c7cf29ba02ff015d99f12d3ed2a077190/swizzle-2.5.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "726b6e4dc1d09d4e17610398147772a72cff6e17e56aa071f402b44c4c0dd722",
"md5": "0976a8f602d5c372b176ebe4158aa745",
"sha256": "28a862ec49b59fe6f55c54e4526fbdff83766a82c299fe73d73c826590acceab"
},
"downloads": -1,
"filename": "swizzle-2.5.1.tar.gz",
"has_sig": false,
"md5_digest": "0976a8f602d5c372b176ebe4158aa745",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 13926,
"upload_time": "2025-07-14T11:59:27",
"upload_time_iso_8601": "2025-07-14T11:59:27.314552Z",
"url": "https://files.pythonhosted.org/packages/72/6b/6e4dc1d09d4e17610398147772a72cff6e17e56aa071f402b44c4c0dd722/swizzle-2.5.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-14 11:59:27",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "janthmueller",
"github_project": "swizzle",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "swizzle"
}