Skip to main content

Python Styx bindings for ETL/ELT declarative mapping syntax using TOML.

Project description

pystyx

Python implementation of Styx ETL declarative TOML.

For more information about Styx, please see the definition here.

Quick Start

  1. In your Python project, mkdir maps

  2. touch functions.styx (TODD: Add init for this) and functions.py (if you want to write custom functions)

  3. In functions.styx, add an array called functions, with the names of any functions you plan on using in your mappings:

    # functions.styx
    
    functions = ["to_upper_case"]
    
  4. In functions.py implement your custom function. Ensure that your function takes the same number of arguments as your expected input_paths, or use *args. The

    # functions.py
    
    from pystyx.functions import styx_function
    
    @styx_function
    def to_upper_case(value):
       return value.upper()
    
  5. 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"]
    
  6. 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"
     }
    

Styx Validation

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).

Styx Implementation

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 :heavy_check_mark:
Validation/Parsing
- Full parsing :heavy_check_mark:
Preprocessing
- input_paths :heavy_check_mark:
- output_paths :heavy_check_mark:
- function :heavy_check_mark:
- or_else :heavy_check_mark:
- on_throw :heavy_check_mark:
Fields
- field header :heavy_check_mark:
- input_paths :heavy_check_mark:
- possible_paths :heavy_check_mark:
- const :heavy_check_mark:
- from_type :heavy_check_mark:
- or_else :heavy_check_mark:
- on_throw :heavy_check_mark:
- many :heavy_check_mark:
Postprocessing
- input_paths :heavy_check_mark:
- output_paths :heavy_check_mark:
- function :heavy_check_mark:
- or_else :heavy_check_mark:
- on_throw :heavy_check_mark:

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

pystyx-0.8.5-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file pystyx-0.8.5-py3-none-any.whl.

File metadata

  • Download URL: pystyx-0.8.5-py3-none-any.whl
  • Upload date:
  • Size: 10.9 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/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2

File hashes

Hashes for pystyx-0.8.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b190e4bebc1edb8bcf0311f0d2c00c2074046644adc86e76bef0d5c4fd18e969
MD5 d9ed33650a25f5ae2e214dec176e58a3
BLAKE2b-256 a23e9dd322b623a57c12afed614bc48e9a1b25d9cc6f0b534cc340fa7c8b8b24

See more details on using hashes here.

Supported by

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