@@ -24,18 +24,18 @@ def __init__(self, data):
24
24
String with lines separated by '\n '.
25
25
26
26
"""
27
- if isinstance (data ,list ):
27
+ if isinstance (data , list ):
28
28
self ._str = data
29
29
else :
30
- self ._str = data .split ('\n ' ) # store string as list of lines
30
+ self ._str = data .split ('\n ' ) # store string as list of lines
31
31
32
32
self .reset ()
33
33
34
34
def __getitem__ (self , n ):
35
35
return self ._str [n ]
36
36
37
37
def reset (self ):
38
- self ._l = 0 # current line nr
38
+ self ._l = 0 # current line nr
39
39
40
40
def read (self ):
41
41
if not self .eof ():
@@ -67,16 +67,18 @@ def read_to_condition(self, condition_func):
67
67
68
68
def read_to_next_empty_line (self ):
69
69
self .seek_next_non_empty_line ()
70
+
70
71
def is_empty (line ):
71
72
return not line .strip ()
73
+
72
74
return self .read_to_condition (is_empty )
73
75
74
76
def read_to_next_unindented_line (self ):
75
77
def is_unindented (line ):
76
78
return (line .strip () and (len (line .lstrip ()) == len (line )))
77
79
return self .read_to_condition (is_unindented )
78
80
79
- def peek (self ,n = 0 ):
81
+ def peek (self , n = 0 ):
80
82
if self ._l + n < len (self ._str ):
81
83
return self [self ._l + n ]
82
84
else :
@@ -139,25 +141,27 @@ def _is_at_section(self):
139
141
if l1 .startswith ('.. index::' ):
140
142
return True
141
143
142
- l2 = self ._doc .peek (1 ).strip () # ---------- or ==========
144
+ l2 = self ._doc .peek (1 ).strip () # ---------- or ==========
143
145
return l2 .startswith ('-' * len (l1 )) or l2 .startswith ('=' * len (l1 ))
144
146
145
- def _strip (self ,doc ):
147
+ def _strip (self , doc ):
146
148
i = 0
147
149
j = 0
148
- for i ,line in enumerate (doc ):
149
- if line .strip (): break
150
+ for i , line in enumerate (doc ):
151
+ if line .strip ():
152
+ break
150
153
151
- for j ,line in enumerate (doc [::- 1 ]):
152
- if line .strip (): break
154
+ for j , line in enumerate (doc [::- 1 ]):
155
+ if line .strip ():
156
+ break
153
157
154
158
return doc [i :len (doc )- j ]
155
159
156
160
def _read_to_next_section (self ):
157
161
section = self ._doc .read_to_next_empty_line ()
158
162
159
163
while not self ._is_at_section () and not self ._doc .eof ():
160
- if not self ._doc .peek (- 1 ).strip (): # previous line was empty
164
+ if not self ._doc .peek (- 1 ).strip (): # previous line was empty
161
165
section += ['' ]
162
166
163
167
section += self ._doc .read_to_next_empty_line ()
@@ -169,14 +173,14 @@ def _read_sections(self):
169
173
data = self ._read_to_next_section ()
170
174
name = data [0 ].strip ()
171
175
172
- if name .startswith ('..' ): # index section
176
+ if name .startswith ('..' ): # index section
173
177
yield name , data [1 :]
174
178
elif len (data ) < 2 :
175
179
yield StopIteration
176
180
else :
177
181
yield name , self ._strip (data [2 :])
178
182
179
- def _parse_param_list (self ,content ):
183
+ def _parse_param_list (self , content ):
180
184
r = Reader (content )
181
185
params = []
182
186
while not r .eof ():
@@ -189,13 +193,13 @@ def _parse_param_list(self,content):
189
193
desc = r .read_to_next_unindented_line ()
190
194
desc = dedent_lines (desc )
191
195
192
- params .append ((arg_name ,arg_type ,desc ))
196
+ params .append ((arg_name , arg_type , desc ))
193
197
194
198
return params
195
199
196
-
197
200
_name_rgx = re .compile (r"^\s*(:(?P<role>\w+):`(?P<name>[a-zA-Z0-9_.-]+)`|"
198
201
r" (?P<name2>[a-zA-Z0-9_.-]+))\s*" , re .X )
202
+
199
203
def _parse_see_also (self , content ):
200
204
"""
201
205
func_name : Descriptive text
@@ -228,7 +232,8 @@ def push_item(name, rest):
228
232
rest = []
229
233
230
234
for line in content :
231
- if not line .strip (): continue
235
+ if not line .strip ():
236
+ continue
232
237
233
238
m = self ._name_rgx .match (line )
234
239
if m and line [m .end ():].strip ().startswith (':' ):
@@ -307,7 +312,8 @@ def _parse(self):
307
312
308
313
for (section , content ) in sections :
309
314
if not section .startswith ('..' ):
310
- section = ' ' .join ([s .capitalize () for s in section .split (' ' )])
315
+ section = (s .capitalize () for s in section .split (' ' ))
316
+ section = ' ' .join (section )
311
317
if section in ('Parameters' , 'Returns' , 'Yields' , 'Raises' ,
312
318
'Warns' , 'Other Parameters' , 'Attributes' ,
313
319
'Methods' ):
@@ -332,7 +338,7 @@ def _str_indent(self, doc, indent=4):
332
338
333
339
def _str_signature (self ):
334
340
if self ['Signature' ]:
335
- return [self ['Signature' ].replace ('*' ,'\*' )] + ['' ]
341
+ return [self ['Signature' ].replace ('*' , '\*' )] + ['' ]
336
342
else :
337
343
return ['' ]
338
344
@@ -352,7 +358,7 @@ def _str_param_list(self, name):
352
358
out = []
353
359
if self [name ]:
354
360
out += self ._str_header (name )
355
- for param ,param_type ,desc in self [name ]:
361
+ for param , param_type , desc in self [name ]:
356
362
if param_type :
357
363
out += ['%s : %s' % (param , param_type )]
358
364
else :
@@ -370,7 +376,8 @@ def _str_section(self, name):
370
376
return out
371
377
372
378
def _str_see_also (self , func_role ):
373
- if not self ['See Also' ]: return []
379
+ if not self ['See Also' ]:
380
+ return []
374
381
out = []
375
382
out += self ._str_header ("See Also" )
376
383
last_had_desc = True
@@ -397,7 +404,7 @@ def _str_see_also(self, func_role):
397
404
def _str_index (self ):
398
405
idx = self ['index' ]
399
406
out = []
400
- out += ['.. index:: %s' % idx .get ('default' ,'' )]
407
+ out += ['.. index:: %s' % idx .get ('default' , '' )]
401
408
for section , references in idx .items ():
402
409
if section == 'default' :
403
410
continue
@@ -414,33 +421,35 @@ def __str__(self, func_role=''):
414
421
out += self ._str_param_list (param_list )
415
422
out += self ._str_section ('Warnings' )
416
423
out += self ._str_see_also (func_role )
417
- for s in ('Notes' ,'References' ,'Examples' ):
424
+ for s in ('Notes' , 'References' , 'Examples' ):
418
425
out += self ._str_section (s )
419
426
for param_list in ('Attributes' , 'Methods' ):
420
427
out += self ._str_param_list (param_list )
421
428
out += self ._str_index ()
422
429
return '\n ' .join (out )
423
430
424
431
425
- def indent (str ,indent = 4 ):
432
+ def indent (str , indent = 4 ):
426
433
indent_str = ' ' * indent
427
434
if str is None :
428
435
return indent_str
429
436
lines = str .split ('\n ' )
430
437
return '\n ' .join (indent_str + l for l in lines )
431
438
439
+
432
440
def dedent_lines (lines ):
433
441
"""Deindent a list of lines maximally"""
434
442
return textwrap .dedent ("\n " .join (lines )).split ("\n " )
435
443
444
+
436
445
def header (text , style = '-' ):
437
446
return text + '\n ' + style * len (text ) + '\n '
438
447
439
448
440
449
class FunctionDoc (NumpyDocString ):
441
450
def __init__ (self , func , role = 'func' , doc = None , config = {}):
442
451
self ._f = func
443
- self ._role = role # e.g. "func" or "meth"
452
+ self ._role = role # e.g. "func" or "meth"
444
453
445
454
if doc is None :
446
455
if func is None :
@@ -457,7 +466,7 @@ def __init__(self, func, role='func', doc=None, config={}):
457
466
else :
458
467
argspec = inspect .getargspec (func )
459
468
argspec = inspect .formatargspec (* argspec )
460
- argspec = argspec .replace ('*' ,'\*' )
469
+ argspec = argspec .replace ('*' , '\*' )
461
470
signature = '%s%s' % (func_name , argspec )
462
471
except TypeError as e :
463
472
signature = '%s()' % func_name
@@ -483,7 +492,7 @@ def __str__(self):
483
492
if self ._role :
484
493
if self ._role not in roles :
485
494
print ("Warning: invalid role %s" % self ._role )
486
- out += '.. %s:: %s\n \n \n ' % (roles .get (self ._role ,'' ),
495
+ out += '.. %s:: %s\n \n \n ' % (roles .get (self ._role , '' ),
487
496
func_name )
488
497
489
498
out += super (FunctionDoc , self ).__str__ (func_role = self ._role )
@@ -500,8 +509,8 @@ def __init__(self, cls, doc=None, modulename='', func_doc=FunctionDoc,
500
509
raise ValueError ("Expected a class or None, but got %r" % cls )
501
510
self ._cls = cls
502
511
503
- self .show_inherited_members = config .get ('show_inherited_class_members' ,
504
- True )
512
+ self .show_inherited_members = config .get (
513
+ 'show_inherited_class_members' , True )
505
514
506
515
if modulename and not modulename .endswith ('.' ):
507
516
modulename += '.'
0 commit comments