def __init__(self): Section.__init__(self) self.hydraulics = HydraulicsOptions() """HydraulicsOptions: Hydraulics options""" self.quality = QualityOptions() """QualityOptions: Water quality options""" self.map = "" """str: Name of a file containing coordinates of the network's nodes, not written if not set"""
class Options(Section): """EPANET Options""" SECTION_NAME = "[OPTIONS]" section_comments = (";; Hydraulics", ";; Water Quality") def __init__(self): Section.__init__(self) self.hydraulics = HydraulicsOptions() """HydraulicsOptions: Hydraulics options""" self.quality = QualityOptions() """QualityOptions: Water quality options""" self.map = "" """str: Name of a file containing coordinates of the network's nodes, not written if not set""" def get_text(self): """Contents of this item formatted for writing to file""" text_list = [self.SECTION_NAME] if self.map: text_list.append(" MAP \t" + self.map) if self.hydraulics is not None: text_list.append(Options.section_comments[0]) text_list.append(self.hydraulics.get_text()) if self.quality is not None: text_list.append(Options.section_comments[1]) text_list.append(self.quality.get_text()) return '\n'.join(text_list) def set_text(self, new_text): """Read properties from text. Args: new_text (str): Text to parse into properties. """ self.__init__() # Skip the comments we insert automatically for comment in Options.section_comments: new_text = new_text.replace(comment + '\n', '') self.hydraulics.set_text(new_text) self.quality.set_text(new_text) for line in new_text.splitlines(): line_list = line.split() if line_list: if str(line_list[0]).strip().upper() == "MAP": self.map = ' '.join(line_list[1:])
def read(new_text): """Read properties from text. Args: new_text (str): Text to parse into properties. """ quality_options = QualityOptions() quality_options.quality = QualityAnalysisType.NONE # default to NONE until found below quality_options.chemical_name = "" quality_options.mass_units = "" quality_options.trace_node = "" for line in new_text.splitlines(): line_list = line.split() if line_list: if str(line_list[0]).strip().upper() == "QUALITY": quality_type = str(line_list[1]).strip().upper() try: quality_options.quality = QualityAnalysisType[quality_type] except: quality_options.quality = QualityAnalysisType.CHEMICAL quality_options.chemical_name = str(line_list[1]) if quality_options.quality == QualityAnalysisType.TRACE: quality_options.trace_node = line_list[2] elif len(line_list) > 2: quality_options.mass_units = line_list[2] elif str(line_list[0]).strip().upper() == "DIFFUSIVITY": quality_options.diffusivity = float(line_list[1]) elif str(line_list[0]).strip().upper() == "TOLERANCE": quality_options.tolerance = float(line_list[1]) return quality_options