An Atop Example that Actually Frikkin' Works and Is Informative



The atop examples in Quotient/examples suck. They mostly only show you how to write into the database; they don't show you how to extract stuff out of it. This example assumes you've at least read Getting Started with Atop and maybe read a few of the crappier examples.




I had a data structure along these lines:




class Thingy:
def __init__(self):
self.bars = []

def addBar(self):
b = Bar(self)
self.bars.append(b)

def getBar(self, i):
return self.bars[i]

class Bar:
def __init__(self, thingy):
self.thingy = thingy

def doIt():
t1 = Thingy()
t1.addBar(); t1.addBar()

t2 = Thingy()
t2.addBar(); t2.addBar(); t2.addBar()

structure = {20000: t1, 20001: t2}

print structure[20000]
print structure[20001]
print structure[20002]

doIt()



Now, how do I convert this 'structure' to atop? After a bunch of fiddling, I came up with this:




from atop import store
from twisted.python import components

class Thingy(store.Item):
number = store.indexed(store.IntIndex)
def __init__(self, st, number):
store.Item.__init__(self, st)
self.bars = []
self.number = number

def addBar(self):
b = Bar(self)
self.bars.append(b.referenceTo())
self.touch()
addBar = store.transacted(addBar)

def getBar(self):
return self.bars[i].getItem()
getBar = store.transacted(getBar)

class Bar(store.Item):
thingy = store.referenced
def __init__(self, thingy):
store.Item.__init__(self, thingy.store)
self.thingy = thingy

class IThingies(components.Interface):
pass

st = store.Store('thingies/db', 'thingies/fs')
def setPool():
if IThingies(st, None) is None:
print "Setting up initial stuff!"
pool = store.Pool(st)
pool.addIndex(store.IntIndex)
st.setComponent(IThingies, pool)
st.transact(setPool)

def doIt():
t1 = Thingy(st, 20000)
t1.addBar(); t1.addBar()

t2 = Thingy(st, 20001)
t2.addBar(); t2.addBar(); t2.addBar()

IThingies(st).addItem(t1)
IThingies(st).addItem(t2)

print IThingies(st).getIndexItem(store.IntIndex, 20000)
print IThingies(st).getIndexItem(store.IntIndex, 20001)
print IThingies(st).getIndexItem(store.IntIndex, 20002)

st.transact(doIt)

st.close()



Several things to note. Firstly, we've turned the dict somewhat inside-out; now, the key is instead an attribute of Thingy (which won't work in all cases, but it does in mine). The pool is basically a big glob of objects: in our case, Thingy instances. The IntIndex that we add to the pool is what gives those objects the form of a dict; we can query the pool via the IntIndex and the particular number to get our Thingy back.




The IThingies interface is just a way to get a "top-level" object (the pool of Thingy instances) out of our store. Some people mumbled some things about using Storeups instead, but this was a lot simpler.




Twisted split


Twisted is being split up into many smaller packages. I just wrote a FAQ about it. It's at http://twistedmatrix.com/products/splitfaq.



Thank you.



I am moving to Australia


At about 8pm US-EST on August 1st, I got word that I can submit my Australia work visa application. I was putting off announcing my move until I could actually do this. But the damned visa-application web site seems to be screwed up and won't accept my nomination-ID, AND their technical support email address is un-frikkin-deliverable. Ah well, hopefully My People over there in aussieland will be able to figure something out.



So! I'm moving to sunny Hobart, Tasmania, to work for Nunatak. A friend of mine works there,Tim Stebbing. Another friend, Jonathan Lange (who was kind enough to offer me a temporary bed), lives nearby, so I will not be lacking in minions chums to carouse with.



Everyone's asking what I'll be doing, and this is what I say: Python/Twisted/Atop/Zope/Plone/content/document/revision management (with some PHP and MySQL, but hopefully not for long).





I also finished The Rise of Endymion, the last in a four-part series, and have been yacking about it a lot. I really hate the way Endymion and Rise of Endymion went. Note, however, that this doesn't mean that I didn't enjoy reading it; a book that I dislike is better than a book that bores me.



I absolutely _loved_ the first two books in that series. Then there was a large gap in time and a big attitude shift with the latter two books. The first two had bewildering twists in a huge conspiracy with a smattering of metaphysics; the second two had thrown out the twists and become entirely about metaphysics. Instead of introducing more elements through layers of conspiracy, there is just a girl who is too much of a cry-baby to explain what the hell is going on.



Oh. And the role of the shrike really sucks in this half of the story. The semi-explanation near the end leaves much to be desired.