btest.py

これは、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 the incorrect number of arguments were given.
22 if (len(sys.argv) != 2):
23  sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
24 
25 integerValue = int(sys.argv[1])
26 
27 # If integerValue is out of range.
28 if (integerValue < min_x) or (integerValue > max_x):
29  sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
30 
31 
32 btrieveClient = btrievePython.BtrieveClient()
33 assert(btrieveClient != None)
34 
35 
36 
37 btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
38 assert(btrieveFileAttributes != None)
39 
40 
41 
42 rc = btrieveFileAttributes.SetFixedRecordLength(recordLength)
43 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
44 
45 
46 
47 rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
48 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
49 
50 
51 
52 btrieveFile = btrievePython.BtrieveFile()
53 assert(btrieveFile != None)
54 
55 
56 
57 rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
58 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
59 
60 
61 # For all the values of x.
62 for i in range(min_x, max_x + 1):
63  record = struct.pack(recordFormat, i, i * i, math.sqrt(i))
64 
65 
66  rc = btrieveFile.RecordCreate(record)
67  assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
68 
69 
70 
71 btrieveKeySegment = btrievePython.BtrieveKeySegment()
72 assert(btrieveKeySegment != None)
73 
74 
75 
76 rc = btrieveKeySegment.SetField(0, 1, btrievePython.Btrieve.DATA_TYPE_UNSIGNED_BINARY)
77 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
78 
79 
80 
81 btrieveIndexAttributes = btrievePython.BtrieveIndexAttributes()
82 assert(btrieveIndexAttributes != None)
83 
84 
85 
86 rc = btrieveIndexAttributes.AddKeySegment(btrieveKeySegment)
87 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
88 
89 
90 
91 rc = btrieveFile.IndexCreate(btrieveIndexAttributes)
92 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::IndexCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
93 
94 
95 # print record
96 record = struct.pack(recordFormat, 0, 0, 0)
97 key = struct.pack(keyFormat, integerValue)
98 
99 
100 rc = btrieveFile.RecordRetrieve(btrievePython.Btrieve.COMPARISON_EQUAL, btrievePython.Btrieve.INDEX_1, key, record)
101 assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieve():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
102 
103 
104 print ("record:", struct.unpack(recordFormat, record))
105 
106 
107 rc = btrieveClient.FileClose(btrieveFile)
108 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
109 
110 
111 
112 rc = btrieveClient.FileDelete(btrieveFileName)
113 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
114