Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/weilueluo/pyfallback
Fallback for python objects
https://github.com/weilueluo/pyfallback
fallback python
Last synced: 3 months ago
JSON representation
Fallback for python objects
- Host: GitHub
- URL: https://github.com/weilueluo/pyfallback
- Owner: weilueluo
- License: apache-2.0
- Created: 2023-01-15T11:48:21.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-16T21:21:58.000Z (almost 2 years ago)
- Last Synced: 2024-03-22T20:54:05.948Z (8 months ago)
- Topics: fallback, python
- Language: Python
- Homepage: https://github.com/weilueluo/pyfallback
- Size: 41 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pyfallback
1. Use `Fallback` to wrap an object with fallback value.
2. Safely do stuff that possibly go wrong.
3. Use the `get()` method to retrieve result.## Install
```bash
pip install pyfallback
```## Usage
```python
from pyfallback import Fallback# fallback
json = Fallback({"key": "value"}, fallback="fallback")
json["key"].get() # "value"
json["bla"].get() # "fallback"# chaining
json = Fallback({"key": "1-2-3"}, fallback="4")
json["key"].split("-")[0].get() # "1"
json["bla"].split("-")[0].get() # "4"# iterating
json = Fallback({"key": [1, 2, 3]}, fallback=[4, 5, 6])
[v.get() for v in json["key"]] # [1, 2, 3]
[v.get() for v in json["bla"]] # [4, 5, 6]# see tests/test_fallback.py for more example
```## Contributing
Just submit a pull request :D
Note: this project uses [poetry](https://github.com/python-poetry/poetry) and [pyenv](https://github.com/pyenv/pyenv).