Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tweet_model
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
serpucga
tweet_model
Commits
5c99fa21
Commit
5c99fa21
authored
Mar 26, 2019
by
Serbaf
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/refactoring' into develop
parents
90680e39
f80d9a00
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
14 deletions
+51
-14
setup.py
setup.py
+1
-1
tweet_model.py
tweet_model/tweet_model.py
+50
-13
No files found.
setup.py
View file @
5c99fa21
...
@@ -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.
3.3
'
,
version
=
'0.
4.0
'
,
zip_safe
=
False
,
zip_safe
=
False
,
)
)
tweet_model/tweet_model.py
View file @
5c99fa21
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
"""Main module."""
"""Main module."""
import
sys
import
logging
from
tweet_manager.lib
import
format_csv
from
tweet_manager.lib
import
format_csv
# Configure logger
LOG_FORMAT
=
'[
%(asctime)-15
s]
%(levelname)
s:
%(message)
s'
logging
.
basicConfig
(
level
=
logging
.
DEBUG
,
format
=
LOG_FORMAT
)
logger
=
logging
.
getLogger
(
"logger"
)
class
Tweet
():
class
Tweet
():
"""
"""
...
@@ -358,6 +363,41 @@ class Tweet():
...
@@ -358,6 +363,41 @@ class Tweet():
return
getattr
(
self
,
key
)
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
):
def
get_tweets_from_csv
(
csv_file
):
"""
"""
Take one argument: a path pointing to a valid CSV file.
Take one argument: a path pointing to a valid CSV file.
...
@@ -374,32 +414,29 @@ def get_tweets_from_csv(csv_file):
...
@@ -374,32 +414,29 @@ def get_tweets_from_csv(csv_file):
header
=
csv_object
.
readline
()
header
=
csv_object
.
readline
()
body
=
csv_object
.
readlines
()
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
# Check that the header contains valid fields
test_tweet
=
Tweet
()
test_tweet
=
Tweet
()
for
field
in
header
:
for
field
in
header
_fields
:
field_components
=
field
.
split
(
"."
)
field_components
=
field
.
split
(
"."
)
checking_dict
=
test_tweet
.
__dict__
checking_dict
=
test_tweet
.
__dict__
error_string
=
""
error_string
=
""
for
component
in
field_components
:
for
component
in
field_components
:
error_string
+=
component
error_string
+=
component
if
(
checking_dict
is
None
)
or
(
component
not
in
checking_dict
):
if
(
checking_dict
is
None
)
or
(
component
not
in
checking_dict
):
print
(
'The field in the header "'
+
error_string
+
'" is '
+
logger
.
error
(
f
'The field in the header "{error_string}" '
+
'not a valid element of a Tweet'
)
'is not a valid element of a Tweet'
)
sys
.
exit
(
1
)
raise
NotValidTweetError
(
"Header contains field which doesn't"
+
" belong to tweet specification: "
+
error_string
)
checking_dict
=
checking_dict
[
component
]
checking_dict
=
checking_dict
[
component
]
error_string
+=
"."
error_string
+=
"."
# Go through every tweet in the file, instance it using the 'Tweet' class
# Go through every tweet in the file, instance it using the 'Tweet' class
# and add it to the list 'tweets'
# and add it to the list 'tweets'
for
j
in
range
(
len
(
body
)):
for
j
in
range
(
len
(
body
)):
body
[
j
]
=
format_csv
.
split_csv_line
(
body
[
j
])
line_fields
=
format_csv
.
split_csv_line
(
body
[
j
])
tweet_contents
=
{}
tweets
.
append
(
get_tweet_from_csv_line
(
header_fields
,
line_fields
))
for
i
in
range
(
len
(
body
[
j
])):
if
body
[
j
][
i
]
!=
''
:
tweet_contents
[
header
[
i
]
.
replace
(
"."
,
"__"
)]
=
body
[
j
][
i
]
tweets
.
append
(
Tweet
(
**
tweet_contents
))
return
tweets
return
tweets
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment