reuse konsume in codeforces

This commit is contained in:
Dima Gerasimov 2019-05-04 00:16:22 +01:00
parent 5709d5e466
commit 07736f7e1e

View file

@ -6,7 +6,7 @@ import json
from typing import Dict, Iterator, Any from typing import Dict, Iterator, Any
from kython import cproperty, fget from kython import cproperty, fget
from kython.konsume import dell, zoom, keq, akeq from kython.konsume import zoom, ignore
from kython.kerror import Res, ytry, unwrap from kython.kerror import Res, ytry, unwrap
from kython.kdatetime import as_utc from kython.kdatetime import as_utc
@ -45,17 +45,14 @@ def get_latest():
class Competition(NamedTuple): class Competition(NamedTuple):
json: Dict[str, Any] contest_id: str
contest: str
cmap: Cmap cmap: Cmap
@cproperty @cproperty
def uid(self) -> str: def uid(self) -> str:
return self.contest_id return self.contest_id
@property
def contest_id(self):
return self.json['contestId']
def __hash__(self): def __hash__(self):
return hash(self.contest_id) return hash(self.contest_id)
@ -63,31 +60,36 @@ class Competition(NamedTuple):
def when(self) -> datetime: def when(self) -> datetime:
return self.cmap[self.uid].when return self.cmap[self.uid].when
@cproperty
def contest(self) -> str:
return self.json['contestName']
@cproperty @cproperty
def summary(self) -> str: def summary(self) -> str:
return f'participated in {self.contest}' # TODO return f'participated in {self.contest}' # TODO
@classmethod @classmethod
def make(cls, cmap, json) -> Iterator[Res['Competition']]: def make(cls, cmap, json) -> Iterator[Res['Competition']]:
yield cls(cmap=cmap, json=json) # TODO try here??
yield from ytry(lambda: akeq(json, 'contestId', 'contestName', 'rank', 'oldRating', 'newRating')) 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')
from kython.konsume import wrap
def iter_data() -> Iterator[Res[Competition]]: def iter_data() -> Iterator[Res[Competition]]:
cmap = get_contests() cmap = get_contests()
j = get_latest() with wrap(get_latest()) as j:
dell(j, 'status') j['status'].ignore()
res = j['result'].zoom()
j = zoom(j, 'result') for c in list(res): # TODO maybe we want 'iter' method??
ignore(c, 'handle', 'ratingUpdateTimeSeconds')
for c in j:
dell(c, 'handle', 'ratingUpdateTimeSeconds')
yield from Competition.make(cmap=cmap, json=c) yield from Competition.make(cmap=cmap, json=c)
c.consume()
# TODO maybe if they are all empty, no need to consume??
def get_data(): def get_data():