Commit 9e03adab by Serbaf

added method to represent tweet as json

parent a5bc9c99
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""Main module.""" """Main module."""
import json
import pysnooper
from typing import List, Dict
class Tweet(): class Tweet():
""" """
...@@ -366,5 +371,30 @@ class Tweet(): ...@@ -366,5 +371,30 @@ class Tweet():
def set_trtext(self, trtext): def set_trtext(self, trtext):
self.trtext = trtext self.trtext = trtext
def get_tweet_fields_subset(self, fields: List[str]) -> Dict:
"""
Keep just the specified fields and return a dict
with just the information specified
"""
tweet_subset = {}
for field in fields:
try:
tweet_subset[field] = self[field]
except AttributeError:
pass
return tweet_subset
def as_json(self) -> Dict:
"""
Return the Tweet object in a JSON-like representation (nested dicts)
"""
json_tweet = {}
for key, value in self.__dict__.items():
if value is not None:
json_tweet[key] = value
return json_tweet
def __getitem__(self, key): def __getitem__(self, key):
return getattr(self, key) return getattr(self, key)
...@@ -92,24 +92,6 @@ def get_tweets_from_csv(csv_file): ...@@ -92,24 +92,6 @@ def get_tweets_from_csv(csv_file):
return tweets return tweets
def get_tweet_fields_subset(
tweet: Tweet,
fields: List[str]
) -> Dict:
"""
Given a Tweet objects, keep just the specified fields and return a dict
with just the information specified
"""
tweet_subset = {}
for field in fields:
try:
tweet_subset[field] = tweet[field]
except AttributeError:
pass
return tweet_subset
def get_tweet_collection_fields_subset( def get_tweet_collection_fields_subset(
tweet_collection: Union[List[Tweet], Generator[Tweet, None, None]], tweet_collection: Union[List[Tweet], Generator[Tweet, None, None]],
fields: List[str] fields: List[str]
...@@ -119,7 +101,7 @@ def get_tweet_collection_fields_subset( ...@@ -119,7 +101,7 @@ def get_tweet_collection_fields_subset(
return a generator of dicts with just the information specified return a generator of dicts with just the information specified
""" """
for tweet in tweet_collection: for tweet in tweet_collection:
yield get_tweet_fields_subset(tweet, fields) yield tweet.get_tweet_fields_subset(fields)
class NotValidTweetError(Exception): class NotValidTweetError(Exception):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment