Created
August 16, 2015 04:13
-
-
Save jacobmischka/012746c8c2fb37cde42e to your computer and use it in GitHub Desktop.
model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TweetModel(QAbstractTableModel): | |
def __init__(self): | |
super().__init__() | |
self.tweets = {} | |
self.indices = [] | |
def rowCount(self, parent): | |
if parent.isValid(): | |
return 0 | |
return len(self.tweets) | |
def columnCount(self, parent): | |
return 3 | |
def data(self, index, role): | |
col = index.column() | |
try: | |
tweet = self.tweets[self.indices[index.row()]] | |
if col == 0: | |
return QVariant(tweet["name"]+" "+tweet["screen_name"]) | |
elif col == 1: | |
return QVariant(tweet["text"]) | |
elif col == 2: | |
return QVariant(tweet["created_at"]) | |
except KeyError: | |
pass | |
return QVariant() | |
def addTweets(self, tweets): | |
self.beginInsertRows() | |
for tweet in tweets: | |
self.tweets[tweet["id_str"]] = tweet | |
self.indices.append(tweet["id_str"]) | |
self.indices.sort() | |
self.endInsertRows() | |
def addTweet(self, tweet): | |
self.indices.append(tweet["id_str"]) | |
self.indices.sort() | |
row_num = self.indices.index(tweet["id_str"]) | |
self.beginInsertRows(QModelIndex(), row_num, row_num) | |
self.tweets[tweet["id_str"]] = tweet | |
self.endInsertRows() | |
def removeTweet(self, tweet_id): | |
index = self.getIndex() | |
self.beginRemoveRows() | |
try: | |
del self.tweets[tweet_id] | |
del self.indices[tweet_id] | |
except KeyError: | |
pass | |
self.endRemoveRows() | |
def getTweet(self, tweet_id): | |
try: | |
return self.tweets[tweet_id] | |
except KeyError: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment