Step 4: Using Sequence Fields

To perform operations on database objects containing sequence fields the Python application uses SequenceIterator methods and tuples. Consider the following class Quote:

 
    class Quote 
    {
        char<8> symbol;
        sequence<time asc> timestamp;
        sequence<float> low;
        sequence<float> high;
        sequence<float> open;
        sequence<float> close;
        sequence<uint4> volume;
 
        hash<symbol> by_sym[1000];
    };
     

To work with the sequence fields, we first need a 'connected' object, i.e. loaded from the database or created with Connection method new(). When an object like Quote is instantiated, SequenceIterators are created for the object's sequence fields and are in 'initial' state. To assign values to the sequence fields we use the SequenceIterator method append(). For example, the following code snippet initializes a Quote object's sequence fields:

  
    con.startTransaction(exdb.Transaction.MCO_READ_WRITE)
     
    quote = con.new("Quote")
    quote.symbol = 'AAA'
     
    op = array.array('f', [2.0] * BUF_SIZE)
    cl = array.array('f', [3.0] * BUF_SIZE)
    hg = array.array('f', [4.0] * BUF_SIZE)
    lw = array.array('f', [1.0] * BUF_SIZE)
     
    i = 0
 
    while i < N_QUOTES:
        quote.timestamp.append( [i+j+1 for j in xrange(BUF_SIZE)])
        quote.open.append(op)
        quote.close.append(cl)
        quote.high.append(hg)
        quote.low.append(lw)
        quote.volume.append( [i+j for j in xrange(BUF_SIZE)] )
        i += BUF_SIZE
    con.commit()
     

To get a specific value or range of values in a sequence field we use SequenceIterator method search(). Then to perform operations on a corresponding group of "dependent" sequence fields we use method project() to create corresponding tuples. For example the following code snippet selects the range of values greater than 10 from the ordered sequence field timestamp, extracts the corresponding range of values from the dependent sequence fields into tuples, then executes a loop to accumulate the sum of values for each sequence:

 
     
    FROM = 10
    TILL = N_QUOTES - 10
     
    conn.startTransaction()
 
    # Find the record in the database
    cursor = conn.cursor()
    quote = cursor.find("Quote", "by_sym", "AAA")
 
    # Perform a search operation on  ordered sequence "timestamp"
    tsit = quote.timestamp.search(FROM, exdb.SeqIteratorBoundary.MCO_SEQ_BOUNDARY_EXCLUSIVE, 
                    TILL, exdb.SeqIteratorBoundary.MCO_SEQ_BOUNDARY_INCLUSIVE)
                     
    # Project the search result to the other 'dependent' sequence fields
    # creating tuples for the dependent sequences
    tsit.project('open', 'close', 'high', 'low', 'volume')
 
    # Iterate over a set of values
    count = 0
    for (ts, open, close, high, low, volume) in tsit:
        timestamp_sum += ts
        open_sum += open
        close_sum += close
        high_sum += high
        low_sum += low
        volume_sum += volume
        count += 1
 
    conn.rollback()
     

SDK sample samples/python/seqbasic illustrates how to use these SequenceIterator methods to perform basic sequence operations. Please build and run the sample application and examine the source code.