{"id":442,"date":"2015-03-06T23:18:20","date_gmt":"2015-03-07T07:18:20","guid":{"rendered":"http:\/\/www.isam.ca\/?p=442"},"modified":"2015-03-21T16:57:07","modified_gmt":"2015-03-21T23:57:07","slug":"d-isam-example-in-python","status":"publish","type":"post","link":"https:\/\/www.isam.ca\/?p=442","title":{"rendered":"D-ISAM example in Python"},"content":{"rendered":"<p>The following example code demonstrates the basic principles of using D-ISAM in Python, by means of the standard <a href=\"http:\/\/docs.python.org\/2\/library\/ctypes.html\" target=\"_blank\">ctypes<\/a> library.<\/p>\n<p>The required disam72 class module is <a href=\"http:\/\/www.isam.ca\/?p=437\">here<\/a><\/p>\n<p>An overview is available <a href=\"http:\/\/www.isam.ca\/?p=434\">here<\/a><\/p>\n<p>Download the example <a title=\"D-ISAM example in Python\" href=\"http:\/\/www.isam.ca\/?download=593\">here<\/a><\/p>\n<pre>#!\/usr\/bin\/python\r\n\r\nfrom ctypes import *\r\nfrom disam72 import d7, d7key, d7error\r\n\r\nname = \"example_isam_file\"\r\n\r\nclass RECORD(Structure):\r\n  _fields_ = [ ( \"code\", c_int ),\r\n               ( \"name\", c_char * 31 ) ]\r\n\r\nkey1 = d7key( d7.ISNODUPS, 1, ( ( 0, 4, d7.LONGTYPE ), ) )\r\n\r\nkey2 = d7key( d7.COMPRESS + d7.TNULL, 1, ( ( 4, 31, d7.STRINGTYPE ), ) )\r\n\r\nd7.iserase( name )\r\n\r\nisam = d7.isBuild( name, 35, 0, byref(key1), d7.ISINOUT + d7.ISMANULOCK )\r\nif isam is None:\r\n  raise d7error( None, \"build %s failed\" % name )\r\n\r\nif d7.isAddIndex( isam, byref(key2) ) == 0:\r\n  raise d7error( isam, \"add second index failed\" );\r\n\r\npad = RECORD( 0, \"\" )\r\n\r\npad.code = 1\r\npad.name = \"banana, junior\"\r\nif d7.isWrite( isam, byref(pad) ) == 0:\r\n  raise d7error( isam, \"add %d %s failed\" % ( pad.code, pad.name ) )\r\n\r\npad.code = 2\r\npad.name = \"apple, macintosh\"\r\nif d7.isWrite( isam, byref(pad) ) == 0:\r\n  raise d7error( isam, \"add %d %s failed\" % ( pad.code, pad.name ) )\r\n\r\npad.code = 999\r\npad.name = \"error, read failed\"\r\n\r\nif d7.isRead( isam, byref(pad), d7.ISFIRST ) == 0:\r\n  raise d7error( isam, \"read first on primary index failed\" )\r\nelse:\r\n  print \"%d: %s\" % ( pad.code, pad.name )\r\n\r\nif d7.isStart( isam, byref(key2), 1, isam, d7.ISFIRST ) == 0:\r\n  raise d7error( isam, \"switch to secondary index failed\" );\r\n\r\nif d7.isRead( isam, byref(pad), d7.ISFIRST ) == 0:\r\n  raise d7error( isam, \"read first on secondary index failed\" )\r\nelse:\r\n  print \"%d: %s\" % ( pad.code, pad.name )<\/pre>\n<p>result:<\/p>\n<pre>1: banana, junior\r\n2: apple, macintosh<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 = &#8220;example_isam_file&#8221; class RECORD(Structure): _fields_ = [ [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[8,7],"_links":{"self":[{"href":"https:\/\/www.isam.ca\/index.php?rest_route=\/wp\/v2\/posts\/442"}],"collection":[{"href":"https:\/\/www.isam.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.isam.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.isam.ca\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.isam.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=442"}],"version-history":[{"count":9,"href":"https:\/\/www.isam.ca\/index.php?rest_route=\/wp\/v2\/posts\/442\/revisions"}],"predecessor-version":[{"id":596,"href":"https:\/\/www.isam.ca\/index.php?rest_route=\/wp\/v2\/posts\/442\/revisions\/596"}],"wp:attachment":[{"href":"https:\/\/www.isam.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.isam.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.isam.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}