Commit b5cc4444 by Serbaf

Added functions that allow to get dictionaries representing a subset of the…

Added functions that allow to get dictionaries representing a subset of the Tweet object information
parent 75fd7cb1
...@@ -60,3 +60,9 @@ Version 0.4.4: ...@@ -60,3 +60,9 @@ Version 0.4.4:
Fixed another requirement error Fixed another requirement error
Version 0.4.5: Version 0.4.5:
Changed f-strings to traditional strings for reasons of compatibility Changed f-strings to traditional strings for reasons of compatibility
Version 0.4.6:
Trying to track an error were Tweets are not instantiated if the CSV registry
contains certain symbols
Version 0.4.7:
Added function to generate dicts representing subsets of Tweet content (return
just the fields indicated by the user and not the full Tweet object)
...@@ -51,6 +51,6 @@ setup( ...@@ -51,6 +51,6 @@ setup(
test_suite='tests', test_suite='tests',
tests_require=test_requirements, tests_require=test_requirements,
url='https://github.com/Serbaf/tweet_model', url='https://github.com/Serbaf/tweet_model',
version='0.4.5', version='0.4.7',
zip_safe=False, zip_safe=False,
) )
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"""Main module.""" """Main module."""
import logging import logging
from typing import Union, Dict, List, Generator
from tweet_manager.lib import format_csv from tweet_manager.lib import format_csv
...@@ -395,13 +396,14 @@ def get_tweet_from_csv_line(header_fields, line_fields): ...@@ -395,13 +396,14 @@ def get_tweet_from_csv_line(header_fields, line_fields):
tweet_contents[header_fields[i].replace(".", "__")] =\ tweet_contents[header_fields[i].replace(".", "__")] =\
line_fields[i] line_fields[i]
try: # try:
tweet = Tweet(**tweet_contents) # tweet = Tweet(**tweet_contents)
except Exception as e: # except Exception as e:
print("An error of type " + type(e).__str__ + "ocurred") # print("An error of type " + type(e).__str__ + "ocurred")
raise Exception # raise Exception
#
return tweet # return tweet
return Tweet(**tweet_contents)
def get_tweets_from_csv(csv_file): def get_tweets_from_csv(csv_file):
...@@ -446,3 +448,33 @@ def get_tweets_from_csv(csv_file): ...@@ -446,3 +448,33 @@ def get_tweets_from_csv(csv_file):
tweets.append(get_tweet_from_csv_line(header_fields, line_fields)) tweets.append(get_tweet_from_csv_line(header_fields, line_fields))
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(
tweet_collection: Union[List[Tweet], Generator[Tweet, None, None]],
fields: List[str]
) -> Generator[Dict, None, None]:
"""
Given a list of Tweet objects, keep just the specified fields and
return a generator of dicts with just the information specified
"""
for tweet in tweet_collection:
yield get_tweet_fields_subset(tweet, fields)
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