Wrapping Python sequences as Metakit views

John Totten asks, on 22-12-1999:

    vw = mk.Wrap(sequence, proplist, byPos=0)
    Can you give an example code of this in use (Mk4py).

Yes, this is a recent addition to Mk4py - try this (3 examples):

    import Mk4py
    mk = Mk4py
    db = mk.Storage('try.mk',1)
    vw = db.getas("vw[name:S,age:I]")
    # wrap a Python sequence
    data = [
      ('me', 34),
      ('you', 23),
      ('him', 12),
    ]
    # use positional access within each item in the sequence
    fake = mk.Wrap(data, (vw.name,vw.age), 1)
    for i in fake.sort(fake.age):
      print i.age, i.name
    #################################
    data = [
      {'name':'me', 'age':345},
      {'name':'you', 'age':234},
      {'name':'him', 'age':123, 'weight':100},
    ]
    # use named access within each item in the sequence
    fake = mk.Wrap(data, (vw.name,vw.age))
    for i in fake.sort(fake.age):
      print i.age, i.name
    #################################
    class Item:
        def __init__(self,n,a):
            self.name = n
            self.age = a
            self.weight = 'not used'
    data = [
      Item('me', 3456),
      Item('you', 2345),
      Item('him', 1234),
    ]
    # use named access within each item in the sequence
    fake = mk.Wrap(data, (vw.name,vw.age))
    for i in fake.sort(fake.age):
      print i.age, i.name

The output is:

  12 him
  23 you
  34 me
  123 him
  234 you
  345 me
  1234 him
  2345 you
  3456 me

-jcw