Skip to content

Commit

Permalink
Add get_value method
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluissscm committed Jul 10, 2018
1 parent f0e6d70 commit 9dee9a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ ENV/

# testing scripts
*.test.py

# pycharm
.idea/
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ print(mt.get_vocabulary_reverse("1234abcd-12ab-34cd-56ef-12345678abcd"))
# [u'foo', u'asdf']

print(mt.get_values("1234abcd-12ab-34cd-56ef-12345678abcd"))


# get vocabulary entity value
# usage:
# mastertables.MasterTablesClient.get_value("<vocabulary_uuid>", "<entity_key>" [, index=<index>])
# output:
# u'bar'

print(mt.get_value("1234abcd-12ab-34cd-56ef-12345678abcd", "foo", 0))
```

## Packaging and distributing
Expand Down
14 changes: 10 additions & 4 deletions mastertables/mastertables.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ class MasterTablesClient:
host = HOST
api_key = "default"


def __init__(self, api_key, host=HOST):
self.host = host
self.api_key = api_key


@vocabulary_cache
def get_vocabulary(self, vocabulary, category=None):
url = self.host
Expand All @@ -39,14 +38,21 @@ def get_vocabulary(self, vocabulary, category=None):
raise AttributeError
return res


def get_vocabulary_reverse(self, vocabulary, category=None):
res = self.get_vocabulary(vocabulary)
res = dict((v,k) for k,v in res.iteritems())
return res


def get_values(self, vocabulary):
res = self.get_vocabulary(vocabulary)
res = res.keys()
return res

def get_value(self, vocabulary, key, index=0):
res = self.get_vocabulary(vocabulary)
try:
res = res[key].split(',')[index]
except Exception as e:
res = 'Incorrect params (' + str(e.message) + ')'

return res
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='mastertables',
version='1.0.0', # should comply with https://semver.org/
version='1.1.0', # should comply with https://semver.org/
description='A Python package to interact with the mastertables.athento.com public API',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 9dee9a0

Please sign in to comment.