Package edPDB :: Module utilities
[hide private]
[frames] | no frames]

Source Code for Module edPDB.utilities

 1  # utilities for edPDB 
 2  # Copyright (c) 2009 Oliver Beckstein <orbeckst@gmail.com> 
 3  # Released under the GNU Public License 3 (or higher, your choice) 
 4  # See the file COPYING for details. 
 5   
 6  """ 
 7  :mod:`edPDB.utilities` -- Helper functions and classes 
 8  ======================================================== 
 9   
10  The module defines some convenience functions and classes that are 
11  used in other modules 
12   
13   
14  Functions 
15  --------- 
16   
17  Functions that improve list processing and which do *not* treat 
18  strings as lists: 
19   
20  .. autofunction:: iterable 
21  .. autofunction:: asiterable 
22   
23  """ 
24   
25   
26 -def iterable(obj):
27 """Returns ``True`` if *obj* can be iterated over and is *not* a string.""" 28 if type(obj) is str: 29 return False # avoid iterating over characters of a string 30 31 if hasattr(obj, 'next'): 32 return True # any iterator will do 33 try: 34 len(obj) # anything else that might work 35 except TypeError: 36 return False 37 return True
38
39 -def asiterable(obj):
40 """Returns obj so that it can be iterated over; a string is *not* treated as iterable""" 41 if not iterable(obj): 42 obj = [obj] 43 return obj
44