开发者

Copying and pasting code into the Python interpreter

There is a snippet of code that I would like to copy and paste into my Python interpreter. Unfortunately due to Python's sensitivity to whitespace it is not straightforward to copy and paste it a way that makes sense. (I think the whitespace gets mangled) Is there a better way? Maybe I can load the snippet from a file.

This is just an small example but if there is a lot of code I would like to avoid typing everything from the definition of the function or copy and pasting line by line.

class bcolors: 
    HEADER = '\033[95m' 
    OKBLUE = '\033[94m' 
    开发者_JAVA百科OKGREEN = '\033[92m' 
    WARNING = '\033[93m' 
    FAIL = '\033[91m' 
    ENDC = '\033[0m' 

    def disable(self):  
        self.HEADER = '' # I think stuff gets mangled because of the extra level of indentation 
        self.OKBLUE = '' 
        self.OKGREEN = '' 
        self.WARNING = '' 
        self.FAIL = '' 
        self.ENDC = ''


You can usually easily and safely do copy-pasting with IPython, through the commands %cpaste (manually end code with --) and %paste (execute code immediately). This is very handy for testing code that you copy from web pages, for instance, or from your editor: these commands even strip leading prompts (like In[1] and ...) for you.

IPython also has a %run command that runs a program and leaves you in a Python shell with all the variables that were defined in the program, so that you can play with them.

In order to get help on these functions: %cpaste?, etc.


You can simply convert all tabs to spaces and remove ALL empty lines. So you will be able to paste any code to python console (e.g.: python2.6)


You can call execfile(filename). More or less the same as importing a module, except that it skips the module administration part and doesn't require you to add a folder to sys.path.

EDIT: To address the original question: copy-pasted code can be executed by calling exec(codestring).


My answer is specifically about copy-pasting into the standard python shell (only tested on linux).

Depending on where the code comes from and how it is originally formatted the whitespace may or may not matter. In particular about your example snippet - copy-pasted from SO's code-formatted section - it doesn't matter (assuming the code is properly indented to be executable).

The empty line, however, does cause trouble in the standard python interpreter because it normally is the shell-s de-indent cmd. In your snippet's case the empty line preceeding the disable() function definition ends/exits the class definition prematurely, so when the disable() definition line comes in an indentation error is detected:

>>> class bcolors: 
...     HEADER = '\033[95m' 
...     OKBLUE = '\033[94m' 
...     OKGREEN = '\033[92m' 
...     WARNING = '\033[93m' 
...     FAIL = '\033[91m' 
...     ENDC = '\033[0m' 
... 
>>> def disable(self):  
  File "<stdin>", line 1
    def disable(self):  
    ^
IndentationError: unexpected indent
>>> 

So you just need to pay attention at those empty lines. Your snippet needs just 2 multi-line copy-paste ops to work around that empty line.

The only other thing I needed - for copy-pasting just sections of already indented code (say functions from inside classes) - one extra level of indentation to not need to re-do the indentation of the copied code. For that a leading if 1: line prior to pasting the snippet and an Enter (i.e. empty line) after do the trick:

>>> if 1:
...     def disable(self):  
...         self.HEADER = '' # I think stuff gets mangled because of the extra level of indentation 
...         self.OKBLUE = '' 
...         self.OKGREEN = '' 
...         self.WARNING = '' 
...         self.FAIL = '' 
...         self.ENDC = ''
... 
>>>


You can just import the file into the python interpreter. This will load the class in, and allow you to run the code.

For instance, create a file named "bgcolors.py" and copy and paste your code inside. Then using the python interpreter, you just type "import bgcolors" and you should be able to run it.

You can read more here:

http://docs.python.org/tutorial/modules.html


You can use IPython which is much better python repl. It has command for getting input from external editor by using %edit command.


Dreampie allows you to copy and paste code with proper indentation.


The IDLE interface does go to effort to preserve the proper indentation of pasted text.


I had this problem recently and ultimately just needed to change my editor's indentation setting from tabs to spaces. (I was running the interpreter using the OSX Terminal.) Once I did this, copy & paste worked fine.


There is an inbuilt method call "indent region & dedent region" and you can just use it. After you paste a lot of code at once you can select them all and adjust the whitespace.


For those having issues copy/pasting using ctrl-c & ctrl-v in the python interpreter shell on Windows where it simply shows:

>>> ^V

simply right-mouse-click the application window title bar and select default or properties, then you want to un-check Use legacy console (requires relaunch) ensuring that now Enable Ctrl key shortcuts is checked, then close and re-open the Python interpreter console window.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