This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

Simple utilities to map JSON to and from Python Objects.

medea

medea is a singledispatch function for JSON encoding.

lists, tuples and sequences are converted to lists. Items and values of dicts are recursively dispatched. Other types should be registered with dispatch function.

medea.register(date, lambda obj: obj.strftime('%Y-%m-%d'))

dob = date(1974, 3, 1)
assert medea(dob) == '1974-03-01'

bob = {
    'name': 'Bob',
    'age': 38,
    'colors': ('blue', 'red'),
    'dob': dob}

assert medea(bob) == {
    'name': 'Bob',
    'age': 38,
    'colors': ['blue', 'red'],
    'dob': '1974-03-01'}

Custom objects can be registered with a mapper:

class Pet(object):
    pass


class Dog(Pet):
    def __init__(self, name):
        self.name = name
        self.kind = 'Dog'


class Cat(Pet):
    def __init__(self, name):
        self.name = name
        self.kind = 'Cat'


class PetPerson(Person):
    pass


medea.register(PetPerson, MedeaCamelMapper('first_name', 'last_name', 'pets'))
medea.register(Pet, MedeaCamelMapper('name', 'kind'))

anne = PetPerson('Anne', 'Frank')
fido = Dog('Fido')
spot = Dog('Spot')
garfield = Cat('Garfield')
anne.pets = [fido, spot, garfield]
assert medea(anne) == {
    'firstName': 'Anne',
    'lastName': 'Frank',
    'pets': [
        {'kind': 'Dog', 'name': 'Fido'},
        {'kind': 'Dog', 'name': 'Spot'},
        {'kind': 'Cat', 'name': 'Garfield'}]}

MedeaEncoder

MedeaEncoder is a JSONEncoder using the medea function.

MedeaMapper

Create a mapper using Python attribute names as arguments. Attributes that are not whitelisted will not be serialized:

class Person(object):
    def __init__(self, first_name, last_name,
                 address='', phone_number='', dob=''):
        self.first_name = first_name
        self.last_name = last_name
        self.address = address
        self.phone_number = phone_number
        self.dob = dob


bob = Person('Bob', 'Hope', '123 Main', '123', '1903-05-29')

mapper = MedeaMapper('first_name', 'last_name',
                     'address', 'phone_number', 'dob')

bob_json = {
    'first_name': 'Bob',
    'last_name': 'Hope',
    'address': '123 Main',
    'phone_number': '123',
    'dob': '1903-05-29'}

assert mapper.to_json(bob) == bob_json

Attribute names can be overridden using **kwargs.

bob = Person('Bob', 'Hope', '123 Main', '123', '1903-05-29')

mapper = MedeaMapper('address', 'dob',
                     first_name='firstName', last_name='lastName')

bob_json = {
    'firstName': 'Bob',
    'lastName': 'Hope',
    'address': '123 Main',
    'dob': '1903-05-29'}

assert mapper.to_json(bob) == bob_json

MedeaCamelMapper may be useful if JSON is camel cased.

bob = Person('Bob', 'Hope', '123 Main', '123', '1903-05-29')

mapper = MedeaCamelMapper('first_name', 'last_name',
                          'address', 'phone_number', 'dob')

bob_json = {
    'firstName': 'Bob',
    'lastName': 'Hope',
    'address': '123 Main',
    'phoneNumber': '123',
    'dob': '1903-05-29'}

assert mapper.to_json(bob) == bob_json

A mapper can also map attribues from JSON onto the object:

bob = Person('Bob', 'Hope', '123 Main', '123', '1903-05-29')

bob_json_full = {
    'firstName': 'Bob',
    'lastName': 'Hope',
    'address': '123 Main',
    'phoneNumber': '123',
    'DOB': '1903-05-29'}

fred_json = {
    'firstName': 'Fred',
    'lastName': 'Rodgers'}

mapper = MedeaCamelMapper('first_name', 'last_name')
mapper_full = MedeaCamelMapper('first_name', 'last_name',
                               'address', 'phone_number', dob='DOB')

assert mapper.to_json(bob) == bob_json
assert mapper_full.to_json(bob) == bob_json_full

# Override Bob's name from Fred
mapper.from_json(fred_json, bob)

# Mapper only serializes names
assert mapper.to_json(bob) == fred_json
assert mapper_full.to_json(bob) != bob_json_full
assert bob.first_name == 'Fred'
assert bob.last_name == 'Rodgers'
assert bob.address == '123 Main'
assert bob.phone_number == '123'
assert bob.dob == '1903-05-29'

# Revert back to Bob's name
mapper.from_json(bob_json, bob)
assert mapper.to_json(bob) == bob_json
assert mapper_full.to_json(bob) == bob_json_full
Release History

Release History

0.3.2

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.3.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.3.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
Medea-0.3.2.tar.gz (3.7 kB) Copy SHA256 Checksum SHA256 Source Dec 27, 2013

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting