Interact with JSON or YAML content as if it's a Python object
Project description
Schema2type
Interact with JSON and YAML content through custom Python classes. How it works:
- You specify the desired or expected format of your content using a JSON Schema or OpenAPI Specification (version 3).
- Schema2type creates dynamic Python types for all the (sub-)schemas described by that schema/specification.
- You can make use of these types from anywhere in your code to interact with the JSON or YAML content.
- Schema2type also generates so-called stub files, which allow you to make use of your IDE's auto-complete functionality for the dynamically created types (this is arguably the most convenient feature of this module and works with both PyCharm and VS Code).
JSON/YAML content can be read from a file, but it can also be content obtained through any other means, such as through an HTTP request.
Example
If you frequently interact with OpenAPI Specifications, you might want make use of dynamic classes based on the JSON schema that describes the format of an OpenAPI Specification (indeed, "a specification of a specification document" is rather meta, but very useful in this case).
import prance
from my_schema_classes.openapi import RootObject as OpenAPISpecification
# resolve references in the specification with the prance module
resolved_specification = prance.ResolvingParser('/path/to/pet_store_openapi_spec.yml').specification
# create a schema-based object from the YAML content
specification_object = OpenAPISpecification(**resolved_specification)
# access information from the specification with auto-complete to guide you along the way:
print(specification_object.info.title) # > Swagger Petstore - OpenAPI 3.0
print(type(specification_object.components)) # > <class 'schema2type.Components'>
pet_schema = specification_object.components.schemas['Pet']
print(type(pet_schema)) # > <class 'schema2type.Schema'>
print(pet_schema.required) # > ['name', 'photoUrls']
Compatibility
At the time of writing, schema2type has been tested with schemas specified in the following formats/documents:
Disclaimer
This module is currently in development. At the moment, it can handle most, but not all situations when it comes to
parsing your JSON schema or OpenAPI specification. Use at your own risk of running into any potential errors. Support
for oneOf
, allOf
, and anyOf
schemas is still limited.
How to install
Schema2type is available from PyPI, and can be installed via pip:
pip install schema2type
Usage
First, generate the stub files based on your schema or specification:
mkdir "/path/to/my_package/my_module/schema_classes"
schema2type gen-stubs --out "/path/to/my_package/my_module/schema_classes" --name "my_schema_name" "/path/to/my_package/a_schema.yml" json_schema
Then, import the schema based classes from within your module wherever you need them:
from my_package.schema_classes.my_schema import *
License
Copyright 2021 Mokkit Oy
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
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
File details
Details for the file schema2type-0.0.3.tar.gz
.
File metadata
- Download URL: schema2type-0.0.3.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc1cbd32c30e23b5948ec0e4f4a2279d2ea26022b932ebdf9dfe6cb922b5d6c8 |
|
MD5 | a8aaa1ecca4b115fb982c888ba1788d4 |
|
BLAKE2b-256 | b889a17d2357df2729341abb1ef7a65a2da87934039216d23aef54c4aa624577 |
File details
Details for the file schema2type-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: schema2type-0.0.3-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad83b2c3fa31560f165f72ecf3f997afbd22918634ce5bbf168bca205c35d0b1 |
|
MD5 | cee114785ba86906d418bc8275e00805 |
|
BLAKE2b-256 | 31dcbf25437e8c0c67e231d58f52b26f05d9f19c8d04e916b1b1e96bf3e662a7 |