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
-
what exactly is twitterstream.py doing?Grr– Grr2016年05月10日 14:31:38 +00:00Commented May 10, 2016 at 14:31
-
It collects the tweets from the twitter.samy– samy2016年05月10日 14:33:36 +00:00Commented May 10, 2016 at 14:33
2 Answers 2
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.
6 Comments
run twitterstream.py >> output.txt is not valid python syntax?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)