Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/IPython/core/macro.py: 48%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Support for interactive macros in IPython"""
2from __future__ import annotations
4#*****************************************************************************
5# Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
6#
7# Distributed under the terms of the BSD License. The full license is in
8# the file COPYING, distributed as part of this software.
9#*****************************************************************************
11import re
13coding_declaration = re.compile(r"#\s*coding[:=]\s*([-\w.]+)")
15class Macro:
16 """Simple class to store the value of macros as strings.
18 Macro is just a callable that executes a string of IPython
19 input when called.
20 """
22 def __init__(self, code: str):
23 """store the macro value, as a single string which can be executed"""
24 lines = [line for line in code.splitlines() if not coding_declaration.match(line)]
25 code = "\n".join(lines)
26 self.value = code + '\n'
28 def __str__(self):
29 return self.value
31 def __repr__(self):
32 return 'IPython.macro.Macro(%s)' % repr(self.value)
34 def __getstate__(self):
35 """ needed for safe pickling via %store """
36 return {'value': self.value}
38 def __setstate__(self, state):
39 self.value = state['value']
41 def __add__(self, other: Macro | str) -> Macro:
42 if isinstance(other, Macro):
43 return Macro(self.value + other.value)
44 elif isinstance(other, str):
45 return Macro(self.value + other)
46 raise TypeError(
47 f"unsupported operand type(s) for +: 'Macro' and {type(other).__name__!r}"
48 )