# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
from construct import *
from vms.rms.IndexedFile import IndexedFile

class MsgRec:
    def __init__(self, id, msg):
        self.id = id
        self.msg = msg

class MsgFil(IndexedFile):
    def __init__(self, fnm):
        IndexedFile.__init__(self, fnm, Struct('msgrec', SNInt32('id'), String('msg', 100, padchar=' ')))
    def primary_keynum(self):
        return 0

# open database
db = MsgFil('msgs.isq')
# write message to database
db.put(MsgRec(6, 'Hi from Python!'))
# dump all messages in database
for msg in db:
    print('|' + msg.msg + '|')
