ship.datastructures package

Submodules

ship.datastructures.dataobject module

Summary:

Contains the ADataObject class and all of its subclasses.

These classes can be used to hold used by the :class:’<RowDataCollection>’. They provide a range of methods for easily acessing, amending and retrieving values to print to file. Classes include support for:

# Numeric data. # String data. # Symbol data. # Constant data (a tuple of legal values).
Author:
Duncan Runnacles
Created:
01 Apr 2016
Copyright:
Duncan Runnacles 2016

TODO:

Updates:

class ADataRowObject(datatype, format_str, **kwargs)[source]

Bases: object

Abstract class for all data objects used in an AUnit class.

Note

It is unlikley that you want to call a class of this type directly. The RowDataFactory will perform checks required when constructing one of these objects. It should be used instead.

addValue(value=None, index=None)[source]

Adds a value to the data_collection.

This values will either be appended or put at a specific index if one is provided.

Parameters:
  • value – Optional - The value to be added. If no value is provided the default value will be used.
  • index (int) – Optional - The index at which to add the value. If no value is given it will be appended to the end.
Raises:

IndexError – If index does not exist.

checkDefault(value)[source]
deleteValue(index)[source]

Delete value at supplied position in unit.

Parameters:index (int) – index of value to be removed.
Raises:IndexError – If index does not exist.
formatPrintString(value)[source]

Abstract method for formatting the value to be printed.

Formats the value as defined by format_string when the object was instantiated. This should be the format required when printing to the .DAT file.

Note

This class must be overridden by the concrete class with behaviour specific to formatting the particular value type.

Parameters:value – The value to format.
Returns:The formatted value.
getDataCollection()[source]
getPrintableValue(index)[source]

Getter for the formatted printable value at the supplied index.

If the default value given == ‘~’ then all formatting should be removed from the value if it is empty. Otherwise we call the subclass formatPrintString() method.

Parameters:index (int) – The index of the required value.
Returns:
String containing the .DAT file print formatted value as defined
by the format_print_string variables set when the object was initialised.
Raises:IndexError – If the given index doesn’t exist.
getValue(index)[source]

Getter for retrieving the value at the supplied index.

Parameters:index (int) – The index of the required value.
Returns:The value at the supplied index.
record_length
setChangeStatus(status)[source]
setValue(value, index)[source]

Changes the value at the given index

Parameters:
  • value – The new value to set.
  • index (int) – The index of the value to be changed
Raises:

IndexError – If index does not exist.

class ConstantData(datatype, legal_values, format_str='{}', **kwargs)[source]

Bases: ship.datastructures.dataobject.ADataRowObject

Overrides the value return methods from ADataObject to return a str value from a list of predefined constants.

addValue(value=None, index=None)[source]

Adds a value to the collection.

See also

ADataRowObject: addValue()

formatPrintString(value)[source]

Method for formatting the value to be printed in the .DAT file.

Note

Overriddes superclass.

Parameters:value (int) – The value to format.
Returns:String formatted value.
setValue(value, index)[source]

Changes the value at the given index

See also

ADataRowObject: setValue()

class FloatData(datatype, format_str='{}', **kwargs)[source]

Bases: ship.datastructures.dataobject.ADataRowObject

Overrides the value return methods from ADataObject to return a float value instead of a string.

addValue(value=None, index=None)[source]

Adds a value to the collection.

See also

ADataRowObject: addValue()

formatPrintString(value)[source]

Method for formatting the value to be printed in the .DAT file.

Note

Overriddes superclass.

Parameters:value (int) – The value to format.
Returns:String formatted value.
setValue(value, index)[source]

Changes the value at the given index

See also

ADataRowObject: setValue()

class IntData(datatype, format_str='{}', **kwargs)[source]

Bases: ship.datastructures.dataobject.ADataRowObject

Concrete implememtation of the ADataRowObject for integer values.

See also

ADataRowObject

addValue(value=None, index=None)[source]

Adds a value to the collection.

See also

ADataRowObject: addValue()

formatPrintString(value)[source]

Method for formatting the value to be printed in the .DAT file.

Note

Overriddes superclass.

Parameters:value (int) – The value to format.
Returns:String formatted value.
setValue(value, index)[source]

Changes the value at the given index

See also

ADataRowObject: setValue()

class StringData(datatype, format_str='{}', **kwargs)[source]

Bases: ship.datastructures.dataobject.ADataRowObject

Overrides the value return methods from ADataObject to return a str value.

addValue(value=None, index=None)[source]

Adds a value to the collection.

See also

ADataRowObject: addValue()

formatPrintString(value)[source]

Method for formatting the value to be printed in the .DAT file.

Note

Overriddes superclass.

Parameters:value (int) – The value to format.
Returns:String formatted value.
setValue(value, index)[source]

Changes the value at the given index

See also

ADataRowObject: setValue()

class SymbolData(datatype, symbol, format_str='{}', **kwargs)[source]

Bases: ship.datastructures.dataobject.ADataRowObject

Overrides the value return methods from ADataObject to return a float value instead of a string.

addValue(value=None, index=None)[source]

Adds a value to the collection.

See also

ADataRowObject: addValue()

formatPrintString(value)[source]

Method for formatting the value to be printed in the .DAT file.

Note

Overriddes superclass.

Parameters:value (int) – The value to format.
Returns:String formatted value.
setValue(value, index)[source]

Changes the value at the given index

See also

ADataRowObject: setValue()

logger = <logging.Logger object>

logging references with a __name__ set to this module.

ship.datastructures.rowdatacollection module

Summary:
Contains the RowDataCollection object. This is an object used to hold all of the data for a specific row of a unit in an ISIS dat file. It acts a collection pattern to make accessing and updating the contents of a row simpler.
Author:
Duncan Runnacles
Created:
01 Apr 2016
Copyright:
Duncan Runnacles 2016

TODO:

Updates:

class RowDataCollection(**kwargs)[source]

Bases: object

Composite/Facade for the ADataRowObject classes.

AUnit classes should instantiate this class in order to manage all the ADataRowObject classes used to hold the units row data. There are several convenience methods to retrieve and safely update the contents.

Note

There are many references to a ‘key’ variable in this class to decipher which object in the collection to access/update/etc. This is one of the ROW_DATA_TYPES enum values in the datunits package.

See also

ROW_DATA_TYPE - in :class:’datunits <ship.fmp.datunits>’ module.

Todo

Need to find a way to safely add values to the collection while making sure that all the data objects stay in sync (i.e. have the same number of rows), otherwise it will be chaos. At the moment this is tricky because we need to add individual values at the start. Possibly stop client from using any get methods etc unless all the data objects have the same number of rows. For the time being there is a convenience method checkRowsInSync() that can be called to verify that all of the data objects in this collection have the same length.

addRow(row_vals, index=None, **kwargs)[source]

Add a new row to the units data rows.

Creates a new row from the values in the supplied value dict at the location given by the index. If the index is None then the value will be appended to the end of the row rather than inserted.

Note

If there is any problem while adding the new row all datarow objects will be returned to the state they were in before the operation. This ensures that they don’t get out of sync if an error is found halfway through adding the different values. This is done by creating a deep copy of the object prior to updating.

**kwargs:
‘no_copy’(bool): if True the deepcopy of the object will not be
made. This is useful if you are loading a lot of data and don’t want the overhead of deepcopy - like loading a new model. Default is False.
Parameters:
  • row_vals (dict) – Contains the names of the data objects of collection as keys and the new row values as values.
  • index (int) – The index at which to insert the row. If None it will be appended to end of the collection.
Raises:
  • KeyError – If any of the keys don’t exist.
  • IndexError – If the index doesn’t exist.
addToCollection(dataobject, index=None)[source]

Setup a new data object and add it to the collection.

Parameters:
  • obj_type (str) – The type of ADataRowObject to create. This should be a DataTypes enum from the ADataObject module.
  • vars (list) – The variables specific to the ADataRowObject that is going to be instantiated.

See also

ADataRowObject (and subclasses), DataTypes - all in ADataObject module.

classmethod bulkInitCollection(dataobjects, **kwargs)[source]
checkRowsInSync()[source]

Checks that the data objects in the collection are in sync.

All the rows should be the same length. If they aren’t then there’s a problem and it will corrupt any output .dat file.

Warning

It isn’t actually that hard to corrupt the collection at the moment. It’s ok if the DataObject classes are only accessed through here. If they are accessed independently of this class and not carefully checked they could fall out of sync.

Returns:
True if all data collections have the same length, otherwise
False.
collectionTypes()[source]

Get a list of the types (names) of all the objects in the collection.

The list returned will contain all of the names used in this row collection. e.g. ‘chainage’, ‘elevation’ etc.

Returns:keys – containing the names of the data objects.
Return type:list
dataObject(name_key)[source]

Return the ADataRowObject instance requested.

Parameters:name_key (str) – The key to use to retrieve the object (e.g. ‘chainage’). This is usually a class declared constant e.g. RiverUnit.CHAINAGE.
Returns:ADataRowObject or False if the key doesn’t match any in the collection.

Note

Returns a shallow copy of the collection. Any changes to the values will remain within the main list. If you want to be able to change it without affecting the main copy use getDataObjectCopy().

dataObjectAsList(key)[source]

Returns a DataObject as a list.

This will return the row_collection DataObject referenced by the key provided (as a ROW_DATA_TYPES) in list form.

If you intend to update the values you should use getRowDataObject instead as the data provided will be mutable and therefore reflected in the values held by the row_collection. If you just want a quick way to loop through the values in one of the data objects and only intend to read the data then use this.

Parameters:key (str) – the key for the data object requested. It is best to use the class constants (i.e. RiverUnit.CHAINAGE) for this.
Returns:
List containing the data in the DataObject that the key points
to. Returns false if there is no row collection.
Raises:KeyError – If key does not exist.
dataObjectCopy(name_key)[source]

Return the ADataRowObject instance requested.

Same as the getDataObject() method except it makes a deep copy of the data object before returning it so that any changes will local to the returned copy only and not to the main reference.

Parameters:name_key (str) – The key to use to retrieve the object (e.g. ‘chainage’). This is usually a class declared constant e.g. RiverUnit.CHAINAGE.
Returns:ADataRowObject or False if the key doesn’t match any in the collection.
dataValue(key, index)[source]

Get the value in a DataObject at index.

Parameters:
  • key (int) – ROW_DATA_TYPES for the DataObject.
  • index (int) – the row to return the value from.
Returns:

The value in the DataObject at given index.

Raises:
  • KeyError - if key does not exist in collection.
  • IndexError - if index does not exist in DataObject.
deleteDataObject(name_key)[source]

Delete the ADataRowObject instance requested.

Parameters:name_key (str) – The key to use to retrieve the object (i.e. ‘chainage’)
Returns:True if the object was successfully deleted; False if not.
deleteRow(index, **kwargs)[source]

Delete a row from the collection.

**kwargs:
‘no_copy’(bool): if True the deepcopy of the object will not be
made. This is useful if you are loading a lot of data and don’t want the overhead of deepcopy - like deleting the dummy row. Default is False.
Parameters:index (int) – the index to delete the values for.
Raise:
IndexError: if index is out of the bounds of the collection.
getPrintableRow(index)[source]

Get the row data in printable form.

Retrieves all of the values in this RowDataObjectCollection in the order that it exists in the list.

Parameters:index (int) – the row collection index to access.
Returns:string formatted for printing to .DAT file.
indexOfDataObject(key)[source]

Get the index of the DataObject with data_type equal to key.

iterateRows(key=None)[source]

Returns a generator for iterating through the rows in the collection.

If no key is given it will return a list containing all of the values in the row.

Parameters:key=None (int) – ROW_DATA_TYPE to return. If None all values in the row will be returned as a list.
Returns:list if key == None, a single value otherwise.
numberOfRows()[source]

Return the number of rows held in the collection

Returns:int - number of rows in this collection.
rowAsDict(index)[source]

Get the data vals in a particular row by index.

Parameters:index (int) – the index of the row to return.
Returns:dict - containing the values for the requested row.
rowAsList(index)[source]

Get the data vals in a particular row by index.

Parameters:index (int) – the index of the row to return.
Returns:dict - containing the values for the requested row.
row_count
setDummyRow(row_vals)[source]

Sets a special ‘dummy row’ as a placeholder until actual values.

Sometimes it can be useful to have placeholder values in a collection. This is particularly true for FMP units that will cause errors in FMP if there is no data in the rows.

This method will add the dummy row data and set the self.has_dummy flag to True. When actual row data is added to the collection it will check the flag and delete the row if it’s True.

toDict()[source]

Returns the row data object as a dict.

Provides a dict where keys are the datunits.ROW_DATA_TYPES and the values are lists of the values for that type in sequence.

If you intend to update the values you should use getRowDataObject instead as the data provided will be mutable and therefore reflected in the values held by the collection. If you just want to read the data then use this.

Returns:dict - containing lists of values by ROW_DATA_TYPE.
toList()[source]

Returns the row data a list.

Collects the row data in each of the ADataObjects in this collection into a list. Then adds them to a list based on the order of this collection. I.e. each inner list is the data pertaining to a single ADataObject.

Example

[
[0.0, 1.5, 3.0], [32.5, 31.0, 31.5], [0.03, 0.03, 0.03]

]

Returns:
List - containing lists of the data in the DataObjects in this
collection.
Raises:KeyError – If key does not exist.
updateRow(row_vals, index, **kwargs)[source]

Add a new row to the units data rows.

Creates a new row from the values in the supplied value dict at the location given by the index. If the index is None then the value will be appended to the end of the row rather than inserted.

**kwargs:
‘no_copy’(bool): if True the deepcopy of the object will not be
made. This is useful if you are loading a lot of data and don’t want the overhead of deepcopy - like loading a new model. Default is False.

Note

If there is any problem while updating the values in the row all datarow objects will be returned to the state they were in before the operation. This ensures that they don’t get out of sync if an error is found halfway through adding the different values. This is done by creating a deep copy of the object prior to updating.

Parameters:
  • row_vals (dict) – Contains the names of the data objects of collection as keys and the new row values as values.
  • index (int) – The index at which to insert the row.
Raises:
  • KeyError – If any of the keys don’t exist.
  • IndexError – If the index doesn’t exist.

Module contents