Skip to main content

Write conversions between python objects using an expressive and declarative class-based API

Project description

Python Package Test and Build

Converters

Install

Install using pip:

pip install python-converters

Overview

At its most basic, Converters provides a small in-python DSL / declarative mechanism to define transformations operations (”conversions") that allow you to rename, alter, add, clean etc. fields of objects / maps conveniently, making it easy to work with nested structures and a host of other common cases.

Here is a quick example of using a subclass of Converter to convert from one instance to another:

from converters import Converter

class MyConverter(Converter):
    from_class = dict
    to_class = dict

    conversions = [
        'a',                            # to['a'] = from['a']
        ('b', 'a'),                     # to['b'] = from['a']
        ('c', NOS, 'a default'),        # to['c'] = 'a default'
        ('d', 'a', lambda x: x * 2),    # to['d'] = from['a'] * 2
        ('e', NOS, dict),               # to['e'] = dict()
    ]

MyConverter({'a': 1}).convert()
# yields {'a': 1, 'b': 1, 'c': 'a default', 'd': 2, 'e': {}}

But that is just the beginning. Its real power comes as you start to nest converters and subclass the core converter class to support different data formats. The power of it is that you can get going with very simple examples, but as your needs grow you’ll find that converters rises to meet them, and are highly extendable for the cases where they don’t.

And to get you started there are already extensions for converting to and from:

  • django models
  • XML documents
  • pandas dataframes
  • fixed width string formats (!)

Why is this useful?

Doing conversions of data (e.g. ingest, exgest) normally seems like a simple job, but naive approaches end up writing a lot of (often one-off) boilerplate and repetitive code that is hard to read and update, inconsistent and error prone, with code often spread throughout several ad hoc functions. Converters provides a single place to put conversions, uses class inheritance to share domain-specific operations, and through that and its maximal approach to features minimizes client boilerplate.

Project details


Download files

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

Source Distribution

python_converters-0.1.1.tar.gz (25.2 kB view hashes)

Uploaded Source

Built Distribution

python_converters-0.1.1-py3-none-any.whl (27.6 kB view hashes)

Uploaded Python 3

Supported by

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