core: helpers for automatic dataframes from sequences of NamedTuple/dataclass

also use in my.rescuetime
This commit is contained in:
Dima Gerasimov 2021-02-13 17:13:05 +00:00 committed by karlicoss
parent df9a7f7390
commit 07f901e1e5
5 changed files with 77 additions and 14 deletions

View file

@ -458,3 +458,16 @@ def guess_datetime(x: Any) -> Optional[datetime]:
if isinstance(v, datetime):
return v
return None
def asdict(thing) -> Json:
# todo primitive?
# todo exception?
if isinstance(thing, dict):
return thing
import dataclasses as D
if D.is_dataclass(thing):
return D.asdict(thing)
# must be a NT otherwise?
# todo add a proper check.. ()
return thing._asdict()