def parse(cls): one_of('+') caseless_string('echo') whitespace() return { 'echo': "".join(remaining()) }
def xmldecl(): caseless_string("xml") whitespace() return ( "xml", optional(partial(xmldecl_attr, "version", version_num), "1.0"), optional(partial(xmldecl_attr, "standalone", standalone), "yes"), )
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 parse(cls): one_of('+') caseless_string('hello')
def parse(cls): one_of('+') caseless_string('bad') from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured("".join(remaining()))
def parse(cls): one_of('+') caseless_string('break')
def parse(cls): one_of('+') caseless_string('error') raise FormatError("error")
def xmldecl(): caseless_string('xml') whitespace() return ('xml', optional(partial(xmldecl_attr, 'version', version_num), "1.0"), optional(partial(xmldecl_attr, 'standalone', standalone), "yes"))