btest.py

This is an example of file creation, load, access, and deletion in Python.

1 import sys
2 import struct
3 import math
4 import os
5 import platform
6 
7 if (platform.system() == "Windows"):
8  sys.path.append("C:\\Program Files\\Actian\\Zen\\bin")
9 else:
10  sys.path.append("/usr/local/actianzen/lib64")
11 
12 import btrievePython
13 
14 min_x = 0
15 max_x = 255
16 btrieveFileName = "squaresAndSquareRoots.btr"
17 recordFormat = "<BHd"
18 recordLength = 11
19 keyFormat = "<B"
20 
21 if (len(sys.argv) != 2):
22  sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
23 
24 integerValue = int(sys.argv[1])
25 
26 if (integerValue < min_x) or (integerValue > max_x):
27  sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
28 
29 
30 btrieveClient = btrievePython.BtrieveClient()
31 assert(btrieveClient != None)
32 
33 
34 
35 btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
36 assert(btrieveFileAttributes != None)
37 
38 
39 
40 rc = btrieveFileAttributes.SetFixedRecordLength(recordLength)
41 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetLogicalFixedRecordLength():%d,%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
42 
43 
44 
45 rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
46 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
47 
48 
49 
50 btrieveFile = btrievePython.BtrieveFile()
51 assert(btrieveFile != None)
52 
53 
54 
55 rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
56 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
57 
58 
59 # For all the values of x.
60 for i in range(min_x, max_x + 1):
61  record = struct.pack(recordFormat, i, i * i, math.sqrt(i))
62 
63 
64  rc = btrieveFile.RecordCreate(record)
65  assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
66 
67 
68 
69 btrieveKeySegment = btrievePython.BtrieveKeySegment()
70 assert(btrieveKeySegment != None)
71 
72 
73 
74 rc = btrieveKeySegment.SetField(0, 1, btrievePython.Btrieve.DATA_TYPE_UNSIGNED_BINARY)
75 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
76 
77 
78 
79 btrieveIndexAttributes = btrievePython.BtrieveIndexAttributes()
80 assert(btrieveIndexAttributes != None), "Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
81 
82 
83 
84 rc = btrieveIndexAttributes.AddKeySegment(btrieveKeySegment)
85 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
86 
87 
88 
89 rc = btrieveFile.IndexCreate(btrieveIndexAttributes)
90 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::IndexCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
91 
92 
93 # print record
94 record = struct.pack(recordFormat, 0, 0, 0)
95 key = struct.pack(keyFormat, integerValue)
96 
97 
98 rc = btrieveFile.RecordRetrieve(btrievePython.Btrieve.COMPARISON_EQUAL,btrievePython.Btrieve.INDEX_1, key, record)
99 assert(rc == recordLength), "Error: BtrieveFileRecordRetrieveEqual():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
100 
101 
102 print ("record:", struct.unpack(recordFormat, record))
103 
104 
105 rc = btrieveClient.FileClose(btrieveFile)
106 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
107 
108 
109 
110 rc = btrieveClient.FileDelete(btrieveFileName)
111 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
112