File: selfref.py

package info (click to toggle)
metakit 2.4.3-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,468 kB
  • ctags: 3,548
  • sloc: xml: 29,455; cpp: 23,339; sh: 9,051; tcl: 1,195; python: 577; makefile: 254; ansic: 14
file content (48 lines) | stat: -rw-r--r-- 891 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Demo of the new 2.3 self-referential view structures.
#
# Output:
# 0
# 00000 1
# 11111 1
# 22222 0
# [Property('S', 's'), Property('V', 'sub')]
# 00000 1
# 11111 1
# 22222 0
# [Property('S', 's'), Property('I', 'i'), Property('V', 'sub')]
# ok

import os
try: os.remove("_selfref.mk")
except: pass

import metakit
db = metakit.storage('_selfref.mk',1)
vw = db.getas('data[s:S,sub[^]]')

print len(vw)

v = vw
for i in range(3):
  v.append(s=`i`*5)
  v = v[0].sub

def show(vw):
  for v in [vw[0], vw[0].sub[0], vw[0].sub[0].sub[0]]:
    print v.s, len(v.sub)
  print vw[0].sub[0].sub[0].sub.structure()
  assert len(vw[0].sub[0].sub[0].sub) == 0
  try:
    metakit.dump(vw[0].sub[0].sub[0].sub[0].sub)
  except IndexError:
    pass

show(vw)

# note that (recursive!) on-the-fly restructuring works
show(db.getas('data[s:S,i:I,sub[^]]'))

db.commit()
print 'ok'

os.remove("_selfref.mk")