Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

""" External library compatibility 

""" 

# cytoolz over toolz 

HAS_TOOLZ = True 

try: 

import cytoolz as toolz 

except ImportError: 

try: 

import toolz 

except ImportError: 

HAS_TOOLZ = False 

pass 

 

 

def requires_module(module): 

""" Decorator to dependency check at function import time 

 

Parameters 

---------- 

module : str 

Module required for the function 

 

Returns 

------- 

callable 

Returns decorated function if dependency is importable, 

otherwise returns a dummy-function that will immediately 

raise an ImportError 

""" 

import functools, importlib 

 

has_module = True 

try: 

mod = importlib.import_module(module) 

except ImportError as e: 

has_module = False 

err = str(e) 

 

def decorator(f): 

@functools.wraps(f) 

def inner(*args, **kwds): 

raise ImportError(f'Function "{f.__name__}" requires module ' 

f'"{module}", but it is not available: {err}') 

 

if has_module: 

return f 

else: 

return inner 

 

return decorator