Paste formatted Python code in command line
Question: I want to run Python code from an
[example](http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html#scipy.cluster.vq.kmeans)
in the [Anaconda](https://store.continuum.io/cshop/anaconda/) shell.
Unfortunately the statement I want to paste has lines starting with `...`. Is
there an easy way to run such a statement without having to manually remove
the `...`? I know that other shells exist, but I don't want to have to try
getting them working with Anaconda

    
    
    >>> features  = array([[ 1.9,2.3],
    ...                    [ 1.5,2.5],
    ...                    [ 0.8,0.6],
    ...                    [ 0.4,1.8],
    ...                    [ 0.1,0.1],
    ...                    [ 0.2,1.8],
    ...                    [ 2.0,0.5],
    ...                    [ 0.3,1.5],
    ...                    [ 1.0,1.0]])
    


Answer: Python's native [doctest
parser](http://docs.python.org/3.3/library/doctest.html#doctest.DocTestParser)
is used to dealing with those pesky repr prompts. :)

    
    
    >>> from doctest import DocTestParser
    >>> repr_code = '''
    ... >>> features  = array([[ 1.9,2.3],
    ... ...                    [ 1.5,2.5],
    ... ...                    [ 0.8,0.6],
    ... ...                    [ 0.4,1.8],
    ... ...                    [ 0.1,0.1],
    ... ...                    [ 0.2,1.8],
    ... ...                    [ 2.0,0.5],
    ... ...                    [ 0.3,1.5],
    ... ...                    [ 1.0,1.0]])
    ... '''
    >>> p = DocTestParser()
    >>> code = next(filter(None, p.parse(repr_code.strip()))) # Filter out the useless parts
    >>> print(code.source)
    features  = array([[ 1.9,2.3],
                   [ 1.5,2.5],
                   [ 0.8,0.6],
                   [ 0.4,1.8],
                   [ 0.1,0.1],
                   [ 0.2,1.8],
                   [ 2.0,0.5],
                   [ 0.3,1.5],
                   [ 1.0,1.0]])
    >>> array = list # Because it's cheaper than numpy
    >>> exec(code.source) # If you're feeling very lucky...
    >>> len(features)
    9    
    

