Commit 5c99fa21 by Serbaf

Merge branch 'feature/refactoring' into develop

parents 90680e39 f80d9a00
......@@ -51,6 +51,6 @@ setup(
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/Serbaf/tweet_model',
version='0.3.3',
version='0.4.0',
zip_safe=False,
)
# -*- coding: utf-8 -*-
"""Main module."""
import sys
import logging
from tweet_manager.lib import format_csv
# Configure logger
LOG_FORMAT = '[%(asctime)-15s] %(levelname)s: %(message)s'
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
logger = logging.getLogger("logger")
class Tweet():
"""
......@@ -358,6 +363,41 @@ class Tweet():
return getattr(self, key)
class NotValidTweetError(Exception):
pass
def get_tweet_from_csv_raw_line(header, line):
"""
Given a CSV header and a CSV line in raw format (strings with comma
separated values), extract the values for every field and then calls
get_tweet_from_csv_line to instance a Tweet.
Returns a Tweet object
"""
header_fields = format_csv.split_csv_line(header)
line_fields = format_csv.split_csv_line(line)
return get_tweet_from_csv_line(header_fields, line_fields)
def get_tweet_from_csv_line(header_fields, line_fields):
"""
Given the fields of a CSV line and header, the function instances a Tweet
object with all the non-empty attributes initialized to the values
indicated in the CSV entry.
Returns a Tweet object
"""
tweet_contents = {}
for i in range(len(line_fields)):
if line_fields[i] != '':
tweet_contents[header_fields[i].replace(".", "__")] =\
line_fields[i]
return Tweet(**tweet_contents)
def get_tweets_from_csv(csv_file):
"""
Take one argument: a path pointing to a valid CSV file.
......@@ -374,32 +414,29 @@ def get_tweets_from_csv(csv_file):
header = csv_object.readline()
body = csv_object.readlines()
header = format_csv.split_csv_line(header)
header_fields = format_csv.split_csv_line(header)
# Check that the header contains valid fields
test_tweet = Tweet()
for field in header:
for field in header_fields:
field_components = field.split(".")
checking_dict = test_tweet.__dict__
error_string = ""
for component in field_components:
error_string += component
if (checking_dict is None) or (component not in checking_dict):
print('The field in the header "' + error_string + '" is ' +
'not a valid element of a Tweet')
sys.exit(1)
logger.error(f'The field in the header "{error_string}" ' +
'is not a valid element of a Tweet')
raise NotValidTweetError("Header contains field which doesn't"
+ " belong to tweet specification: "
+ error_string)
checking_dict = checking_dict[component]
error_string += "."
# Go through every tweet in the file, instance it using the 'Tweet' class
# and add it to the list 'tweets'
for j in range(len(body)):
body[j] = format_csv.split_csv_line(body[j])
tweet_contents = {}
for i in range(len(body[j])):
if body[j][i] != '':
tweet_contents[header[i].replace(".", "__")] = body[j][i]
tweets.append(Tweet(**tweet_contents))
line_fields = format_csv.split_csv_line(body[j])
tweets.append(get_tweet_from_csv_line(header_fields, line_fields))
return tweets
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