home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import re
- import inspect
- import pkgutil
- import pydoc
- _lookfor_caches = { }
- _function_signature_re = re.compile('[a-z_]+\\(.*[,=].*\\)', re.I)
-
- def lookfor(what, modules = None, import_modules = True, regenerate = False):
- cache = { }
- for module in modules:
-
- try:
- c = _lookfor_generate_cache(module, import_modules, regenerate)
- cache.update(c)
- continue
- except ImportError:
- continue
-
-
-
- found = []
- whats = str(what).lower().split()
- if not whats:
- return None
- for docstring, kind, index in cache.iteritems():
- ok = True
- doc = docstring.lower()
- for w in whats:
- if w not in doc:
- ok = False
- break
- continue
- None<EXCEPTION MATCH>ImportError if kind in ('module', 'object') else whats
-
- if ok:
- found.append(name)
- continue
-
- kind_relevance = {
- 'func': 1000,
- 'class': 1000,
- 'module': -1000,
- 'object': -1000 }
-
- def relevance(name, docstr, kind, index):
- r = 0
- first_doc = '\n'.join(docstr.lower().strip().split('\n')[:3])
- [] += [](_[1])
- [] += [](_[2])
- r += -len(name) * 5
- r += kind_relevance.get(kind, -1000)
- r += -name.count('.') * 10
- r += max(-index / 100, -100)
- return r
-
-
- def relevance_sort(a, b):
- dr = relevance(b, *cache[b]) - relevance(a, *cache[a])
- if dr != 0:
- return dr
- return cmp(a, b)
-
- found.sort(relevance_sort)
- s = "Search results for '%s'" % ' '.join(whats)
- help_text = [
- s,
- '-' * len(s)]
- for name in found:
- (doc, kind, ix) = cache[name]
- doclines = _[1]
-
- try:
- first_doc = doclines[0].strip()
- if _function_signature_re.search(first_doc):
- first_doc = doclines[1].strip()
- except IndexError:
- []
- []
- []
- first_doc = ''
- except:
- (None, (None, None))
-
- help_text.append('%s\n %s' % (name, first_doc))
-
- if len(help_text) > 10:
- pager = pydoc.getpager()
- pager('\n'.join(help_text))
- else:
- print '\n'.join(help_text)
-
-
- def _lookfor_generate_cache(module, import_modules, regenerate):
- if module is None:
- module = 'numpy'
-
- if isinstance(module, str):
- module = __import__(module)
-
- if id(module) in _lookfor_caches and not regenerate:
- return _lookfor_caches[id(module)]
- cache = { }
- _lookfor_caches[id(module)] = cache
- seen = { }
- index = 0
- stack = [
- (module.__name__, module)]
- while stack:
- (name, item) = stack.pop(0)
- if id(item) in seen:
- continue
-
- seen[id(item)] = True
- index += 1
- kind = 'object'
- if inspect.ismodule(item):
- kind = 'module'
-
- try:
- _all = item.__all__
- except AttributeError:
- _all = None
-
- if import_modules and hasattr(item, '__path__'):
- for m in pkgutil.iter_modules(item.__path__):
- if _all is not None and m[1] not in _all:
- continue
-
-
- try:
- __import__('%s.%s' % (name, m[1]))
- continue
- except ImportError:
- continue
- continue
-
-
-
-
- for n, v in inspect.getmembers(item):
- if _all is not None and n not in _all:
- continue
-
- stack.append(('%s.%s' % (name, n), v))
-
- elif inspect.isclass(item):
- kind = 'class'
- for n, v in inspect.getmembers(item):
- stack.append(('%s.%s' % (name, n), v))
-
- elif callable(item):
- kind = 'func'
-
- doc = inspect.getdoc(item)
- if doc is not None:
- cache[name] = (doc, kind, index)
- continue
- return cache
-
- import IPython.ipapi as IPython
- ip = IPython.ipapi.get()
- _lookfor_modules = [
- 'numpy',
- 'scipy']
-
- def lookfor_f(self, arg = ''):
- lookfor(arg, modules = _lookfor_modules)
-
-
- def lookfor_modules_f(self, arg = ''):
- global _lookfor_modules
- if not arg:
- print 'Modules included in %lookfor search:', _lookfor_modules
- else:
- _lookfor_modules = arg.split()
-
- ip.expose_magic('lookfor', lookfor_f)
- ip.expose_magic('lookfor_modules', lookfor_modules_f)
-