1""" 2metakit.py -- Utility code for the Python interface to Metakit 3$Id: metakit.py 1669 2007-06-16 00:23:25Z jcw $ 4This is part of Metakit, see http://www.equi4.com/metakit.html 5 6This wraps the raw Mk4py compiled extension interface. 7To use Metakit through this interface, simply do: 8 import metakit 9After that, things like metakit.storage(...) are available, 10as well as utilities defined below. This assumes that both 11both metakit.py and Mk4py.{dll,so} can be found by Python. 12""" 13 14_oldname = __name__ 15__name__ = "metakit" 16__version__ = "2.4.9.7" 17__description__ = "Python bindings to the Metakit database library" 18__author__ = "Gordon McMillan / Jean-Claude Wippler" 19__email__ = "jcw@equi4.com" 20__url__ = "http://www.equi4.com/metakit/python.html" 21__license__ = "X/MIT style, see: http://www.equi4.com/mklicense.html" 22from Mk4py import * 23import string 24 25def dump(view, title=None): 26 """pretty printer for MK views""" 27 28 widths = [] 29 cols = [] 30 justs = [] 31 32 props = view.structure() 33 for prop in props: 34 widths.append(len(prop.name)) 35 cols.append([None]) 36 if prop.type in ('I','F','D','V'): 37 justs.append(string.rjust) 38 else: 39 justs.append(string.ljust) 40 41 for row in view: 42 for c in range(len(props)): 43 attr = getattr(row, props[c].name, None) 44 if type(attr) is type(view): 45 text = '%d rows' % len(attr) 46 else: 47 text = str(attr) 48 if len(text) > 20: 49 text = text[0:17] + '...' 50 widths[c] = max(widths[c],len(text)) 51 cols[c].append(text) 52 53 if title: print title 54 55 for c in range(len(props)): 56 cols[c][0] = widths[c] * '-' 57 cols[c].append(cols[c][0]) 58 print '', string.ljust(props[c].name, widths[c]), 59 print 60 61 for r in xrange(len(view)+2): 62 for c in range(len(props)): 63 print '', justs[c](cols[c][r], widths[c]), 64 print 65 66 print " Total: %d rows" % len(view) 67 68if _oldname == '__main__': 69 db = storage() 70 f = db.getas('frequents[drinker,bar,perweek:I]') 71 s = db.getas('serves[bar,beer,quantity:I]') 72 73 f.append(drinker='adam', bar='lolas', perweek=1) 74 f.append(drinker='woody', bar='cheers', perweek=5) 75 f.append(drinker='sam', bar='cheers', perweek=5) 76 f.append(drinker='lola', bar='lolas', perweek=6) 77 78 s.append(bar='cheers', beer='bud', quantity=500) 79 s.append(bar='cheers', beer='samaddams', quantity=255) 80 s.append(bar='lolas', beer='mickies', quantity=1515) 81 82 dump(f, 'frequents:') 83 dump(s, 'serves:') 84 dump(f.join(s, s.bar), 'join on "bar":') 85