Ejemplo n.º 1
0
def make_date_parser():

    date_expr = ppc.iso8601_date.copy()
    date_expr.setParseAction(ppc.convertToDate())

    expr_last = (
        CaselessKeyword('LAST') & ppc.integer.setResultsName('n')
        & StringEnd()).setResultsName('interval').setParseAction(handle_last)

    expr_prev = (CaselessKeyword('PREVIOUS') & Or(
        CaselessKeyword('DAY').setResultsName('day')
        | CaselessKeyword('WEEK').setResultsName('week')
        | CaselessKeyword('MONTH').setResultsName('month')) + StringEnd()
                 ).setResultsName('interval').setParseAction(handle_previous)

    expr_fromto_date = (
        CaselessKeyword('FROM') + date_expr.setResultsName('start') +
        CaselessKeyword('TO') + date_expr.setResultsName('end') +
        StringEnd()).setResultsName('interval').setParseAction(handle_fromto)

    parser = expr_fromto_date | expr_last | expr_prev
    return parser
Ejemplo n.º 2
0
 def getLiteral(self):
     '''
     get the literal sub Grammar
     '''
     uri=Regex(SiDIFParser.getUriRegexp())('uri')
     booleanLiteral=oneOf(["true","false"]).setParseAction(self.convertToBoolean)('boolean')
     hexLiteral=(Suppress("0x")+(Word(hexnums).setParseAction(tokenMap(int, 16))))('hexLiteral')
     integerLiteral=pyparsing_common.signed_integer('integerLiteral')
     floatingPointLiteral=Group(
         pyparsing_common.sci_real|pyparsing_common.real
     ).setParseAction(self.handleGroup)('floatingPointLiteral')
     timeLiteral=Regex(r"[0-9]{2}:[0-9]{2}(:[0-9]{2})?").setParseAction(self.convertToTime)('timeLiteral')
     dateLiteral=pyparsing_common.iso8601_date.copy().setParseAction(pyparsing_common.convertToDate())('dateLiteral')
     dateTimeLiteral=Group(
         dateLiteral+Optional(timeLiteral)
     ).setParseAction(self.handleDateTimeLiteral)('dateTimeLiteral')
     stringLiteral=Group(
         Suppress('"')+ZeroOrMore(CharsNotIn('"')|LineEnd())+Suppress('"')
     ).setParseAction(self.handleStringLiteral)('stringLiteral')
     literal=Group(
         uri | stringLiteral |  booleanLiteral | hexLiteral | dateTimeLiteral | timeLiteral | floatingPointLiteral| integerLiteral 
     ).setParseAction(self.handleGroup)("literal")
     return literal
Ejemplo n.º 3
0
    Optional('-')
    + Word(nums)
    + Literal('f').suppress()
).addParseAction(ppc.convertToFloat)

NUMBER = (
        FLOAT2 | FLOAT | INTEGER
).setName("NUMBER")

DATE_DDMMYYYY = Combine(
    DIGIT + DIGIT
    + '-'
    + DIGIT + DIGIT
    + '-'
    + DIGIT + DIGIT + DIGIT + DIGIT
).addParseAction(ppc.convertToDate('%d-%m-%Y'))

DATE_YYYYMMDD = Combine(
    DIGIT + DIGIT + DIGIT + DIGIT
    + '-'
    + DIGIT + DIGIT
    + '-'
    + DIGIT + DIGIT
).addParseAction(ppc.convertToDate('%Y-%m-%d'))

DATE = (
        DATE_DDMMYYYY
        | DATE_YYYYMMDD
)

DATETIME_DDMMYYYY = Combine(
Ejemplo n.º 4
0
date_expr.runTests("""\
    2000/1/1

    # invalid month
    2000/13/1

    # 1900 was not a leap year
    1900/2/29

    # but 2000 was
    2000/2/29
    """)

# if dates conform to ISO8601, use definitions in pyparsing_common
date_expr = ppc.iso8601_date.setParseAction(ppc.convertToDate())
date_expr.ignore(pp.pythonStyleComment)

date_expr.runTests("""\
    2000-01-01

    # invalid month
    2000-13-01

    # 1900 was not a leap year
    1900-02-29

    # but 2000 was
    2000-02-29
    """)