Skip to content

karlicoss/hiccup

Repository files navigation

A library for making xpath queries to arbitrary python objects and retreiving ‘subobjects’ as a result.

Motivation

Originally came from porg which is a tool I implemented to query things from my Org-mode notes and logs. But perhaps there are be more uses to this!

Usage example

See tests, but here’s a (made-up) example:

from hiccup import *
class Tree:
    def __init__(self, node, *children):
        self.node = node
        self.children = children

    __repr__ = lambda s: repr(s.__dict__)

left = Tree('left')
right = Tree('right')
tt = Tree('aaa', left, right)

As an intermediate xml, it looks somewhat like that:

>>> print(Hiccup()._as_xmlstr(tt))
<Tree _python_id="140684831777792">
  <children _python_id="140684845499144">
    <Tree _python_id="140684831777848">
      <children _python_id="140685393653832"/>
      <node _python_id="140685362678000">left</node>
    </Tree>
    <Tree _python_id="140684831777680">
      <children _python_id="140685393653832"/>
      <node _python_id="140685362678056">right</node>
    </Tree>
  </children>
  <node _python_id="140684856442808">aaa</node>
</Tree>

to actually query, we can use hiccup.xfind or a hiccup.Hiccup class which allows some extra configuration.

res = xfind(tt, '//Tree[./node[text()="left"]]')
assert res is left

TODOs

[2018-12-11 Tue 06:38] abstract away from xpath? e.g. allow to use jq-style queries