Python implementation of Styx ETL declarative TOML.
For more information about Styx, please see the definition here.
-
In your Python project,
mkdir
maps -
touch
functions.styx (TODD: Add init for this) and functions.py (if you want to write custom functions) -
In
functions.styx
, add an array calledfunctions
, with the names of any functions you plan on using in your mappings:# functions.styx functions = ["to_upper_case"]
-
In
functions.py
implement your custom function. Ensure that your function takes the same number of arguments as your expectedinput_paths
, or use*args
. The# functions.py from pystyx.functions import styx_function @styx_function def to_upper_case(value): return value.upper()
-
Add any of your Styx Definitions to the
maps/
directory. For example,from_type = "erp_address" to_type = "Address" [fields] [fields.address1] input_paths = ["addr1"] [fields.address2] input_paths = ["addr2"] [fields.city] input_paths= ["city"] [fields.state] input_paths = ["province"] function = "to_upper_case" [fields.zip] input_paths = ["postalCode"]
-
Initialize maps in your Python code:
// blob.json { "addr1": "123 Street", "addr2": "Ste. 800", "city": "Dallas", "province": "tx", "postalCode": "75080", }
# main.py import json import pathlib import pystyx # Ensure that your decorated functions are evaluated from functions import * with pathlib.Path("blob.json").open() as json_file: blob = json.load(json_file) maps = pystyx.create_maps() # Defaults to `/maps` and `functions.styx` mapper = maps.get("erp_address") # 'from_type` from the Definition above mapped_obj = mapper(blob) print(mapped_obj)
Prints:
{ "address1": "123 Street", "address2": "Ste. 800", "city": "Dallas", "state": "TX", // Upper case now "zip": "75080" }
pystyx.create_maps()
parses (and thereby validates) the Styx files before loading them. I hope to extract this validation as a CLI tool (along with generating Styx structures).
I wanted this to be a good first example of a Styx implementation, so I believe it currently implements all features of the Styx standard. Parsing and validation I would like to extract into its own library eventually.
Feature | Implemented |
---|---|
Structures | |
- JSON objects | ✔️ |
Validation/Parsing | |
- Full parsing | ✔️ |
Preprocessing | |
- input_paths | ✔️ |
- output_paths | ✔️ |
- function | ✔️ |
- or_else | ✔️ |
- on_throw | ✔️ |
Fields | |
- field header | ✔️ |
- input_paths | ✔️ |
- possible_paths | ✔️ |
- const | ✔️ |
- from_type | ✔️ |
- or_else | ✔️ |
- on_throw | ✔️ |
- many | ✔️ |
Postprocessing | |
- input_paths | ✔️ |
- output_paths | ✔️ |
- function | ✔️ |
- or_else | ✔️ |
- on_throw | ✔️ |