2

I'm trying to run python program from ipython notebook. If run below command it's works.

run twitterstream.py >> output.txt 

However if run with while loop it fails. I don't know why it fails?

import time
t_end = time.time() + 60 * 3
while time.time() < t_end:
    print ('entered')
    run twitterstream.py >> output.txt 

Syntax error:

File "<ipython-input-28-842e0185b3a8>", line 5
    run twitterstream.py >> output.txt
                    ^
SyntaxError: invalid syntax
Thomas K
  • 39,200
  • 7
  • 84
  • 86
samy
  • 65
  • 1
  • 8

2 Answers2

1

Your while statement is structured properly. Although it will print "entered" as many times as possible until 180 seconds has elapsed (that's a lot of times), and it will also try to call your script in the same way. You would likely be better served by only calling your script once every 1,5,10, or whatever number of seconds as it is unnecessary to call it constantly.

As pointed out by Tadhg McDonald-Jensen using %run you will be able to call your script. Also there are limits to the rate of calls to twitter that you must consider see here. Basically 15 per 15 minutes or 180 per 15 minutes, though I'm not sure which applies here.

Assuming 15 per 15 minutes worst case scenario, you could run 15 calls in your three minute window. So you could do something like:

from math import floor

temp = floor(time.time())
    while time.time() < t_end:
        if floor(time.time()) == temp + 12:
            %run twitterstream.py >> output.txt
        temp = floor(time.time())

This would call your script every 12 seconds.

Grr
  • 15,553
  • 7
  • 65
  • 85
1

the run "magic command" is not valid python code or syntax.

If you want to use the magic commands from code you need to reference How to run an IPython magic from a script (or timing a Python script)

Community
  • 1
  • 1
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59