public
Description: Assorted and divers code of varying length and utility.
Homepage: http://samuelhuckins.com/
Clone URL: git://github.com/shuckins/sph_code.git
100644 47 lines (40 sloc) 1.436 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
#!/usr/bin/env python
"""
Assorted utilities.
"""
 
def mf(obj, term):
    """
Searches through the methods defined for obj,
looks for those containing the term passed.
Prints all matches or 'No matches' if none found.
 
Initial version improved by Jason R. Coombs:
https://svn.jaraco.com/jaraco/python/jaraco.util/jaraco/lang/python.py
 
>>> mf(set, "diff")
['difference', 'difference_update', 'symmetric_difference', 'symmetric_difference_update']
"""
    methods = dir(obj)
    term = term.lower()
    result = [m for m in methods if term in m.lower()] or 'No matches'
    return result
 
def obinfo(obj):
    """
Print useful information about object.
 
From http://www.ibm.com/developerworks/library/l-pyint.html
 
Initial version improved by Jason R. Coombs:
https://svn.jaraco.com/jaraco/python/jaraco.util/jaraco/lang/python.py
"""
    if hasattr(obj, '__name__'):
        print "NAME: ", obj.__name__
    if hasattr(obj, '__class__'):
        print "CLASS: ", obj.__class__.__name__
    print("ID: ", id(obj))
    print("TYPE: ", type(obj))
    print("VALUE: ", repr(obj))
    print("CALLABLE:", ['No', 'Yes'][callable(obj)])
    if hasattr(obj, '__doc__'):
        doc = getattr(obj, '__doc__')
        doc = doc.strip()
        topfive = doc.split('\n')[0:4]
        print "DOC: ", "\n".join(topfive)
    else:
        print "No docstring. Yell at the author."