-
Notifications
You must be signed in to change notification settings - Fork 17
/
api.py
67 lines (45 loc) · 1.83 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# For importing von externally
import sys
from . import model
from .puid import inferPUID
sys.modules["model"] = model
index = model.VonIndex().store # get the underlying dict
source_to_puid_lookup = {inferPUID(source): source for source in index}
def has(source: str) -> bool:
"""Checks whether a given source exists in database"""
return source in index
def has_solution(source: str) -> bool:
"""Checks whether a given source exists in database AND has a solution"""
if not has(source):
return False
entry = index[source]
return len(entry.full.bodies) > 1
def get_index(source: str, brave=False) -> model.PickleMappingEntry | None:
"""Returns the index entry for a given source, or None"""
entry = index.get(source)
if entry is None:
puid = source.upper()
for index_source, index_entry in index.items():
if puid == inferPUID(index_source):
entry = index_entry
break
if entry is not None and entry.secret:
assert brave, f"{entry} requires security clearance"
return entry
def get(source: str, brave=False) -> model.Problem:
"""Returns the full data for a given source"""
entry = get_index(source, brave)
assert entry is not None, f"{source} is None"
return entry.full
def get_statement(source: str, brave=False) -> str:
"""Returns just the problem statement for a given source"""
return get(source, brave).bodies[0]
def get_solution(source: str, brave=False) -> str:
"""Returns just the solution for a given source (asserts existence)"""
bodies = get(source, brave).bodies
assert len(bodies) > 1, f"{source} has no solution"
return bodies[1]
def get_puid(source: str) -> str:
return inferPUID(source)
def get_source(puid: str) -> str | None:
return source_to_puid_lookup.get(puid)