Skip to content

Instantly share code, notes, and snippets.

@mkirlin
Last active February 28, 2020 19:59
Show Gist options
  • Save mkirlin/3e821bb0d8320ab98f8408aca8a17cf9 to your computer and use it in GitHub Desktop.
Save mkirlin/3e821bb0d8320ab98f8408aca8a17cf9 to your computer and use it in GitHub Desktop.
def list_available_params(self):
"""Iterates over Odin query methods and creates a list of available queries for a
client. Any methods added to this class will be included in this response if they
are prefaced with "_get_".
Returns:
list -- list of strings corresponding to methods on this class that are prefaced
with "_get_", excluding "_get_odin_info"
"""
method_list = [
func[len("_get_") :]
for func in dir(OdinQueryEngine)
if callable(getattr(OdinQueryEngine, func))
and func.startswith("_get")
and func is not "_get_odin_info"
]
return method_list
def query_odin(self, query_list):
"""Takes a list of strings from a client, translates them into the names of methods
on this class, creates a list of those methods, iterates over that list to call each
method and create another list featuring the return values for each method, and zips
those return values into a dictionary using the client's strings as keys and the
method responses as values.
Arguments:
query_list {list} -- a list of objects, sent from a client, corresponding
with Odin query methods on this class. Each object should have two keys: "name"
and "params". "name" should be a string containing the name of the method you
want to call, and params should be a list of arguments for that method
Returns:
dict -- a dictionary where the keys are the strings sent from the client and the
values are the returned responses from the corresponding methods on this class
"""
methods = [
(getattr(self, f"_get_{query.get('name')}"), query.get('params'))
for query in query_list
]
results = [
func_tuple[0](func_tuple[1])
if func_tuple[1]
else func_tuple[0]()
for func_tuple in methods
]
return dict(
zip(
[query.get('name') for query in query_list],
results
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment