etcdb package

Submodules

etcdb.cli module

Command line functions

etcdb.cli.get_query(prompt)[source]

Get input from a user terminated by a semicolon and return a string.

Parameters:prompt (str) – A prompt string.
Returns:User input without a trailing semicolon.
Return type:str
etcdb.cli.printf(fmt, *args)[source]

Printf implemetnation in Python.

Parameters:
  • fmt (str) – Format string. See man 3 printf for syntax.
  • args – Arguments to print.

etcdb.connection module

Connection class definition

etcdb.connection.Connect(**kwargs)[source]

Factory function for Connection.

class etcdb.connection.Connection(timeout=1, **kwargs)[source]

Bases: object

Etcd connection

static autocommit(autocommit)[source]

Set autocommit mode. Does nothing for non-transactional etcd

client

Return etcd client instance

static close()[source]

Close the connection now (rather than whenever . __del__() is called).

static commit()[source]

Commit any pending transaction to the database.

cursor()[source]

Return a new Cursor Object using the connection.

db

Current database.

static rollback()[source]

This method is optional since not all databases provide transaction support.

timeout

Connection timeout.

etcdb.cursor module

class etcdb.cursor.ColInfo(name='', width=None)[source]

Bases: object

class etcdb.cursor.Cursor(connection)[source]

Bases: object

These objects represent a database cursor, which is used to manage the context of a fetch operation. Cursors created from the same connection are not isolated, i.e. , any changes done to the database by a cursor are immediately visible by the other cursors. Cursors created from different connections can or can not be isolated, depending on how the transaction support is implemented (see also the connection’s .rollback () and .commit () methods).

arraysize = 1

This read/write attribute specifies the number of rows to fetch at a time with .fetchmany(). It defaults to 1 meaning to fetch a single row at a time.

static close()[source]

Close the cursor now (rather than whenever __del__ is called).

connection = None

Etcd connection object

description = None

This read-only attribute is a sequence of 7-item sequences.

Each of these sequences contains information describing one result column:

name type_code display_size internal_size precision scale null_ok

The first two items ( name and type_code ) are mandatory, the other five are optional and are set to None if no meaningful values can be provided.

execute(query, args=None)[source]

Prepare and execute a database operation (query or command).

Parameters:
  • query (str) – Query text.
  • args (tuple) – Optional query arguments.
Raises:
static executemany(operation, **kwargs)[source]

Prepare a database operation (query or command) and then execute it against all parameter sequences or mappings found in the sequence seq_of_parameters .

fetchall()[source]

Fetch all (remaining) rows of a query result, returning them as a sequence of sequences (e.g. a list of tuples). Note that the cursor’s arraysize attribute can affect the performance of this operation.

fetchmany(n)[source]

Fetch the next set of rows of a query result, returning a sequence of sequences (e.g. a list of tuples). An empty sequence is returned when no more rows are available.

fetchone()[source]

Fetch the next row of a query result set, returning a single sequence, or None when no more data is available.

static morgify(query, args)[source]

Prepare query string that will be sent to parser

Parameters:
  • query – Query text
  • args – Tuple with query arguments
Returns:

Query text

Return type:

str

n_cols
n_rows
result_set
rowcount
static setinputsizes(sizes)[source]

This can be used before a call to .execute*() to predefine memory areas for the operation’s parameters.

static setoutputsize(size)[source]

Set a column buffer size for fetches of large columns (e.g. LONG s, BLOB s, etc.). The column is specified as an index into the result sequence. Not specifying the column will set the default size for all large columns in the cursor.

etcdb.etcddate module

class etcdb.etcddate.EtcdDate(year, month, day)[source]

Bases: object

An object holding a date value

etcdb.etcdstring module

class etcdb.etcdstring.EtcdString(string)[source]

Bases: object

An object capable of holding a binary (long) string value.

etcdb.etcdtime module

class etcdb.etcdtime.EtcdTime(hour, minute, second)[source]

Bases: object

An object holding a time value

etcdb.etcdtimestamp module

class etcdb.etcdtimestamp.EtcdTimestamp(year, month, day, hour, minute, second)[source]

Bases: object

An object holding a time stamp value.

day
hour
minute
month
second
year

etcdb.eval_expr module

