Skip to content

Commit

Permalink
Move Property to wikidata_value.py too
Browse files Browse the repository at this point in the history
  • Loading branch information
dseomn committed Oct 23, 2023
1 parent 22d9750 commit fe74480
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
24 changes: 10 additions & 14 deletions rock_paper_sand/wikidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from collections.abc import Generator, Iterable, Sequence
import contextlib
import datetime
import enum
import re
import typing
from typing import Any
Expand Down Expand Up @@ -49,13 +48,6 @@ def _max(
return max((x for x in iterable if x is not None), default=None)


class _Property(enum.Enum):
DATE_OF_FIRST_PERFORMANCE = "P1191"
END_TIME = "P582"
PUBLICATION_DATE = "P577"
START_TIME = "P580"


@contextlib.contextmanager
def requests_session() -> Generator[requests.Session, None, None]:
"""Returns a context manager for a session for Wikidata APIs."""
Expand All @@ -66,9 +58,11 @@ def requests_session() -> Generator[requests.Session, None, None]:
yield session


def _truthy_statements(item: Any, prop: _Property) -> Sequence[Any]:
def _truthy_statements(
item: Any, prop: wikidata_value.Property
) -> Sequence[Any]:
# https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Truthy_statements
statements = item["claims"].get(prop.value, ())
statements = item["claims"].get(prop.id, ())
return tuple(
statement
for statement in statements
Expand Down Expand Up @@ -206,13 +200,15 @@ def _release_status(
start = _min(
(
_min(_parse_statement_time(statement))
for statement in _truthy_statements(item, _Property.START_TIME)
for statement in _truthy_statements(
item, wikidata_value.P_START_TIME
)
)
)
end = _max(
(
_max(_parse_statement_time(statement))
for statement in _truthy_statements(item, _Property.END_TIME)
for statement in _truthy_statements(item, wikidata_value.P_END_TIME)
)
)
if start is not None and now < start:
Expand All @@ -225,8 +221,8 @@ def _release_status(
return config_pb2.WikidataFilter.ReleaseStatus.ONGOING
assert start is None and end is None
for prop in (
_Property.PUBLICATION_DATE,
_Property.DATE_OF_FIRST_PERFORMANCE,
wikidata_value.P_PUBLICATION_DATE,
wikidata_value.P_DATE_OF_FIRST_PERFORMANCE,
):
released = _min(
(
Expand Down
48 changes: 22 additions & 26 deletions rock_paper_sand/wikidata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
from rock_paper_sand import wikidata_value
from rock_paper_sand.proto import config_pb2

# pylint: disable=protected-access
_Property = wikidata._Property
# pylint: enable=protected-access

_PRECISION_YEAR = 9
_PRECISION_MONTH = 10
_PRECISION_DAY = 11
Expand Down Expand Up @@ -136,15 +132,15 @@ class WikidataUtilsTest(parameterized.TestCase):
testcase_name="preferred",
item={
"claims": {
_Property.PUBLICATION_DATE.value: [
"P1": [
{"id": "foo", "rank": "preferred"},
{"id": "quux", "rank": "normal"},
{"id": "baz", "rank": "deprecated"},
{"id": "bar", "rank": "preferred"},
],
},
},
prop=_Property.PUBLICATION_DATE,
prop=wikidata_value.Property("P1"),
statements=(
{"id": "foo", "rank": "preferred"},
{"id": "bar", "rank": "preferred"},
Expand All @@ -154,14 +150,14 @@ class WikidataUtilsTest(parameterized.TestCase):
testcase_name="normal",
item={
"claims": {
_Property.PUBLICATION_DATE.value: [
"P1": [
{"id": "foo", "rank": "normal"},
{"id": "quux", "rank": "deprecated"},
{"id": "bar", "rank": "normal"},
],
},
},
prop=_Property.PUBLICATION_DATE,
prop=wikidata_value.Property("P1"),
statements=(
{"id": "foo", "rank": "normal"},
{"id": "bar", "rank": "normal"},
Expand All @@ -171,36 +167,36 @@ class WikidataUtilsTest(parameterized.TestCase):
testcase_name="deprecated",
item={
"claims": {
_Property.PUBLICATION_DATE.value: [
"P1": [
{"id": "quux", "rank": "deprecated"},
],
},
},
prop=_Property.PUBLICATION_DATE,
prop=wikidata_value.Property("P1"),
statements=(),
),
dict(
testcase_name="empty",
item={
"claims": {
_Property.PUBLICATION_DATE.value: [],
"P1": [],
},
},
prop=_Property.PUBLICATION_DATE,
prop=wikidata_value.Property("P1"),
statements=(),
),
dict(
testcase_name="missing",
item={"claims": {}},
prop=_Property.PUBLICATION_DATE,
prop=wikidata_value.Property("P1"),
statements=(),
),
)
def test_truthy_statements(
self,
*,
item: Any,
prop: _Property,
prop: wikidata_value.Property,
statements: Sequence[Any],
) -> None:
self.assertSequenceEqual(
Expand Down Expand Up @@ -500,13 +496,13 @@ def setUp(self) -> None:
api_data={
"Q1": {
"claims": {
_Property.START_TIME.value: [
wikidata_value.P_START_TIME.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_FUTURE_1),
},
],
_Property.END_TIME.value: [
wikidata_value.P_END_TIME.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_FUTURE_2),
Expand All @@ -524,7 +520,7 @@ def setUp(self) -> None:
api_data={
"Q1": {
"claims": {
_Property.START_TIME.value: [
wikidata_value.P_START_TIME.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_FUTURE_1),
Expand All @@ -542,13 +538,13 @@ def setUp(self) -> None:
api_data={
"Q1": {
"claims": {
_Property.START_TIME.value: [
wikidata_value.P_START_TIME.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_PAST_1),
},
],
_Property.END_TIME.value: [
wikidata_value.P_END_TIME.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_FUTURE_1),
Expand All @@ -566,7 +562,7 @@ def setUp(self) -> None:
api_data={
"Q1": {
"claims": {
_Property.START_TIME.value: [
wikidata_value.P_START_TIME.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_PAST_1),
Expand All @@ -584,7 +580,7 @@ def setUp(self) -> None:
api_data={
"Q1": {
"claims": {
_Property.END_TIME.value: [
wikidata_value.P_END_TIME.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_FUTURE_1),
Expand All @@ -602,13 +598,13 @@ def setUp(self) -> None:
api_data={
"Q1": {
"claims": {
_Property.START_TIME.value: [
wikidata_value.P_START_TIME.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_PAST_2),
},
],
_Property.END_TIME.value: [
wikidata_value.P_END_TIME.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_PAST_1),
Expand All @@ -626,7 +622,7 @@ def setUp(self) -> None:
api_data={
"Q1": {
"claims": {
_Property.END_TIME.value: [
wikidata_value.P_END_TIME.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_PAST_1),
Expand All @@ -644,7 +640,7 @@ def setUp(self) -> None:
api_data={
"Q1": {
"claims": {
_Property.PUBLICATION_DATE.value: [
wikidata_value.P_PUBLICATION_DATE.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_FUTURE_1),
Expand All @@ -662,7 +658,7 @@ def setUp(self) -> None:
api_data={
"Q1": {
"claims": {
_Property.PUBLICATION_DATE.value: [
wikidata_value.P_PUBLICATION_DATE.id: [
{
"rank": "normal",
"mainsnak": _snak_time(_TIME_IN_PAST_1),
Expand Down
17 changes: 17 additions & 0 deletions rock_paper_sand/wikidata_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,20 @@ def uri(self) -> str:

Q_GREGORIAN_CALENDAR = Item("Q12138")
Q_PROLEPTIC_GREGORIAN_CALENDAR = Item("Q1985727")


@dataclasses.dataclass(frozen=True)
class Property:
"""Wikidata property.
Attributes:
id: ID of the item, e.g., "P580".
"""

id: str


P_DATE_OF_FIRST_PERFORMANCE = Property("P1191")
P_END_TIME = Property("P582")
P_PUBLICATION_DATE = Property("P577")
P_START_TIME = Property("P580")

0 comments on commit fe74480

Please sign in to comment.