예제 #1
0
파일: pico.py 프로젝트: djangosms/core
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))
예제 #2
0
def expression():
  expr = choice(eval_expression, let_expression, fn_expression)
  where = optional(where_expression, None)
  if where:
	return ('where', expr, where)
  else:
	return expr
예제 #3
0
파일: pico.py 프로젝트: djangosms/core
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)))
예제 #4
0
파일: forms.py 프로젝트: malthe/cvs
    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
예제 #5
0
파일: forms.py 프로젝트: malthe/cvs
    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
예제 #6
0
def identifier_char():
  return choice(identifier_char1, digit)
예제 #7
0
def value():
  return choice(number, string_literal)
예제 #8
0
파일: xml.py 프로젝트: stjordanis/picoparse
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))
예제 #9
0
def program_part():
  expr = choice(definition, expression)
  optional(semi)
  return expr
예제 #10
0
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))
예제 #11
0
파일: xml.py 프로젝트: stjordanis/picoparse
def standalone():
    return choice(partial(string, 'yes'), partial(string, 'no'))
예제 #12
0
def standalone():
    return choice(partial(string, "yes"), partial(string, "no"))
예제 #13
0
def node():
    return choice(processing, element, text_node, comment)
예제 #14
0
def numeric_entity():
    one_of("#")
    return choice(hex_entity, dec_entity)
예제 #15
0
def entity():
    one_of("&")
    ent = choice(named_entity, numeric_entity)
    one_of(";")
    return ent
예제 #16
0
def value():
    is_negative = optional(partial(one_of, '-'), False)
    val = choice(float_value, int_value) * (is_negative and -1 or 1)
    return ValueNode(val)
예제 #17
0
    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
예제 #18
0
파일: xml.py 프로젝트: stjordanis/picoparse
def numeric_entity():
    one_of('#')
    return choice(hex_entity, dec_entity)
예제 #19
0
def value():
    is_negative = optional(partial(one_of, '-'), False)
    val = choice(float_value, int_value) * (is_negative and -1 or 1)
    return ValueNode(val)
예제 #20
0
def expression_part():
  return choice(value, identifier, operator, parenthetical)
예제 #21
0
파일: xml.py 프로젝트: stjordanis/picoparse
def entity():
    one_of('&')
    ent = choice(named_entity, numeric_entity)
    one_of(';')
    return ent
예제 #22
0
파일: xml.py 프로젝트: stjordanis/picoparse
def node():
    return choice(processing, element, text_node, comment)