pyCDB reference

pyCDB.client

COMPASS database main module

Contains functions to connect, retrieve and store data using the COMPASS database.

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

Fundamental connector class for CDB

CDB2FS_ref(computer_id, board_id, channel_id)

Return the CDB DAQ_channel reference of a CDB channel

FS2CDB_ref(nodeuniqueid, hardwareuniqueid, parameteruniqueid)

Return the CDB DAQ_channel reference of a FireSignal channel

add_unit(unit_system_id, unit_name, auto_dim=False, **dimensions)

Add unit to unit system.

Parameters:auto_dim – if true, dimensions are inferred automatically.
Returns:unit_id of the unit
Return type:int
apply_unit_factor_tree(signal, unit_tree)

Apply unit conversion of signal data tree.

Parameters:
  • signal – a CDBSignal object with dependent axes and fully read data.
  • unit_tree – a tree of units to convert to as returned by get_unit_factor_tree.
create_generic_signal(generic_signal_name=None, alias=None, first_record_number=1, last_record_number=-1, data_source_id=None, time_axis_id=None, axis1_id=None, axis2_id=None, axis3_id=None, axis4_id=None, axis5_id=None, axis6_id=None, units=None, description=None, signal_type='FILE', **kwargs)

Creates a generic signal in database.

Returns the id of the new generic signal.

create_record(record_type='EXP', record_time=None, description='', data_root=None, FS_event_number=0, FS_event_id='', record_number=None, autonumber=False)

Creates a new record in the database

Parameters:
  • record_type – ‘EXP’, ‘VOID’ or ‘MODEL’
  • record_time – default current, can be integer time stamp or a datetime.datetime obejct
  • description – record description
  • data_root – CDB data root path, see get_data_root_path()
  • FS_event_number – FireSignal event number (optional) # TODO: Remove?
  • FS_event_id – FireSignal event id (optional) # TODO: Remove?
  • record_number – new record number (None for first available)
  • autonumber – automatic numbering (effective only if record_number is not None)
Return type:

new record number

delete_signal(str_id='', **kwargs)

Delete a signal

Parameters:
  • str_id – string id of the signal
  • kwargs – alternative signal id (signal_reference)

kwargs - fields from data signal reference:

Parameters:
  • timestamp
  • note
find_generic_signals(alias_or_name='', regexp=False)

Find generic signals by a name or alias

Parameters:
  • alias_or_name – search string
  • regexp – use regular expressions (MySQL REGEXP)
Return type:

tuple of signal references (like get_generic_signal_references)

The search criterion in MySQL is “SELECT ... WHERE generic_signal_name LIKE %%alias_or_name%% OR alias LIKE %%alias_or_name%%”

get_FS_signal_reference(nodeuniqueid, hardwareuniqueid, parameteruniqueid)

Get the currently attached generic signal reference for a FireSignal channel.

Returns None if not found.

get_attachment_table(timestamp=None, computer_id=None, board_id=None, channel_id=None, generic_signal_id=None, record_number=None)

Returns tuple of rows from a “view” corresponding to channel attachments based on specified criteria.

Parameters:
  • timestamp – reference time (default current)
  • computer_id – DAQ computer id
  • board_id – DAQ board id
  • channel_id – DAQ channel id
  • generic_signal_id – attached generic signal id
  • record_number – timestamp of this record number will be used
get_channel_references(computer_name=None, computer_id=None, board_id=None, channel_id=None)

Get DAQ channel reference(s)

get_coefficient_V2unit(generic_signal_id)

Get coefficient_V2unit (needed for FireSignal operation)

get_coefficient_lev2V(generic_signal_id)

Get coefficient_lev2V (needed for FireSignal operation)

get_computer_id(computer_name=None, description=None)

Returns computer ID by computer name or description

get_data_file_reference(**kwargs)

Returns a tuple of dictionaries containing complete file references containing the signal. Returns one reference for each file revision.

Typical parameters: data_file_id: unique id generic_signal_name, data_source_id (name), data_source_id, generic_signal_id record_number

