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

Method cpaste

IPython/terminal/magics.py:87–138  ·  view source on GitHub ↗

Paste & execute a pre-formatted code block from clipboard. You must terminate the block with '--' (two minus-signs) or Ctrl-D alone on the line. You can also provide your own sentinel with '%paste -s %%' ('%%' is the new sentinel for this operation). The block is de

(self, parameter_s='')

Source from the content-addressed store, hash-verified

85
86 @line_magic
87 def cpaste(self, parameter_s=''):
88 """Paste & execute a pre-formatted code block from clipboard.
89
90 You must terminate the block with '--' (two minus-signs) or Ctrl-D
91 alone on the line. You can also provide your own sentinel with '%paste
92 -s %%' ('%%' is the new sentinel for this operation).
93
94 The block is dedented prior to execution to enable execution of method
95 definitions. '>' and '+' characters at the beginning of a line are
96 ignored, to allow pasting directly from e-mails, diff files and
97 doctests (the '...' continuation prompt is also stripped). The
98 executed block is also assigned to variable named 'pasted_block' for
99 later editing with '%edit pasted_block'.
100
101 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
102 This assigns the pasted block to variable 'foo' as string, without
103 dedenting or executing it (preceding >>> and + is still stripped)
104
105 '%cpaste -r' re-executes the block previously entered by cpaste.
106 '%cpaste -q' suppresses any additional output messages.
107
108 Do not be alarmed by garbled output on Windows (it's a readline bug).
109 Just press enter and type -- (and press enter again) and the block
110 will be what was just pasted.
111
112 IPython statements (magics, shell escapes) are not supported (yet).
113
114 See also
115 --------
116 paste: automatically pull code from clipboard.
117
118 Examples
119 --------
120 ::
121
122 In [8]: %cpaste
123 Pasting code; enter '--' alone on the line to stop.
124 :>>> a = ["world!", "Hello"]
125 :>>> print " ".join(sorted(a))
126 :--
127 Hello world!
128 """
129 opts, name = self.parse_options(parameter_s, 'rqs:', mode='string')
130 if 'r' in opts:
131 self.rerun_pasted()
132 return
133
134 quiet = ('q' in opts)
135
136 sentinel = opts.get('s', u'--')
137 block = '\n'.join(get_pasted_lines(sentinel, quiet=quiet))
138 self.store_or_execute(block, name)
139
140 @line_magic
141 def paste(self, parameter_s=''):

Callers

nothing calls this directly

Calls 4

rerun_pastedMethod · 0.95
store_or_executeMethod · 0.95
get_pasted_linesFunction · 0.85
parse_optionsMethod · 0.80

Tested by

no test coverage detected