Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: clean data accepted key #131

Merged
merged 5 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix: Refactor and organize modules (@lwasser, #76)
- Fix: Rename and organize `clean.py` module into `utils_parse` and `utils_clean` (@lwasser, @willingc, #121)
- Fix: Add tests for all utils functions (@lwasser, #122)
- Fix: Bug where date_accepted is removed (@lwasser, #129)


## [v0.2.3] - 2024-02-29
Expand Down
3 changes: 1 addition & 2 deletions src/pyosmeta/cli/process_reviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ def main():
# date_accepted_(month/day/year)
# Get all issues for approved packages - load as dict
issues = process_review.return_response()
# TODO: this method parse_issue_header is working but the parsing code is
# hard to follow
accepted_reviews = process_review.parse_issue_header(issues, 45)

# Update gh metrics via api for all packages
Expand All @@ -48,6 +46,7 @@ def main():
# First add gh meta to each dict
print("Parsing & validating", key)
try:

final_reviews[key] = ReviewModel(**review)
except ValidationError as ve:
print(key, ":", ve)
Expand Down
4 changes: 1 addition & 3 deletions src/pyosmeta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ class ReviewModel(BaseModel):
version_accepted: str | None = None
date_accepted: str | None = Field(
default=None,
validation_alias=AliasChoices(
"date_accepted_(month/day/year)", "Date accepted"
),
validation_alias=AliasChoices("Date accepted", "date_accepted"),
)
created_at: str = None
updated_at: str = None
Expand Down
8 changes: 6 additions & 2 deletions src/pyosmeta/parse_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any

from .contributors import ProcessContributors
from .utils_clean import clean_markdown
from .utils_clean import clean_date_accepted_key, clean_markdown
from .utils_parse import parse_user_names


Expand Down Expand Up @@ -150,6 +150,10 @@ def parse_issue_header(
continue

review[pkg_name] = self.get_issue_meta(body_data, total_lines)

# Normalize date accepted key to be the same in each review
review[pkg_name] = clean_date_accepted_key(review[pkg_name])

# Add issue open and close date to package meta from GH response
# Date cleaning happens via pydantic validator not here
for a_date in meta_dates:
Expand Down Expand Up @@ -191,7 +195,7 @@ def parse_issue_header(
"archive",
"version_accepted",
"joss_doi",
"date_accepted_(month/day/year)",
"date_accepted",
"categories",
"partners",
"domain",
Expand Down
32 changes: 32 additions & 0 deletions src/pyosmeta/utils_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import re
from datetime import datetime

from typing import Any


def clean_date(source_date: str | None) -> datetime | str:
"""Cleans up a date string to a consistent datetime format.
Expand Down Expand Up @@ -61,3 +63,33 @@ def clean_markdown(source_text: str) -> str:
cleaned = re.sub(pattern, "", source_text)

return cleaned


def clean_date_accepted_key(review_dict: dict[str, Any]) -> dict[str, str]:
"""
Normalize date_accepted keys in our review dictionary.

In our reviews we have various templates that have evolved over the past
5 years (since 2019). Some used date accepted, some have date accepted
(month/day/year) and some have month-day-year. Rather than try to
account for all of these this is a helper that simply updates the key
to be date_accepted regardless of what is found after that text.

Parameters
----------
review_dict : dict
Dictionary containing date_accepted key and other review data.

Returns
-------
dict
The modified review dictionary with normalized date_accepted key.
"""
for key in list(review_dict.keys()):
if key.startswith("date_accepted"):
# Remove the old key
value = review_dict.pop(key)
# Add the new key with the value
review_dict["date_accepted"] = value
break
return review_dict
Empty file added tests/integration/__init__.py
Empty file.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this contained the failing test. it wasn't running for me locally and i didn't notice that it wasn't running. this fixes that issue and ensures i see broken tests before pushing to GH thinking everything is green!

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"archive": "[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.8384174.svg)](https://doi.org/10.5281/zenodo.8384174)",
"version_accepted": "5.1.1",
"joss_doi": "[![DOI](https://joss.theoj.org/papers/10.21105/joss.01832/status.svg)](https://joss.theoj.org/papers/10.21105/joss.01832)",
"date_accepted_(month/day/year)": "01/18/2024",
"date_accepted": "01/18/2024",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was breaking the test because i fixed this key to ALWAYS be date_Accepted in this pr! so it needs to be fixed here too in the sample data.

"categories": [
"data-retrieval",
"data-extraction",
Expand Down
31 changes: 30 additions & 1 deletion tests/unit/test_utils_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import pytest

from pyosmeta.utils_clean import clean_date, clean_markdown, clean_name
from pyosmeta.utils_clean import (
clean_date,
clean_date_accepted_key,
clean_markdown,
clean_name,
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -55,3 +60,27 @@ def test_clean_markdown(input_string, expected_output):
)
def test_clean_name(input_name, expected_output):
assert clean_name(input_name) == expected_output


@pytest.mark.parametrize(
"input_dict, expected_output",
[
# Test case where key starts with "date_accepted"
(
{"date_accepted_(month-day-year)": "2024-03-07"},
{"date_accepted": "2024-03-07"},
),
(
{"date_accepted_(month/day/year)": "2024/03/07"},
{"date_accepted": "2024/03/07"},
),
# Test where key does not start with "date_accepted"
({"other_key": "value"}, {"other_key": "value"}),
(
{"date_accepted": "2024-03-07"},
{"date_accepted": "2024-03-07"},
),
],
)
def test_clean_date_accepted_key(input_dict, expected_output):
assert clean_date_accepted_key(input_dict) == expected_output