def __init__(self, filename): """Parses an ISP parameter file from disk. Args: filename: The filename or file object representing the ISP parameter file. """ if isinstance(filename, str): par_file = open(filename, 'r') else: par_file = filename self.keys = [] try: par_file.readline() # Skip header line for line in par_file: match = ISPPar._re_kv_pair.match(line) if not match: continue # Skip malformed lines with no key-value pairs key = match.group(1) items = match.group(2).split() if len(items) == 0: value = None elif len(items) == 1: value = parse_literal(items[0]) else: if not ISPPar._re_float_literal.match(items[0]): # Value is a string literal containing whitespace characters value = match.group(2) else: # Evaluate each item and stop at the first non-float literal value = [] for i in items: match = ISPPar._re_float_literal.match(i) if match: value.append(parse_literal(match.group())) else: # If the first float literal is immediately followed by a non-float literal handle the # first one as singular value, e.g. in '20.0970 dB' if len(value) == 1: value = value[0] break self.keys.append(key) setattr(self, key, value) finally: par_file.close() if hasattr(self, 'date'): self.date = '{}-{:02d}-{:02d}T{:02d}:{:02d}:{:02f}'.format( *self.date)
def test_parse_literal(): assert anc.parse_literal(['1', '2.2', 'a']) == [1, 2.2, 'a'] with pytest.raises(TypeError): anc.parse_literal(1)
def __init__(self, filename): """Parses an ISP parameter file from disk. Args: filename: The filename or file object representing the ISP parameter file. """ if isinstance(filename, str): par_file = open(filename, 'r') else: par_file = filename self.keys = [] try: par_file.readline() # Skip header line content = par_file.read().split('\n') except UnicodeDecodeError: par_file = codecs.open(filename, 'r', encoding='utf-8', errors='ignore') content = par_file.read() printable = set(string.printable) content = filter(lambda x: x in printable, content) content = ''.join(list(content)).split('\n') try: for line in content: match = ISPPar._re_kv_pair.match(line) if not match: continue # Skip malformed lines with no key-value pairs key = match.group(1) items = match.group(2).split() if len(items) == 0: value = None elif len(items) == 1: value = parse_literal(items[0]) else: if not ISPPar._re_float_literal.match(items[0]): # Value is a string literal containing whitespace characters value = match.group(2) else: # Evaluate each item and stop at the first non-float literal value = [] for i in items: match = ISPPar._re_float_literal.match(i) if match: value.append(parse_literal(match.group())) else: # If the first float literal is immediately followed by a non-float literal handle the # first one as singular value, e.g. in '20.0970 dB' if len(value) == 1: value = value[0] break self.keys.append(key) setattr(self, key, value) finally: par_file.close() if hasattr(self, 'date'): try: self.date = '{}-{:02d}-{:02d}T{:02d}:{:02d}:{:02f}'.format( *self.date) except: # if only date available self.date = '{}-{:02d}-{:02d}'.format(*self.date)