add demo; proper readme; remove kython dependencies

This commit is contained in:
Dima Gerasimov 2019-09-27 08:26:44 +01:00
parent 943c572e00
commit e5b90407dc
8 changed files with 201 additions and 12 deletions

View file

@ -4,7 +4,7 @@ from .. import paths
@lru_cache()
def kobuddy_module():
from kython import import_from
from ..common import import_from
return import_from(paths.kobuddy.repo, 'kobuddy')
kobuddy = kobuddy_module()

View file

@ -4,7 +4,7 @@ from ... import paths
@lru_cache()
def ghexport():
from kython import import_file
from ...common import import_file
return import_file(paths.ghexport.repo / 'model.py')

56
my/common.py Normal file
View file

@ -0,0 +1,56 @@
from pathlib import Path
import functools
from typing import Union, Callable, Dict, List, Iterable, TypeVar
# some helper functions
def import_file(p: Union[str, Path], name=None):
p = Path(p)
if name is None:
name = p.stem
import importlib.util
spec = importlib.util.spec_from_file_location(name, p) # type: ignore
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo) # type: ignore
return foo
def import_from(path, name):
path = str(path)
import sys
try:
sys.path.append(path)
import importlib
return importlib.import_module(name)
finally:
sys.path.remove(path)
T = TypeVar('T')
K = TypeVar('K')
V = TypeVar('V')
def the(l: Iterable[T]) -> T:
it = iter(l)
try:
first = next(it)
except StopIteration as ee:
raise RuntimeError('Empty iterator?')
assert all(e == first for e in it)
return first
def group_by_key(l: Iterable[T], key: Callable[[T], K]) -> Dict[K, List[T]]:
res: Dict[K, List[T]] = {}
for i in l:
kk = key(i)
lst = res.get(kk, [])
lst.append(i)
res[kk] = lst
return res
Cl = TypeVar('Cl')
R = TypeVar('R')
def cproperty(f: Callable[[Cl], R]) -> R:
return property(functools.lru_cache(maxsize=1)(f)) # type: ignore

View file

@ -1,26 +1,27 @@
from functools import lru_cache
from pathlib import Path
from . import paths
@lru_cache()
def hypexport():
from kython import import_file
return import_file(paths.hypexport.repo / 'model.py')
from .common import import_file
return import_file(Path(paths.hypexport.repo) / 'model.py')
Annotation = hypexport().Annotation
def get_model():
sources = list(sorted(paths.hypexport.export_dir.glob('*.json')))
export_dir = Path(paths.hypexport.export_dir)
sources = list(sorted(export_dir.glob('*.json')))
model = hypexport().Model(sources)
return model
from kython import listdir_abs
from typing import Dict, List, NamedTuple, Optional, Sequence
from pathlib import Path
from datetime import datetime
from kython import group_by_key, the, cproperty
from .common import group_by_key, the, cproperty
class Page(NamedTuple):

View file

@ -8,7 +8,7 @@ from .. import paths
@lru_cache()
def goodrexport():
from kython import import_file
from ..common import import_file
return import_file(paths.goodrexport.repo / 'model.py')

View file

@ -4,7 +4,7 @@ from . import paths
@lru_cache()
def stexport():
from kython import import_file
from .common import import_file
stexport_model = import_file(paths.stexport.repo / 'model.py')
return stexport_model