@@ -17,7 +17,7 @@ class silent_list(list):
17
17
def __init__ (self , type , seq = None ):
18
18
self .type = type
19
19
if seq is not None : self .extend (seq )
20
-
20
+
21
21
def __repr__ (self ):
22
22
return '<a list of %d %s objects>' % (len (self ), self .type )
23
23
@@ -55,7 +55,7 @@ def iterable(obj):
55
55
try : len (obj )
56
56
except : return 0
57
57
return 1
58
-
58
+
59
59
60
60
def is_string_like (obj ):
61
61
if hasattr (obj , 'shape' ): return 0 # this is a workaround
@@ -119,7 +119,7 @@ class Sorter:
119
119
sort(list) # default sort
120
120
sort(list, 1) # sort by index 1
121
121
sort(dict, 'a') # sort a list of dicts by key 'a'
122
-
122
+
123
123
"""
124
124
125
125
def _helper (self , data , aux , inplace ):
@@ -233,8 +233,8 @@ def __delattr__(self, name): return self
233
233
def mkdirs (newdir , mode = 0777 ):
234
234
try : os .makedirs (newdir , mode )
235
235
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 ):
238
238
raise
239
239
240
240
@@ -243,7 +243,7 @@ def dict_delall(d, keys):
243
243
for key in keys :
244
244
try : del d [key ]
245
245
except KeyError : pass
246
-
246
+
247
247
248
248
class RingBuffer :
249
249
""" class that implements a not-yet-full buffer """
@@ -277,7 +277,7 @@ def __get_item__(self, i):
277
277
return self .data [i % len (self .data )]
278
278
279
279
280
- # use enumerate builtin if available, else use python version
280
+ # use enumerate builtin if available, else use python version
281
281
try :
282
282
import __builtin__
283
283
enumerate = __builtin__ .enumerate
@@ -290,7 +290,7 @@ def enumerate(seq):
290
290
yield i , seq [i ]
291
291
292
292
293
- # use itertools.izip if available, else use python version
293
+ # use itertools.izip if available, else use python version
294
294
try :
295
295
import itertools
296
296
izip = itertools .izip
@@ -303,21 +303,21 @@ def izip(*iterables):
303
303
while iterables :
304
304
result = [i .next () for i in iterables ]
305
305
yield tuple (result )
306
-
306
+
307
307
308
308
def get_split_ind (seq , N ):
309
309
"""seq is a list of words. Return the index into seq such that
310
310
len(' '.join(seq[:ind])<=N
311
311
"""
312
-
312
+
313
313
sLen = 0
314
314
# todo: use Alex's xrange pattern from the cbook for efficiency
315
315
for (word , ind ) in zip (seq , range (len (seq ))):
316
316
sLen += len (word ) + 1 # +1 to account for the len(' ')
317
317
if sLen >= N : return ind
318
318
return len (seq )
319
-
320
-
319
+
320
+
321
321
def wrap (prefix , text , cols ):
322
322
'wrap text with prefix at length cols'
323
323
pad = ' ' * len (prefix .expandtabs ())
@@ -338,6 +338,27 @@ def wrap(prefix, text, cols):
338
338
ret += pad + ' ' .join (line ) + '\n '
339
339
return ret
340
340
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 )
341
362
342
363
343
364
@@ -377,7 +398,7 @@ def get_recursive_filelist(args):
377
398
return the files as a list of strings
378
399
"""
379
400
files = []
380
-
401
+
381
402
for arg in args :
382
403
if os .path .isfile (arg ):
383
404
files .append (arg )
@@ -438,11 +459,11 @@ def allpairs(x):
438
459
return all possible pairs in sequence x
439
460
440
461
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
442
463
"""
443
464
return [ (s , f ) for i , f in enumerate (x ) for s in x [i + 1 :] ]
444
465
445
-
466
+
446
467
447
468
448
469
# python 2.2 dicts don't have pop
@@ -456,7 +477,7 @@ def popd(d, *args):
456
477
457
478
# returns value for key if key exists, else default. Delete key,
458
479
# val item if it exists. Will not raise a KeyError
459
- val = popd(d, key, default)
480
+ val = popd(d, key, default)
460
481
"""
461
482
if len (args )== 1 :
462
483
key = args [0 ]
@@ -485,7 +506,7 @@ def __setitem__(self, k, v):
485
506
del self [self ._killkeys [0 ]]
486
507
del self ._killkeys [0 ]
487
508
dict .__setitem__ (self , k , v )
488
- self ._killkeys .append (k )
509
+ self ._killkeys .append (k )
489
510
490
511
491
512
@@ -499,7 +520,7 @@ class Stack:
499
520
def __init__ (self , default = None ):
500
521
self .clear ()
501
522
self ._default = default
502
-
523
+
503
524
def __call__ (self ):
504
525
'return the current element, or None'
505
526
if not len (self ._elements ): return self ._default
@@ -525,7 +546,7 @@ def push(self, o):
525
546
self ._elements .append (o )
526
547
self ._pos = len (self ._elements )- 1
527
548
return self ()
528
-
549
+
529
550
def home (self ):
530
551
'push the first element onto the top of the stack'
531
552
if not len (self ._elements ): return
@@ -567,15 +588,15 @@ def remove(self, o):
567
588
for thiso in old :
568
589
if thiso == o : continue
569
590
else : self .push (thiso )
570
-
591
+
571
592
def popall (seq ):
572
593
'empty a list'
573
594
for i in xrange (len (seq )): seq .pop ()
574
595
575
596
def finddir (o , match , case = False ):
576
597
"""
577
598
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.
579
600
"""
580
601
if case :
581
602
names = [(name ,name ) for name in dir (o ) if is_string_like (name )]
@@ -594,4 +615,4 @@ def reverse_dict(d):
594
615
assert (not allequal ([1 ,1 ,0 ]) )
595
616
assert ( allequal ([]) )
596
617
assert ( allequal (('a' , 'a' )))
597
- assert ( not allequal (('a' , 'b' )))
618
+ assert ( not allequal (('a' , 'b' )))
0 commit comments