Giter VIP home page Giter VIP logo

nobel's Introduction

A Pythonic wrapper for the Nobel Prize API

Build Status Coverage Status

A simple Python wrapper for the Nobel Prize API.

Quickstart

First, initialize the API wrapper:

import nobel
api = nobel.Api()

Now you have access to the resources defined by the API (prizes, laureates and countries) and their methods.

For example, to list all laureates:

>>> for laureate in api.laureates.all():
...     print '%s (%s)' % (laureate, laureate.born_country)

To filter (check the Nobel Prize API documentation for all available filtering parameters):

>>> api.laureates.filter(gender='female', born_country='Iran')
[<Laureate id=773>, <Laureate id=817>]
>>> api.prizes.filter(year=1969)
[<Prize category="physics" year=1969>, <Prize category="chemistry" year=1969>,
<Prize category="medicine" year=1969>, <Prize category="literature" year=1969>,
<Prize category="peace" year=1969>, <Prize category="economics" year=1969>]

To retrieve a single resource:

>>> laureate = api.laureates.get(id=26)
>>> laureate.firstname
u'Albert'
>>> laureate.surname
u'Einstein'
>>> laureate.prizes
[<Prize category="physics" year=1921>]
>>> laureate.born_country
<Country code="DE" name="Germany">
>>> laureate.died_country
<Country code="US" name="USA">

>>> prize = api.prizes.get(year=2000, category='economics')
>>> print ", ".join([str(l) for l in prize.laureates])
James J. Heckman, Daniel L. McFadden

As you can see, every Prize object is given a laureates attribute populated with a list of its Laureate objects. Likewise, every Laureate objects is given a prizes attribute with Prize objects.

Attributes and query parameters in the Nobel Prize API are mixedCase, but this wrapper uses the more pythonic lower_case_with_underscores style and takes care of the conversion when filtering and accessing attributes.

Installation

To install Nobel, simply:

$ pip install nobel

TODO

  • Documentation and full reference
  • More ORM-ish stuff, like filtering using Prize, Laureate or Country objects as arguments
  • Add relation attributes, like affiliations.

Credits

This is inspired by the beautifully written hipchat-api wrapper.

nobel's People

Contributors

vibragiel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

deparkes candeira

nobel's Issues

Query parameters for the API

Is there a method for finding the full set of query parameters for the API? It's not the same as attributes, I know, but there are several keys at http://api.nobelprize.org/v1/laureate.json that can't be accessed by the searches.

For example:

import nobel
api = nobel.Api()
api.laureates.filter(born = 1950)

returns:

---------------------------------------------------------------------------
BadRequest                                Traceback (most recent call last)
<ipython-input-307-7b4fc1d99cdd> in <module>()
----> 1 api.laureates.filter(born=1950)

/Users/willettk/Documents/Nobel/nobel/nobel/data.py in filter(cls, **kwargs)
     78 
     79         camel_kwargs = dict((cls._camelize(k), v) for k, v in kwargs.items())
---> 80         data = cls.api._get(cls.resource + '.json', **camel_kwargs)
     81         return [cls._parse(p, full=True) for p in data[cls.resource_plural]]
     82 

/Users/willettk/Documents/Nobel/nobel/nobel/api.py in _get(self, resource, **kwargs)
     61         url = self.base_url + resource
     62         resp = requests.get(url, params=kwargs)
---> 63         return self._unwrap_response(resp)
     64 
     65     @property

/Users/willettk/Documents/Nobel/nobel/nobel/api.py in _unwrap_response(resp)
     52             return json
     53         elif code == 400:
---> 54             raise BadRequest(errmsg)
     55         elif code == 503:
     56             raise ServiceUnavailable(errmsg)

BadRequest: Using an unknown parameter

Similarly, died, first_name, and surname all show up as invalid parameters. Is this a bug, or just user misunderstanding?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.