Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b693ed

Browse files
committed
Moved pylab._shift_string() to cbook.dedent() to facilitate its use
svn path=/trunk/matplotlib/; revision=2959
1 parent 8c34b22 commit 7b693ed

File tree

3 files changed

+124
-123
lines changed

3 files changed

+124
-123
lines changed

boilerplate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def %(func)s(*args, **kwargs):
2828
hold(b)
2929
return ret
3030
if Axes.%(func)s.__doc__ is not None:
31-
%(func)s.__doc__ = _shift_string(Axes.%(func)s.__doc__) + \"\"\"
31+
%(func)s.__doc__ = dedent(Axes.%(func)s.__doc__) + \"\"\"
3232
Addition kwargs: hold = [True|False] overrides default hold state\"\"\"
3333
"""
3434

@@ -41,7 +41,7 @@ def %(func)s(*args, **kwargs):
4141
draw_if_interactive()
4242
return ret
4343
if Axes.%(func)s.__doc__ is not None:
44-
%(func)s.__doc__ = _shift_string(Axes.%(func)s.__doc__)
44+
%(func)s.__doc__ = dedent(Axes.%(func)s.__doc__)
4545
"""
4646

4747
# these methods are all simple wrappers of Axes methods by the same

lib/matplotlib/cbook.py

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class silent_list(list):
1717
def __init__(self, type, seq=None):
1818
self.type = type
1919
if seq is not None: self.extend(seq)
20-
20+
2121
def __repr__(self):
2222
return '<a list of %d %s objects>' % (len(self), self.type)
2323

@@ -55,7 +55,7 @@ def iterable(obj):
5555
try: len(obj)
5656
except: return 0
5757
return 1
58-
58+
5959

