python-autoviv

Overview
The Autovivification library for Python
"In the Perl programming language, autovivification is the automatic creation of new arrays and hashes as required every time an undefined value is dereferenced. Perl autovivification allows a programmer to refer to a structured variable, and arbitrary sub-elements of that structured variable, without expressly declaring the existence of the variable and its complete structure beforehand." https://en.wikipedia.org/wiki/Autovivification
Dev Prerequisites
- python 3.12
- pipx, an optional tool for prerequisite installs
- poetry (install globally with
pipx install poetry) - flake8 (install globally with
pipx install flake8)- flake8-bugbear extension (install with
pipx inject flake8 flake8-bugbear) - flake8-naming extension (install with
pipx inject flake8 pep8-naming)
- flake8-bugbear extension (install with
- black (install globally with
pipx install black) - pre-commit (install globally with
pipx install pre-commit) - just, a Justfile command runner
Windows
Justfile support for Windows requires cygwin. Once installed your PATH will need to be updated to resolve cygpath.exe (probably C:\cygwin64\bin). Justfile will forward any targets with shebangs starting with / to cygwin for execution.
Consider using a bash terminal through WSL instead.
Updating python version:
- Update python version in
Dev Prerequisitesabove - Update [tool.poetry.dependencies] section of
pyproject.toml - Update pyupgrade hook in
.pre-commit-config.yaml - Update python version in
.gitlab-ci.yml
Justfile Targets
install: installs poetry dependencies and pre-commit git hooksupdate_boilerplate: fetches and applies updates from the boilerplate remotetest: runs pytest with test coverage report
Usage
Import autoviv and call parse on any list, dict, or primitive. You can also call loads on serialized JSON
>>> import autoviv
>>> import requests
>>> r = requests.get('http://jsonplaceholder.typicode.com/users')
>>> users = autoviv.parse(r.json())
>>> # or
... users = autoviv.loads(r.text)
>>> for user in users:
... print(user.name)
...
Leanne Graham
Ervin Howell
Clementine Bauch
Patricia Lebsack
Chelsey Dietrich
Mrs. Dennis Schulist
Kurtis Weissnat
Nicholas Runolfsdottir V
Glenna Reichert
Clementina DuBuque
>>> user = users[0]
>>> print(autoviv.pprint(user, indent=4))
{
"username": "Bret",
"website": "hildegard.org",
"name": "Leanne Graham",
"company": {
"bs": "harness real-time e-markets",
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net"
},
"id": 1,
"phone": "1-770-736-8031 x56442",
"address": {
"suite": "Apt. 556",
"street": "Kulas Light",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
},
"zipcode": "92998-3874",
"city": "Gwenborough"
},
"email": "Sincere@april.biz"
}
>>> user.name = 'auto-vivification'
>>> r = requests.put('http://jsonplaceholder.typicode.com/users/{0}'.format(user.id), json=user)
>>> response = autoviv.parse(r.json())
>>> print(response.name)
auto-vivification
>>> new = autoviv.parse({})
>>> new.id = 5
>>> if not new.username:
... new.username = 'New User'
...
>>> new.address.geo.lat = "-42.3433"
>>> new.address.geo.lng = "74.3433"
>>> new.email = 'someone@somewhere.biz'
>>> print(autoviv.pprint(new))
{
"username": "New User",
"email": "someone@somewhere.biz",
"id": 5,
"address": {
"geo": {
"lat": "-42.3433",
"lng": "74.3433"
}
}
}
NoneProp
It should be noted that missing referenced properties, including nested, are gracefully falsey.
>>> import autoviv
>>> data = autoviv.parse({})
>>> data.property.is_none
>>> bool(data.property.is_none)
False
>>> isinstance(data.property.is_none, autoviv.NoneProp)
True
>>> 'some data' in data.property.is_none
False
>>> [x for x in data.property.is_none]
[]
>>> data.property.is_none = None
>>> isinstance(data.property.is_none, autoviv.NoneProp)
False
>>> print(autoviv.pprint(data))
{
"property": {
"is_none": null
}
}
Contributing
Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
This package is available as open source under the terms of the MIT License.
Boilerplate
To support pulling updates from the pyplate python boilerplate, add the boilerplate git remote:
git remote add boilerplate git@gitlab.com:tysonholub/pyplate.git
Then moving forward, run just update_boilerplate to pull latest changes from the boilerplate remote. NOTE: you must keep the boilerplate remote history intact to successfully merge updates from the boilerplate remote.
Metadata
Release files for python-autoviv 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_autoviv-1.0.4.tar.gz | 5.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_autoviv-1.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.6 kB
Release files / python_autoviv-1.0.4.tar.gz
| Download URL | python_autoviv-1.0.4.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8064621dd19e3bfd0ac0d521df1c62566631e0b6854bc099a8d1f3deeeae82ac
|
|
BLAKE2b-256 checksum How to use checksums |
91986f06b23bb02bb0b3f7c7499c3737c0c1db0e062f9d24d7e92809ac1c1a29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.0.1 CPython/3.13.1 Linux/5.15.154+
|
Release files / python_autoviv-1.0.4-py3-none-any.whl
| Download URL | python_autoviv-1.0.4-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b013eac19e70047d040d9c3498755c39ad5227c20480d2911202ac038e3f4319
|
|
BLAKE2b-256 checksum How to use checksums |
4b95844a77b783789f8709cf096e97cb45294ec962c5dc40627c542da89aea83
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.0.1 CPython/3.13.1 Linux/5.15.154+
|