If data_source_id is not specified the signal is first sought in generic signal names.

get_data_root_path(data_root=None)

Return the CDB data root path

Parameters:data_root – user specified root directory, if None given by CDB_DATA_ROOT
Return type:the full path with expanded variables
get_data_source_id(data_source_name)

Returns data source id of a given data source name

get_data_source_references(**kwargs)

Returns a tuple with references to data sources

Typical parameters: data_source_name, data_source_id, description, record_number

get_data_source_subdir(data_source_id)

Returns the data storage subdirectory for a given data source id

get_generic_signal_id(generic_signal_name, data_source_id=None)

Returns generic signal id given the signal name

get_generic_signal_references(str_id='', **kwargs)

Returns a tuple of references to generic signal, based on specified criteria

Parameters:str_id – generic signal string id - see see decode_generic_signal_strid()

Keyword parameters: generic_signal_id (or signal_id): id of the generic signal generic_signal_ref or generic_signal_reference generic_signal_name + (optionally) data_source_id or data_source_name + record_number (for validity) alias_or_name: first find by alias; if no match find by name generic_signal_strid: deprecated - use str_id

classmethod get_max_revisions(signals, deleted=False)

From a set of signals, take only those that have maximum revision number for a combination of (record_number, generic_signal_id).

Parameters:deleted – If true, keep deleted, otherwise filter them out.
get_record_datetime(**kwargs)

Return record datetime (timestamp)

kwargs: :param record_number: record number

get_record_path(**kwargs)

Returns data storage path (from the database) for a given record number

get_signal(str_id=None, squeeze=True, deleted=False, dtype=None, **kwargs)

Get signal information and data.

Parameters:
  • str_id – signal string id, see decode_signal_strid
  • squeeze – remove singleton dimensions in the received data
  • deleted – return or not deleted signals (this will not work anyway ...)
  • dtype – force a certain numpy data type

Keyword parameters: :param units: raw / dav / default (case insensitive, conflict with str_id) :param signal_ref: signal reference :param generic_signal_ref: generic signal reference :param n_samples: number of samples :param time_limit: time limit (instead of n_samples) :param slice_ standard Python slice :rtype: CDBSignal instance

If str_id argument is not present, keyword arguments are used in the search.

get_signal_base_tree(signal_ref)

Create a reference tree of dependent signals.

Parameters:signal_ref – A valid signal reference

It reads parameters from the database and sets some signal properties as well.

get_signal_calibration(str_id=None, sig_ref=None, units=None, gs_ref=None)

Calculate signal offset and coefficient based on signal reference

Parameters:
  • str_id – signal string id
  • sig_ref – signal reference
  • units – signal units, None uses default

The calibration transformations is: (data + offset) * coefficient

If str_id is input, all the references are fetched from the database. Note: Applies only to FILE signals, LINEAR cannot be transformed this way

get_signal_data(signal_ref=None, generic_signal_ref=None, signal=None, squeeze=True, raw=False, **kwargs)

Get signal data.

Parameters:squeze – Reduce N... x 1 x...M data_sets into N... x ...M (numpy squeeze function).

For FILE data signals, it returns the data without any modifications (apart from squeeze or slicing). For LINEAR data signals, it returns: - if n_samples is set: an array of linear values - else: a linear function

kwargs:

Parameters:x0 – if set, it is used instead of signal_ref[“offset”]

Parameters: time_limit, n_samples x0,x1 - axis limits raw - if True, data won’t be scaled

get_signal_data_tree(signal, units='default', n_samples=None, squeeze=True, x0=None, slice_=Ellipsis, is_this_axis=False, dtype=None)

Recursively read data for signal and all its axes.

Parameters:
  • signal – the signal object to set data to.
  • x0 – additive constant
get_signal_parameters(str_id='', parse_json=True, **kwargs)

Get data signal parameters from data_signal_parameters table

Parameters:
  • str_id – string id
  • kwargs – keyword arguments for signal identification
  • parse_json – if True, the method tries to parse fields as JSON.
