No project description provided
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_autoviv-1.0.4.tar.gz.
File metadata
- Download URL: python_autoviv-1.0.4.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.13.1 Linux/5.15.154+
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8064621dd19e3bfd0ac0d521df1c62566631e0b6854bc099a8d1f3deeeae82ac
|
|
| MD5 |
a596b8ef4db370917e7eec80597e3966
|
|
| BLAKE2b-256 |
91986f06b23bb02bb0b3f7c7499c3737c0c1db0e062f9d24d7e92809ac1c1a29
|
File details
Details for the file python_autoviv-1.0.4-py3-none-any.whl.
File metadata
- Download URL: python_autoviv-1.0.4-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.0.1 CPython/3.13.1 Linux/5.15.154+
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b013eac19e70047d040d9c3498755c39ad5227c20480d2911202ac038e3f4319
|
|
| MD5 |
c69e6ad6231cffb012cab6f4c9ab38ce
|
|
| BLAKE2b-256 |
4b95844a77b783789f8709cf096e97cb45294ec962c5dc40627c542da89aea83
|