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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file python_converters-0.1.1.tar.gz.

File metadata

  • Download URL: python_converters-0.1.1.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.6 Darwin/22.6.0

File hashes

Hashes for python_converters-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8fc2472d28c47f4567216ef320fb752500dcfebc2cba083b3094269c7f378df6
MD5 952d43a316f47d3513ab2d7b9e65df53
BLAKE2b-256 3461287620d90ceaaa15db9c706be0260a9a04a94fb6ce7464da59bbf1faebfa

See more details on using hashes here.

File details

Details for the file python_converters-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_converters-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dc080dd4fc27ed126b56c64ca1673077014d68107e315061cb30129e488a484f
MD5 ba18b679a213156026626021fd6482c8
BLAKE2b-256 4eba51d9625f07524c51f1e1e4477e37ad4f76d0d69ca73e334794846c93a4d5

See more details on using hashes here.

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