From 8df2df93f64bc56702ff11eab86e3a1718386435 Mon Sep 17 00:00:00 2001 From: John Helmert Date: Mon, 29 Jul 2019 19:10:26 -0500 Subject: [PATCH] Simplify score and comment count data This commit changes the functionality of the Content class (and its subclasses) by removing the text from the comments and score fields of Reddit data dictionaries created by Content.strip*. data['comments'] is now an integer, but data['score'] remains a string to preserve using '-' when the score is hidden. It is now the responsibility of wherever this data is used to provide the extra text that these variables used to include, and modifications have been made to the Submission and Subreddit classes to preserve the previous way the data was displayed. Tests modified to expect these changes. --- tests/test_subreddit.py | 7 ++++--- tuir/content.py | 8 ++++---- tuir/submission_page.py | 6 +++--- tuir/subreddit_page.py | 8 ++++---- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/tests/test_subreddit.py b/tests/test_subreddit.py index 1e4f8d5..99a5388 100644 --- a/tests/test_subreddit.py +++ b/tests/test_subreddit.py @@ -736,9 +736,9 @@ def test_subreddit_page__create_format(terminal, subreddit_page): data = { 'index': '1', 'title': 'Without saying what the category is, what are your top five?', - 'score': '144655 pts', + 'score': '144655', 'likes': True, - 'comments': '26584 comments', + 'comments': '26584', 'created': '10month', 'edited': '(edit 5month)', 'author': 'reddit_user', @@ -858,7 +858,8 @@ def test_subreddit_page__create_format(terminal, subreddit_page): assert format_list[6][0](data) == expected_str['%n'] + ' ' else: # General case - assert format_list[1][0](data) == expected_str[fmt] + assert format_list[1][0](data) == expected_str[fmt], \ + "On specifier {0}".format(fmt) format_list[1][1](data) terminal.attr.assert_called_with(expected_attr[fmt]) diff --git a/tuir/content.py b/tuir/content.py index e1b984f..4f36413 100644 --- a/tuir/content.py +++ b/tuir/content.py @@ -152,7 +152,7 @@ class Content(object): data['body'] = comment.body data['html'] = comment.body_html data['created'] = cls.humanize_timestamp(comment.created_utc) - data['score'] = '{0} pts'.format( + data['score'] = '{0}'.format( '-' if comment.score_hidden else comment.score) data['author'] = name data['is_author'] = (name == sub_name) @@ -187,7 +187,7 @@ class Content(object): data['nsfw'] = comment.over_18 data['subreddit'] = six.text_type(comment.subreddit) data['url_type'] = 'selfpost' - data['score'] = '{0} pts'.format( + data['score'] = '{0}'.format( '-' if comment.score_hidden else comment.score) data['likes'] = comment.likes data['created'] = cls.humanize_timestamp(comment.created_utc) @@ -233,8 +233,8 @@ class Content(object): data['html'] = sub.selftext_html or '' data['created'] = cls.humanize_timestamp(sub.created_utc) data['created_long'] = cls.humanize_timestamp(sub.created_utc, True) - data['comments'] = '{0} comments'.format(sub.num_comments) - data['score'] = '{0} pts'.format('-' if sub.hide_score else sub.score) + data['comments'] = sub.num_comments + data['score'] = '{0}'.format('-' if sub.hide_score else sub.score) data['author'] = name data['permalink'] = sub.permalink data['subreddit'] = six.text_type(sub.subreddit) diff --git a/tuir/submission_page.py b/tuir/submission_page.py index 6f5db44..d97a467 100644 --- a/tuir/submission_page.py +++ b/tuir/submission_page.py @@ -284,7 +284,7 @@ class SubmissionPage(Page): attr = self.term.attr('Score') self.term.add_space(win) - self.term.add_line(win, '{score}'.format(**data), attr=attr) + self.term.add_line(win, '{score} pts'.format(**data), attr=attr) attr = self.term.attr('Created') self.term.add_space(win) @@ -385,7 +385,7 @@ class SubmissionPage(Page): row = len(data['split_title']) + len(split_text) + 3 attr = self.term.attr('Score') - self.term.add_line(win, '{score}'.format(**data), row, 1, attr=attr) + self.term.add_line(win, '{score} pts'.format(**data), row, 1, attr=attr) arrow, attr = self.term.get_arrow(data['likes']) self.term.add_space(win) @@ -393,7 +393,7 @@ class SubmissionPage(Page): attr = self.term.attr('CommentCount') self.term.add_space(win) - self.term.add_line(win, '{comments}'.format(**data), attr=attr) + self.term.add_line(win, '{comments} comments'.format(**data), attr=attr) if data['gold']: attr = self.term.attr('Gold') diff --git a/tuir/subreddit_page.py b/tuir/subreddit_page.py index 9bb5f4c..b7953c9 100644 --- a/tuir/subreddit_page.py +++ b/tuir/subreddit_page.py @@ -316,7 +316,7 @@ class SubredditPage(Page): lambda data: self._submission_attr(data), first)) elif item == "%s": # Need to cut off the characters that aren't the score number - form.append((lambda data: data['score'][:-4], + form.append((lambda data: str(data['score']), lambda data: self.term.attr('Score'), first)) elif item == "%v": # This isn't great, self.term.get_arrow gets called twice @@ -324,7 +324,7 @@ class SubredditPage(Page): lambda data: self.term.get_arrow(data['likes'])[1], first)) elif item == "%c": form.append(( - lambda data: '{0}'.format(data['comments'][:-9]) + lambda data: data['comments'] if data['comments'] else None, # Don't try to subscript null items lambda data: self.term.attr('CommentCount'), first)) @@ -483,7 +483,7 @@ class SubredditPage(Page): if row in valid_rows: attr = self.term.attr('Score') - self.term.add_line(win, '{score}'.format(**data), row, 1, attr) + self.term.add_line(win, '{score} pts'.format(**data), row, 1, attr) self.term.add_space(win) arrow, attr = self.term.get_arrow(data['likes']) @@ -500,7 +500,7 @@ class SubredditPage(Page): attr = self.term.attr('CommentCount') self.term.add_space(win) - self.term.add_line(win, '{comments}'.format(**data), attr=attr) + self.term.add_line(win, '{comments} comments'.format(**data), attr=attr) if data['saved']: attr = self.term.attr('Saved')