Skip to content

Instantly share code, notes, and snippets.

@Swizec
Created Aug 10, 2012
Embed
What would you like to do?
scary piece of python
def limits(base=None, row=None):
if base:
row = base.find('table', {'class': 'CLPTable'})\
.find('tr', {'class': 'results-row'})
regex = ur"(.+); (H[0-9]+): (C (≥|>) ([0-9,]+)%|([0-9,]+)% (≤|&lt;|<) C (≤|&lt;|<) ([0-9,]+)%)([ *]*)"
else:
regex = ur"(.+)(): (C (≥|>) ([0-9,]+)%|([0-9,]+)% (≤|&lt;|<) C (≤|&lt;|<) ([0-9,]+)%)([ *]*)"
raw = row.find_all('td', {'rowspan': re.compile('[0-9]+')})[2]\
.findAll(text=True)
parsed = [{'category': m.group(1),
'code': m.group(2),
'low_eq': m.group(5) if m.group(4) and m.group(4) == u'≥' else m.group(6) if m.group(7) and m.group(7) == u'≤' else u'',
'low': m.group(5) if m.group(4) and m.group(4) == u'>' else m.group(6) if m.group(7) and m.group(7) != u'≤' else u'',
'high_eq': m.group(9) if m.group(9) and m.group(8) == u'≤' else u'',
'high': m.group(9) if m.group(9) and m.group(8) != u'≤' else u'',
'remark': m.group(10).strip()}
if m != None else {'special': s}
for (m, s) in
((re.match(regex,
limit.strip()),
limit.strip())
for limit in raw
if len(limit.strip()) > 0)]
return {'raw': [l.strip() for l in raw if len(l.strip()) > 0],
'parsed': parsed}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment