cape.attdb.ftypes.basefile: Common ATTDB file type attributes

This module provides the class BaseFile as a subclass of dict that contains methods common to each of the other data-like file readers and writers. It defines common attributes that each other such data-file class will have and provides several other common methods.

For example, it defines a __repr__() method that updates with the name of any other classes subclassed to this one.

Finally, having this common template class provides a single point of entry for testing if an object is based on a product of the cape.attdb.ftypes module. The following Python sample tests if any Python object db is an instance of any class from this data-file collection.

isinstance(db, cape.attdb.ftypes.BaseFile)
class cape.attdb.ftypes.basefile.BaseFile(**kw)

Generic class for storing data from a data-style file

This class has no initialization method, and as such it is unlikely that there will be instances of this class in use. It provides methods and structure to other classes.

This class inherits from dict and can be used in that matter in the unlikely event that it’s useful.

Outputs:
db: cape.attdb.ftypes.csv.CSVFile

CSV file interface

db.cols: list[str]

List of columns read

db.opts: dict

Options for this instance

db.defns: dict

Definitions for each column/coefficient

db[col]: np.ndarray | list

Numeric array or list of strings for each column

See also:
Versions:
  • 2019-11-26 @ddalle: First version

append_colval(col, v)

Save the next value to a column’s array or list

This will update counts and allocate a new chunk if necessary.

Call:
>>> db.init_col(col)
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

col: str

Name of column to which to save value

v: db.get_col_type(col)

Value to save to array/list

Effects:
db[col]: np.ndarray | list

Column’s array with extra new entry

db._n[col]: int

Updated length of array/list

Versions:
  • 2019-11-25 @ddalle: First version

init_col(col)

Initialize column

Call:
>>> db.init_col(col)
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

col: str

Name of column to initialize

Effects:
db[col]: np.ndarray | list

Initialized array with appropriate type

db._n[col]: 0

Number of entries saved to db[col]

db._nmax[col]: None | int

Number of entries allocated, if appropriate

Versions:
init_col_class(col)

Initialize a class-specific column

This is used for special classes and should be overwritten in specific classes if that class has its own "Type" definitions that are not generic.

Call:
>>> db.init_col_class(col)
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

col: str

Name of column to initialize

Versions:
  • 2019-12-03 @ddalle: First version

init_cols(cols)

Initialize list of columns

Call:
>>> db.init_cols(cols)
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

col: str

Name of column to initialize

See Also:
Versions:
  • 2019-11-25 @ddalle: First version

register_attribute(col)

Register a data field as an attribute

For example, if col is "mach", this will create db.mach, which will be a reference to db["mach"].

Call:
>>> db.register_attribute(col)
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

col: str

Name of existing column

Versions:
  • 2019-11-10 @ddalle: First version

translate_colnames(cols)

Translate column names

This method utilizes the options Translators, Prefix, and Suffix from the db.opts dictionary. The Translators are applied before Prefix and Suffix.

Call:
>>> dbcols = db.translate_colnames(cols)
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

cols: list[str]

List of “original” column names, e.g. from file

Outputs:
dbcols: list[str]

List of column names as stored in db

Versions:
  • 2019-12-04 @ddalle: First version

translate_colnames_reverse(dbcols)

Reverse translation of column names

This method utilizes the options Translators, Prefix, and Suffix from the db.opts dictionary.*Prefix* and Suffix removed before reverse translation.

Call:
>>> cols = db.translate_colnames_reverse(dbcols)
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

dbcols: list[str]

List of raw column names as stored in db

Outputs:
cols: list[str]

List of “original” column names, e.g. from file

Versions:
trim_colarray(col)

Trim extra entries from data rows

Call:
>>> db.trim_colarray(col)
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

col: str

Name of column to which to save value

Effects:
db[col]: np.ndarray | list

Trimmed to length db._n[col] if an array

Versions:
  • 2019-11-25 @ddalle: First version

class cape.attdb.ftypes.basefile.BaseFileDefn(_optsdict=None, _warnmode=1, **kw)
class cape.attdb.ftypes.basefile.BaseFileOpts(_optsdict=None, _warnmode=1, **kw)
class cape.attdb.ftypes.basefile.TextInterpreter

Class to contain methods for interpreting text

The class is kept separate from BaseFile because not all file-type interfaces need sophisticated rules for converting text to numeric or other values.

This class provides several methods for inheritance, but the intent is that instances of this class are not useful and should not be used.

Versions:
  • 2019-11-26 @ddalle: First version

  • 2019-12-02 @ddalle: Changed from TextFile

fromtext_base(txt, clsname)

Convert a string to appropriate numeric/string type

Call:
>>> v = db.fromtext_num(txt, clsname)
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

txt: str

Text to be converted to float

clsname: {"float64"} | "int32" | str

Valid data type name

Outputs:
v: clsname

Text translated to requested type

Versions:
  • 2019-11-25 @ddalle: First version

fromtext_complex(txt, clsname=None)

Convert a string to complex float

This conversion allows for the format "2.40D+00 + 1.2I" where I, i, and J are converted to j; and D and d are converted to E if necessary.

Special processing of specific complex subtypes is handled if the clsname keyword is specified. Specific types are handled by valid NumPy classes.

Call:
>>> v = db.fromtext_complex(txt)
>>> v = db.fromtext_complex(txt, clsname="complex128")
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

txt: str

Text to be converted to float

clsname: {"complex128"} | "complex64"

Specific data type

Outputs:
v: float

Converted value

Versions:
  • 2019-11-25 @ddalle: First version

fromtext_float(txt, clsname=None)

Convert a string to float

This conversion allows for the format "2.40D+00" if the built-in float() converter fails. Python expects the exponent character to be E or e, but D and d are allowed here. Other exceptions are not handled.

Special processing of specific float subtypes is handled if the clsname keyword is specified. Specific types are handled by valid NumPy classes.

Call:
>>> v = db.fromtext_float(txt)
>>> v = db.fromtext_float(txt, clsname="float64")
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

txt: str

Text to be converted to float

clsname: {"float64"} | "float32" | "float128"

Specific data type

Outputs:
v: float

Converted value

Versions:
  • 2019-11-25 @ddalle: First version

fromtext_int(txt, clsname=None)

Convert a string to integer

Special processing of specific int and uint subtypes is handled if the clsname keyword is specified. Specific types are handled by valid NumPy classes.

Call:
>>> v = db.fromtext_float(txt)
>>> v = db.fromtext_float(txt, clsname="int32")
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

txt: str

Text to be converted to float

clsname: {"int32"} | "int64" | "uint64"

Specific data type

Outputs:
v: float

Converted value

Versions:
  • 2019-11-25 @ddalle: First version

fromtext_val(txt, clsname)

Convert a string to appropriate type

Call:
>>> v = db.fromtext_val(txt, clsname)
Inputs:
db: cape.attdb.ftypes.basefile.BaseFile

Data file interface

txt: str

Text to be converted to float

clsname: {"float64"} | "int32" | str

Valid data type name

Outputs:
v: clsname

Text translated to requested type

Versions:
  • 2019-11-25 @ddalle: First version