def parse(cls): one_of('+') caseless_string('epi') aggregates = {} if whitespace(): while peek(): try: code = "".join(pico.one_of_strings(*( tuple(cls.TOKENS) + tuple(cls.ALIAS)))) code = code.upper() except: raise FormatError( "Expected an epidemiological indicator " "such as TB or MA (got: %s)." % \ "".join(remaining())) # rewrite alias code = cls.ALIAS.get(code, code) if code in aggregates: raise FormatError("Duplicate value for %s." % code) whitespace1() try: minus = optional(partial(one_of, '-'), '') value = int("".join([minus]+pico.digits())) except: raise FormatError("Expected a value for %s." % code) if value < 0: raise FormatError("Got %d for %s. You must " "report a positive value." % ( value, cls.TOKENS[code].lower())) aggregates[code] = value many(partial(one_of, ' ,;.')) return { 'aggregates': aggregates }
def parse(cls): result = {} prefix = optional(tri(identifier), None) if prefix is not None: result['patient_id'] = "".join(prefix) whitespace() one_of('+') caseless_string('muac') if prefix is None: try: whitespace1() part = optional(tri(identifier), None) if part is not None: result['patient_id'] = "".join(part) else: result['name'] = name() except: raise FormatError("Expected a patient id or name.") if 'name' in result: try: separator() result['sex'] = one_of('MmFf').upper() except: raise FormatError("Expected either M or F to indicate the patient's gender.") try: separator() except: raise FormatError("Expected age or birthdate of patient.") try: result['age'] = choice(*map(tri, (date, timedelta))) except: received, stop = many_until(any_token, comma) raise FormatError("Expected age or birthdate of patient, but " "received %s." % "".join(received)) try: if prefix is None: separator() else: whitespace1() reading = choice( partial(one_of_strings, 'red', 'green', 'yellow', 'r', 'g', 'y'), digits) try: reading = int("".join(reading)) except: reading = reading[0].upper() else: whitespace() unit = optional(partial(one_of_strings, 'mm', 'cm'), None) if unit is None: reading = cls.get_reading_in_mm(reading) elif "".join(unit) == 'cm': reading = reading * 10 result['reading'] = reading except: raise FormatError( "Expected MUAC reading (either green, yellow or red), but " "received %s." % "".join(remaining())) if optional(separator, None): result['tags'] = tags() return result
def name(): whitespace1() return pico.name()
def number(): whitespace1() return many1(partial(one_of, string.digits + ' -+()'))
def ident(): whitespace1() pico.hash() commit() return many1(any_token)
def name(): whitespace1() result['name'] = "".join(many1(partial(not_one_of, ',')))
def ident(): whitespace1() one_of('#') commit() result['ident'] = "".join(many1(partial(not_one_of, ',')))