Python Analytics Methods

The eXtremeDB Python wrapper provides a library of analytics operations with sequences implemented as methods of the SequenceIterator class.

Please use the links on the Analytics Python Methods by Category page to view detailed explanations of these methods. The examples in these pages use the following schema definition for a database of stock quotes:

 
    #define uint4 unsigned<4>
    #define MAX_SYMBOL_LEN 21
     
    declare database stockdb;
 
    class Quote 
    {
        char<MAX_SYMBOL_LEN> symbol;
        sequence<date asc> day;
        sequence<float> low;
        sequence<float> high;
        sequence<float> open;
        sequence<float> close;
        sequence<uint4> volume;
         
        unique tree<symbol> by_sym;
    };
     

Note that the primary key for this table is the unique index by_sym on field symbol and all of the other fields are of type sequence where the field day is an ordered sequence. Typically analytics operations are performed on one or more sequence fields extracted from a Quote object which is effectively a "row" from a "result set" cursor. For example, the following code snippet iterates through the entire database printing out the values of the Quote character field symbol and sequence fields day and high:

 
    con.startTransaction(exdb.Transaction.MCO_READ_ONLY)
     
    # Iterate through all objects
    cursor = con.cursor("Quote", "by_sym")
    for quote in cursor:
        # Bind iterators together
        quote.day.project('high')
        for (day,high) in quote.day:
            print "%s[%s]: %f" % (quote.symbol, day, high)
    con.rollback()
     

SQL and Python

The eXtremeDB Python wrapper also extends the SQL DB API specification by allowing operations with sequence as mentioned in page Using Sequences in Python . A SequenceIterator may appear as a result of a select statement, or, as a parameter in insert or update statements. Performing operations with sequences obtained with SQL queries involves mapping (projecting) or storing one sequence onto another SequencetIterator, then calling the appropriate method. For example the following statements select the low and high values from the Quote object with symbol "AAA", copies the low sequence to avg then calculates the weighted average of the elements of the two sequences:

 
    cursor.execute("SELECT stamp, low, high FROM Quote WHERE symbol = ?", ('AAA',))
    row = cursor.fetchone()
    SequenceIterator avg;
    avg.store(low)
    avg.wavg(high)