https://github.com/alexdelorenzo/unpackable
📦 Destructure Python objects
https://github.com/alexdelorenzo/unpackable
Last synced: 26 days ago
JSON representation
📦 Destructure Python objects
- Host: GitHub
- URL: https://github.com/alexdelorenzo/unpackable
- Owner: alexdelorenzo
- License: lgpl-3.0
- Created: 2021-07-04T01:59:08.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-01-27T03:24:39.000Z (over 3 years ago)
- Last Synced: 2025-05-08T04:45:12.476Z (26 days ago)
- Language: Python
- Homepage: https://alexdelorenzo.dev
- Size: 20.5 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unpackable: Object destructuring for Python
`unpackable` is a module that lets you use [Python's destructuring assignment](https://www.python.org/dev/peps/pep-3132/) to unpack an object's attributes.## Use case
Consider [JavaScript's destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) feature that allows you to do the following:
```javascript
class User {
constructor(id, email) {
this.id = id;
this.email = email;
}
}const user = new User(1, '[email protected]')
const {id, email} = user; // destructure
````unpackable` allows you to do something similar in Python:
```python
from dataclasses import dataclass
from unpackable import Unpackable@dataclass
class User(Unpackable):
id: int
email: struser = User(1, '[email protected]')
id, email = user # destructure
````unpackable` can also unpack objects that don't subclass `Unpackable`:
```python
from unpackable import unpack@dataclass
class User:
id: int
email: struser = User(1, '[email protected]')
id, email = unpack(user) # destructure
```# Status
`unpackable` currently works with iterable objects, `dataclasses` and simple objects.This is alpha software and is not ready for use beyond limited use cases like in [my `app_paths` project](https://github.com/alexdelorenzo/app_paths).
# Installation
## Requirements
- Python 3.8+## PyPI
```bash
python3 -m pip install unpackable
```# Support
Want to support this project and [other open-source projects](https://github.com/alexdelorenzo) like it?# License
See `LICENSE`. If you'd like to use this project with a different license, please get in touch.