Allow function to be taken over by globals This modifies a method so that occurrences of it may be taken over by functions registered in the global options. Can be used as a decorator or a function. Parameters ---------- default : callable The default callable to us
(default=None, key=None, falsey=None)
| 16 | |
| 17 | |
| 18 | def 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 | |
| 57 | class GlobalMethod: |
no test coverage detected
searching dependent graphs…