get_signal_references(str_id='', deleted=False, limit=10000, dereference=False, **kwargs)

Get signal references by specified criteria

Parameters:
  • str_id – signal string id (see decode_signal_strid())
  • deleted – return or not deleted references for revision=-1
  • limit – limit the number of results
  • dereference – return explicit gs_ref, data_file_ref and data_source_ref

keyword arguments offer an alternative to str_id: :param record_number: record number :param revision: revision :param signal_ref: signal reference :param signal_reference: the same as signal_ref

get_signal_setup(gs_str_id, record_number=None, timestamp=None)

Get signal setup for a generic signal

Parameters:gs_str_id – generic signal alias, name or numeric id
get_unit(**kwargs)

Get a unit.

kwargs:

Parameters:
  • unit_id – Direct ID of the unit.
  • (or unit_system_id or unit_system_name) (unit_system) – The unit system to use.
  • construct – whether to construct unit from basic units (default: True)
For unit system, specify any of the following keyword arguments:
m, kg, s, A, K, mol, cd (as dimensionality)

You can specify either unit_id OR unit_system (unit_system_id/unit_system_name) and dimensionality.

get_unit_factor_tree(signal, units=None, unit_system=None)

Get tree of factors and unit names for a signal object.

Parameters:
  • signal – CDBSignal + its axes signals in a tree
  • units – name of the unit or unit system to convert to
  • unit_system – unit system to convert to (takes precedence)

Works recursively but only unit system is passed along the recursion. Example return value: { ‘units’: ‘mm’, ‘factor’ : 0.001}

get_unit_system(unit_system_name=None, unit_system_id=None, load_parent=True, **ignored_args)

Get a unit system.

Parameters:
  • unit_system_name – Name of the system.
  • unit_system_id – The numerical ID of the system.
  • load_parent – if true, automatically load parent unit systems.

You have to specify either the system name (takes precedence) or unit_system_id.

is_file_ready(data_file_id)

Set file_ready to True

last_record_number(record_type='EXP')

Returns the “record number” of the last available record

Parameters:record_type – record type: EXP, VOID, MODEL or empty string (any type)
Return type:record number (integer)
last_shot_number()

The same as last_record_number for record_type = ‘EXP’

Return type:record number (integer)
new_data_file(collection_name, file_format='HDF5', file_extension=None, create_subdir=None, data_root=None, **kwargs)

Creates a record in the COMPASS database for a new data file. Returns the created data file reference.

Parameters:
  • collection_name – signal collection name (used for file name)
  • create_subdir – automatically create the file subdirectory (default if data_root is not set)
  • data_root – use user-supplied data_root instead of the default one
  • kwargs – keyword parameters * record_number: resolve_record_number * data_source_id, data_source_ref, or data_source (name)

First looks for a record with the input record number, data source and collection. If found, a new revision is created. Otherwise a new id with revision 1 is created.

put_signal(gs_alias_or_id, record_number, data, variant='', time0=None, data_coef=None, data_offset=None, data_coef_V2unit=None, note=None, no_debug=None, data_quality='UNKNOWN', time_axis_data=None, time_axis_coef=None, time_axis_offset=None, time_axis_note=None, time_axis_id=None, axis1_data=None, axis1_coef=None, axis1_offset=None, axis1_note=None, axis2_data=None, axis2_coef=None, axis2_offset=None, axis2_note=None, axis3_data=None, axis3_coef=None, axis3_offset=None, axis3_note=None, axis4_data=None, axis4_coef=None, axis4_offset=None, axis4_note=None, axis5_data=None, axis5_coef=None, axis5_offset=None, axis5_note=None, axis6_data=None, axis6_coef=None, axis6_offset=None, axis6_note=None, gs_parameters=None, daq_parameters=None)

Write CDB signal to HDF5 and database

cdb_signal = cdb_put_signal(gs_alias_or_id, record_number, data, ...) writes signal data into CDB, including HDF5 file.

