本文整理汇总了Python中IPython.core.magic_arguments.magic_arguments方法的典型用法代码示例。如果您正苦于以下问题:Python magic_arguments.magic_arguments方法的具体用法?Python magic_arguments.magic_arguments怎么用?Python magic_arguments.magic_arguments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IPython.core.magic_arguments
的用法示例。
在下文中一共展示了magic_arguments.magic_arguments方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _make_script_magic
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def _make_script_magic(self, name):
"""make a named magic, that calls %%script with a particular program"""
# expand to explicit path if necessary:
script = self.script_paths.get(name, name)
@magic_arguments.magic_arguments()
@script_args
def named_script_magic(line, cell):
# if line, add it as cl-flags
if line:
line = "%s %s" % (script, line)
else:
line = script
return self.shebang(line, cell)
# write a basic docstring:
named_script_magic.__doc__ = \
"""%%{name} script magic
Run cells with {script} in a subprocess.
This is a shortcut for `%%script {script}`
""".format(**locals())
return named_script_magic
示例2: writefile
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def writefile(self, line, cell):
"""Write the contents of the cell to a file.
The file will be overwritten unless the -a (--append) flag is specified.
"""
args = magic_arguments.parse_argstring(self.writefile, line)
filename = os.path.expanduser(unquote_filename(args.filename))
if os.path.exists(filename):
if args.append:
print "Appending to %s" % filename
else:
print "Overwriting %s" % filename
else:
print "Writing %s" % filename
mode = 'a' if args.append else 'w'
with io.open(filename, mode, encoding='utf-8') as f:
f.write(cell)
示例3: heat
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def heat(self, line, cell):
"""Method to profile the python code in the ipython cell and display it
as a heatmap using py-heat package.
:param line: Line value for the ipython line this magic is called from.
:param cell: Cell value for the ipython cell this magic is called from.
"""
args = magic_arguments.parse_argstring(self.heat, line)
filename = args.out
if filename is not None:
filename = os.path.expanduser(args.out)
_, tmp_file = mkstemp()
with open(tmp_file, "wb") as f:
f.write(cell.encode())
pyheat = PyHeat(tmp_file)
pyheat.create_heatmap()
pyheat.show_heatmap(output_file=filename)
pyheat.close_heatmap()
os.remove(tmp_file)
示例4: b64
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def b64(self, line: str = "", cell: str = None) -> str:
"""
Base64 IPython magic extension.
Parameters
----------
line : str, optional
Line contents, by default ""
cell : str, optional
Cell contents, by default None
Returns
-------
str
Decoded text
"""
if cell is None:
results, df_results = base64.unpack(line)
else:
results, df_results = base64.unpack(cell)
args = magic_arguments.parse_argstring(self.b64, line)
if args.clean:
results = re.sub(self._STRIP_TAGS, "", results)
elif args.pretty:
if _BS_AVAILABLE:
xml_str = f"<decoded_string>{results}</decoded_string>"
b_soup = BeautifulSoup(xml_str, "xml")
results = b_soup.prettify()
if args.out is not None:
self.shell.user_ns[args.out] = (results, df_results)
return results
示例5: ioc
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def ioc(self, line="", cell=None) -> List[Tuple[str, List[str]]]:
"""
Ioc Extract IPython magic extension.
Parameters
----------
line : str, optional
Line contents, by default ""
cell : str, optional
Cell contents, by default None
Returns
-------
List[Tuple[str, List[str]]]
List of tuples of IoCs found grouped by type.
"""
args = magic_arguments.parse_argstring(self.ioc, line)
ioc_types = None
if args.ioc_types:
ioc_types = [ioc_type.strip() for ioc_type in args.ioc_types.split(",")]
if cell is None:
results = self._ioc_extract.extract(src=line, ioc_types=ioc_types)
else:
results = self._ioc_extract.extract(src=cell, ioc_types=ioc_types)
iocs = [(ioc_type, list(ioc_res)) for ioc_type, ioc_res in results.items()]
if args.out is not None:
self.shell.user_ns[args.out] = results
return iocs
示例6: exec_args
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def exec_args(f):
"""decorator for adding block/targets args for execution
applied to %pxconfig and %%px
"""
args = [
magic_arguments.argument('-b', '--block', action="store_const",
const=True, dest='block',
help="use blocking (sync) execution",
),
magic_arguments.argument('-a', '--noblock', action="store_const",
const=False, dest='block',
help="use non-blocking (async) execution",
),
magic_arguments.argument('-t', '--targets', type=str,
help="specify the targets on which to execute",
),
magic_arguments.argument('--local', action="store_const",
const=True, dest="local",
help="also execute the cell in the local namespace",
),
magic_arguments.argument('--verbose', action="store_const",
const=True, dest="set_verbose",
help="print a message at each execution",
),
magic_arguments.argument('--no-verbose', action="store_const",
const=False, dest="set_verbose",
help="don't print any messages",
),
]
for a in args:
f = a(f)
return f
示例7: output_args
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def output_args(f):
"""decorator for output-formatting args
applied to %pxresult and %%px
"""
args = [
magic_arguments.argument('-r', action="store_const", dest='groupby',
const='order',
help="collate outputs in order (same as group-outputs=order)"
),
magic_arguments.argument('-e', action="store_const", dest='groupby',
const='engine',
help="group outputs by engine (same as group-outputs=engine)"
),
magic_arguments.argument('--group-outputs', dest='groupby', type=str,
choices=['engine', 'order', 'type'], default='type',
help="""Group the outputs in a particular way.
Choices are:
type: group outputs of all engines by type (stdout, stderr, displaypub, etc.).
engine: display all output for each engine together.
order: like type, but individual displaypub output from each engine is collated.
For example, if multiple plots are generated by each engine, the first
figure of each engine will be displayed, then the second of each, etc.
"""
),
magic_arguments.argument('-o', '--out', dest='save_name', type=str,
help="""store the AsyncResult object for this computation
in the global namespace under this name.
"""
),
]
for a in args:
f = a(f)
return f
示例8: pxconfig
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def pxconfig(self, line):
"""configure default targets/blocking for %px magics"""
args = magic_arguments.parse_argstring(self.pxconfig, line)
if args.targets:
self.view.targets = self._eval_target_str(args.targets)
if args.block is not None:
self.view.block = args.block
if args.set_verbose is not None:
self.verbose = args.set_verbose
示例9: result
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def result(self, line=''):
"""Print the result of the last asynchronous %px command.
This lets you recall the results of %px computations after
asynchronous submission (block=False).
Examples
--------
::
In [23]: %px os.getpid()
Async parallel execution on engine(s): all
In [24]: %pxresult
Out[8:10]: 60920
Out[9:10]: 60921
Out[10:10]: 60922
Out[11:10]: 60923
"""
args = magic_arguments.parse_argstring(self.result, line)
if self.last_result is None:
raise UsageError(NO_LAST_RESULT)
self.last_result.get()
self.last_result.display_outputs(groupby=args.groupby)
示例10: debug
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def debug(self, line='', cell=None):
"""Activate the interactive debugger.
This magic command support two ways of activating debugger.
One is to activate debugger before executing code. This way, you
can set a break point, to step through the code from the point.
You can use this mode by giving statements to execute and optionally
a breakpoint.
The other one is to activate debugger in post-mortem mode. You can
activate this mode simply running %debug without any argument.
If an exception has just occurred, this lets you inspect its stack
frames interactively. Note that this will always work only on the last
traceback that occurred, so you must call this quickly after an
exception that you wish to inspect has fired, because if another one
occurs, it clobbers the previous one.
If you want IPython to automatically do this on every exception, see
the %pdb magic for more details.
"""
args = magic_arguments.parse_argstring(self.debug, line)
if not (args.breakpoint or args.statement or cell):
self._debug_post_mortem()
else:
code = "\n".join(args.statement)
if cell:
code += "\n" + cell
self._debug_exec(code, args.breakpoint)
示例11: capture
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def capture(self, line, cell):
"""run the cell, capturing stdout/err"""
args = magic_arguments.parse_argstring(self.capture, line)
out = not args.no_stdout
err = not args.no_stderr
with capture_output(out, err) as io:
self.shell.run_cell(cell)
if args.output:
self.shell.user_ns[args.output] = io
示例12: matplotlib
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def matplotlib(self, line=''):
"""Set up matplotlib to work interactively.
This function lets you activate matplotlib interactive support
at any point during an IPython session.
It does not import anything into the interactive namespace.
If you are using the inline matplotlib backend for embedded figures,
you can adjust its behavior via the %config magic::
# enable SVG figures, necessary for SVG+XHTML export in the qtconsole
In [1]: %config InlineBackend.figure_format = 'svg'
# change the behavior of closing all figures at the end of each
# execution (cell), or allowing reuse of active figures across
# cells:
In [2]: %config InlineBackend.close_figures = False
Examples
--------
In this case, where the MPL default is TkAgg::
In [2]: %matplotlib
Using matplotlib backend: TkAgg
But you can explicitly request a different backend::
In [3]: %matplotlib qt
"""
args = magic_arguments.parse_argstring(self.matplotlib, line)
gui, backend = self.shell.enable_matplotlib(args.gui)
self._show_matplotlib_backend(args.gui, backend)
示例13: notebook
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def notebook(self, s):
"""Export and convert IPython notebooks.
This function can export the current IPython history to a notebook file
or can convert an existing notebook file into a different format. For
example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
To export the history to "foo.py" do "%notebook -e foo.py". To convert
"foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
formats include (json/ipynb, py).
"""
args = magic_arguments.parse_argstring(self.notebook, s)
from IPython.nbformat import current
args.filename = unquote_filename(args.filename)
if args.export:
fname, name, format = current.parse_filename(args.filename)
cells = []
hist = list(self.shell.history_manager.get_range())
for session, prompt_number, input in hist[:-1]:
cells.append(current.new_code_cell(prompt_number=prompt_number,
input=input))
worksheet = current.new_worksheet(cells=cells)
nb = current.new_notebook(name=name,worksheets=[worksheet])
with io.open(fname, 'w', encoding='utf-8') as f:
current.write(nb, f, format);
elif args.format is not None:
old_fname, old_name, old_format = current.parse_filename(args.filename)
new_format = args.format
if new_format == u'xml':
raise ValueError('Notebooks cannot be written as xml.')
elif new_format == u'ipynb' or new_format == u'json':
new_fname = old_name + u'.ipynb'
new_format = u'json'
elif new_format == u'py':
new_fname = old_name + u'.py'
else:
raise ValueError('Invalid notebook format: %s' % new_format)
with io.open(old_fname, 'r', encoding='utf-8') as f:
nb = current.read(f, old_format)
with io.open(new_fname, 'w', encoding='utf-8') as f:
current.write(nb, f, new_format)
示例14: vegalite
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def vegalite(line, cell):
"""Cell magic for displaying vega-lite visualizations in CoLab.
%%vegalite [dataframe] [--json] [--version=3]
Visualize the contents of the cell using Vega-Lite, optionally
specifying a pandas DataFrame object to be used as the dataset.
if --json is passed, then input is parsed as json rather than yaml.
"""
args = magic_arguments.parse_argstring(vegalite, line)
version = args.version
assert version in RENDERERS["vega-lite"]
VegaLite = RENDERERS["vega-lite"][version]
data_transformers = TRANSFORMERS["vega-lite"][version]
if args.json:
spec = json.loads(cell)
elif not YAML_AVAILABLE:
try:
spec = json.loads(cell)
except json.JSONDecodeError:
raise ValueError(
"%%vegalite: spec is not valid JSON. "
"Install pyyaml to parse spec as yaml"
)
else:
spec = yaml.load(cell, Loader=yaml.FullLoader)
if args.data is not None:
data = _get_variable(args.data)
spec["data"] = _prepare_data(data, data_transformers)
return VegaLite(spec)
示例15: _shell_cell_magic
# 需要导入模块: from IPython.core import magic_arguments [as 别名]
# 或者: from IPython.core.magic_arguments import magic_arguments [as 别名]
def _shell_cell_magic(args, cmd):
"""Run the cell via a shell command, allowing input to be provided.
Also available as a line magic.
Usage:
# Returns a ShellResult.
%%shell
echo "hello"
This is similar to Jupyter's `!` magic, but additionally allows input to be
provided to the subprocess. By default, if the subprocess returns a non-zero
exit code a `subprocess.CalledProcessError` is raised. The provided command
is run within a bash shell.
Args:
args: Optional arguments.
cmd: The shell command to execute.
Returns:
ShellResult containing the results of the executed command.
Raises:
subprocess.CalledProcessError: If the subprocess exited with a non-zero
exit code and the `ignore_errors` argument wasn't provided.
"""
parsed_args = magic_arguments.parse_argstring(_shell_cell_magic, args)
result = _run_command(cmd, clear_streamed_output=False)
if not parsed_args.ignore_errors:
result.check_returncode()
return result