MCPcopy Index your code
hub / github.com/ipython/ipython / alias_magic

Method alias_magic

IPython/core/magics/basic.py:104–181  ·  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 107 ns ± 43.6 ns p

(self, line='')

Source from the content-addressed store, hash-verified

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