Skip to content

Instantly share code, notes, and snippets.

@sampsyo
Created August 10, 2011 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sampsyo/1137717 to your computer and use it in GitHub Desktop.
Save sampsyo/1137717 to your computer and use it in GitHub Desktop.
making MusicBrainz getters using templates
def _make_id_getter(entity):
def getter(id, **kwargs):
includes = []
for name in kwargs:
if not name.startswith('inc_'):
raise TypeError('got unexpected keyword argument...')
elif kwargs[name]:
includes.append(name[4:])
return _do_mb_query(entity, id, includes)
getter.__name__ = 'get_%s_by_id' % entity
getter.__doc__ = """Fetches a %s given its MBID. Possible keyword
arguments specifying includes:
%s
""" % (entity, '\n'.join(['inc_' + i for i in VALID_INCLUDES[entity]]))
return getter
get_artist_by_id = _make_id_getter('artist')
get_label_by_id = _make_id_getter('label')
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment