Skip to content

Instantly share code, notes, and snippets.

@Swizec
Created August 10, 2012 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swizec/3315138 to your computer and use it in GitHub Desktop.
Save Swizec/3315138 to your computer and use it in GitHub Desktop.
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