Module provides functions to evaluate an expression given in a WHERE statement. Grammar of expression is described on MySQL website (https://dev.mysql.com/doc/refman/5.7/en/expressions.html).

class etcdb.eval_expr.EtcdbFunction(*args, **kwargs)[source]

Bases: object

EtcdbFunction represents an SQL function.

Parameters:
  • function_name (callable) – python function that implements SQL function.
  • group (bool) – True if the functions is aggregate function
  • args – Arguments to pass to function_name
  • kwargs – Keyword arguments
function

Return function name

group

Return whether the function is aggregate

etcdb.eval_expr.etcdb_count(result_set)[source]

Count rows in result set

Parameters:result_set (ResultSet) – ResultSet instance
Returns:number of rows in ResultSet
Return type:int
etcdb.eval_expr.etcdb_version()[source]

Get etcdb version

etcdb.eval_expr.eval_bit_expr(row, tree)[source]

Evaluate bit_expr

etcdb.eval_expr.eval_bool_primary(row, tree)[source]

Evaluate bool_primary

etcdb.eval_expr.eval_expr(row, tree)[source]

Evaluate expression

Returns:Tuple with string representation and value. For example, (‘id’, 5).
Return type:tuple
etcdb.eval_expr.eval_function_call(row, tree)[source]

Evaluate function call :return: tuple with field name and EtcdbFunction instance

etcdb.eval_expr.eval_identifier(row, identifier)[source]

Get value of identifier for a given row

Parameters:
  • row (tuple(ColumnSet, Row)) – row
  • identifier (str) – Identifier
Returns:

value of identifier

etcdb.eval_expr.eval_predicate(row, tree)[source]

Evaluate predicate

etcdb.eval_expr.eval_simple_expr(row, tree)[source]

Evaluate simple_expr

etcdb.eval_expr.eval_string(value)[source]

Evaluate string token

etcdb.exception module

etcdb exceptions

exception etcdb.exception.DataError[source]

Bases: etcdb.exception.DatabaseError

Exception raised for errors that are due to problems with the processed data like division by zero, numeric value out of range, etc.

exception etcdb.exception.DatabaseError[source]

Bases: etcdb.exception.Error

Exception raised for errors that are related to the database.

exception etcdb.exception.Error[source]

Bases: exceptions.Exception

Exception that is the base class of all other error exceptions.

exception etcdb.exception.IntegrityError[source]

Bases: etcdb.exception.DatabaseError

Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails.

exception etcdb.exception.InterfaceError[source]

Bases: etcdb.exception.Error

Exception raised for errors that are related to the database interface rather than the database itself.

exception etcdb.exception.InternalError[source]

Bases: etcdb.exception.DatabaseError

Exception raised when the database encounters an internal error, e.g. the cursor is not valid anymore, the transaction is out of sync, etc.

exception etcdb.exception.NotSupportedError[source]

Bases: etcdb.exception.DatabaseError

Exception raised in case a method or database API was used which is not supported by the database, e.g. requesting a .rollback() on a connection that does not support transaction or has transactions turned off.

exception etcdb.exception.OperationalError[source]

Bases: etcdb.exception.DatabaseError

Exception raised for errors that are related to the database’s operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name is not found, a transaction could not be processed, a memory allocation error occurred during processing, etc.

exception etcdb.exception.ProgrammingError[source]

Bases: etcdb.exception.DatabaseError

Exception raised for programming errors, e.g. table not found or already exists, syntax error in the SQL statement, wrong number of parameters specified, etc.

exception etcdb.exception.Warning[source]

Bases: exceptions.Exception

Exception raised for important warnings like data truncations while inserting, etc.

etcdb.lock module

Locking for etcdb

etcdb implements MyISAM style locking. There is an exclusive writer lock and there are many shared reader locks. A client can acquire a writer lock if there are no readers and no writers. A client can acquire a reader lock if there are no writers.

class etcdb.lock.Lock(etcd_client, db, tbl, **kwargs)[source]

Bases: object

Instantiate Lock instance for a table.

Parameters:
  • etcd_client (Client) – Etcd client
  • db – Database name.
  • tbl – Table name.
acquire(timeout=50, ttl=50, **kwargs)[source]

Get a lock

Parameters:
  • timeout (int) – Timeout to acquire a lock.
  • ttl (int) – Place a lock on this time in seconds. 0 for permanent lock.
  • kwargs

    Keyword arguments.

    • author (str) - Who requests the lock.
      By default, ‘etcdb’.
    • reason (str) - Human readable reason to get the lock.
      By default, ‘etcdb internal operation’.
Raises:
  • InternalError – This class shouldn’t be used directly and if user doesn’t set lock_prefix the method should raise exception.
  • OperationalError – If lock wait timeout expires.
author
Returns:String that identifies who acquired the lock.
Return type:str
created_at
Returns:When the lock was acquired in Unix timestamp.
Return type:int
id

Lock identifier

readers()[source]

Get list of reader locks.

Returns:List of ReadLock() instances
Return type:list(ReadLock)
reason
Returns:String that explains why lock was acquired.
Return type:str
release()[source]

Release a lock

writers()[source]

Get list of writer locks.

Returns:List of WriteLock() instances
Return type:list(WriteLock)
class etcdb.lock.MetaLock(etcd_client, db, tbl)[source]

Bases: etcdb.lock.Lock

Meta lock is needed to place a read or write lock.

class etcdb.lock.ReadLock(etcd_client, db, tbl, lock_id=None)[source]

Bases: etcdb.lock.Lock

Read lock.

acquire(timeout=50, ttl=50, **kwargs)[source]

Get a read lock

class etcdb.lock.WriteLock(etcd_client, db, tbl, lock_id=None)[source]

Bases: etcdb.lock.Lock

Write lock.

acquire(timeout=50, ttl=50, **kwargs)[source]

Get a write lock

etcdb.resultset module

Classes that represent query results

class etcdb.resultset.Column(colname, coltype=None, options=None)[source]

Bases: object

Instantiate a Column

Parameters:
  • colname (str) – Column name.
  • coltype (str) – Column type
  • options (ColumnOptions) – Column options
auto_increment

True if column is auto_incrementing.

default

Column default value.

name

Column name

nullable

True if column is NULL-able.

primary

True if column is primary key.

print_width

How many symbols client has to spare to print column value. A column name can be short, but its values may be longer. To align column and its values print_width is number of characters a client should allocate for the column name so it will be as lager as the largest columns length value.

set_print_width(width)[source]

Sets print_width.

type

Column type e.g. INT, VARCHAR, etc.

unique

True if column is unique key.

class etcdb.resultset.ColumnOptions(*options, **kwoptions)[source]

Bases: object

ColumnOptions represents column options like NULL-able or not

auto_increment = False
default = None
nullable = None
primary = False
unique = None
class etcdb.resultset.ColumnSet(columns=None)[source]

Bases: object

Instantiate a Column set

Parameters:columns (dict) – Optional dictionary with column definitions
add(column)[source]

Add column to ColumnSet

Parameters:column (Column) – Column instance
Returns:Updated CoulmnSet instance
Return type:ColumnSet
columns

Returns list of Columns

empty

True if there are no columns in the ColumnSet

index(column)[source]

Find position of the given column in the set.

next()[source]

Return next Column

primary

Return primary key column

class etcdb.resultset.ResultSet(columns, rows=None)[source]

Bases: object

Represents query result

Parameters:
  • columns (ColumnSet) – Column set instance.
  • rows (list(Row)) – List of Rows
add_row(row)[source]

Add row to result set

Parameters:row (Row) – Row instance
Returns:Updated result set
Return type:ResultSet
Raises:InternalError – if row is not a Row class instance.
n_cols

Return number of columns in the result set.

n_rows

Return number of rows in the result set.

next()[source]

Return next row in the result set.

rewind()[source]

Move internal records pointer to the beginning of the result set. After this call .fetchone() will start returning rows again.

class etcdb.resultset.Row(row, etcd_index=0, modified_index=0)[source]

Bases: object

Row class

Parameters:row (tuple) – Row values
etcd_index

A row in etcdb is a key. Etcd index corresponds to X-Etcd-Index in etcd response header.

Returns:Etcd index.
Return type:int
modified_index

modifiedIndex of a key in etcd

next()[source]

Return next field in the row.

row
Returns:Return tuple with row values..
Return type:tuple

Module contents

PEP-249 implementation for etcd

etcdb.Binary(string)[source]

This function constructs an object capable of holding a binary (long) string value.

etcdb.Date(year, month, day)[source]

This function constructs an object holding a date value.

Parameters:
  • year – Year, e.g. 2016
  • month – Month, e.g. 9
  • day – Day, e.g. 21
Returns:

EtcdDate instance

etcdb.DateFromTicks(ticks)[source]

This function constructs an object holding a time value from the given ticks value (number of seconds since the epoch; see the documentation of the standard Python time module for details).

Parameters:ticks – Seconds since Epoch
Returns:EtcdDate
etcdb.ETCDTABLELOCK

alias of etcdb.EtcdTableLock

etcdb.Time(hour, minute, second)[source]

This function constructs an object holding a time value.

Parameters:
  • hour – Hour, e.g. 15
  • minute – Minute, e.g. 53
  • second – Second, e.g. 16
Returns:

EtcdTime instance

etcdb.TimeFromTicks(ticks)[source]

This function constructs an object holding a time value from the given ticks value (number of seconds since the epoch; see the documentation of the standard Python time module for details).

Parameters:ticks – Seconds since Epoch
Returns:EtcdTime
etcdb.Timestamp(year, month, day, hour, minute, second)[source]

This function constructs an object holding a time stamp value.

Parameters:
  • year – See Date() and Time() arguments
  • month
  • day
  • hour
  • minute
  • second
Returns:

EtcdTimestamp instance

etcdb.TimestampFromTicks(ticks)[source]

This function constructs an object holding a time stamp value from the given ticks value

(number of seconds since the epoch; see the documentation of the standard Python time module for details).

param ticks:Seconds since Epoch
return:EtcTimestamp
etcdb.apilevel = '1.0'

supported DB API level.

etcdb.paramstyle = 'qmark'

the type of parameter marker formatting. Question mark style, e.g. …WHERE name=?.

etcdb.threadsafety = 3

the level of thread safety. Threads may share the module, connections and cursors.