Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/dask/context.py: 70%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

23 statements  

1""" 

2Control global computation context 

3""" 

4 

5from __future__ import annotations 

6 

7import threading 

8from functools import partial 

9 

10from dask import config 

11 

12_globals = config.config 

13 

14 

15thread_state = threading.local() 

16 

17 

18def globalmethod(default=None, key=None, falsey=None): 

19 """Allow function to be taken over by globals 

20 

21 This modifies a method so that occurrences of it may be taken over by 

22 functions registered in the global options. Can be used as a decorator or a 

23 function. 

24 

25 Parameters 

26 ---------- 

27 default : callable 

28 The default callable to use. 

29 key : str 

30 Key under which we register this function in the global parameters 

31 falsey : callable, None, optional 

32 A function to use if the option is falsey. If not provided, the default 

33 is used instead. 

34 

35 Examples 

36 -------- 

37 >>> import dask 

38 >>> class Foo: 

39 ... @globalmethod(key='bar', falsey=lambda: 3) 

40 ... def bar(): 

41 ... return 1 

42 >>> f = Foo() 

43 >>> f.bar() 

44 1 

45 >>> with dask.config.set(bar=lambda: 2): 

46 ... print(f.bar()) 

47 2 

48 >>> with dask.config.set(bar=False): 

49 ... print(f.bar()) 

50 3 

51 """ 

52 if default is None: 

53 return partial(globalmethod, key=key, falsey=falsey) 

54 return GlobalMethod(default=default, key=key, falsey=falsey) 

55 

56 

57class GlobalMethod: 

58 def __init__(self, default, key, falsey=None): 

59 self._default = default 

60 self._key = key 

61 self._falsey = falsey 

62 

63 def __get__(self, instance, owner=None): 

64 if self._key in _globals: 

65 if _globals[self._key]: 

66 return _globals[self._key] 

67 elif self._falsey is not None: 

68 return self._falsey 

69 return self._default