Last year there was a post from Tom Jones about database of recipes and ingredients:

http://trixie.triqs.com/pipermail/metakit/2002-June/000729.html

There was some wierd behavior using metakit.rwrap and view.minus

It turns out that metakit.wrap wrapped views act strangely for quite a few view operations. I have updated the example above to compare different ways of performing the same task which is performing a test to see if a list of ingredients from a recipe is contained in another list of available ingredients.

In short four approaches to producing the list of available ingredients are:
1) making a new table in the database (works)
2) making a temporary non-stored view (works)
3) wrapping a python dictionary (fails)
4) wrapping a python list (fails)

When inspected under metakit.dump all four views appear to be correct and identical.

I'll make this into a little metakit HOW-TO if someone can explain why views created by metakit.wrap don't seem to work :) I have a python version of metakit.wrap that seems to do what I want. (which is essentially 2) If this is the "correct" behavior I'll post it as well.

Attached is the test code

--
Brian Kelley [EMAIL PROTECTED]
Whitehead Institute for Biomedical Research 617 258-6191

import metakit
# metakit set operations example
db = metakit.storage()
recipes = db.getas("recipes[name:S,ingredients[ingredient:S]]")

ts = 'tabasco sauce'
v = 'vodka'
tj = 'tomato juice'
cj = 'cramberry juice'
lj = 'lime juice'
i = recipes.append(name='bloody mary')
recipes[i].ingredients.append(ingredient=ts)
recipes[i].ingredients.append(ingredient=v)
recipes[i].ingredients.append(ingredient=tj)

i = recipes.append(name='cosmopolitan')
recipes[i].ingredients.append(ingredient=v)
recipes[i].ingredients.append(ingredient=cj)
recipes[i].ingredients.append(ingredient=lj)

def checkView(view, recipes=recipes):
    metakit.dump(view)

    found = []
    for r in recipes:
        left = r.ingredients.minus(view)
        if len(left) == 0:
            found.append(r.name)

    if "bloody mary" not in found:
        print "\tERORR: failed finding bloody mary"

    if "cosmopolitan" not in found:
        print "\tERROR: failed finding cosmopolitan"

views = []
print "*"*44
print "make a new table in the view"
ingredients = db.getas("ingredients[ingredient:S]")
ingredients.append(ingredient=v)
ingredients.append(ingredient=cj)
ingredients.append(ingredient=lj)
ingredients.append(ingredient=tj)
ingredients.append(ingredient=ts)
checkView(ingredients)
views.append(('new table', ingredients))

print "*"*44
print "make a new non stored temporary view"
ingredients = metakit.view()
ingredients.addproperty(metakit.property('S', "ingredient"))
ingredients.append(ingredient=v)
ingredients.append(ingredient=cj)
ingredients.append(ingredient=lj)
ingredients.append(ingredient=tj)
ingredients.append(ingredient=ts)
checkView(ingredients)
views.append(('temporary table', ingredients))
#
print "*"*44
print "make a view from a wrapped dictionary"
have = [ { 'ingredient' : v },
         { 'ingredient' : cj },
         { 'ingredient' : lj},
         { 'ingredient' : tj },
         { 'ingredient' : ts },
       ]

ingredients = metakit.wrap(
    have,
    [metakit.property('S', "ingredient")]
    )
checkView(ingredients)
views.append(('wrapped dictionary', ingredients))
#
print "*"*44
print "make a view from a wrapped list"
have = [("vodka",), ("cramberry juice",), ("lime juice",),
        ("tomato juice",), (ts,)]
ingredients = metakit.wrap(
    have,
    [metakit.property('S', "ingredient")],
    1
    )
checkView(ingredients)
views.append(('wrapped list', ingredients))

print "testing tables versus wrapped tables"
i = 0
for n, v in views:
    i += 1
    for n2, v2 in views[i:]:
        if len(v.minus(v2)) != 0:
            print "minus:\t\t", `n`, "minus test failed with", `n2`
        if len(v.intersect(v2)) != len(v):
            print "intersection:\t", `n`, "intersection", `n2`, "failed"
        
        

Reply via email to