-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add explanation about imports and global variables
- Loading branch information
Showing
4 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Example Scripts | ||
|
||
Scripts which illustrate example from the documentation that do not run well as part of the pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Tests documentation related to building apps. Must reside outside the Parsl library to be effective""" | ||
from typing import List, Union | ||
|
||
import numpy as np | ||
|
||
from parsl import python_app, HighThroughputExecutor, Config | ||
import parsl | ||
|
||
parsl.load(Config(executors=[HighThroughputExecutor(label='htex_spawn', max_workers=1, start_method='spawn', address='127.0.0.1')])) | ||
|
||
|
||
# Part 1: Explain imports | ||
# BAD: Assumes library has been imported | ||
@python_app(executors=['htex_spawn']) | ||
def bad_imports(x: Union[List[float], np.ndarray], m: float, b: float): | ||
return np.multiply(x, m) + b | ||
|
||
|
||
# GOOD: Imports libraries itself | ||
@python_app(executors=['htex_spawn']) | ||
def good_imports(x: Union[List[float], 'np.ndarray'], m: float, b: float): | ||
import numpy as np | ||
return np.multiply(x, m) + b | ||
|
||
|
||
future = bad_imports([1.], 1, 0) | ||
|
||
try: | ||
future.result() | ||
raise ValueError() | ||
except NameError as e: | ||
print('Failed, as expected. Error:', e) | ||
|
||
future = good_imports([1.], 1, 0) | ||
print(f'Passed, as expected: {future.result()}') | ||
|
||
# Part 2: Test other types of globals | ||
# BAD: Uses global variables | ||
global_var = {'a': 0} | ||
|
||
|
||
@python_app | ||
def bad_global(string: str, character: str = 'a'): | ||
global_var[character] += string.count(character) # `global_var` will not be accessible | ||
|
||
|
||
# GOOD | ||
@python_app | ||
def good_global(string: str, character: str = 'a'): | ||
return {character: string.count(character)} | ||
|
||
|
||
try: | ||
bad_global('parsl').result() | ||
except NameError as e: | ||
print(f'Failed, as expected: {e}') | ||
|
||
for ch, co in good_global('parsl', 'a').result().items(): | ||
global_var[ch] += co | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters