##// END OF EJS Templates
the __future__ is now.
the __future__ is now.

File last commit:

r13372:575b670f
r22963:2961b531
Show More
wildcard.py
112 lines | 4.5 KiB | text/x-python | PythonLexer
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37 # -*- coding: utf-8 -*-
"""Support for wildcard pattern matching in object inspection.
Fernando Perez
Update copyright/author statements....
r1875
Authors
-------
- Jörgen Stenarson <jorgen.stenarson@bostream.nu>
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 - Thomas Kluyver
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37 """
#*****************************************************************************
# Copyright (C) 2005 Jörgen Stenarson <jorgen.stenarson@bostream.nu>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#*****************************************************************************
fperez
Cosmetic cleanups: put all imports in a single line, and sort them...
r52 import re
import types
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 from IPython.utils.dir2 import dir2
Thomas Kluyver
Fix references to dict.iteritems and dict.itervalues
r13361 from .py3compat import iteritems
fperez
- Implement a traits-aware tab-completer. See ipy_traits_completer in...
r742
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 def create_typestr2type_dicts(dont_include_in_type2typestr=["lambda"]):
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 """Return dictionaries mapping lower case typename (e.g. 'tuple') to type
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 objects from the types package, and vice versa."""
typenamelist = [tname for tname in dir(types) if tname.endswith("Type")]
typestr2type, type2typestr = {}, {}
Bernardo B. Marques
remove all trailling spaces
r4872
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37 for tname in typenamelist:
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 name = tname[:-4].lower() # Cut 'Type' off the end of the name
obj = getattr(types, tname)
typestr2type[name] = obj
if name not in dont_include_in_type2typestr:
type2typestr[obj] = name
return typestr2type, type2typestr
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 typestr2type, type2typestr = create_typestr2type_dicts()
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 def is_type(obj, typestr_or_type):
Bernardo B. Marques
remove all trailling spaces
r4872 """is_type(obj, typestr_or_type) verifies if obj is of a certain type. It
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 can take strings or actual python types for the second argument, i.e.
'tuple'<->TupleType. 'all' matches all types.
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 TODO: Should be extended for choosing more than one type."""
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 if typestr_or_type == "all":
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37 return True
Thomas Kluyver
Fix references to deprecated objects in the types module...
r13372 if type(typestr_or_type) == type:
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 test_type = typestr_or_type
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37 else:
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 test_type = typestr2type.get(typestr_or_type, False)
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37 if test_type:
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 return isinstance(obj, test_type)
return False
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 def show_hidden(str, show_all=False):
fperez
Wildcard system cleanup, ipmaker speedups, bugfix in globals handling...
r41 """Return true for strings starting with single _ if show_all is true."""
return show_all or str.startswith("__") or not str.startswith("_")
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 def dict_dir(obj):
"""Produce a dictionary of an object's attributes. Builds on dir2 by
checking that a getattr() call actually succeeds."""
ns = {}
for key in dir2(obj):
# This seemingly unnecessary try/except is actually needed
# because there is code out there with metaclasses that
# create 'write only' attributes, where a getattr() call
# will fail even if the attribute appears listed in the
# object's dictionary. Properties can actually do the same
# thing. In particular, Traits use this pattern
try:
ns[key] = getattr(obj, key)
except AttributeError:
pass
return ns
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 def filter_ns(ns, name_pattern="*", type_pattern="all", ignore_case=True,
show_all=True):
"""Filter a namespace dictionary by name pattern and item type."""
pattern = name_pattern.replace("*",".*").replace("?",".")
if ignore_case:
reg = re.compile(pattern+"$", re.I)
else:
reg = re.compile(pattern+"$")
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 # Check each one matches regex; shouldn't be hidden; of correct type.
Thomas Kluyver
Fix references to dict.iteritems and dict.itervalues
r13361 return dict((key,obj) for key, obj in iteritems(ns) if reg.match(key) \
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 and show_hidden(key, show_all) \
and is_type(obj, type_pattern) )
fperez
New wildcard support. Lightly tested, so proceed with caution. We need to...
r37
Thomas Kluyver
Further tidying up of IPython.utils.wildcard.
r3264 def list_namespace(namespace, type_pattern, filter, ignore_case=False, show_all=False):
Thomas Kluyver
Fix for issue 129. Tests in previous commit now pass.
r3262 """Return dictionary of all objects in a namespace dictionary that match
type_pattern and filter."""
fperez
Wildcard system cleanup, ipmaker speedups, bugfix in globals handling...
r41 pattern_list=filter.split(".")
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 if len(pattern_list) == 1:
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 return filter_ns(namespace, name_pattern=pattern_list[0],
type_pattern=type_pattern,
ignore_case=ignore_case, show_all=show_all)
fperez
Wildcard system cleanup, ipmaker speedups, bugfix in globals handling...
r41 else:
# This is where we can change if all objects should be searched or
# only modules. Just change the type_pattern to module to search only
# modules
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 filtered = filter_ns(namespace, name_pattern=pattern_list[0],
type_pattern="all",
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 ignore_case=ignore_case, show_all=show_all)
results = {}
Thomas Kluyver
Fix references to dict.iteritems and dict.itervalues
r13361 for name, obj in iteritems(filtered):
Bernardo B. Marques
remove all trailling spaces
r4872 ns = list_namespace(dict_dir(obj), type_pattern,
Thomas Kluyver
Yet more revisions to the wildcard module.
r3266 ".".join(pattern_list[1:]),
ignore_case=ignore_case, show_all=show_all)
Thomas Kluyver
Fix references to dict.iteritems and dict.itervalues
r13361 for inner_name, inner_obj in iteritems(ns):
Thomas Kluyver
Simplify and tidy up IPython.utils.wildcard.
r3263 results["%s.%s"%(name,inner_name)] = inner_obj
return results