Skip to content

Commit

Permalink
Improve env variables handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lmazuel committed Nov 21, 2017
1 parent 3b47304 commit 47e6139
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ We provide several meta-packages to help you install several packages at a time.
pip install -r requirements.txt
```
3. Set up the environment variable ENTITYSEARCH_SUBSCRIPTION_KEY with your CS key if you want to execute EntitySearch tests.
4. Set up the environment variable WEBSEARCH_SUBSCRIPTION_KEY with your CS key if you want to execute WebSearch tests.
3. Set up the environment variable `ENTITYSEARCH_SUBSCRIPTION_KEY` with your CS key if you want to execute EntitySearch tests.
4. Set up the environment variable `WEBSEARCH_SUBSCRIPTION_KEY` with your CS key if you want to execute WebSearch tests.
## Demo
Expand All @@ -62,8 +62,8 @@ To run the complte demo, execute `python example.py`
To run each individual demo, point directly to the file:
1. `python samples/entity_search_sample.py`
2. `python samples/web_search_sample.py`
1. `python samples/entity_search_samples.py`
2. `python samples/web_search_samples.py`
To see the code of each example, simply look at the examples in the Samples folder. They are written to be isolated in scope so that you can see only what you're interested in.
Expand Down
6 changes: 4 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

def run_all_samples():
for _, sample_name, _ in pkgutil.walk_packages(samples.__path__):
if not sample_name.endswith("samples"):
sample_module = importlib.import_module('samples.'+sample_name)
subkey_env_name = getattr(sample_module, "SUBSCRIPTION_KEY_ENV_NAME", None)
if not subkey_env_name:
continue
print("Executing sample from ", sample_name)
sample_module = importlib.import_module('samples.'+sample_name)
samples.tools.execute_samples(sample_module.__dict__)
samples.tools.execute_samples(sample_module.__dict__, subkey_env_name)

if __name__ == "__main__":
run_all_samples()
3 changes: 2 additions & 1 deletion samples/entity_search_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from azure.cognitiveservices.search.entitysearch.models import Place, ErrorResponseException
from msrest.authentication import CognitiveServicesCredentials

SUBSCRIPTION_KEY_ENV_NAME = "ENTITYSEARCH_SUBSCRIPTION_KEY"

def dominant_entity_lookup(subscription_key):
"""DominantEntityLookup.
Expand Down Expand Up @@ -182,4 +183,4 @@ def error(subscription_key):

if __name__ == "__main__":
from tools import execute_samples
execute_samples(globals())
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)
6 changes: 3 additions & 3 deletions samples/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ def start_sample(func, subscription_key):
print("\n\n")


def execute_samples(module_globals):
def execute_samples(module_globals, key_env_variable):
"""Execute samples based on a dict <name, function>
"""
try:
subscription_key = sys.argv[1] if len(sys.argv) >= 2 else os.environ["SUBSCRIPTION_KEY"]
subscription_key = sys.argv[1] if len(sys.argv) >= 2 else os.environ[key_env_variable]
except KeyError:
sys.exit("You need to either set the SUBSCRIPTION_KEY env variable or give it as env variable.")
sys.exit("You need to either set the {} env variable or give it as env variable.".format(key_env_variable))

for func in list(module_globals.values()):
if not isinstance(func, types.FunctionType):
Expand Down
4 changes: 3 additions & 1 deletion samples/web_search_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from azure.cognitiveservices.search.websearch.models import SafeSearch
from msrest.authentication import CognitiveServicesCredentials

SUBSCRIPTION_KEY_ENV_NAME = "WEBSEARCH_SUBSCRIPTION_KEY"

def result_types_lookup(subscription_key):
"""WebSearchResultTypesLookup.
Expand Down Expand Up @@ -154,4 +156,4 @@ def web_search_with_answer_count_promote_and_safe_search(subscription_key):

if __name__ == "__main__":
from tools import execute_samples
execute_samples(globals())
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

0 comments on commit 47e6139

Please sign in to comment.