Parameters:
  • gs_parameters (JSON string or dict) – generic signal parameters
  • daq_parameters (JSON string or dict) – data acquistion channel parameters
record_exists(record_number=None, **kwargs)

Returns True if a record exists, False otherwise.

Parameters:kwargs – named parameters interpretable by resolve_record_number (see)
Return type:bool
resolve_record_number(argv)

Resolves record number from input parameters dictionary argv

Parameters:argv – dictionary with parameters
Return type:record number

Recognized argv keys:

  • record_number (-1 returns last record number)
  • record_type
set_file_ready(data_file_id, chmod_ro=True)

Set file_ready to True

Parameters:
  • data_file_id – data file id
  • chmod_ro – make the file read-only (strongly recommended)
classmethod signal_dim(signal_ref)

Signal dimensionality from signal dict reference

If the signal has no axes, it is assumed to be an axis and expected to have dimension 1. Otherwise the expected dimension is equal to the number of signal axes.

store_channel_data_as_signal(computer_id, board_id, channel_id, generic_signal_id, record_numbers, time_axis_id=None, time0=None, offset=None, coefficient=None, coefficient_V2unit=None, delete=True, note=None, enable_deleted=False, restore_default=True)

Store data signal(s) from a particular DAQ channel as a given generic signal

Parameters:
  • computer_id – numeric id or computer name
  • board_id
  • channel_id
  • generic_signal_id
  • record_numbers – list (or any iterable) of record numbers (or a single record number)
  • time_axis_id – time axis (generic signal) id, latest revision will be used
  • time0 – time0, None keeps the last value
  • offset – set offset (in raw levels), None keeps the last value
  • coefficient – DAQ levels to Volts coefficient, None keeps the last value
  • coefficient_V2unit – DAQ Volts to units coefficient, None keeps the last value
  • delete – delete the previous data signal
  • enable_deleted – do it even if there is no generic signal that is not deleted.
  • restore_default – if another data signal belongs to the g.s., it is restored to default g.s.
store_signal(generic_signal_id, **kwargs)

Insert new signal into SQL database. Does not create/write to the data file!

Parameters:generic_signal_id – generic signal id

kwargs - fields from data signal reference:

Parameters:
  • record_number – must be specified
  • data_file_id
  • data_file_key
  • time0
  • coefficient
  • offset
  • coefficient_V2unit
  • variant
  • time_axis_id
  • time_axis_revision
  • axis1_revision
  • axis2_revision
  • axis3_revision
  • axis4_revision
  • axis5_revision
  • axis6_revision
  • time_axis_variant
  • axis1_variant
  • axis2_variant
  • axis3_variant
  • axis4_variant
  • axis5_variant
  • axis6_variant
  • note – text note (new revision reason etc.)
  • computer_id – specify originating A/D
  • computer_name – alternative to computer_id
  • board_id
  • channel_id
  • data_quality – enum(‘UNKNOWN’, ‘POOR’, ‘GOOD’, ‘VALIDATED’)
  • deleted
  • daq_parameters – DAQ parameters (setup)
  • gs_parameters – generic signal parameters (setup)
  • ignore_if_exists – Will not write if such signal exists (useful when multiple sources want to write it at the same time)

Attachment information (computer, board & channel) is automatically filled in if not specified (using get_attachment_table method).

update_signal(str_id='', sig_ref=None, **kwargs)

Create a new revision of a signal with some properties changed.

Use ether str_id or sig_ref as signal identifier.

Parameters:
  • str_id – signal string id
  • sig_ref – signal reference (dict)
  • kwargs – new values of the fields
class pyCDB.client.CDBSignal(data=None, name='', units='', axes=None, description='', ref=None, gs_ref=None, file_ref=None, daq_attachment=None)

CDB signal class, contains description and data

get_axes_info(remove_empty_dim=True)

Return list of dicts describing each viable axis

get_log_level()

Get logging level

info()

Information string about the signal

log_level

Get logging level

plot(fig=None, subplot=111, mplbackend=None, down_sample=1, start_time=-1, stop_time=-1, y_start=-1, y_stop=-1, xmin=None, xmax=None, color_set=None, plot_kwargs={}, contour=True, colormap=True, **kwargs)

Plots the signal

Parameters:
  • fig – existing figure to add plot to
  • subplot – subplot to plot to
  • mplbackend – matplotlib backend to use (None - use default), use ‘Agg’ for no screen output
  • color_set – Color set of the figure, currently supported None (default) and logbook
  • plot_kwargs – dictionary with keyword arguments for the matplotlib plotting command
  • (only 2D) (colormap) – show contour map
  • (only 2D) – show color map
set_log_level(value)

Set logging level

url

WebCDB URL.

pyCDB.client.apply_calibration(data, calibration)

Apply calibration to RAW data.

Parameters:
  • data – numpy array (or anything that can be multiplied and added)
  • calibration – dictionary having keys [offset, coefficient] (signal_ref can be used)
Equation:
( data + offset ) * coefficient
pyCDB.client.decode_generic_signal_strid(generic_signal_strid)

Decode generic signal string reference and return a dictionary

Parameters:generic_signal_strid – is defined as ‘alias or generic_signal_name’ or ‘generic_signal_name/data_source_name’ or ‘generic_signal_name/data_source_id’
Return type:dictionary containing ‘alias_or_name’ or (‘generic_signal_name’ and (‘data_source_id’ or ‘data_source_name’))
pyCDB.client.decode_signal_strid(str_id)

Decode signal string id

Parameters:str_id – string identifier, defined as id_type:identification[units_or_slice][units_or_slice]

units_or_slice can be either units or slice specification. If two brackets are supplied, the second section in brackets has to differ from the first one. id_type := CDB | FS | DAQ

identification := (based on id_type)
  • CDB -> generic_signal_strid:record_number:variant:revision
  • FS -> nodeuniqueid/hardwareuniqueid/parameteruniqueid:record_number:variant:revision
  • DAQ -> computer_id/board_id/channel_id:record_number:variant:revision * if computer_id is a string (contains non-numeric characters), it’s understood as computer_name * alternatively, computer_id and channel_id can be prefixed by “board_” and “channel_” * in DAQ and FS, you can use ”.” instead of “/”
special units:
  • [RAW] or [raw] - get raw signal
  • [DAV] - signal in Data Acquistion Volts (applied get_coefficient_lev2V)
  • [] or [DEFAULT] or [default] - default units
  • [unit_name] or [unit_system_name] - conversion to unit stored in the database
certain parts can be omitted, the defaults are:
  • id_type = CDB
  • revision = -1
  • variant = ‘’
  • record_number = -1
  • default units

i.e., “ne/thomson:100” == “CDB:ne/thomson:100:-1”

pyCDB.client.format_record_path(record_number, record_type)

Return data path for a given record number

Return type:string with the subdirectory name
pyCDB.client.get_h5_dataset(file_path='', file_key='', file_format='HDF5', slice_=Ellipsis)

Get the HDF5 dataset.

pyCDB.client.get_h5_dataset_shape(file_path='', file_key='', file_format='HDF5')

Get the HDF5 dataset shape.

pyCDB.client.read_file_data(file_path='', file_key='', file_format='HDF5', slice_=Ellipsis)

Read data from a file

Parameters:
  • file_path – file path of the file
  • file_key – data location in the file (groupA/groupB/.../dataset for HDF5)
  • file_format – data format of the file
  • file_handle – file handle if file is already open
  • slice – slice specification
pyCDB.client.validate_variant(variant)

Check that the supplied variant follows the rules.

pyCDB.DAQClient

@package DAQClient COMPASS Data AcQuisition module

Contains class for DAQ management.

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

CDB data acquisition class - extends CDBClient

attach_channel(pc_id, board_id, channel_id, generic_signal_id, offset=None, coefficient_lev2V=None, coefficient_V2unit=None, uid=None, note='', parameters=None, timestamp=None, force_detach=False, **kwargs)

