User Tools

Site Tools


rmsaccesshighapiexample

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

rmsaccesshighapiexample [2016/04/05 07:52]
jfp created
rmsaccesshighapiexample [2016/04/22 10:45]
jfp
Line 1: Line 1:
 +Using the initial module IndexedFile.py
 <code python> <code python>
 import struct import struct
Line 161: Line 162:
 </​code>​ </​code>​
  
 +Using the new module IndexedFile2
 +
 +File FDL
 +<​code>​
 +SYSTEM
 +        SOURCE ​                 "​OpenVMS"​
 +
 +FILE
 +        NAME                    "​master001.dbs"​
 +        ORGANIZATION ​           indexed
 +        GLBUFF_FLAGS_V83 ​       none
 +
 +RECORD
 +        CARRIAGE_CONTROL ​       none
 +        FORMAT ​                 fixed
 +        SIZE                    30
 +
 +KEY 0
 +        CHANGES ​                no
 +        SEG0_LENGTH ​            10
 +        SEG0_POSITION ​          6
 +        TYPE                    string
 +
 +KEY 1
 +        SEG0_LENGTH ​            6
 +        SEG0_POSITION ​          16
 +        TYPE                    string
 +        SEG1_LENGTH ​            2
 +        SEG1_POSITION ​          26
 +        TYPE                    string
 +</​code>​
 +
 +Initial Data
 +<​code>​
 +F01AAAF003AAAAA1XXXXA30005AAZZ
 +F01BABF003AAAAB1XXXXA10004BBXX
 +F01CACF003AAAAC1XXXXA20003DDEE
 +F01DADF003AAAAD1XXXXA40001TTDD
 +</​code>​
 +
 +<code python>
 +# -*- coding: iso-8859-15 -*-
 +
 +from __future__ import with_statement
 +import struct
 +from vms.rms.IndexedFile2 import IndexedFile
 +from construct import *
 +
 +# import pdb; pdb.set_trace()
 +
 +class KeyStruct(object):​
 +    pass
 +
 +
 +RecordMASTER_1 = Struct("​RecordMASTER_1",​
 +    String('​F01L001',​ 4, padchar='​ '),
 +    String('​F01L002',​ 2, padchar='​ '),
 +    String('​F01L003',​ 10, padchar='​ '),
 +    String('​F01L004',​ 6, padchar='​ '),
 +    String('​F01L005',​ 4, padchar='​ '),
 +    String('​F01L006',​ 2, padchar='​ '),
 +    String('​F01L007',​ 2, padchar='​ '),
 +)
 +
 +
 +class AccessIntFileKey0Struct(KeyStruct):​
 +    key = String('​F01L003',​ 10, padchar='​ ')
 +
 +
 +class AccessIntFileKey1Struct(KeyStruct):​
 +    key = Struct('​key',​
 +                 ​String('​F01L004',​ 6, padchar='​ '),
 +                 ​String('​F01L006',​ 2, padchar='​ '),
 +                 )
 +
 +
 +class TestFile(IndexedFile):​
 +    name = '​master001.dbs'​
 +    def __init__(self):​
 +        super(TestFile,​ self).__init__(self.name,​ RecordMASTER_1,​
 +                         ​[AccessIntFileKey0Struct.key,​
 +                          AccessIntFileKey1Struct.key])
 +
 +    def keyval(self,​ rec, keynum):
 +        return getattr(rec,​ self.keys[keynum])
 +
 +f = TestFile()
 +f.open()
 +
 +f.usekey(1)
 +f.rewind()
 +print '​Find',​ f.find(1, Container(F01L004='​XXXXA3',​ F01L006='​AA'​))
 +for rec in f:
 +    print rec # prints out a record form DB file.
 +
 +
 +f.usekey(0)
 +f.rewind()
 +print '​Find',​ f.find(0, '​F003AAAAC1'​)
 +for rec in f:
 +    print rec # prints out a record form DB file.
 +
 +f.rewind()
 +print '​Find',​ f.find(0, '​F003AAAAC',​ '>​='​)
 +for rec in f:
 +    print rec # prints out a record form DB file.
 +</​code>​
 +
 +Result
 +<​code>​
 +Find True
 +Container:
 +    F01L001 = '​F01A'​
 +    F01L002 = '​AA'​
 +    F01L003 = '​F003AAAAA1'​
 +    F01L004 = '​XXXXA3'​
 +    F01L005 = '​0005'​
 +    F01L006 = '​AA'​
 +    F01L007 = '​ZZ'​
 +Container:
 +    F01L001 = '​F01D'​
 +    F01L002 = '​AD'​
 +    F01L003 = '​F003AAAAD1'​
 +    F01L004 = '​XXXXA4'​
 +    F01L005 = '​0001'​
 +    F01L006 = '​TT'​
 +    F01L007 = '​DD'​
 +Find True
 +Container:
 +    F01L001 = '​F01C'​
 +    F01L002 = '​AC'​
 +    F01L003 = '​F003AAAAC1'​
 +    F01L004 = '​XXXXA2'​
 +    F01L005 = '​0003'​
 +    F01L006 = '​DD'​
 +    F01L007 = '​EE'​
 +Container:
 +    F01L001 = '​F01D'​
 +    F01L002 = '​AD'​
 +    F01L003 = '​F003AAAAD1'​
 +    F01L004 = '​XXXXA4'​
 +    F01L005 = '​0001'​
 +    F01L006 = '​TT'​
 +    F01L007 = '​DD'​
 +Find True
 +Container:
 +    F01L001 = '​F01C'​
 +    F01L002 = '​AC'​
 +    F01L003 = '​F003AAAAC1'​
 +    F01L004 = '​XXXXA2'​
 +    F01L005 = '​0003'​
 +    F01L006 = '​DD'​
 +    F01L007 = '​EE'​
 +Container:
 +    F01L001 = '​F01D'​
 +    F01L002 = '​AD'​
 +    F01L003 = '​F003AAAAD1'​
 +    F01L004 = '​XXXXA4'​
 +    F01L005 = '​0001'​
 +    F01L006 = '​TT'​
 +    F01L007 = '​DD'​
 +</​code>​
rmsaccesshighapiexample.txt ยท Last modified: 2016/04/22 10:45 by jfp