Monthly Archives: March 2015

D-ISAM example in Python

The following example code demonstrates the basic principles of using D-ISAM in Python, by means of the standard ctypes library.

The required disam72 class module is here

An overview is available here

Download the example here

#!/usr/bin/python

from ctypes import *
from disam72 import d7, d7key, d7error

name = "example_isam_file"

class RECORD(Structure):
  _fields_ = [ ( "code", c_int ),
               ( "name", c_char * 31 ) ]

key1 = d7key( d7.ISNODUPS, 1, ( ( 0, 4, d7.LONGTYPE ), ) )

key2 = d7key( d7.COMPRESS + d7.TNULL, 1, ( ( 4, 31, d7.STRINGTYPE ), ) )

d7.iserase( name )

isam = d7.isBuild( name, 35, 0, byref(key1), d7.ISINOUT + d7.ISMANULOCK )
if isam is None:
  raise d7error( None, "build %s failed" % name )

if d7.isAddIndex( isam, byref(key2) ) == 0:
  raise d7error( isam, "add second index failed" );

pad = RECORD( 0, "" )

pad.code = 1
pad.name = "banana, junior"
if d7.isWrite( isam, byref(pad) ) == 0:
  raise d7error( isam, "add %d %s failed" % ( pad.code, pad.name ) )

pad.code = 2
pad.name = "apple, macintosh"
if d7.isWrite( isam, byref(pad) ) == 0:
  raise d7error( isam, "add %d %s failed" % ( pad.code, pad.name ) )

pad.code = 999
pad.name = "error, read failed"

if d7.isRead( isam, byref(pad), d7.ISFIRST ) == 0:
  raise d7error( isam, "read first on primary index failed" )
else:
  print "%d: %s" % ( pad.code, pad.name )

if d7.isStart( isam, byref(key2), 1, isam, d7.ISFIRST ) == 0:
  raise d7error( isam, "switch to secondary index failed" );

if d7.isRead( isam, byref(pad), d7.ISFIRST ) == 0:
  raise d7error( isam, "read first on secondary index failed" )
else:
  print "%d: %s" % ( pad.code, pad.name )

result:

1: banana, junior
2: apple, macintosh