forked from librosa/librosa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.py
67 lines (53 loc) · 2.06 KB
/
decorators.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# CREATED:2015-02-15 10:06:03 by Brian McFee <brian.mcfee@nyu.edu>
'''Helpful tools for deprecation'''
import warnings
from decorator import decorator
import six
def moved(moved_from, version, version_removed):
'''This is a decorator which can be used to mark functions
as moved/renamed.
It will result in a warning being emitted when the function is used.
'''
def __wrapper(func, *args, **kwargs):
'''Warn the user, and then proceed.'''
code = six.get_function_code(func)
warnings.warn_explicit(
"{:s}\n\tThis function was moved to '{:s}.{:s}' in "
"librosa version {:s}."
"\n\tThis alias will be removed in librosa version "
"{:s}.".format(moved_from, func.__module__,
func.__name__, version, version_removed),
category=DeprecationWarning,
filename=code.co_filename,
lineno=code.co_firstlineno + 1
)
return func(*args, **kwargs)
return decorator(__wrapper)
def deprecated(version, version_removed):
'''This is a decorator which can be used to mark functions
as deprecated.
It will result in a warning being emitted when the function is used.'''
def __wrapper(func, *args, **kwargs):
'''Warn the user, and then proceed.'''
code = six.get_function_code(func)
warnings.warn_explicit(
"{:s}.{:s}\n\tDeprecated as of librosa version {:s}."
"\n\tIt will be removed in librosa version {:s}."
.format(func.__module__, func.__name__,
version, version_removed),
category=DeprecationWarning,
filename=code.co_filename,
lineno=code.co_firstlineno + 1
)
return func(*args, **kwargs)
return decorator(__wrapper)
def optional_jit(f):
'''This is a decorator which can be used to mark functions
as numba functions.'''
try:
import numba
return numba.jit(nopython=f)
except ImportError:
return f