Skip to content

Commit

Permalink
Refactoring and minor codestyle improvements (#102)
Browse files Browse the repository at this point in the history
* Add decorators for static methods

* Simplify regex for date matching (thanks ChatGPT)

* Fix typo

* Remove unused code

* Refactor message parser for telegram

* Set codecov CI to informational
  • Loading branch information
joweich authored Aug 12, 2023
1 parent b8c34bf commit d3ef04f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
23 changes: 8 additions & 15 deletions chatminer/chatparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, filepath: str):
self._logger.info(
"""
Depending on the platform, the message format in chat logs might not be
standardized accross devices/versions/localization and might change over
standardized across devices/versions/localization and might change over
time. Please report issues including your message format via GitHub.
"""
)
Expand Down Expand Up @@ -132,7 +132,7 @@ def __init__(self, filepath: str):

def _read_raw_messages_from_file(self):
def _is_new_message(line: str):
regex = r"^[\u200e]?\[?((\d{1})|(\d{2})|(\d{4}))((\.)|(\/)|(\-))((\d{1})|(\d{2}))((\.)|(\/)|(\-))((\d{4})|(\d{2}))((\,)|(\ ))"
regex = r"^[\u200e]?\[?(\d{1,4})([./,-])\d{1,2}\2\d{2,4}([, ])"
return re.match(regex, line)

with self._file.open(encoding="utf-8") as f:
Expand Down Expand Up @@ -279,18 +279,10 @@ def _parse_message(self, mess: Dict[str, Any]):
if isinstance(mess["text"], str):
body = mess["text"]
elif isinstance(mess["text"], list):
assert all(
[
(isinstance(m, dict) and "text" in m) or isinstance(m, str)
for m in mess["text"]
]
)
body = " ".join(
map(
lambda m: m["text"] if isinstance(m, dict) else m,
mess["text"],
)
)
text_elements = [
m["text"] if isinstance(m, dict) else m for m in mess["text"]
]
body = " ".join(text_elements)
else:
raise ValueError(f"Unable to parse type {type(mess['text'])} in {mess}")

Expand All @@ -317,7 +309,8 @@ def infer_format(self, raw_messages: List[str]):
self.is_dayfirst = self._infer_dayfirst(raw_messages)
self._log_resulting_format()

def _infer_brackets(self, mess: str):
@staticmethod
def _infer_brackets(mess: str):
return mess[0] == "["

def _infer_date_author_sep(self):
Expand Down
10 changes: 2 additions & 8 deletions chatminer/visualizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,6 @@ def calendar_heatmap(
ax.xaxis.set_tick_params(which="both", length=0)
ax.yaxis.set_tick_params(which="both", length=0)

if monthticks is True:
monthticks = range(len(monthlabels))
elif monthticks is False:
monthticks = []
elif isinstance(monthticks, int):
monthticks = range(len(monthlabels))[monthticks // 2 :: monthticks]

if dayticks is True:
dayticks = range(len(daylabels))
elif dayticks is False:
Expand Down Expand Up @@ -404,7 +397,8 @@ def plot(self, *args, **kwargs):
for line in lines:
self._close_line(line)

def _close_line(self, line: Line2D):
@staticmethod
def _close_line(line: Line2D):
x, y = line.get_data()
# FIXME: markers at x[0], y[0] get doubled-up
if x[0] != x[-1]:
Expand Down
11 changes: 11 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
coverage:
status:
project:
default:
target: 90%
threshold: 5%
informational: true
patch:
default:
target: 90%
informational: true

0 comments on commit d3ef04f

Please sign in to comment.