Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- def addElement(d, el, cnt):
- if el in d:
- d[el] += cnt
- else:
- d[el] = cnt
- def parse_simple(formula):
- out = {}
- match = re.findall(r'([A-Z][a-z]?)(\d+)?', formula)
- if match:
- for element, cnt in match:
- cnt = cnt or '1'
- addElement(out, element, int(cnt))
- return out
- def expand(formula, mult):
- match = re.findall(r'([A-Z][a-z]?)(\d+)?', formula)
- ret = ''
- if match:
- for element, cnt in match:
- cnt = cnt or '1'
- cnt = str(int(cnt) * int(mult))
- ret += element + cnt
- return ret
- def parse_molecule(formula):
- def repl(match):
- form = match.group(1)
- mul = match.group(2) or 1
- return expand(form, mul)
- formula = re.sub(r'\((\w+)\)(\d+)?', repl, formula)
- formula = re.sub(r'\[(\w+)\](\d+)?', repl, formula)
- formula = re.sub(r'\{(\w+)\}(\d+)?', repl, formula)
- return parse_simple(formula)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement