def parse(cls): result = {} @tri def ident(): pico.hash() commit() return many1(any_token) @tri def number(): return many1(partial(one_of, string.digits + ' -+()')) ident = optional(partial(choice, ident, number), None) if ident is not None: result['ident'] = re.sub('[ \-+()]', '', "".join(ident)) else: name = optional(tri(pico.name), None) if name is not None: result['name'] = name whitespace() if peek() and not result: raise FormatError( "We did not understand: %s." % "".join(remaining())) return result
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(self, health_id=None): result = {} if health_id is None: try: part = optional(tri(pico.identifier), None) if part is not None: health_id = "".join(part) else: result['name'] = pico.name() except: raise FormatError("Expected a patient id or name.") if 'name' in result: try: pico.separator() result['sex'] = pico.one_of_strings( 'male', 'female', 'm', 'f')[0].upper() except: raise FormatError( "Expected either M or F " \ "to indicate the patient's gender (got: %s)." % "".join(remaining())) try: pico.separator() except: raise FormatError("Expected age or birthdate of patient.") try: result['age'] = choice(*map(tri, (pico.date, pico.timedelta))) except: raise FormatError("Expected age or birthdate of patient, but " "received %s." % "".join( remaining()).split(',')[0]) if health_id is not None: result['health_id'] = health_id try: whitespace() optional(pico.separator, None) reading = choice( partial(pico.one_of_strings, 'red', 'green', 'yellow', 'r', 'g', 'y'), pico.digits) try: reading = int("".join(reading)) except: result['category'] = reading[0].upper() else: whitespace() unit = optional(partial(pico.one_of_strings, 'mm', 'cm'), None) if unit is None: reading = self.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(partial(choice, tri(pico.separator), tri(whitespace)), None): if optional(partial( pico.one_of_strings, 'oedema', 'odema', 'oe'), None): result['oedema'] = True elif peek(): raise FormatError( "Specify \"oedema\" or \"oe\" if the patient shows " "signs of oedema, otherwise leave empty (got: %s)." % \ "".join(remaining())) return result
def parse(cls, keyword=None, keywords=None): if keywords is None: keywords = cls.KEYWORDS slug = keywords[keyword.lower()] kind = ReportKind.objects.get(slug=slug) observations = {} result = { 'observations': observations, 'kind': kind, } total = "".join(optional(pico.digits, ())) if total: result['total'] = int(total) many1(partial(one_of, ' ,;')) kinds = ObservationKind.objects.filter(slug__startswith="%s_" % slug).all() observation_kinds = dict((kind.slug, kind) for kind in kinds) codes = [kind.abbr for kind in kinds if kind.abbr] # we allow both the observation kinds and any aliases allowed_codes = tuple(codes) + tuple(cls.ALIASES) while peek(): # look up observation kinds that double as user input # for the aggregate codes try: code = "".join(pico.one_of_strings(*allowed_codes)).lower() except: raise FormatError( "Expected an indicator code " "such as %s (got: %s)." % ( " or ".join(map(unicode.upper, codes[:2])), "".join(remaining()).strip() or u"nothing")) # rewrite alias if required, then look up kind munged= "%s_%s" % (slug, code) munged = cls.ALIASES.get(munged, munged) kind = observation_kinds[munged] # guard against duplicate entries if kind.slug in observations: raise FormatError("Duplicate value for %s." % code) whitespace() 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, kind.name)) observations[kind.slug] = value many(partial(one_of, ' ,;.')) return result