diff --git a/src/icartt/dataset.py b/src/icartt/dataset.py index c318109079baeba9e6cf1077e0c43c083cc38314..b9e105984a16790eba667c48d229d1a000fc8d24 100644 --- a/src/icartt/dataset.py +++ b/src/icartt/dataset.py @@ -12,14 +12,14 @@ DEFAULT_NUM_FORMAT = "%f" DEFAULT_FIELD_DELIM = ", " -class Formats(IntEnum): - """File Format Indicators (FFI)""" +class Formats(IntEnum): # TODO: FormatIndex + """File Format Indicators (FFI)""" # TODO: File Format Indices FFI1001 = 1001 FFI2110 = 2110 -class VariableType(Enum): +class VariableType(Enum): # TODO: could use IntEnum here as well IndependentVariable = 1 IndependentBoundedVariable = 2 AuxiliaryVariable = 3 @@ -27,6 +27,7 @@ class VariableType(Enum): def sanitize(val, miss): + # TODO: should not use == for float comparison instead, use np.isclose return float(val) if not float(val) == float(miss) else np.NaN @@ -70,7 +71,7 @@ class DataStore1001: if key in self.varnames: newline[key] = sanitize(kwargs[key], self.missvals[key]) - if type(self.data) is type(None): + if type(self.data) is type(None): # TODO: None is the sole instance of None ;-) => can use "if self.data is None" self.data = newline self.data = self.data.reshape(1) # don't even ask else: @@ -162,7 +163,7 @@ class DataStore2110(collections.UserDict): ivarvalue = sanitize(kwargs[self.ivarname], self.missvals[self.ivarname]) # this is an AUX line - if any([x in self.auxvarnames for x in kwargs.keys()]): + if any([x in self.auxvarnames for x in kwargs.keys()]): # TODO: any (all as well) do not require a comprehension; pure generator expression works fine # and we create the whole dataset if needed if not ivarvalue in self.data.keys(): self.data[ivarvalue] = { @@ -172,7 +173,7 @@ class DataStore2110(collections.UserDict): self.data[ivarvalue]["AUX"].add(**kwargs) # this is a DEP line - if any([x in self.dvarnames for x in kwargs.keys()]): + if any([x in self.dvarnames for x in kwargs.keys()]): # TODO: see above: remove [ ] if not self.ibvarname in kwargs.keys(): raise Exception("Need independent (bounded) variable data.") @@ -199,7 +200,7 @@ class KeywordComment: self.data.append(data) def __str__(self): - d = "\n".join(self.data) if not self.data is [] else "N/A" + d = "\n".join(self.data) if not self.data is [] else "N/A" # TODO: use implicit boolean instead of "is []" return self.key + ": " + d @@ -250,7 +251,7 @@ class StandardNormalComments(collections.UserList): for key in self.keywords: if self.keywords[key].data == []: warnings.warn( - "Normal comments: required keyword {:s} is missing.".format(key) + "Normal comments: required keyword {:s} is missing.".format(key) # TODO: in genaral: use f-strings ) def __init__(self): @@ -362,7 +363,9 @@ class Variable: self.scale = scale self.miss = miss - def __repr__(self): + # TODO: should this also get a __str__ ? + + def __repr__(self): # TODO: this could be more meaningful? return "ICARTT Variable description" @@ -382,6 +385,8 @@ class Dataset: :param format: """ + # TODO: should this also get a __str__ and a __repr__ ? + @property def nHeader(self): """Header line count @@ -428,10 +433,10 @@ class Dataset: :return: dictionary of all variables :rtype: dict of Variable(s) """ - vars = {} - if not self.independentVariable is None: + vars = {} # TODO rename - "vars" is a Python built-in + if not self.independentVariable is None: # TODO use "is not None" for readbility vars[self.independentVariable.shortname] = self.independentVariable - if not self.independentBoundedVariable is None: + if not self.independentBoundedVariable is None: # TODO use "is not None" for readbility vars[ self.independentBoundedVariable.shortname ] = self.independentBoundedVariable @@ -459,6 +464,8 @@ class Dataset: if self.inputFhandle.closed: self.inputFhandle = open(self.inputFhandle.name, encoding='utf-8') + # TODO: refactor following try/except statement + # this one is challenging since we need an "unpopulated" file pointer if no file was specified :) try: f = FilehandleWithLinecounter(self.inputFhandle, splitChar) @@ -476,6 +483,8 @@ class Dataset: dmp = f.readline() nHeaderSuggested = int(dmp[0]) + + # TODO: refactor following try/except statement try: self.format = Formats(int(dmp[1])) except: @@ -590,7 +599,7 @@ class Dataset: vstandardname = [standardname] vlongname = [longname] - for i in range(1, nvar): + for _ in range(1, nvar): dmp = f.readline() shortname, units, standardname, longname = extractVardesc(dmp) vshortname += [shortname] @@ -598,7 +607,7 @@ class Dataset: vstandardname += [standardname] vlongname += [longname] - return { + return { # TODO: refactor dict comp for readability? shortname: Variable( shortname, unit, @@ -665,6 +674,7 @@ class Dataset: if self.inputFhandle.closed: self.inputFhandle = open(self.inputFhandle.name, encoding='utf-8') + # TODO: refactor following try/except statement try: nul = [self.inputFhandle.readline() for i in range(self.nHeaderFile)] @@ -729,7 +739,7 @@ class Dataset: :type f: file handle or StringIO stream, defaults to sys.stdout """ - def prnt(txt): + def prnt(txt): # TODO: rename? it writes to file, so "write_to_file"? f.write(str(txt) + "\n") # Number of lines in header, file format index (most files use 1001) - comma delimited. @@ -860,13 +870,14 @@ class Dataset: ) def __del__(self): + # TODO: refactor following try/except statement try: if not self.inputFhandle.closed: self.inputFhandle.close() except: pass - def __init__(self, f=None, loadData=True, splitChar=",", format=Formats.FFI1001): + def __init__(self, f=None, loadData=True, splitChar=",", format=Formats.FFI1001): # TODO: why is init comming last? """Constructor method""" self.format = format self.version = None