mcsas.datafile package¶
Submodules¶
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)
-
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.
-
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.
-
-
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
-
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.
-
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
¶
-
classmethod
-
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