MCPcopy Create free account
hub / github.com/ipython/ipython / alias_magic

Method alias_magic

IPython/core/magics/basic.py:99–176  ·  view source on GitHub ↗

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

(self, line='')

Source from the content-addressed store, hash-verified

97 )
98 @line_magic
99 def alias_magic(self, line=''):
100 """Create an alias for an existing line or cell magic.
101
102 Examples
103 --------
104 ::
105
106 In [1]: %alias_magic t timeit
107 Created `%t` as an alias for `%timeit`.
108 Created `%%t` as an alias for `%%timeit`.
109
110 In [2]: %t -n1 pass
111 1 loops, best of 3: 954 ns per loop
112
113 In [3]: %%t -n1
114 ...: pass
115 ...:
116 1 loops, best of 3: 954 ns per loop
117
118 In [4]: %alias_magic --cell whereami pwd
119 UsageError: Cell magic function `%%pwd` not found.
120 In [5]: %alias_magic --line whereami pwd
121 Created `%whereami` as an alias for `%pwd`.
122
123 In [6]: %whereami
124 Out[6]: u'/home/testuser'
125
126 In [7]: %alias_magic h history "-p -l 30" --line
127 Created `%h` as an alias for `%history -l 30`.
128 """
129
130 args = magic_arguments.parse_argstring(self.alias_magic, line)
131 shell = self.shell
132 mman = self.shell.magics_manager
133 escs = ''.join(magic_escapes.values())
134
135 target = args.target.lstrip(escs)
136 name = args.name.lstrip(escs)
137
138 params = args.params
139 if (params and
140 ((params.startswith('"') and params.endswith('"'))
141 or (params.startswith("'") and params.endswith("'")))):
142 params = params[1:-1]
143
144 # Find the requested magics.
145 m_line = shell.find_magic(target, 'line')
146 m_cell = shell.find_magic(target, 'cell')
147 if args.line and m_line is None:
148 raise UsageError('Line magic function `%s%s` not found.' %
149 (magic_escapes['line'], target))
150 if args.cell and m_cell is None:
151 raise UsageError('Cell magic function `%s%s` not found.' %
152 (magic_escapes['cell'], target))
153
154 # If --line and --cell are not specified, default to the ones
155 # that are available.
156 if not args.line and not args.cell:

Callers

nothing calls this directly

Calls 4

UsageErrorClass · 0.90
parse_argstringMethod · 0.80
find_magicMethod · 0.80
register_aliasMethod · 0.80

Tested by

no test coverage detected