""" from autoparse import cast as _cast import autoparse.find as apf import autoparse.pattern as app # VALUE_PATTERN = app.one_of_these([app.FLOAT]) VALUE_PATTERN = app.one_of_these([app.EXPONENTIAL_FLOAT_D, app.FLOAT]) SEP_PATTERN = app.LINESPACES def read(string, start_ptt, val_ptt=VALUE_PATTERN, last=True, case=False): """ read energy from a string """ ptt_ = pattern(start_ptt=start_ptt, val_ptt=app.capturing(val_ptt)) ene_str = (apf.last_capture(ptt_, string, case=case) if last else apf.first_capture(ptt_, string, case=case)) ene = _cast(ene_str.replace('D', 'E')) return ene def pattern(start_ptt, val_ptt=VALUE_PATTERN): """ energy pattern """ return start_ptt + app.lpadded(val_ptt)
""" read geometry from a string """ line_ptt_ = line_pattern( sym_ptt=app.capturing(sym_ptt), val_ptt=app.capturing(val_ptt), sep_ptt=line_sep_ptt, start_ptt=line_start_ptt) block_ptt_ = app.capturing(block_pattern( sym_ptt=sym_ptt, val_ptt=val_ptt, line_sep_ptt=line_sep_ptt, line_start_ptt=line_start_ptt)) block_ptt_ = block_ptt_ if start_ptt is None else start_ptt + block_ptt_ block_str = (apf.last_capture(block_ptt_, string, case=case) if last else apf.first_capture(block_ptt_, string, case=case)) caps = apf.all_captures(line_ptt_, block_str) syms, xcomps, ycomps, zcomps = zip(*_cast(caps)) xyzs = tuple(zip(xcomps, ycomps, zcomps)) return syms, xyzs def read_xyz(string, sym_ptt=par.Pattern.ATOM_SYMBOL, val_ptt=par.Pattern.NUMERIC_VALUE): """ read geometry from a .xyz string """ lines = string.splitlines() try: natms = int(lines[0]) except ValueError: raise ValueError('Invalid xyz string') geo_str = '\n'.join(lines[2:natms+2])
entry_ptt_ = entry_pattern( name_ptt=app.capturing(name_ptt), val_ptt=app.capturing(val_ptt), sep_ptt=entry_sep_ptt, start_ptt=entry_start_ptt, end_ptt=entry_end_ptt) block_ptt_ = app.capturing(block_pattern( name_ptt=name_ptt, val_ptt=val_ptt, entry_sep_ptt=entry_sep_ptt, entry_start_ptt=entry_start_ptt, entry_end_ptt=entry_end_ptt, sep_ptt=sep_ptt)) block_ptt_ = block_ptt_ if start_ptt is None else start_ptt + block_ptt_ block_str = (apf.last_capture(block_ptt_, string, case=case) if last else apf.first_capture(block_ptt_, string, case=case)) caps = apf.all_captures(entry_ptt_, block_str, case=case) val_dct = dict(_cast(caps)) return val_dct def block_pattern(name_ptt=NAME_PATTERN, val_ptt=VALUE_PATTERN, entry_sep_ptt=ENTRY_SEP_PATTERN, entry_start_ptt=None, entry_end_ptt=None, sep_ptt=SEP_PATTERN): """ a single setvalue entry """ entry_ptt = entry_pattern( name_ptt=name_ptt, val_ptt=val_ptt, sep_ptt=entry_sep_ptt,
:param block_str: string to read lines from :type block_str: str :param val_ptt: matches numeric matrix entry :type val_ptt: str :param line_ptt_: pattern for matching line of the block :type line_ptt_: str :param case: make the match case-sensitive? :type case: bool :rtype: list(float) """ rows = [] val_ptt_ = app.capturing(val_ptt) for line_str in apf.all_captures(line_ptt_, block_str, case=case): row = _cast(apf.all_captures(val_ptt_, line_str)) rows.append(row) return rows def blocks_pattern(val_ptt=VALUE_PATTERN, start_ptt=None, block_start_ptt=None, line_start_ptt=None, capture_blocks=False): """ Build a pattern that matches a multi-block matrix. :param val_ptt: matches numeric matrix entry :type val_ptt: str :param start_ptt: pattern before start of the matrix block