-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathmodutils.py
More file actions
182 lines (136 loc) · 4.59 KB
/
modutils.py
File metadata and controls
182 lines (136 loc) · 4.59 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""Utility functions & decorators for dealing with FOOOF, as a module."""
from importlib import import_module
from functools import wraps
###################################################################################################
###################################################################################################
def safe_import(*args):
"""Try to import a module, with a safety net for if the module is not available.
Parameters
----------
*args : str
Module to import.
Returns
-------
mod : module or False
Requested module, if successfully imported, otherwise boolean (False).
Notes
-----
The input, `*args`, can be either 1 or 2 strings, as pass through inputs to import_module:
- To import a whole module, pass a single string, ex: ('matplotlib').
- To import a specific package, pass two strings, ex: ('.pyplot', 'matplotlib')
"""
try:
mod = import_module(*args)
except ImportError:
mod = False
# Prior to Python 3.5.4, import module could throw a SystemError
# Older approach requires the parent module be imported first
# If triggered, re-check for module after first importing the parent
except SystemError:
try:
_ = import_module(args[-1])
mod = import_module(*args)
except ImportError:
mod = False
return mod
def docs_drop_param(docstring):
"""Drop the first parameter description for a string representation of a docstring.
Parameters
----------
docstring : str
Docstring to drop first parameter from.
Returns
-------
str
New docstring, with first parameter dropped.
Notes
-----
This function assumes numpy docs standards.
It also assumes the parameter description to be dropped is only 2 lines long.
"""
sep = '----------\n'
ind = docstring.find(sep) + len(sep)
front, back = docstring[:ind], docstring[ind:]
for loop in range(2):
back = back[back.find('\n')+1:]
return front + back
def docs_append_to_section(docstring, section, add):
"""Append extra information to a specified section of a docstring.
Parameters
----------
docstring : str
Docstring to update.
section : str
Name of the section within the docstring to add to.
add : str
Text to append to specified section of the docstring.
Returns
-------
str
Updated docstring.
Notes
-----
This function assumes numpydoc documentation standard.
"""
return '\n\n'.join([split + add if section in split else split \
for split in docstring.split('\n\n')])
def copy_doc_func_to_method(source):
"""Decorator that copies method docstring from function, dropping first parameter.
Parameters
----------
source : function
Source function to copy docstring from.
Returns
-------
wrapper : function
The decorated function, with updated docs.
"""
def wrapper(func):
func.__doc__ = docs_drop_param(source.__doc__)
return func
return wrapper
def copy_doc_class(source, section='Attributes', add=''):
"""Decorator that copies method docstring from class, to another class, adding extra info.
Parameters
----------
source : cls
Source class to copy docstring from.
section : str, optional, default: 'Attributes'
Name of the section within the docstring to add to.
add : str, optional
Text to append to specified section of the docstring.
Returns
-------
wrapper : cls
The decorated class, with updated docs.
"""
def wrapper(func):
func.__doc__ = docs_append_to_section(source.__doc__, section, add)
return func
return wrapper
def check_dependency(dep, name):
"""Decorator that checks if an optional dependency is available.
Parameters
----------
dep : module or False
Module, if successfully imported, or boolean (False) if not.
name : str
Full name of the module, to be printed in message.
Returns
-------
wrap : callable
The decorated function.
Raises
------
ImportError
If the requested dependency is not available.
"""
def wrap(func):
@wraps(func)
def wrapped_func(*args, **kwargs):
if not dep:
raise ImportError("Optional FOOOF dependency " + name + \
" is required for this functionality.")
func(*args, **kwargs)
return wrapped_func
return wrap