mcsas.datafile package

Module contents

class DataFile(filename, **kwargs)[source]

Bases: object

Base class for handling data files. Can be initialized with a file (name) to read or with a data array.

Test error behaviour >>> from utils import DataFile, getTempFileName, getTempFile, FileNotFound >>> fn = getTempFileName() >>> try: DataFile.checkFilename(fn) … except FileNotFound, e: str(e).find(fn) > 0 True

Prepare test data file >>> fd = getTempFile() >>> l = [‘123 234n’, ‘1,23 43.4rn’, ‘2.3; 34,4n’, … ‘21.2 42 2n’, ‘23,2 3.4 n’] >>> fd.writelines(l) >>> fd.close()

Test data parsing >>> df = DataFile() >>> df.loadFile(fd.name) [(‘123’, ‘234’), (‘1.23’, ‘43.4’), (‘2.3’, ‘34.4’), (‘21.2’, ‘42’, ‘2’), (‘23.2’, ‘3.4’)]

Remove test data finally >>> import os >>> if os.path.isfile(fd.name): … os.remove(fd.name)

classmethod extensions()[source]

Warning

method ‘datafile.DataFile.extensions’ undocumented

fileFilter

classmethod(function) -> method

Convert a function to be a class method.

A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:

class C:
def f(cls, arg1, arg2, …): … f = classmethod(f)

It can be called either on the class (e.g. C.f()) or on an instance (e.g. C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

Class methods are different than C++ or Java static methods. If you want those, see the staticmethod builtin.

filename

Absolute path name of this file.

getDataObj()[source]

Creates and returns the appropriate DataObj instance for this file type.

name

The plain name of the file with path and extension stripped.

readFile(**kwargs)[source]

Gets a proper file name and returns file data. May modify the instance. To be reimplemented.

static sanitizeReadFilename(filename)[source]

Checks provided filename for plausibility and updates LastPath.

classmethod sanitizeWriteFilename(filename)[source]

Checks and sets the file name to write to.

setFilename(filename)[source]

Checks provided filename for plausibility and updates LastPath

write(filename, **kwargs)[source]

Warning

method ‘datafile.DataFile.write’ undocumented

classmethod writeData(filename, data, **kwargs)[source]

Convenience method to write data directly to file.

classmethod writeFile(filename, data, **kwargs)[source]

Gets a proper file name and numpy array and writes it to file. Reimplement this.

class AsciiFile(filename, **kwargs)[source]

Bases: datafile.datafile.DataFile

A generic ascii data file.

classmethod appendFile(filename, data, **kwargs)[source]

like writeFile but appends data to an existing file

classmethod appendHeaderLine(filename, header)[source]

writes a single-line header to a file consisting of a string or tuple of strings to be joined

classmethod formatData(data, **kwargs)[source]

Warning

method ‘datafile.AsciiFile.formatData’ undocumented

classmethod formatRow(row, **kwargs)[source]

Warning

method ‘datafile.AsciiFile.formatRow’ undocumented

classmethod formatValue(value)[source]

Warning

method ‘datafile.AsciiFile.formatValue’ undocumented

newline = '\n'
parseLines(asciiLines, **kwargs)[source]

Parses lines of an ASCII file in order to extract a single array of numbers. Reimplement this in subclasses for different behaviour.

readArray(asciiLines, dataType=<class 'float'>, startLine=0, endLine=None, **kwargs)[source]

Reads a numpy.array from a specified segment (startLine, endLine) of a line buffer given by asciiLines. Stops at lines incompatible to previous lines read due to different number of fields or incompatible data type. Returns the last line successfully parsed and the populated numpy.array.

readFile(**kwargs)[source]

Gets a proper file name and returns file data. May modify the instance. To be reimplemented.

readTuple(fields, dataType=<class 'float'>, **kwargs)[source]

Converts each field to the requested datatype. Raises an error if it is incompatible, the line is skipped in that case.

separator = ' '
valueFormat = '{0: 14.6E}'
classmethod writeFile(filename, data, **kwargs)[source]

Gets a proper file name and numpy array and writes it to file. Reimplement this.

classmethod writeHeaderLine(filename, header)[source]

writes a single-line header to a file consisting of a string or tuple of strings to be joined

class ArrayFile(filename, **kwargs)[source]

Bases: datafile.asciifile.AsciiFile

A data file containing a single array of data, mostly.

classmethod fileFilter()[source]

classmethod(function) -> method

Convert a function to be a class method.

A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:

class C:
def f(cls, arg1, arg2, …): … f = classmethod(f)

It can be called either on the class (e.g. C.f()) or on an instance (e.g. C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

Class methods are different than C++ or Java static methods. If you want those, see the staticmethod builtin.

getDataObj()[source]

Creates and returns the appropriate DataObj instance for this file type.

parseLines(asciiLines, **kwargs)[source]

Parses lines of an ASCII file in order to extract a single array of numbers. Reimplement this in subclasses for different behaviour.

rawArray
class PDHFile(filename, **kwargs)[source]

Bases: datafile.arrayfile.ArrayFile

classmethod fileFilter()[source]

classmethod(function) -> method

Convert a function to be a class method.

A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:

class C:
def f(cls, arg1, arg2, …): … f = classmethod(f)

It can be called either on the class (e.g. C.f()) or on an instance (e.g. C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

Class methods are different than C++ or Java static methods. If you want those, see the staticmethod builtin.

classmethod formatData(data, description=None)[source]

Warning

method ‘datafile.PDHFile.formatData’ undocumented

parseLines(asciiLines, **kwargs)[source]

Parses lines of an ASCII file in order to extract a single array of numbers. Reimplement this in subclasses for different behaviour.

class PDHHeader(dataCount, description=None)[source]

Bases: object

line(index)[source]

Returns the specified line of the header as string.

classmethod maxLines()[source]

Warning

method ‘datafile.PDHHeader.maxLines’ undocumented

getFileFilter()[source]

Returns the file filter text of all available data file formats which can be used with a file selection dialog UI.