From c1b04a741e567a23a2843dea2806a40c1a5a745f Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Mon, 13 Dec 2021 17:37:00 -0500 Subject: [PATCH] [+] Class attributes --- src/collect_twitter.py | 1 + src/processing.py | 30 +++++++++++++++++++----------- src/report.py | 3 +++ src/visualization.py | 27 ++++++++++++++++++++------- 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/collect_twitter.py b/src/collect_twitter.py index c5385b3..1008b3c 100644 --- a/src/collect_twitter.py +++ b/src/collect_twitter.py @@ -4,6 +4,7 @@ It contains functions related scraping users/tweets, including: - getting the tweets of a user - downloading many users by checking their followers and follower's followers, etc. """ + import json import math import os diff --git a/src/processing.py b/src/processing.py index cc237c9..799afae 100644 --- a/src/processing.py +++ b/src/processing.py @@ -27,14 +27,16 @@ class ProcessedUser(NamedTuple): example, using dataclass, the json for one UserPopularity object will be: {"username": "a", "popularity": 1, "num_postings": 1}, while using NamedTuple, the json will be: ["a", 1, 1], which saves an entire 42 bytes for each user. + + Attributes: + - username: The Twitter user's screen name + - popularity: A measurement of a user's popularity, such as followers count + - num_postings: Number of tweets + - language: Language code in Twitter's language code format """ - # Username username: str - # A measurement of a user's popularity, such as followers count popularity: int - # Number of tweets num_postings: int - # Language lang: str @@ -107,6 +109,11 @@ class UserSample: """ This is a data class storing our different samples. + Attributes: + - most_popular: Our sample of the most popular users on Twitter + - random: Our sample of random users on Twitter + - english_news: Our sample of news media accounts on Twitter + Representation Invariants: - all(news != '' for news in self.english_news) @@ -224,20 +231,21 @@ def load_user_sample() -> UserSample: class Posting(NamedTuple): """ - Posting data stores the processed tweets data, and it contains info such as whether or not a - tweet is covid-related + Posting data stores the processed tweets data, and it contains info such as whether a tweet is + covid-related + + Attributes: + - covid_related: True if the post is determined to be covid-related + - popularity: A measure of tweet popularity measured by comments + likes + - repost: Whether the post is a repost + - date: Posting date and time in ISO format ("YYYY-MM-DDThh-mm-ss") Representation Invariants: - popularity >= 0 - """ - # Full text of the post's content covid_related: bool - # Popularity of the post popularity: int - # Is it a repost repost: bool - # Date in ISO format date: str diff --git a/src/report.py b/src/report.py index 242244a..242c6a8 100644 --- a/src/report.py +++ b/src/report.py @@ -20,6 +20,9 @@ def generate_report() -> str: """ Compile the report document and generate a markdown report + Preconditions: + - RES_DIR exists, and contains the necessary resources used in this project. + :return: Markdown report """ # Load markdown diff --git a/src/visualization.py b/src/visualization.py index 00f71f9..3b2c227 100644 --- a/src/visualization.py +++ b/src/visualization.py @@ -30,9 +30,12 @@ class UserFloat: This is used for both COVID tweet frequency and popularity ratio data, because both of these are floating point data. + Attributes: + - name: Twitter user's screen name + - data: The float data that's associated with this user + Representation Invariants: - self.name != '' - """ name: str data: float @@ -42,23 +45,33 @@ class Sample: """ A sample of many users, containing statistical data that will be used in graphs. + Attributes: + - name: Sample name + - users: List of user screen names in this sample + - user_freqs: Total frequencies of all posts for each user across all dates (sorted) + - user_pops: Total popularity ratios of all posts for each user across all dates (sorted) + - user_all_pop_avg: Average popularity of all u's posts + - user_date_covid_pop_avg: Average popularity of COVID tweets by a specific user on a date + (user_covid_tweets_pop[user][date] = Average popularity of COVID-posts by {user} on {date}) + - date_covid_freq: Total COVID-tweets frequency on a specific date for all users. + - dates: dates[i] = The i-th day since the first tweet + - date_freqs: date_freqs[i] = COVID frequency of all posts from all sampled users on date[i] + - date_pops: date_pops[i] = Average pop-ratio of all posts from all sampled users on date[i] + Representation Invariants: - self.name != '' - all(name != '' for name in self.users) - """ name: str users: list[str] - # Total frequencies of all posts for each user across all dates (sorted) + user_freqs: list[UserFloat] - # Total popularity ratios of all posts for each user across all dates (sorted) user_pops: list[UserFloat] - # Average popularity of all u's posts user_all_pop_avg: dict[str, float] - # Average popularity of COVID tweets by a specific user on a specific date + # user_covid_tweets_pop[user][date] = Average popularity of COVID-posts by {user} on {date} user_date_covid_pop_avg: dict[str, dict[str, float]] - # Total COVID-tweets frequency on a specific date for all users. + date_covid_freq: dict[str, float] # dates[i] = The i-th day since the first tweet dates: list[datetime]