def date(formats=("%m/%d/%Y",)): """Parses a date using one of the supplied formats. To integrate with Django's date format settings, pass in the ``DATE_INPUT_FORMATS`` setting. The default settings is defined in :mod:`django.conf.global_settings` as:: DATE_INPUT_FORMATS = ( '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06' '%b %d %Y', '%b %d, %Y', # 'Oct 25 2006', 'Oct 25, 2006' '%d %b %Y', '%d %b, %Y', # '25 Oct 2006', '25 Oct, 2006' '%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006' '%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006' ) To use this setting, wrap the function like this: >>> from django.conf import settings >>> date = partial(date, formats=settings.DATE_INPUT_FORMATS) The standard settings enables a wide set of input formats; we demonstrate some of them here: >>> run_parser(date, '12/31/1999')[0].isoformat() '1999-12-31T00:00:00' >>> run_parser(date, 'December 31, 1999')[0].isoformat() '1999-12-31T00:00:00' >>> run_parser(date, '12/31/99')[0].isoformat() '1999-12-31T00:00:00' """ parsers = [partial(_parse_date_format, f) for f in formats] return choice(*map(tri, parsers))
def expression(): expr = choice(eval_expression, let_expression, fn_expression) where = optional(where_expression, None) if where: return ('where', expr, where) else: return expr
def one_of_strings(*strings): """Parses one of the strings provided, caseless. >>> parse(partial(one_of_strings, 'abc', 'def'), 'abc') 'abc' >>> parse(partial(one_of_strings, 'abc', 'def'), 'def') 'def' """ return choice(*map(tri, map(partial(partial, caseless_string), strings)))
def parse(cls): result = {} try: identifiers = optional(tri(pico.ids), None) if identifiers: result['ids'] = [id.upper() for id in identifiers] else: result['name'] = pico.name() except: raise FormatError( "Expected a name, or a patient's health or tracking ID " "(got: %s)." % "".join(remaining())) if 'name' in result: try: many1(partial(one_of, ' ,;')) result['sex'] = pico.one_of_strings( 'male', 'female', 'm', 'f')[0].upper() except: raise FormatError( "Expected the infant's gender " "(\"male\", \"female\", or simply \"m\" or \"f\"), " "but received instead: %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())) return result
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 identifier_char(): return choice(identifier_char1, digit)
def value(): return choice(number, string_literal)
def element(): open_angle() name = xml_name() commit() attributes = lexeme(partial(sep, attribute, whitespace1)) return "NODE", name, attributes, choice(closed_element, partial(open_element, name))
def program_part(): expr = choice(definition, expression) optional(semi) return expr
def standalone(): return choice(partial(string, 'yes'), partial(string, 'no'))
def standalone(): return choice(partial(string, "yes"), partial(string, "no"))
def node(): return choice(processing, element, text_node, comment)
def numeric_entity(): one_of("#") return choice(hex_entity, dec_entity)
def entity(): one_of("&") ent = choice(named_entity, numeric_entity) one_of(";") return ent
def value(): is_negative = optional(partial(one_of, '-'), False) val = choice(float_value, int_value) * (is_negative and -1 or 1) return ValueNode(val)
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 numeric_entity(): one_of('#') return choice(hex_entity, dec_entity)
def expression_part(): return choice(value, identifier, operator, parenthetical)
def entity(): one_of('&') ent = choice(named_entity, numeric_entity) one_of(';') return ent