Show More
@@ -4,6 +4,7 b'' | |||||
4 | Authors |
|
4 | Authors | |
5 | ------- |
|
5 | ------- | |
6 | - Jörgen Stenarson <jorgen.stenarson@bostream.nu> |
|
6 | - Jörgen Stenarson <jorgen.stenarson@bostream.nu> | |
|
7 | - Thomas Kluyver | |||
7 | """ |
|
8 | """ | |
8 |
|
9 | |||
9 | #***************************************************************************** |
|
10 | #***************************************************************************** | |
@@ -19,7 +20,7 b' import types' | |||||
19 | from IPython.utils.dir2 import dir2 |
|
20 | from IPython.utils.dir2 import dir2 | |
20 |
|
21 | |||
21 | def create_typestr2type_dicts(dont_include_in_type2typestr=["lambda"]): |
|
22 | def create_typestr2type_dicts(dont_include_in_type2typestr=["lambda"]): | |
22 | """Return dictionaries mapping lower case typename (e.g. 'tuple) to type |
|
23 | """Return dictionaries mapping lower case typename (e.g. 'tuple') to type | |
23 | objects from the types package, and vice versa.""" |
|
24 | objects from the types package, and vice versa.""" | |
24 | typenamelist = [tname for tname in dir(types) if tname.endswith("Type")] |
|
25 | typenamelist = [tname for tname in dir(types) if tname.endswith("Type")] | |
25 | typestr2type, type2typestr = {}, {} |
|
26 | typestr2type, type2typestr = {}, {} | |
@@ -40,7 +41,7 b' def is_type(obj, typestr_or_type):' | |||||
40 | 'tuple'<->TupleType. 'all' matches all types. |
|
41 | 'tuple'<->TupleType. 'all' matches all types. | |
41 |
|
42 | |||
42 | TODO: Should be extended for choosing more than one type.""" |
|
43 | TODO: Should be extended for choosing more than one type.""" | |
43 | if typestr_or_type=="all": |
|
44 | if typestr_or_type == "all": | |
44 | return True |
|
45 | return True | |
45 | if type(typestr_or_type) == types.TypeType: |
|
46 | if type(typestr_or_type) == types.TypeType: | |
46 | test_type = typestr_or_type |
|
47 | test_type = typestr_or_type | |
@@ -50,76 +51,61 b' def is_type(obj, typestr_or_type):' | |||||
50 | return isinstance(obj, test_type) |
|
51 | return isinstance(obj, test_type) | |
51 | return False |
|
52 | return False | |
52 |
|
53 | |||
53 | def show_hidden(str,show_all=False): |
|
54 | def show_hidden(str, show_all=False): | |
54 | """Return true for strings starting with single _ if show_all is true.""" |
|
55 | """Return true for strings starting with single _ if show_all is true.""" | |
55 | return show_all or str.startswith("__") or not str.startswith("_") |
|
56 | return show_all or str.startswith("__") or not str.startswith("_") | |
56 |
|
57 | |||
57 | class NameSpace(dict): |
|
58 | def dict_dir(obj): | |
58 | """NameSpace holds the dictionary for a namespace and implements filtering |
|
59 | """Produce a dictionary of an object's attributes. Builds on dir2 by | |
59 | on name and types""" |
|
60 | checking that a getattr() call actually succeeds.""" | |
60 |
|
61 | ns = {} | ||
61 | @classmethod |
|
62 | for key in dir2(obj): | |
62 | def from_object(cls, obj, *args, **kwargs): |
|
63 | # This seemingly unnecessary try/except is actually needed | |
63 | """Instantiate a namespace by constructing a dictionary of an object's |
|
64 | # because there is code out there with metaclasses that | |
64 | attributes. A class method, returns a new NameSpace instance.""" |
|
65 | # create 'write only' attributes, where a getattr() call | |
65 | ns = cls() |
|
66 | # will fail even if the attribute appears listed in the | |
66 | for key in dir2(obj): |
|
67 | # object's dictionary. Properties can actually do the same | |
67 | if isinstance(key, basestring): |
|
68 | # thing. In particular, Traits use this pattern | |
68 | # This seemingly unnecessary try/except is actually needed |
|
69 | try: | |
69 | # because there is code out there with metaclasses that |
|
70 | ns[key] = getattr(obj, key) | |
70 | # create 'write only' attributes, where a getattr() call |
|
71 | except AttributeError: | |
71 | # will fail even if the attribute appears listed in the |
|
72 | pass | |
72 | # object's dictionary. Properties can actually do the same |
|
73 | return ns | |
73 | # thing. In particular, Traits use this pattern |
|
|||
74 | try: |
|
|||
75 | ns[key] = getattr(obj,key) |
|
|||
76 | except AttributeError: |
|
|||
77 | pass |
|
|||
78 | return ns |
|
|||
79 |
|
||||
80 | def filter(self, name_pattern="*", type_pattern="all", ignore_case=True, |
|
|||
81 | show_all=True): |
|
|||
82 | """Return a dictionary of the namespace filtered by regex pattern and |
|
|||
83 | item type.""" |
|
|||
84 | pattern=name_pattern.replace("*",".*").replace("?",".") |
|
|||
85 | if ignore_case: |
|
|||
86 | reg=re.compile(pattern+"$",re.I) |
|
|||
87 | else: |
|
|||
88 | reg=re.compile(pattern+"$") |
|
|||
89 |
|
74 | |||
90 | # Check each one matches regex; shouldn't be hidden; of correct type. |
|
75 | def filter_ns(ns, name_pattern="*", type_pattern="all", ignore_case=True, | |
91 | return dict((key,obj) for key,obj in self.iteritems() if reg.match(key)\ |
|
76 | show_all=True): | |
92 | and show_hidden(key, show_all)\ |
|
77 | """Filter a namespace dictionary by name pattern and item type.""" | |
93 | and is_type(obj, type_pattern)) |
|
78 | pattern = name_pattern.replace("*",".*").replace("?",".") | |
|
79 | if ignore_case: | |||
|
80 | reg = re.compile(pattern+"$", re.I) | |||
|
81 | else: | |||
|
82 | reg = re.compile(pattern+"$") | |||
|
83 | ||||
|
84 | # Check each one matches regex; shouldn't be hidden; of correct type. | |||
|
85 | return dict((key,obj) for key, obj in ns.iteritems() if reg.match(key) \ | |||
|
86 | and show_hidden(key, show_all) \ | |||
|
87 | and is_type(obj, type_pattern) ) | |||
94 |
|
88 | |||
95 | def list_namespace(namespace, type_pattern, filter, ignore_case=False, show_all=False): |
|
89 | def list_namespace(namespace, type_pattern, filter, ignore_case=False, show_all=False): | |
96 | """Return dictionary of all objects in a namespace dictionary that match |
|
90 | """Return dictionary of all objects in a namespace dictionary that match | |
97 | type_pattern and filter.""" |
|
91 | type_pattern and filter.""" | |
98 | pattern_list=filter.split(".") |
|
92 | pattern_list=filter.split(".") | |
99 | ns = NameSpace(namespace) |
|
|||
100 | return _list_namespace(ns, type_pattern, pattern_list, ignore_case,show_all) |
|
|||
101 | # This function is a more convenient wrapper around the recursive one below. |
|
|||
102 |
|
||||
103 | def _list_namespace(ns, type_pattern, pattern_list, ignore_case=False, show_all=False): |
|
|||
104 | """Return dictionary of objects in a namespace which match type_pattern |
|
|||
105 | and filter (pattern_list). |
|
|||
106 |
|
||||
107 | This is a recursive function behind list_namespace, which is intended to be |
|
|||
108 | the public interface. Unlike that function, this expects a NameSpace |
|
|||
109 | instance as the first argument, and the name pattern split by '.'s.""" |
|
|||
110 | if len(pattern_list) == 1: |
|
93 | if len(pattern_list) == 1: | |
111 |
return |
|
94 | return filter_ns(namespace, name_pattern=pattern_list[0], | |
112 | ignore_case=ignore_case, show_all=show_all) |
|
95 | type_pattern=type_pattern, | |
|
96 | ignore_case=ignore_case, show_all=show_all) | |||
113 | else: |
|
97 | else: | |
114 | # This is where we can change if all objects should be searched or |
|
98 | # This is where we can change if all objects should be searched or | |
115 | # only modules. Just change the type_pattern to module to search only |
|
99 | # only modules. Just change the type_pattern to module to search only | |
116 | # modules |
|
100 | # modules | |
117 |
filtered = |
|
101 | filtered = filter_ns(namespace, name_pattern=pattern_list[0], | |
|
102 | type_pattern="all", | |||
118 | ignore_case=ignore_case, show_all=show_all) |
|
103 | ignore_case=ignore_case, show_all=show_all) | |
119 | results = {} |
|
104 | results = {} | |
120 | for name, obj in filtered.iteritems(): |
|
105 | for name, obj in filtered.iteritems(): | |
121 |
ns = |
|
106 | ns = list_namespace(dict_dir(obj), type_pattern, | |
122 | pattern_list[1:], ignore_case=ignore_case, show_all=show_all) |
|
107 | ".".join(pattern_list[1:]), | |
123 | for inner_name,inner_obj in ns.iteritems(): |
|
108 | ignore_case=ignore_case, show_all=show_all) | |
|
109 | for inner_name, inner_obj in ns.iteritems(): | |||
124 | results["%s.%s"%(name,inner_name)] = inner_obj |
|
110 | results["%s.%s"%(name,inner_name)] = inner_obj | |
125 | return results |
|
111 | return results |
General Comments 0
You need to be logged in to leave comments.
Login now