codeforces/topcode: move to top level and check in ci

This commit is contained in:
karlicoss 2023-11-10 23:02:11 +00:00
parent 657ce08ac8
commit 7b1cec9326
3 changed files with 10 additions and 23 deletions

View file

@ -1,91 +0,0 @@
#!/usr/bin/env python3
from my.config import codeforces as config # type: ignore[attr-defined]
from datetime import datetime, timezone
from functools import cached_property
import json
from typing import NamedTuple, Dict, Iterator
from ..core import get_files, Res, unwrap
from ..core.konsume import ignore, wrap
Cid = int
class Contest(NamedTuple):
cid: Cid
when: datetime
@classmethod
def make(cls, j) -> 'Contest':
return cls(
cid=j['id'],
when=datetime.fromtimestamp(j['startTimeSeconds'], tz=timezone.utc),
)
Cmap = Dict[Cid, Contest]
def get_contests() -> Cmap:
last = max(get_files(config.export_path, 'allcontests*.json'))
j = json.loads(last.read_text())
d = {}
for c in j['result']:
cc = Contest.make(c)
d[cc.cid] = cc
return d
class Competition(NamedTuple):
contest_id: Cid
contest: str
cmap: Cmap
@cached_property
def uid(self) -> Cid:
return self.contest_id
def __hash__(self):
return hash(self.contest_id)
@cached_property
def when(self) -> datetime:
return self.cmap[self.uid].when
@cached_property
def summary(self) -> str:
return f'participated in {self.contest}' # TODO
@classmethod
def make(cls, cmap, json) -> Iterator[Res['Competition']]:
# TODO try here??
contest_id = json['contestId'].zoom().value
contest = json['contestName'].zoom().value
yield cls(
contest_id=contest_id,
contest=contest,
cmap=cmap,
)
# TODO ytry???
ignore(json, 'rank', 'oldRating', 'newRating')
def iter_data() -> Iterator[Res[Competition]]:
cmap = get_contests()
last = max(get_files(config.export_path, 'codeforces*.json'))
with wrap(json.loads(last.read_text())) as j:
j['status'].ignore()
res = j['result'].zoom()
for c in list(res): # TODO maybe we want 'iter' method??
ignore(c, 'handle', 'ratingUpdateTimeSeconds')
yield from Competition.make(cmap=cmap, json=c)
c.consume()
# TODO maybe if they are all empty, no need to consume??
def get_data():
return list(sorted(iter_data(), key=Competition.when.fget))

View file

@ -1,83 +0,0 @@
#!/usr/bin/env python3
from my.config import topcoder as config # type: ignore[attr-defined]
from datetime import datetime
from functools import cached_property
import json
from typing import NamedTuple, Dict, Iterator
from ..core import get_files, Res, unwrap, Json
from ..core.error import Res, unwrap
from ..core.konsume import zoom, wrap, ignore
def _get_latest() -> Json:
pp = max(get_files(config.export_path))
return json.loads(pp.read_text())
class Competition(NamedTuple):
contest_id: str
contest: str
percentile: float
dates: str
@cached_property
def uid(self) -> str:
return self.contest_id
def __hash__(self):
return hash(self.contest_id)
@cached_property
def when(self) -> datetime:
return datetime.strptime(self.dates, '%Y-%m-%dT%H:%M:%S.%fZ')
@cached_property
def summary(self) -> str:
return f'participated in {self.contest}: {self.percentile:.0f}'
@classmethod
def make(cls, json) -> Iterator[Res['Competition']]:
ignore(json, 'rating', 'placement')
cid = json['challengeId'].zoom().value
cname = json['challengeName'].zoom().value
percentile = json['percentile'].zoom().value
dates = json['date'].zoom().value
yield cls(
contest_id=cid,
contest=cname,
percentile=percentile,
dates=dates,
)
def iter_data() -> Iterator[Res[Competition]]:
with wrap(_get_latest()) as j:
ignore(j, 'id', 'version')
res = j['result'].zoom()
ignore(res, 'success', 'status', 'metadata')
cont = res['content'].zoom()
ignore(cont, 'handle', 'handleLower', 'userId', 'createdAt', 'updatedAt', 'createdBy', 'updatedBy')
cont['DEVELOP'].ignore() # TODO handle it??
ds = cont['DATA_SCIENCE'].zoom()
mar, srm = zoom(ds, 'MARATHON_MATCH', 'SRM')
mar = mar['history'].zoom()
srm = srm['history'].zoom()
# TODO right, I guess I could rely on pylint for unused variables??
for c in mar + srm:
yield from Competition.make(json=c)
c.consume()
def get_data():
return list(sorted(iter_data(), key=Competition.when.fget))