def _provide_feed(self): try: __IPYTHON__ # pylint: disable=E0602 from IPython.terminal.interactiveshell import InteractiveShell InteractiveShell.instance().user_ns["feed"] = self.feed except NameError: globals()["feed"] = self.feed
def __call__(self, ds): """Convert IPython prompts to python ones in a string.""" from . import globalipapp pyps1 = '>>> ' pyps2 = '... ' pyout = '' dnew = ds dnew = self.rps1.sub(pyps1, dnew) dnew = self.rps2.sub(pyps2, dnew) dnew = self.rout.sub(pyout, dnew) ip = InteractiveShell.instance() # Convert input IPython source into valid Python. out = [] newline = out.append for line in dnew.splitlines(): mps1 = self.rpyps1.match(line) if mps1 is not None: prompt, text = mps1.groups() newline(prompt+ip.prefilter(text, False)) continue mps2 = self.rpyps2.match(line) if mps2 is not None: prompt, text = mps2.groups() newline(prompt+ip.prefilter(text, True)) continue newline(line) newline('') # ensure a closing newline, needed by doctest #print "PYSRC:", '\n'.join(out) # dbg return '\n'.join(out)
def __call__(cls, *args, **kwargs): """ Checks for misprints in argument names """ obj = super(UnitRegistry, cls).__call__(*args, **kwargs) if hasattr(cls, "DISABLE_KWARGS_CHECK") or not UnitRegistry.enabled: return obj def warning(*largs): obj.warning(*largs) if root.common.trace.misprints: obj.warning( "Stack trace:\n%s", "".join( format_list( extract_stack( inspect.currentframe().f_back.f_back)))) # Build the matrix of differences matrix = {} matched = set() for given_kwarg in kwargs: for kwattr in cls.KWATTRS: if (kwattr, given_kwarg) in matrix: continue matrix[(given_kwarg, kwattr)] = d = \ damerau_levenshtein_distance(given_kwarg, kwattr) if d == 0: # perfect match, stop further comparisons matched.add(given_kwarg) break if len(matched) < len(kwargs): # Find replacement candidates with distance = 1 ignored_kwargs = set() for given_kwarg in set(kwargs).difference(matched): candidates = [] for kwattr in cls.KWATTRS: d = matrix.get((given_kwarg, kwattr)) if d == 1: candidates.append(kwattr) if len(candidates) == 0: ignored_kwargs.add(given_kwarg) else: warning( "Creating %s: potential misprint in keyword argument " "name: expected %s - got %s", obj, " or ".join(candidates), given_kwarg) try: __IPYTHON__ # pylint: disable=E0602 from IPython.terminal.interactiveshell import \ InteractiveShell ignored_kwargs -= set(InteractiveShell.instance().user_ns) except NameError: pass if len(ignored_kwargs) > 0: warning( "Creating %s: ignored the following keyword arguments: %s", obj, ", ".join(sorted(ignored_kwargs))) return obj
def _revoke_feed(self): try: __IPYTHON__ # pylint: disable=E0602 from IPython.terminal.interactiveshell import InteractiveShell user_ns = InteractiveShell.instance().user_ns if "feed" in user_ns: del user_ns["feed"] except NameError: if "feed" in globals(): del globals()["feed"]
def __call__(cls, *args, **kwargs): """ Checks for misprints in argument names """ obj = super(UnitRegistry, cls).__call__(*args, **kwargs) if hasattr(cls, "DISABLE_KWARGS_CHECK") or not UnitRegistry.enabled: return obj def warning(*largs): obj.warning(*largs) if root.common.trace.misprints: obj.warning("Stack trace:\n%s", "".join(format_list(extract_stack( inspect.currentframe().f_back.f_back)))) # Build the matrix of differences matrix = {} matched = set() for given_kwarg in kwargs: for kwattr in cls.KWATTRS: if (kwattr, given_kwarg) in matrix: continue matrix[(given_kwarg, kwattr)] = d = \ damerau_levenshtein_distance(given_kwarg, kwattr) if d == 0: # perfect match, stop further comparisons matched.add(given_kwarg) break if len(matched) < len(kwargs): # Find replacement candidates with distance = 1 ignored_kwargs = set() for given_kwarg in set(kwargs).difference(matched): candidates = [] for kwattr in cls.KWATTRS: d = matrix.get((given_kwarg, kwattr)) if d == 1: candidates.append(kwattr) if len(candidates) == 0: ignored_kwargs.add(given_kwarg) else: warning( "Creating %s: potential misprint in keyword argument " "name: expected %s - got %s", obj, " or ".join(candidates), given_kwarg) try: __IPYTHON__ # pylint: disable=E0602 from IPython.terminal.interactiveshell import \ InteractiveShell ignored_kwargs -= set(InteractiveShell.instance().user_ns) except NameError: pass if len(ignored_kwargs) > 0: warning( "Creating %s: ignored the following keyword arguments: %s", obj, ", ".join(sorted(ignored_kwargs))) return obj
def restore_initial_interactive_exit(): if not is_interactive(): return try: __IPYTHON__ # pylint: disable=E0602 from IPython.terminal.interactiveshell import InteractiveShell shell = InteractiveShell.instance() shell.exiter.__call__ = ThreadPool.exit_initial shell.ask_exit = ThreadPool._ipython_ask_exit_initial except NameError: try: import builtins builtins.exit = ThreadPool.exit_initial builtins.quit = ThreadPool.quit_initial except: pass