-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from runprism/utils
`requires_dependency` util function
- Loading branch information
Showing
14 changed files
with
163 additions
and
25 deletions.
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
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
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
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
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
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
20 changes: 10 additions & 10 deletions
20
...s/integration/test_projects/009_simple_dbt_project/prism/output/jaffle_shop_customers.csv
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
CUSTOMER_ID,FIRST_NAME,LAST_NAME,FIRST_ORDER,MOST_RECENT_ORDER,NUMBER_OF_ORDERS,CUSTOMER_LIFETIME_VALUE | ||
1.0,Michael,P.,2018-01-01,2018-02-10,2.0,33.0 | ||
2.0,Shawn,M.,2018-01-11,2018-01-11,1.0,23.0 | ||
3.0,Kathleen,P.,2018-01-02,2018-03-11,3.0,65.0 | ||
4.0,Jimmy,C.,,,, | ||
5.0,Katherine,R.,,,, | ||
6.0,Sarah,R.,2018-02-19,2018-02-19,1.0,8.0 | ||
7.0,Martin,M.,2018-01-14,2018-01-14,1.0,26.0 | ||
8.0,Frank,R.,2018-01-29,2018-03-12,2.0,45.0 | ||
9.0,Jennifer,F.,2018-03-17,2018-03-17,1.0,30.0 | ||
10.0,Henry,W.,,,, | ||
1,Michael,P.,2018-01-01,2018-02-10,2.0,33.0 | ||
2,Shawn,M.,2018-01-11,2018-01-11,1.0,23.0 | ||
3,Kathleen,P.,2018-01-02,2018-03-11,3.0,65.0 | ||
4,Jimmy,C.,,,, | ||
5,Katherine,R.,,,, | ||
6,Sarah,R.,2018-02-19,2018-02-19,1.0,8.0 | ||
7,Martin,M.,2018-01-14,2018-01-14,1.0,26.0 | ||
8,Frank,R.,2018-01-29,2018-03-12,2.0,45.0 | ||
9,Jennifer,F.,2018-03-17,2018-03-17,1.0,30.0 | ||
10,Henry,W.,,,, |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Util functions | ||
""" | ||
|
||
# Imports | ||
from typing import ( | ||
Any, | ||
Callable, | ||
List, | ||
Optional, | ||
Union, | ||
) | ||
import importlib | ||
from functools import wraps | ||
|
||
|
||
# Util functions | ||
def requires_dependencies( | ||
dependencies: Union[str, List[str]], | ||
extras: Optional[str] = None, | ||
): | ||
""" | ||
Wrapper used to prompt the user to `pip install` a package and/or Prism extracts in | ||
order to run a function. Borrowed heavily from the `unstructured` library: | ||
https://github.com/Unstructured-IO/unstructured/blob/main/unstructured/utils.py | ||
args: | ||
dependencies: required dependencies | ||
extracts: list of Prism extras that the user can `pip install` | ||
""" | ||
if isinstance(dependencies, str): | ||
dependencies = [dependencies] | ||
|
||
def decorator(func: Callable[..., Any]) -> Callable[..., Any]: | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
missing_deps: List[str] = [] | ||
for dep in dependencies: | ||
if not dependency_exists(dep): | ||
missing_deps.append(dep) | ||
if len(missing_deps) > 0: | ||
raise ImportError( | ||
f"""Following dependencies are missing: {', '.join(["`" + dep + "`" for dep in missing_deps])}. """ # noqa | ||
+ ( # noqa | ||
f"""Please install them using `pip install "prism-ds[{extras}]"`.""" # noqa | ||
if extras | ||
else f"Please install them using `pip install {' '.join(missing_deps)}`." # noqa | ||
), | ||
) | ||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
return decorator | ||
|
||
|
||
def dependency_exists(dependency: str): | ||
try: | ||
importlib.import_module(dependency) | ||
except ImportError as e: | ||
# Check to make sure this isn't some unrelated import error. | ||
if dependency in repr(e): | ||
return False | ||
return True |
Oops, something went wrong.