def alias_magic(self, line=''): """Create an alias for an existing line or cell magic. Examples -------- :: In [1]: %alias_magic t timeit Created `%t` as an alias for `%timeit`. Created `%%t` as an alias for `%%timeit`. In [2]: %t -n1 pass 1 loops, best of 3: 954 ns per loop In [3]: %%t -n1 ...: pass ...: 1 loops, best of 3: 954 ns per loop In [4]: %alias_magic --cell whereami pwd UsageError: Cell magic function `%%pwd` not found. In [5]: %alias_magic --line whereami pwd Created `%whereami` as an alias for `%pwd`. In [6]: %whereami Out[6]: u'/home/testuser' """ args = magic_arguments.parse_argstring(self.alias_magic, line) shell = self.shell mman = self.shell.magics_manager escs = ''.join(magic_escapes.values()) target = args.target.lstrip(escs) name = args.name.lstrip(escs) # Find the requested magics. m_line = shell.find_magic(target, 'line') m_cell = shell.find_magic(target, 'cell') if args.line and m_line is None: raise UsageError('Line magic function `%s%s` not found.' % (magic_escapes['line'], target)) if args.cell and m_cell is None: raise UsageError('Cell magic function `%s%s` not found.' % (magic_escapes['cell'], target)) # If --line and --cell are not specified, default to the ones # that are available. if not args.line and not args.cell: if not m_line and not m_cell: raise UsageError( 'No line or cell magic with name `%s` found.' % target ) args.line = bool(m_line) args.cell = bool(m_cell) if args.line: mman.register_alias(name, target, 'line') print('Created `%s%s` as an alias for `%s%s`.' % ( magic_escapes['line'], name, magic_escapes['line'], target)) if args.cell: mman.register_alias(name, target, 'cell') print('Created `%s%s` as an alias for `%s%s`.' % ( magic_escapes['cell'], name, magic_escapes['cell'], target))
def alias_magic(self, line=''): """Create an alias for an existing line or cell magic. Examples -------- :: In [1]: %alias_magic t timeit In [2]: %t -n1 pass 1 loops, best of 3: 954 ns per loop In [3]: %%t -n1 ...: pass ...: 1 loops, best of 3: 954 ns per loop In [4]: %alias_magic --cell whereami pwd UsageError: Cell magic function `%%pwd` not found. In [5]: %alias_magic --line whereami pwd In [6]: %whereami Out[6]: u'/home/testuser' """ args = magic_arguments.parse_argstring(self.alias_magic, line) shell = self.shell escs = ''.join(list(magic_escapes.values())) target = args.target.lstrip(escs) name = args.name.lstrip(escs) # Find the requested magics. m_line = shell.find_magic(target, 'line') m_cell = shell.find_magic(target, 'cell') if args.line and m_line is None: raise UsageError('Line magic function `%s%s` not found.' % (magic_escapes['line'], target)) if args.cell and m_cell is None: raise UsageError('Cell magic function `%s%s` not found.' % (magic_escapes['cell'], target)) # If --line and --cell are not specified, default to the ones # that are available. if not args.line and not args.cell: if not m_line and not m_cell: raise UsageError( 'No line or cell magic with name `%s` found.' % target ) args.line = bool(m_line) args.cell = bool(m_cell) if args.line: def wrapper(line): return m_line(line) wrapper.__name__ = str(name) wrapper.__doc__ = "Alias for `%s%s`." % \ (magic_escapes['line'], target) shell.register_magic_function(wrapper, 'line', name) if args.cell: def wrapper(line, cell): return m_cell(line, cell) wrapper.__name__ = str(name) wrapper.__doc__ = "Alias for `%s%s`." % \ (magic_escapes['cell'], target) shell.register_magic_function(wrapper, 'cell', name)
def alias_magic(self, line=""): """Create an alias for an existing line or cell magic. Examples -------- :: In [1]: %alias_magic t timeit Created `%t` as an alias for `%timeit`. Created `%%t` as an alias for `%%timeit`. In [2]: %t -n1 pass 1 loops, best of 3: 954 ns per loop In [3]: %%t -n1 ...: pass ...: 1 loops, best of 3: 954 ns per loop In [4]: %alias_magic --cell whereami pwd UsageError: Cell magic function `%%pwd` not found. In [5]: %alias_magic --line whereami pwd Created `%whereami` as an alias for `%pwd`. In [6]: %whereami Out[6]: u'/home/testuser' In [7]: %alias_magic h history -p "-l 30" --line Created `%h` as an alias for `%history -l 30`. """ args = magic_arguments.parse_argstring(self.alias_magic, line) shell = self.shell mman = self.shell.magics_manager escs = "".join(magic_escapes.values()) target = args.target.lstrip(escs) name = args.name.lstrip(escs) params = args.params if params and ( (params.startswith('"') and params.endswith('"')) or (params.startswith("'") and params.endswith("'")) ): params = params[1:-1] # Find the requested magics. m_line = shell.find_magic(target, "line") m_cell = shell.find_magic(target, "cell") if args.line and m_line is None: raise UsageError( "Line magic function `%s%s` not found." % (magic_escapes["line"], target) ) if args.cell and m_cell is None: raise UsageError( "Cell magic function `%s%s` not found." % (magic_escapes["cell"], target) ) # If --line and --cell are not specified, default to the ones # that are available. if not args.line and not args.cell: if not m_line and not m_cell: raise UsageError("No line or cell magic with name `%s` found." % target) args.line = bool(m_line) args.cell = bool(m_cell) params_str = "" if params is None else " " + params if args.line: mman.register_alias(name, target, "line", params) print( "Created `%s%s` as an alias for `%s%s%s`." % ( magic_escapes["line"], name, magic_escapes["line"], target, params_str, ) ) if args.cell: mman.register_alias(name, target, "cell", params) print( "Created `%s%s` as an alias for `%s%s%s`." % ( magic_escapes["cell"], name, magic_escapes["cell"], target, params_str, ) )