6060
def is_string_like(obj):
6161
if hasattr(obj, 'shape'): return 0 # this is a workaround
@@ -119,7 +119,7 @@ class Sorter:
119119
sort(list) # default sort
120120
sort(list, 1) # sort by index 1
121121
sort(dict, 'a') # sort a list of dicts by key 'a'
122-
122+
123123
"""
124124

125125
def _helper(self, data, aux, inplace):
@@ -233,8 +233,8 @@ def __delattr__(self, name): return self
233233
def mkdirs(newdir, mode=0777):
234234
try: os.makedirs(newdir, mode)
235235
except OSError, err:
236-
# Reraise the error unless it's about an already existing directory
237-
if err.errno != errno.EEXIST or not os.path.isdir(newdir):
236+
# Reraise the error unless it's about an already existing directory
237+
if err.errno != errno.EEXIST or not os.path.isdir(newdir):
238238
raise
239239

240240

@@ -243,7 +243,7 @@ def dict_delall(d, keys):
243243
for key in keys:
244244
try: del d[key]
245245
except KeyError: pass
246-
246+
247247

248248
class RingBuffer:
249249
""" class that implements a not-yet-full buffer """
@@ -277,7 +277,7 @@ def __get_item__(self, i):
277277
return self.data[i % len(self.data)]
278278

279279

280-
# use enumerate builtin if available, else use python version
280+
# use enumerate builtin if available, else use python version
281281
try:
282282
import __builtin__
283283
enumerate = __builtin__.enumerate
@@ -290,7 +290,7 @@ def enumerate(seq):
290290
yield i, seq[i]
291291

292292

293-
# use itertools.izip if available, else use python version
293+
# use itertools.izip if available, else use python version
294294
try:
295295
import itertools
296296
izip = itertools.izip
@@ -303,21 +303,21 @@ def izip(*iterables):
303303
while iterables:
304304
result = [i.next() for i in iterables]
305305
yield tuple(result)
306-
306+
307307

308308
def get_split_ind(seq, N):
309309
"""seq is a list of words. Return the index into seq such that
310310
len(' '.join(seq[:ind])<=N
311311
"""
312-
312+
313313
sLen = 0
314314
# todo: use Alex's xrange pattern from the cbook for efficiency
315315
for (word, ind) in zip(seq, range(len(seq))):
316316
sLen += len(word) + 1 # +1 to account for the len(' ')
317317
if sLen>=N: return ind
318318
return len(seq)
319-
320-
319+
320+
321321
def wrap(prefix, text, cols):
322322
'wrap text with prefix at length cols'
323323
pad = ' '*len(prefix.expandtabs())
@@ -338,6 +338,27 @@ def wrap(prefix, text, cols):
338338
ret += pad + ' '.join(line) + '\n'
339339
return ret
340340

341+
def dedent(s):
342+
"""
343+
Remove excess indentation from docstrings.
344+
345+
Discards any leading blank lines, then removes up to
346+
n whitespace characters from each line, where n is
347+
the number of leading whitespace characters in the
348+
first line. It differs from textwrap.dedent in its
349+
deletion of leading blank lines and its use of the
350+
first non-blank line to determine the indentation.
351+
"""
352+
lines = s.splitlines(True)
353+
ii = 0
354+
while lines[ii].strip() == '':
355+
ii += 1
356+
lines = lines[ii:]
357+
nshift = len(lines[0]) - len(lines[0].lstrip())
358+
for i, line in enumerate(lines):
359+
nwhite = len(line) - len(line.lstrip())
360+
lines[i] = line[min(nshift, nwhite):]
361+
return ''.join(lines)
341362

342363

343364

@@ -377,7 +398,7 @@ def get_recursive_filelist(args):
377398
return the files as a list of strings
378399
"""
379400
files = []
380-
401+
381402
for arg in args:
382403
if os.path.isfile(arg):
383404
files.append(arg)
@@ -438,11 +459,11 @@ def allpairs(x):
438459
return all possible pairs in sequence x
439460
440461
Condensed by Alex Martelli from this thread on c.l.python
441-
http://groups.google.com/groups?q=all+pairs+group:*python*&hl=en&lr=&ie=UTF-8&selm=mailman.4028.1096403649.5135.python-list%40python.org&rnum=1
462+
http://groups.google.com/groups?q=all+pairs+group:*python*&hl=en&lr=&ie=UTF-8&selm=mailman.4028.1096403649.5135.python-list%40python.org&rnum=1
442463
"""
443464
return [ (s, f) for i, f in enumerate(x) for s in x[i+1:] ]
444465

445-
466+
446467

447468

448469
# python 2.2 dicts don't have pop
@@ -456,7 +477,7 @@ def popd(d, *args):
456477
457478
# returns value for key if key exists, else default. Delete key,
458479
# val item if it exists. Will not raise a KeyError
459-
val = popd(d, key, default)
480+
val = popd(d, key, default)
460481
"""
461482
if len(args)==1:
462483
key = args[0]
@@ -485,7 +506,7 @@ def __setitem__(self, k, v):
485506
del self[self._killkeys[0]]
486507
del self._killkeys[0]
487508
dict.__setitem__(self, k, v)
488-
self._killkeys.append(k)
509+
self._killkeys.append(k)
489510

490511

491512

@@ -499,7 +520,7 @@ class Stack:
499520
def __init__(self, default=None):
500521
self.clear()
501522
self._default = default
502-
523+
503524
def __call__(self):
504525
'return the current element, or None'
505526
if not len(self._elements): return self._default
@@ -525,7 +546,7 @@ def push(self, o):
525546
self._elements.append(o)
526547
self._pos = len(self._elements)-1
527548
return self()
528-
549+
529550
def home(self):
530551
'push the first element onto the top of the stack'
531552
if not len(self._elements): return
@@ -567,15 +588,15 @@ def remove(self, o):
567588
for thiso in old:
568589
if thiso==o: continue
569590
else: self.push(thiso)
570-
591+
571592
def popall(seq):
572593
'empty a list'
573594
for i in xrange(len(seq)): seq.pop()
574595

575596
def finddir(o, match, case=False):
576597
"""
577598
return all attributes of o which match string in match. if case
578-
is True require an exact case match.
599+
is True require an exact case match.
579600
"""
580601
if case:
581602
names = [(name,name) for name in dir(o) if is_string_like(name)]
@@ -594,4 +615,4 @@ def reverse_dict(d):
594615
assert(not allequal([1,1,0]) )
595616
assert( allequal([]) )
596617
assert( allequal(('a', 'a')))
597-
assert( not allequal(('a', 'b')))
618+
assert( not allequal(('a', 'b')))

0 commit comments

Comments
 (0)