Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Python Serializable Entities

Project Description


Basic Python Entities

An entity is useful for preparing models for an API or view.

Allows view or API UI specific logic to be removed from models.

### Example

We have some object..

class GeorgeWashington(object):
id = 1776
first_name = 'George'
last_name = 'Washington'
phone_number = '(202) 456-1111'
phone_number_private = True
email = ''
accomplishments = ['president', 'general', 'quarter model', 'woodworking']
birthday = datetime.datetime(1732, 2, 22, 0, 0)

Converting this object to json would take a bit of work. We need to write
some kind of `to_dictionary` method, or a custom encoder. There will stil
probably be an issue with the `datetime` (not JSON-encodable) as well as
the fact that we want to conditionally include the `phone_number`, as long
as `phone_number_private` is not `True`.

Using an `Entity`:

class UserEntity(pyentity.Entity):
_FIELDS_ = [
_ALIAS_ = 'user'

def name(self):
return "%s %s" % (self.user.first_name, self.user.last_name)

def birthday(self):
return self.user.birthday.isoformat()

def phone_number(self):
if self.user.phone_number_private:
raise pyentity.SuppressField
return self.user.phone_number


george_entity = UserEntity(GeorgeWashington())
print json.dumps(george_entity(), indent=2)

"email": "",
"accomplishments": [
"quarter model",
"id": 1776,
"birthday": "1732-02-22T00:00:00",
"name": "George Washington"

Somtimes, we want to have different views over the same data. One may be a detailed
description of an object, but some other may just be a summary. Entity inheritance helps here.

class SummaryEntity(UserEntity):
_FIELDS_ = ['name', 'email']


summary_entity = SummaryEntity(GeorgeWashington())
print json.dumps(summary_entity(), indent=2)

"name": "George Washington",
"email": ""

Release History

This version
History Node


History Node


Download Files

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

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(5.9 kB) Copy SHA256 Hash SHA256
Source None Jan 21, 2014

Supported By

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 Google Google Cloud Servers