Skip to content

Instantly share code, notes, and snippets.

@pilipolio
Created November 26, 2014 09:20
Show Gist options
  • Save pilipolio/bfc3e1e9509cc91e4774 to your computer and use it in GitHub Desktop.
Save pilipolio/bfc3e1e9509cc91e4774 to your computer and use it in GitHub Desktop.
import requests
from functional import compose
from functools import partial
from collections import namedtuple
import itertools
class DescriptorDefinition(namedtuple('DescriptorDefinition', ['id', 'type', 'name'])):
@classmethod
def from_json(cls, definition_json):
return cls(
id=definition_json['descriptorId'],
type=definition_json['descriptorType'],
name=definition_json['descriptorName'])
def get_definitions_json(api_url):
definitions_uri = '{api_url}/v1/gracenote/batch/descriptorDefinitions'.format(**locals())
get_response = requests.get(definitions_uri)
get_response.raise_for_status()
return get_response.json()
definitions_list_from_json = partial(map, DescriptorDefinition.from_json)
definitions_from_api = compose(definitions_list_from_json, get_definitions_json)
load_definitions = partial(definitions_from_api, api_url='http://localhost:8000')
print load_definitions()[:2]
import numpy
a = numpy.array([0])
b = list()
"""
PROS :
* partialed/composed definitions so declarative and simple that just DescriptorDefinition.from_json needs a unit test
* get_definitions_json(api_url) as a straight forward declarative and simple function
CONS :
* Ugly stack trace, not obvious what is load_definitions when debugging
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment