Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

A plugin to interact with Cherrytable

Project description

CHERRYTABLE CONNECTOR

The Cherrytable Connector allows the interaction with Cherrytable through HTTP requests. Its functions permit to insert, read, update and delete rows through sequential, pipelined or queued requests. The component is provided as a public python module which can be found at PyPI and can be installed in any machine with a version of Python > 3.

Installation

The module can be installed with the following command

pip install cherrytable-connector

Configuration

The connector object can be created as follows:

from cherrytable_connector import cherrytable_connector as cc
connector = cc.CherrytableConnector(host, port, username, password, table_name, table_column_names, log_dir, debug, max_pipeline_requests)

where:

  • host and port refer to the Cherrytable installation
  • username and password are the credentials
  • table_name is the name of the table, e.g. “usertable”
  • table_column_names is the list of the column names, e.g. ["key_column","field0","field1"]
  • log_dir is the path of the folder containing the logs, e.g. “../log”
  • debug is True to set the log in debug mode
  • max_pipeline_requests (optional parameter) is the maximum size of the pipeline

Methods

Sequential

Requests can be sequentially sent, i.e. waiting for the response of the first request before sending the second, using the send function:

send(req_type, key, data, options)

where:

  • req_type is the type of the request
  • key is the value of the primary key of the row
  • data is the entire row, where values are divided by “;”
  • options is a list of column names, employed in the read function

Parameters vary according to the request type and examples of usage are the following:

INSERT

res = connector.send(req_type=cc.RequestType.INSERT, data="1;33;172")

If the key (first value) already exists, the request won’t be successful.

UPDATE

res = connector.send(req_type=cc.RequestType.UPDATE, data="1;30;170")

To be successful the key should already exist.

READ

res = connector.send(req_type=cc.RequestType.READ, key=1, options=[key_column, field0])

To be successful the key should already exist. With options it is possible to specify the columns to be retrieved, otherwise if options is None all the columns are retrieved.

DELETE

res = connector.send(req_type=cc.RequestType.DELETE, key=1)

To be successful the key should already exist.

The outcome res is a dictionary containing the fields status and body, corresponding to the status code of the response and its body.

Pipelining

Pipelining allows to send multiple requests without waiting for individual responses. Requests are defined in a list and their responses are returned together. The maximum size of the pipeline can be specified in the constructor of CherrytableConnector. Requests are sent through the sendPipeline function:

sendPipeline(requests)

which takes as input a list of requests, defined as tuples (req_type,key,data,options)

requests = [(cc.RequestType.INSERT, None, "1;33;172", None),\
            (cc.RequestType.INSERT, None, "2;33;172", None),\
            (cc.RequestType.INSERT, None, "3;33;172", None),\
            (cc.RequestType.DELETE, "1", None, None),\
            (cc.RequestType.DELETE, "2", None, None),\
            (cc.RequestType.UPDATE, None, "3;20;100", None),\
            (cc.RequestType.READ, "3", None, [key_column,field0]),\
            (cc.RequestType.DELETE, "3", None, None)]  
responses = connector.sendPipeline(requests)

responses is the list of all the response dictionaries, with status and body fields.

Queued

With this feature it is possible to send requests without waiting for their responses. After having sent some requests it is possible to perform other operations before retrieving their responses. Requests are automatically added to an internal queue and handled as pipelines. The main difference with respect to the prior method is that it is not necessary to prepare all the requests before sending them, instead requests are individually sent using:

sendWithQueue(req_type, key, data, options)

For example:

requests = []
requests.append(connector.sendWithQueue(req_type=cc.RequestType.INSERT, key=None, data="1;33;172", options=None))
requests.append(connector.sendWithQueue(req_type=cc.RequestType.INSERT, key=None, data="2;33;172", options=None))
requests.append(connector.sendWithQueue(req_type=cc.RequestType.INSERT, key=None, data="3;33;172", options=None))
requests.append(connector.sendWithQueue(req_type=cc.RequestType.DELETE, key="1", data=None, options=None))

#Other operations…

responses=[]
last_completed = 0
while last_completed != (len(requests)-1):
if requests[last_completed+1].getResult()[0] == cc.RequestStatus.COMPLETED or requests[last_completed+1].getResult()[0] == cc.RequestStatus.ERROR:
               	last_completed +=1 
		responses.append(requests[last_completed+1].res) 

Working with objects

Instead of using plain text to represent rows, it is possible to map rows to objects. For example, a Cherrytable row composed of the fields key_column, field0, field1 can be mapped to a runtime object having the following fields:       c_id → key_column       age → field0       height → field1

In this way, object fields are updated according to the operations performed on the table.

from cherrytable_connector import cherrytable_connector_obj as cc
connector = cc.CherrytableConnector(host, port, username, password, table_name, table_column_names, obj_mapping, log_dir, debug)

where obj_mapping is a list of fields of the object mapped positionally to table_column_names, in this example ["c_id","age","height"]

The object class must inherit from cc.CherryObject and can be defined for example as follows:

class Customer(cc.CherryObject):
    def __init__(self, c_id, età, altezza):
        cc.CherryObject.__init__(self)
        self.c_id = c_id
        self.age = age
        self.height = height

customer = Customer(c_id="1", age="33", height="172")

Sequential

Sequential requests are executed by sending the object as parameter:

send(req_type, obj, options=None)

where

  • req_type is the type of the request
  • obj is the object corresponding to the table
  • options is a list of column names, employed in the read function

Parameters vary according to the request type and examples of usage are the following:

INSERT

res = connector.send(req_type=cc.RequestType.INSERT, obj=customer)

If the key of the object (parameter corresponding to the primary key of the table) already exists, the request won’t be successful.

UPDATE

res = connector.send(req_type=cc.RequestType.UPDATE, obj=customer)

To be successful the key of the object should already exist.

READ

res = connector.send(req_type=cc.RequestType.READ, obj=customer, options=[key_column, field0])

To be successful the key of the object should already exist. With options it is possible to specify the columns to be retrieved, otherwise if options is None all the columns are retrieved. When a read operation is performed, the object parameters are updated with the values contained in the corresponding table.

DELETE

res = connector.send(req_type=cc.RequestType.DELETE, obj=customer)

To be successful the key of the object should already exist.

Pipelining

The list of requests contained in a pipeline are defined as tuples (req_type,object,options)

requests = [(cc.RequestType.INSERT,customer,None),\
            (cc.RequestType.INSERT,customer,None),\
            (cc.RequestType.INSERT,customer,None),\
            (cc.RequestType.DELETE,customer,None),\
            (cc.RequestType.DELETE,customer,None),\
            (cc.RequestType.UPDATE,customer_modified,None),\
            (cc.RequestType.READ,customer,None),\
            (cc.RequestType.DELETE,customer,None)]  

responses = connector.sendPipeline(requests)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for cherrytable-connector, version 0.4
Filename, size File type Python version Upload date Hashes
Filename, size cherrytable_connector-0.4-py3-none-any.whl (20.8 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size cherrytable_connector-0.4.tar.gz (32.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page