Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lionheart/objectifier

Objectify your Python objects.
https://github.com/lionheart/objectifier

python python-library

Last synced: 3 months ago
JSON representation

Objectify your Python objects.

Awesome Lists containing this project

README

        

Objectifier
===========

.. image:: https://secure.travis-ci.org/aurorasoftware/objectifier.png?branch=master
:target: https://travis-ci.org/aurorasoftware/objectifier

Objectifier is a tool that makes traversing dictionaries, lists, and other
Python objects a lot easier.

If you have an bug to report or a feature request, add it to our `issue tracker
`_.

.. _installation:

Installation
------------

Objectifier is on `PyPi`_. You can install it through `pip`_ or easy_install,
whichever you prefer. ::

$ pip install objectifier

.. _pip: http://www.pip-installer.org/en/latest/
.. _PyPi: http://pypi.python.org

.. _configuration:

Usage
-----

Objectifier takes any Python object--a string, dictionary, list, tuple,
etc--and turns it into an object with some pretty cool properties.

The best way to explain is through some examples. Let's say you are interacting
with the Twitter API and have `a JSON tweet`_ that you'd like to manipulate or
traverse.

.. _a JSON tweet: javascript:toggleTweet();

.. raw:: html


$(function() {
$.getJSON("https://api.twitter.com/1/statuses/show.json?id=112652479837110273&include_entities=true&callback=?", function(json) {
$("pre#tweet").html(JSON.stringify(json, null, 4));
});
});

var tweetHidden = true;
function toggleTweet() {
if (tweetHidden) {
tweetHidden = false;
$("pre#tweet").fadeIn();
}
else {
tweetHidden = true;
$("pre#tweet").fadeOut();
}
}

After parsing the response into a Python dictionary using the ``json``
module, this is how we might display all the user mentions in a list. ::

>>> tweet = json.loads(response.read())
>>> ", ".join(user['screen_name'] for user in tweet['entities']['user_mentions'])

This isn't too different than what you'd do with Objectifier. The main
difference is that you can access attributes with dot notation. ::

>>> tweet = Objectifier(response.read())
>>> ", ".join(user.screen_name for user in tweet.entities.user_mentions)

The ``Objectifier`` class will wrap any Python string, unicode string,
dictionary, list, or tuple. If the input is a JSON string, Objectifier will
attempt to parse it before leaving it as text only. This allows you to do
things like the above, without having to use ``json.load`` for the response
data.

You can test that an attribute exists (as you could with a dictionary). ::

>>> 'user' in tweet
True

And get the number of items in an object that defines ``__len__``. ::

>>> len(tweet.entities.user_mentions)
3

The above things are nice, but not game changers. Objectifier's real strength
shines in the Python console. ::

>>> tweet

Everything in the object is recursively wrapped with Objectifier, so attributes
of the original object get all the benefits of pretty display. For example ::

>>> tweet.user

>>> tweet.user.profile_image_url
u'http://a0.twimg.com/profile_images/1380912173/Screen_shot_2011-06-03_at_7.35.36_PM_normal.png'

If you're inspecting a list, Objectifier will tell you the number of elements. ::

>>> tweet.entities.user_mentions

And finally, if you use IPython, pressing tab will give you a nice rundown of
all the attributes in the object. ::

>>> tweet.
...contributors ...id ...in_reply_to_user_id_str ...source
...coordinates ...id_str ...objectify_if_needed ...text
...created_at ...in_reply_to_screen_name ...place ...truncated
...entities ...in_reply_to_status_id ...possibly_sensitive ...user
...favorited ...in_reply_to_status_id_str ...retweet_count
...geo ...in_reply_to_user_id ...retweeted

There are probably a lot of other things Objectifier could do too, so if you
have an idea, fork the `code `_.

Contributing, feedback, and questions
-------------------------------------

* Github: https://github.com/lionheart/objectifier
* Email: [email protected]
* Twitter: `@lionheartsw `_