> Is there a way to name a derived view? If I create a derived view > and I later want to make use of it, do I have to keep a reference > to it around? If I want to store the derived view, do I need to go > through some sequence of steps to create a (named) empty view and > then merge that with the derived one? > Pretty much. As long as your derived view doesn't have any subviews, the following code should work...
def cloneView(storage, view, name, commitBlock=None):
"""storage, view, name, commitBlock=None -> create a new named view
which is a copy of the contents of view. View cannot contain any
subviews.
commitBlock is optional, if it is not None then commit every
commitBlock appends to the new view. This is useful when cloning
large views"""
structure = []
properties = view.properties().keys()
# create the new derived view
for p in view.structure():
if p.type == "V":
raise ValueError("view contains subviews, cannot clone")
structure.append("%s:%s"%(p.name, p.type))
vw = storage.getas("name[%s]"%",".join(structure))
# populate it with the data from view
for i, row in enumerate(view):
values = {}
for p in properties:
values[p] = getattr(row, p)
vw.append(values)
if commitBlock and i % commitBlock == 0:
storage.commit()return vw
if __name__ == "__main__": import metakit st = metakit.storage() vw = st.getas("test[id:I,a,b,c]") for i in range(100): vw.append((i, 'a','b','c'))
vw2 = st.getas("test[id:i,a]")
for i in range(100):
vw2.append((i, 'a'))
vw3 = vw.join(vw2, vw2.id) clone = cloneView(st, vw3, "derived")
assert vw3.properties() == clone.properties()
assert len(vw3) == len(clone)
props = vw3.properties()
for r1, r2 in zip(vw3, clone):
for p in props:
assert getattr(r1, p) == getattr(r2, p)
_____________________________________________ Metakit mailing list - [EMAIL PROTECTED] http://www.equi4.com/mailman/listinfo/metakit
