|def __init__(self, api=None):|
|self.num_tweets = 0|
|self.file = open("tweets.txt", "w")|
|def on_status(self, status):|
|tweet = status._json|
|self.file.write( json.dumps(tweet) + '\n' )|
|self.num_tweets += 1|
|if self.num_tweets < 100:|
|def on_error(self, status):|
@wkkim-se of course not :-) Other than that, for the sake of correctness and memory leaks,
# __init__() self.file_name = "tweets.txt" # on_status() with open(self.file_name, 'w') as file: file.write(json.dumps(tweet) + '\n')
@Divkar94 It's bit advance of python (Object Oriented Par / Class Part).
In simple self here is like a variable which will hold the object address when we will call any method of that class using the object (for that instance)
adding to that using self we can allocate value to that particular instance.
Hope you understand.
Can't we just put the close() statement above the return statement in the else block? As this is written now we never hit the close statement, so I am confused how the solution is a while loop. Is there anything wrong with this?
else: self.file.close() return False
As per the comments from @wkkim-se @plumps and @hiliev I believe there are corrections required in this class for downloading streaming Twitter data. I am new to Python so not yet into creating classes. I have made the corrections as per my understanding and it appends the text file. Hope it is correct. Could you review below code and possibly correct the code here in Github since there will be many like me who will be confused and hunting for the solution!