2

I want to run Python code from an example in the 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]])
4
  • 1
    This is where notepad++ shines as the best text editor ever. Commented Sep 23, 2013 at 1:19
  • @ShashankGupta: Can you explain? Commented Sep 23, 2013 at 1:19
  • 2
    Notepad++ lets you delete columns from a select number of rows. You can literally take out all those dots and spaces by just holding down alt, highlighting it and hitting delete (pretty much a 2 second operation). I'm sure there are other advanced text editors out there that let you do the same. Commented Sep 23, 2013 at 1:21
  • @Josh At just the slightest glance, it looks very slick. Thanks for the reference, I'm downloading it now. Commented Sep 23, 2013 at 1:38

2 Answers 2

5

Python's native doctest parser 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    
Sign up to request clarification or add additional context in comments.

1 Comment

I guess that works, but they really need to build something into the shell
2

As far as I understand you correctly: you want to remove dots. I would do it this way:

  1. open up vim
  2. press i
  3. paste the text (ctrl+v)
  4. press ESC to exit Insert mode
  5. press ctrl+v to enter Visual Block
  6. move cursor up to select all dots
  7. press x to remove them.

Than do what ever you want with this formated text (can copy and paste)

1 Comment

That works, I'm just surprised that the console doesn't have an easy manner of running code copied from the web

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.