MCPcopy
hub / github.com/psf/black / visit_Expr

Method visit_Expr

src/black/handle_ipynb_magics.py:476–515  ·  view source on GitHub ↗

Look for magics in body of cell. For examples, !ls !!ls ?ls ??ls would (respectively) get transformed to get_ipython().system('ls') get_ipython().getoutput('ls') get_ipython().run_line_magic('pinf

(self, node: ast.Expr)

Source from the content-addressed store, hash-verified

474 self.generic_visit(node)
475
476 def visit_Expr(self, node: ast.Expr) -> None:
477 """Look for magics in body of cell.
478
479 For examples,
480
481 !ls
482 !!ls
483 ?ls
484 ??ls
485
486 would (respectively) get transformed to
487
488 get_ipython().system('ls')
489 get_ipython().getoutput('ls')
490 get_ipython().run_line_magic('pinfo', 'ls')
491 get_ipython().run_line_magic('pinfo2', 'ls')
492
493 and we look for instances of any of the latter.
494 """
495 if isinstance(node.value, ast.Call) and _is_ipython_magic(node.value.func):
496 args = _get_str_args(node.value.args)
497 if node.value.func.attr == "run_line_magic":
498 if args[0] == "pinfo":
499 src = f"?{args[1]}"
500 elif args[0] == "pinfo2":
501 src = f"??{args[1]}"
502 else:
503 src = f"%{args[0]}"
504 if args[1]:
505 src += f" {args[1]}"
506 elif node.value.func.attr == "system":
507 src = f"!{args[0]}"
508 elif node.value.func.attr == "getoutput":
509 src = f"!!{args[0]}"
510 else:
511 raise NothingChanged # unsupported magic.
512 self.magics[node.value.lineno].append(
513 OffsetAndMagic(node.value.col_offset, src)
514 )
515 self.generic_visit(node)

Callers

nothing calls this directly

Calls 4

_is_ipython_magicFunction · 0.85
_get_str_argsFunction · 0.85
OffsetAndMagicClass · 0.85
appendMethod · 0.45

Tested by

no test coverage detected