From f2f1db6bc9f4cebafc2f647ab24dfa3453b7d83b Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 5 Jan 2020 20:35:15 -0800 Subject: [PATCH] Move the DBEntry class to tsdb. --- timesearch/offline_reading.py | 22 ++-------------------- timesearch/tsdb.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/timesearch/offline_reading.py b/timesearch/offline_reading.py index 32d177a..caada00 100644 --- a/timesearch/offline_reading.py +++ b/timesearch/offline_reading.py @@ -98,24 +98,6 @@ HTML_SUBMISSION = ''' '''.strip() -class DBEntry: - def __init__(self, fetch): - if fetch[1].startswith('t3_'): - columns = tsdb.SQL_SUBMISSION_COLUMNS - self.object_type = 'submission' - else: - columns = tsdb.SQL_COMMENT_COLUMNS - self.object_type = 'comment' - - self.id = None - self.idstr = None - for (index, attribute) in enumerate(columns): - setattr(self, attribute, fetch[index]) - - def __repr__(self): - return 'DBEntry(\'%s\')' % self.id - - class TreeNode: def __init__(self, identifier, data, parent=None): assert isinstance(identifier, str) @@ -352,8 +334,8 @@ def tree_from_submission(submission_dbrow, comments_dbrows): Given the sqlite data for a submission and all of its comments, return a tree with the submission id as the root ''' - submission = DBEntry(submission_dbrow) - comments = [DBEntry(c) for c in comments_dbrows] + submission = tsdb.DBEntry(submission_dbrow) + comments = [tsdb.DBEntry(c) for c in comments_dbrows] comments.sort(key=lambda x: x.created) print('Building tree for %s (%d comments)' % (submission.idstr, len(comments))) diff --git a/timesearch/tsdb.py b/timesearch/tsdb.py index a45902b..2c49c41 100644 --- a/timesearch/tsdb.py +++ b/timesearch/tsdb.py @@ -141,6 +141,29 @@ SQL_COMMENT = {key:index for (index, key) in enumerate(SQL_COMMENT_COLUMNS)} SUBMISSION_TYPES = (common.praw.models.Submission, pushshift.DummySubmission) COMMENT_TYPES = (common.praw.models.Comment, pushshift.DummyComment) + +class DBEntry: + ''' + This class converts a tuple row from the database into an object so that + you can access the attributes with dot notation. + ''' + def __init__(self, dbrow): + if dbrow[1].startswith('t3_'): + columns = SQL_SUBMISSION_COLUMNS + self.object_type = 'submission' + else: + columns = SQL_COMMENT_COLUMNS + self.object_type = 'comment' + + self.id = None + self.idstr = None + for (index, attribute) in enumerate(columns): + setattr(self, attribute, dbrow[index]) + + def __repr__(self): + return 'DBEntry(\'%s\')' % self.id + + class TSDB: def __init__(self, filepath, do_create=True): self.filepath = pathclass.Path(filepath)