watch this The wheels are turning, slowly turning. home
Axiom Queries 2005-11-16


Yesterday, I demonstrated how to write an axiomatic plugin. That was pretty cool, but to actually do anything interesting, you probably need to know how to define Axiom Items and perform queries. Without any ado, here’s an Item definition for a popular kind of thing, a tag:

from axiom import item, attributes

class Tag(item.Item):
    typeName = 'tag'
    schemaVersion = 1

    name = attributes.text(doc="""
    A short, descriptive string giving this tag some meaning.  eg, "fruit" or "vacation pictures".
    """, allowNone=False)

    item = attributes.reference(doc="""
    The object which has been tagged.
    """, allowNone=False)



There isn’t anything too complex going on here:





Now let’s see how we can put this into use:

Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> (Tag class definition elided)
>>> from axiom import store
>>> s = store.Store()
>>> Tag(store=s, name=u'system', item=s)1
<__main__.Tag object at 0xb783cadc>
>>> s.query(Tag).count() # How many Tags are there in the database, total?
1
>>> t = list(s.query(Tag))[0]
>>> print t, t.name, t.item # What is the tag I just found?
<__main__.Tag object at 0xb783cadc>, system, <Store (in memory)@0xb7c969cc>
>>> 



axiom.store.Store.query takes a few more optional arguments, too.





Tomorrow: Axiom Powerups




1: Yes, the Store itself can be referenced just like any Item inside it. This is convenient for a number of reasons, although in this case it is just convenient because I don’t have any other Items handy to use in the example.