コード例 #1
0
ファイル: cross_trainer.py プロジェクト: pjain02/HPI
def cross_trainer_data():
    # FIXME some manual entries in python
    # I guess just convert them to org

    from porg import Org
    # FIXME should use all org notes and just query from them?
    wlog = Org.from_file(config.workout_log)
    cross_table = wlog.xpath('//org[heading="Cross training"]//table')

    def maybe(f):
        def parse(s):
            if len(s) == 0:
                return None
            return f(s)

        return parse

    def parse_mm_ss(x: str) -> timedelta:
        hs, ms = x.split(':')
        return timedelta(seconds=int(hs) * 60 + int(ms))

    # todo eh. not sure if there is a way of getting around writing code...
    # I guess would be nice to have a means of specifying type in the column? maybe multirow column names??
    # need to look up org-mode standard..
    from ...core.orgmode import parse_org_datetime
    mappers = {
        'duration': lambda s: parse_mm_ss(s),
        'date': lambda s: tzify(parse_org_datetime(s)),
        'comment': str,
    }
    for row in cross_table.lines:
        # todo make more defensive, fallback on nan for individual fields??
        try:
            d = {}
            for k, v in row.items():
                # todo have something smarter... e.g. allow pandas to infer the type??
                mapper = mappers.get(k, maybe(float))
                d[k] = mapper(v)  # type: ignore[operator]
            yield d
        except Exception as e:
            # todo add parsing context
            yield {'error': str(e)}
コード例 #2
0
 def query_all(self, query):
     res: List[Org] = []
     for of in self.files:
         org = Org.from_file(str(of))
         res.extend(query(org))
     return res
コード例 #3
0
def _load_test_file(name: str) -> Org:
    return Org.from_file(Path(__file__).parent / 'data' / name)
コード例 #4
0
 def query_all(self, query):
     for of in self.files:
         org = Org.from_file(str(of))
         yield from query(org)
コード例 #5
0
 def _iterate(self, f: Path) -> Iterable[OrgNote]:
     o = Org.from_file(f)
     for x in o.iterate():
         yield to_note(x)