pyCDB reference

pyCDB.client

pyCDB.DAQClient

pyCDB.pyCDBBase

Created on Jun 13, 2012

@author: jurban

exception pyCDB.pyCDBBase.CDBException

A generic CDB exception class

class pyCDB.pyCDBBase.OrderedDict(*args, **kwargs)

Customized ordered dictionary

diff(other, mode='norm')

Find differences to another OrderedDict, ignoring the keys order

Parameters:other – OrderedDict object to compare to
classmethod load_h5(filename)

Deserialize from an HDF5 file (created by save_h5)

Parameters:filename – input file name
save_h5(filename, mode='w')

Serialize to an HDF5 file

Parameters:
  • filename – output file name
  • mode – file open mode, typically ‘w’ or ‘a’
pyCDB.pyCDBBase.cdb_base_check(func)

Basic checking (database connection in particular) before calling CDB functions.

To be used as decorator for client methods. Raises an exception if problem occurs.

pyCDB.pyCDBBase.isintlike(obj)

Is object int-like?

In Python 2, this means int or long. In Python 3, this means just int.

pyCDB.pyCDBBase.isstringlike(obj)

Is object string-like?

In Python 2, this means string or unicode. In Python 3, this means just string.

class pyCDB.pyCDBBase.pyCDBBase(host=None, user=None, passwd=None, db=None, port=3306, log_level=None, data_root=None)

PyCDB base class for other pyCDB classes

check_connection(reconnect=True)

Checks if connection is open, optionally (re)connects.

close()

Close all connections (SQL)

cursor

cursor from self.db

db

Connection to the database.

  • each thread has its local connection
  • connection is automatically opened
delete(table_name, fields)

Delete a row from the table.

Parameters:
  • table_name – name of table to insert to
  • fields – dictionary of fields identifying the row

Deletes at most one row: First, it checks for its existence in the table. If more than one row is found, raises exception. Returns True is something was deleted, False if not.

error(exception)

Log an error and throw the exception

Parameters:exception – can be an exception or string with the message.
file_log_level

Get logging level

classmethod filter_sql_str(sql_str, extra_chars='_', quiet=False)

Filter a string intended to be stored to SQL

Parameters:sql_str – input string

Keep only white listed characters: letters, digits, _.

classmethod format_file_name(collection_name, revision, file_format='HDF5', file_extension=None)

Returns generic file name

classmethod get_conf_value(var_name, section='database', required=False)

Get configuration value for a given variable name

First looks for environment variables, then to ./.CDBrc, then to ~/.CDBrc

Parameters:
  • var_name – one of CDB_HOST, CDB_USER, CDB_PASSWD, CDB_DB
  • required – if True, exception is raised telling the user that he/she has to set the option.
Return type:

variable value

get_file_log_level()

Get logging level

get_log_level()

Get logging level

get_table_struct(table_name)

Get a table structure (as a dict)

insert(table_name, fields, return_inserted_id=False, check_structure=True)

Insert a new row to a table.

Parameters:
  • table_name – name of table to insert to
  • fields – dictionary of ( field name, value ) to insert
  • return_inserted_id – if true, returns the id of the inserted row
  • check_structure – if true, checks, whether fields agree with table layout,

Values are processed to be DB-friendly by mysql_values (see).

classmethod is_json(text)

Checks whether text has a valid JSON syntax

Parameters:text – text (string) to check
log_level

Get logging level

classmethod makedirs(path[, mode=2047])

Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. This is recursive.

classmethod mysql_str(val, float_format='%.17g', datetime_format='%Y-%m-%d %H:%M:%S', quote=True)

Convert Python value into MySQL string and quote if necessary

Use as the first character of MySQL builtin functions to avoid quoting

Lists and tuples are converted into parentheses-wrapped lists (using recursion) to work with IN operator.

classmethod mysql_values(val_list, float_format='%.17g', datetime_format='%Y-%m-%d %H:%M:%S', quote=True)

Construct whole VALUES MySQL INSERT construct for a list of Python values

query(sql, error_if_empty=False, warn_if_empty=False, error_if_multiple=False)

Execute SQL query and returns a list of rows as dictionaries.

Parameters:
  • sql – valid SQL query
  • error_if_empty – raises exception if the result set is empty
  • warn_if_empty – issues warning if the result set is empty but successfully returns it
  • error_if_multiple – the result has to include at maximum 1 row
classmethod row_as_dict(row, description)

Convert SQL row tuple to python dict

set_file_log_level(value)

Set logging level

set_log_level(value)

Set logging level

classmethod sql_to_python_type(sql_type_str)

Transforms MySQL type definition to Python type and its length

update(table_name, id_field, id_value, fields)

Update a row in a table.

Parameters:
  • table_name – name of table to insert to
  • id_field – name of the unique key field
  • id_value – value of the unique key
  • fields – dictionary of ( field name, value ) to insert

pyCDB.logbook

Created on Jun 13, 2012

@author: jurban

class pyCDB.logbook.logbook(host=None, user=None, passwd=None, db=None, port=3306, log_level=None)

logbook client

campaign_name(cid)

Translates cid to the campaign name

get_shot_comments(shot_number)

Get shot comments

get_shot_info(shot_number)

Get shot info

get_shot_linear_profile_value(shot_number, prof_name)

Get a profile as an array

get_shot_linear_profiles(shot_number)

Get all shot parameters (in a dictionary)

get_shot_param_value(shot_number, param_name)

Get the parameter’s value

get_shot_params(shot_number)

Get all shot parameters (in a dictionary)

get_tags(shot_number)

Get shot tags

has_tag(shot_number, tag)

Check for a tag

shot_param_name(param_id)

Translate param_id to param name

uid_name(uid)

Translates uid to (LDAP) name

pyCDB.CodeGeneration