Attach a channel to a generic signal (specified by id)

Parameters that are not specified (default values None) will be inherited from previous channel attachment record.

parameters is a JSON text that describes channel parameters

attached_channel_reference(pc_id, board_id, channel_id)

Checks whether a physical channel is attached

change_calibration(generic_signal_id=None, computer_id=None, board_id=None, channel_id=None, **kwargs)

Shortcut method for changing the calibration of channel / generic signal)

Specify channel / generic signal by either choice (overspecification will be rejected): 1) generic_signal_id 2) computer_id, board_id, channel_id

Arguments that are specified, will be changed, other won’t.

create_DAQ_channels(nodeuniqueid, hardwareuniqueid, parameteruniqueid, data_source_id=None, note='', time_axis_id=None, axis1_id=None, axis2_id=None, axis3_id=None, axis4_id=None, axis5_id=None, axis6_id=None, **kwargs)

Creates a new DAQ channel.

It enables automatic creation of new channels and boards (if requested). New computers cannot be created automatically. Use create_computer method in such case.

Parameters:data_source_id – Not required only if you specify generic_signal_id

Keyword arguments:

Parameters:
  • default_generic_signal_id – If set, it is used as default generic signal instead of creating a new one.
  • computer_id – Valid computer id that will be used if there is no channel for the computer yet.
  • board_id – Board id for a non-existent board (if found, stored board_id is always used). If set to -1, the number will be evaluated automatically. If not set, exception is thrown. Please note that two hardwareuniqueid’s can be used as one board (although not recommended).
  • channel_id – If set, it will be used. Otherwise (or value of -1), a new one will be created.
  • coefficient_lev2V – coefficient_lev2V for the default channel attachment.
  • offset – offset for the default channel attachment.
create_computer(computer_name, description=None, location=None)

Create new computer in the database.

detach_channel(pc_id, board_id, channel_id, timestamp=None)

Detach channel in the channel setup

find_daq_signals(record_number=-1)

Find all data signals that originate in any DAQ channel.

find_duplicate_signals(*record_numbers)

Find duplicate signals from DAQ channels in a set of records.

Example: cdb.find_duplicate_signals(6228, 6229)

Parameters:*record_numbers

two or more record numbers

get_FS_attachement(nodeuniqueid, hardwareuniqueid, parameteruniqueid)

Return the currently attached generic signal reference and the corresponding line from the channel_attachments view for a FireSignal channel

get_board_id(description='', description2='')

Returns board ID by computer and board name

get_record_number_from_FS_event(event_number, event_id='0x0000')

Return record number for a specific FireSignal event.

Event_number:Number of the FS event.
Event_id:string with the ID of the event. Default is “0x0000”, shot event.
is_channel_attached(pc_id, board_id, channel_id)

Checks whether a physical channel is attached

is_generic_signal_attached(generic_signal_id)

Checks whether a generic signal is attached to a physical channel

nodiff_signal_data(signal1, signal2)

Compare data of two signals. Returns True for identical signals.

Parameters:
  • signal1 – first signal str_id or reference
  • signal2 – second signal str_id or reference
signal_setup(generic_signal_id, parameters='', timestamp=None, uid=None)

Write generic signal parameters (setup) into signal_setup

Parameters:
  • generic_signal_id – numerical generic_signal_id
  • parameters – signal parameters (typically JSON/YAML formatted text)
  • timestamp – time, default is now
  • uid – user id
switch_channels(pc_id_1, board_id_1, channel_id_1, pc_id_2, board_id_2, channel_id_2, uid=None, note='', timestamp=None)

Switch 2 channels attached generic signals

Keeps the offset and coefficient from the original channel, i.e., the offset and the coefficient are switched as well.

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)

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

pyCDB.CodeGeneration.generate_pxi(filename='cyCDB_api.pxi', conn=None)

Generates .pxi file with C-api definitions

pyCDB.CodeGeneration.get_table_struct(table_name, conn=None)

Reads table structure and returns a dict containing row_name : type