This is an example of record retrieve both by percentage and fraction written in Python.
1import sys
2import struct
3import math
4import os
5import platform
6
7if (platform.system() == "Windows"):
8 sys.path.append("C:\\Program Files\\Actian\\Zen\\bin")
9else:
10 sys.path.append("/usr/local/actianzen/lib64")
11
12import btrievePython
13
14min_x = 0
15max_x = 255
16btrieveFileName = "squaresAndSquareRoots.btr"
17numberOfRecords = max_x - min_x + 1
18recordFormat = "<BHd"
19recordLength = 11
20keyFormat = "<B"
21
22
23if (len(sys.argv) != 1):
24 sys.exit("Usage: " + os.path.basename(sys.argv[0]))
25
26btrieveClient = btrievePython.BtrieveClient()
27assert(btrieveClient != None)
28
29
30btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
31assert(btrieveFileAttributes != None)
32
33rc = btrieveFileAttributes.SetFixedRecordLength(recordLength)
34assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
35
36rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
37assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
38
39
40btrieveFile = btrievePython.BtrieveFile()
41assert(btrieveFile != None)
42
43rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
44assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
45
46
47
48for i in range(min_x, max_x + 1):
49 record = struct.pack(recordFormat, i, i * i, math.sqrt(i))
50
51 rc = btrieveFile.RecordCreate(record)
52 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
53
54
55btrieveKeySegment = btrievePython.BtrieveKeySegment()
56assert(btrieveKeySegment != None)
57
58rc = btrieveKeySegment.SetField(0, 1, btrievePython.Btrieve.DATA_TYPE_UNSIGNED_BINARY)
59assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
60
61btrieveIndexAttributes = btrievePython.BtrieveIndexAttributes()
62assert(btrieveIndexAttributes != None)
63
64rc = btrieveIndexAttributes.AddKeySegment(btrieveKeySegment)
65assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
66
67rc = btrieveFile.IndexCreate(btrieveIndexAttributes)
68assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::IndexCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
69
70
71record = struct.pack(recordFormat, 0, 0, 0)
72
73rc = btrieveFile.RecordRetrieveFirst(btrievePython.Btrieve.INDEX_1, record)
74assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
75
76print ("First:", struct.unpack(recordFormat, record))
77
78rc = btrieveFile.RecordRetrieveByPercentage(btrievePython.Btrieve.INDEX_1, 0, record)
79assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveByPercentage():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
80
81print ("ByPercentage: 0% record:", struct.unpack(recordFormat, record))
82
83rc = btrieveFile.RecordRetrieveByFraction(btrievePython.Btrieve.INDEX_1, 0, numberOfRecords, record)
84assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveByFraction():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
85
86print ("ByFraction:", 0, "/", numberOfRecords, "record:", struct.unpack(recordFormat, record))
87
88
89rc = btrieveFile.RecordRetrieveByPercentage(btrievePython.Btrieve.INDEX_1, 5000, record)
90assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveByPercentage():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
91
92
93print ("ByPercentage: 50% record:", struct.unpack(recordFormat, record))
94
95
96rc = btrieveFile.RecordRetrieveByFraction(btrievePython.Btrieve.INDEX_1, int(numberOfRecords / 2), numberOfRecords, record)
97assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveByFraction():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
98
99
100print ("ByFraction:", int(numberOfRecords / 2), "/", numberOfRecords, "record:", struct.unpack(recordFormat, record))
101
102
103cursorPosition = btrieveFile.GetCursorPosition()
104assert(cursorPosition != -1), "Error: BtrieveFile::GetCursorPosition():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
105
106
107
108rc = btrieveFile.RecordRetrieveLast(btrievePython.Btrieve.INDEX_1, record)
109assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveLast():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
110
111
112print ("Last:", struct.unpack(recordFormat, record))
113
114rc = btrieveFile.RecordRetrieveByPercentage(btrievePython.Btrieve.INDEX_1, 10000, record)
115assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveByPercentage():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
116
117print ("ByPercentage: 100% record:", struct.unpack(recordFormat, record))
118
119rc = btrieveFile.RecordRetrieveByFraction(btrievePython.Btrieve.INDEX_1, numberOfRecords, numberOfRecords, record)
120assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveByFraction():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
121
122print ("ByFraction:", numberOfRecords, "/", numberOfRecords, "record:", struct.unpack(recordFormat, record))
123
124
125rc = btrieveFile.RecordRetrieveByCursorPosition(btrievePython.Btrieve.INDEX_1, cursorPosition, record)
126assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveByCursorPosition():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
127
128
129print ("ByCursorPosition: record:", struct.unpack(recordFormat, record))
130keyValue = int((max_x - min_x) / 2)
131key = struct.pack(keyFormat, keyValue)
132
133
134percentage = btrieveFile.GetPercentage(btrievePython.Btrieve.INDEX_1, key)
135assert(percentage != -1), "Error: BtrieveFile::GetPercentage():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
136
137
138print ("GetPercentage using Index:", percentage / 100, "%% key:", keyValue)
139
140
141percentage = btrieveFile.GetPercentage(cursorPosition)
142assert(percentage != -1), "Error: BtrieveFile::GetPercentage():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
143
144
145print ("GetPercentage using Cursor Position:", percentage / 100, "%%")
146
147
148numerator = btrieveFile.GetNumerator(btrievePython.Btrieve.INDEX_1, key, numberOfRecords)
149assert(numerator != -1), "Error: BtrieveFile::GetNumerator():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
150
151
152print ("GetNumerator using Index:", numerator, "/", numberOfRecords, "key:", keyValue)
153
154
155numerator = btrieveFile.GetNumerator(cursorPosition, numberOfRecords)
156assert(numerator != -1), "Error: BtrieveFile::GetNumerator():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
157
158
159print ("GetNumerator using Cursor Position:", numerator, "/", numberOfRecords)
160
161
162rc = btrieveClient.FileClose(btrieveFile)
163assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
164
165
166rc = btrieveClient.FileDelete(btrieveFileName)
167assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))