Skip to main content

A lightweight package for converting JSON schemas

Project description

Switchboard for Python

Code style: black

Switchboard makes working with API's easy. Switchboard helps you can easily convert JSON schemas. Switchboard is still work on progress, but you are more than welcome to check it out!

But why Switchboard?

When working with integrations and 3rd party API's, for instance, you often run into situation where the data must be digged manually. Let us consider that you have the following kind of simple database schema:

 Column        | Type
---------------+-------
 first_name    | string
 last_name     | string
 email         | string

You are working with 3rd party user API, for instance, which returns the following kind of payload:

{
    "id": 12335,
    "firstName": "John",
    "lastName": "Doe",
    "contactInfo": {
        "primaryEmail": "john.doe@foo.bar"
    }
}

So, what to do now? What is the best way to convert payload into your own format? Does this look familiar:

response_dict = json.loads(response.body)

data = {
    "first_name": response_dict.get("firstName"),
    "last_name": response_dict.get("lastName"),
    "email": response_dict.get("contactInfo", {}).get("primaryEmail")
}

Unfortunately, the solution above becomes extremely messy when working with nested JSON structures, multiple 3rd party API's, or combination of them. This is why Switchboard is useful, by defining a new switchboard, you can easily manage data mappings between differen schemas.

from switchboard import Switchboard, Wire

class UserSwitchboard(Switchboard):
    first_name = Wire("firstName")
    last_name = Wire("lastName")
    email = Wire(["contactInfo", "primaryEmail"])  # Notice how simple it is to access nested data!

Now, the code looks much better. Nice!

response_dict = json.loads(response.body)
data = UserSwitchboard().apply(response_dict)

Documentation

This project is still in progress. Better documentation is coming later.

Defining new Switchboards is easy. All you need to do is to import it, and define some chords.

from switchboard import Switchboard, Cord

class MySwitchboard(Switchboard):
    pet_name = Cord(
        source="petName",  # Source tells field location in source schema
        required=True,     # If field is required and missing, exception is raised
        default="Mr. Dog"  # If field is missing, default value is being used
    )

Meta class

Switchboard functionality can be tweaked using Meta class:

from switchboard import Switchboard, Cord

class MySwitchboard(Switchboard):
    class Meta:
        # Tells what to do with missing fields. Default functionlity is INCLUDE,
        # which means that if source field is missing, field is appended but the field
        # value will be None
        missing = Switchboard.EXCLUDE  # Options: Switchboard.EXCLUDE | Switchboard.INCLUDE | Switchboard.RAISE

    pet_name = Cord("petName")

Nested schemas

Cords can access nested fields, even lists.

from switchboard import Switchboard, Cord

class MySwitchboard(Switchboard):
    target_field = Cord(
        source=[
            "field1",  # Matches to field key
            0,         # Matches to list index
            "field2"   # Matches to field key
        ]
    )

MySwitchboard().apply({ "field1": [{ "field2": "value" }] })
# > { "target_field": "value" }

Lists

Switchboard many attribute helps with dealing with lists. For example:

class MySwitchboard(Switchboard):
    target = Cord("source")


data_in = [
    {"source": 1},
    {"source": 2},
    {"source": 3},
    {"source": 4}
]

MySwitchboard(many=True).apply(data_in)
# > { "target": 1, "target": 2, "target": 3, "target": 4 }

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_switchboard-0.0.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file python_switchboard-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: python_switchboard-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.48.1 CPython/3.8.2

File hashes

Hashes for python_switchboard-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bfb608281cfacc29be4af39d57cc74d9fbb09896e59ecb8c89728a2ef6a3170f
MD5 73a309b1c571d860e6981ff9379b3b55
BLAKE2b-256 48f85557ea1bdaaaaf1252b0e62fe5d4a1d2cf6e8fd50964a46a6debbe96375c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page