File: metakit.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 (74 lines) | stat: -rw-r--r-- 2,142 bytes parent folder | download
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# metakit.py -- Utility code for the Python interface to MetaKit
# $Id: metakit.py,v 1.5 2001/11/25 21:10:38 wcvs Exp $
# This is part of MetaKit, see http://www.equi4.com/metakit/
# 
# This wraps the raw Mk4py compiled extension interface.
# To use MetaKit through this interface, simply do:
#   import metakit
# After that, things like metakit.storage(...) are available,
# as well as utilities defined below.  This assumes that both
# both mk.py and Mk4py.{dll,so} can be found by Python.

from Mk4py import *
import string

def dump(view, title=None):
  """pretty printer for MK views"""

  widths = []
  cols = []
  justs = []

  props = view.structure()
  for prop in props:
    widths.append(len(prop.name))
    cols.append([None])
    if prop.type in ('I','F','D','V'):
      justs.append(string.rjust)
    else:
      justs.append(string.ljust)

  for row in view:
    for c in range(len(props)):
      attr = getattr(row, props[c].name, None)
      if type(attr) is type(view):
        text = '%d rows' % len(attr)
      else:
        text = str(attr)
        if len(text) > 20:
          text = text[0:17] + '...'
      widths[c] = max(widths[c],len(text))
      cols[c].append(text)

  if title: print title

  for c in range(len(props)):
    cols[c][0] = widths[c] * '-'
    cols[c].append(cols[c][0])
    print '', string.ljust(props[c].name, widths[c]),
  print

  for r in xrange(len(view)+2):
    for c in range(len(props)):
      print '', justs[c](cols[c][r], widths[c]),
    print

  print " Total: %d rows" % len(view)

if __name__ == '__main__':
  db = storage()
  f = db.getas('frequents[drinker,bar,perweek:I]')
  s = db.getas('serves[bar,beer,quantity:I]')

  f.append(drinker='adam', bar='lolas', perweek=1)
  f.append(drinker='woody', bar='cheers', perweek=5)
  f.append(drinker='sam', bar='cheers', perweek=5)
  f.append(drinker='lola', bar='lolas', perweek=6)

  s.append(bar='cheers', beer='bud', quantity=500)
  s.append(bar='cheers', beer='samaddams', quantity=255)
  s.append(bar='lolas', beer='mickies', quantity=1515)

  dump(f, 'frequents:')
  dump(s, 'serves:')
  dump(f.join(s, s.bar), 'join on "bar":')