Build a simple parser that is able to parse different date formats using PyParse

Fizi

I am building a simple parser that takes a query like the following: 'show fizi commits from 1/1/2010 to 11/2/2006' So far I have:

class QueryParser(object):

def parser(self, stmnt):

    keywords = ["select", "from","to", "show","commits", "where", "group by", "order by", "and", "or"]
    [select, _from, _to, show, commits, where, groupby, orderby, _and, _or] = [ CaselessKeyword(word) for word in keywords ]

    user = Word(alphas+"."+alphas)
    user2 = Combine(user + "'s")

    startdate=self.getdate()
    enddate=self.getdate()

    bnf = (show|select)+(user|user2).setResultsName("user")+(commits).setResultsName("stats")\
    +Optional(_from+startdate.setResultsName("start")+_to+enddate.setResultsName("end"))

    a = bnf.parseString(stmnt)
    return a

def getdate(self):
    integer = Word(nums).setParseAction(lambda t: int(t[0]))
    date = Combine(integer('year') + '/' + integer('month') + '/' + integer('day'))
    #date.setParseAction(self.convertToDatetime)
    return date

I would like the dates to be more generic. Meaning user can provide 20 Jan, 2010 or some other date format. I found a good date parsing online that does exactly that. It takes a date as a string and then parses it. So what I am left with is to feed that function the date string I get from my parser. How do I go about tokenizing and capturing the two date strings. For now it only captures the format 'y/m/d' format. Is there a way to just get the entire string regarless of how its formatted. Something like capture the word right after keywords and . Any help is greatly appreciated.

Jon Clements

A simple approach is to require the date be quoted. A rough example is something like this, but you'll need to adjust to fit in with your current grammar if needs be:

from pyparsing import CaselessKeyword, quotedString, removeQuotes
from dateutil.parser import parse as parse_date

dp = (
    CaselessKeyword('from') + quotedString.setParseAction(removeQuotes)('from') +
    CaselessKeyword('to') + quotedString.setParseAction(removeQuotes)('to')
)

res = dp.parseString('from "jan 20" to "apr 5"')
from_date = parse_date(res['from'])
to_date = parse_date(res['to'])
# from_date, to_date == (datetime.datetime(2015, 1, 20, 0, 0), datetime.datetime(2015, 4, 5, 0, 0))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Build a simple parser that is able to parse different date formats using PyParse

From Dev

Parse and group multiple items together using Pyparse

From Dev

Unable to parse date string using mutliple formats

From Dev

SimpleDateFormat.parse() - generates wrong date for different date-formats

From Dev

Not able to sort array using Date.parse()

From Dev

Check a date for different formats

From Dev

Different date formats in Excel

From Dev

Java date with different formats

From Dev

Parse Multiple Date Formats using RegEx in C#

From Dev

Using NodaTime to parse an input and output different dateTime formats

From Dev

Using NodaTime to parse an input and output different dateTime formats

From Dev

Parse output using PHP Simple HTML DOM parser

From Dev

Parse output using PHP Simple HTML DOM parser

From Dev

Swift parse string with different formats

From Dev

Not able to parse different parameters from a sentence using perl regex

From Dev

Parse using Jackson Parser

From Dev

Regex for capturing different date formats

From Dev

Transform and sort different date formats

From Dev

How to sort date of different formats

From Dev

Validate a date list with different formats

From Dev

Simple XLSX Parser: Date output

From Dev

A Java date parser which reads all British date formats

From Dev

Java or Scala fast way to parse dates with many different formats using java.time

From Dev

Using standard or custom date formats

From Dev

Using standard or custom date formats

From Dev

Parse website and save a specific DIV using 'PHP Simple HTML DOM Parser'

From Dev

Java 8 Date equivalent to Joda's DateTimeFormatterBuilder with multiple parser formats?

From Dev

pandas to_datetime formats date from same column in different formats

From Dev

Convert date (different formats) to day in r

Related Related

  1. 1

    Build a simple parser that is able to parse different date formats using PyParse

  2. 2

    Parse and group multiple items together using Pyparse

  3. 3

    Unable to parse date string using mutliple formats

  4. 4

    SimpleDateFormat.parse() - generates wrong date for different date-formats

  5. 5

    Not able to sort array using Date.parse()

  6. 6

    Check a date for different formats

  7. 7

    Different date formats in Excel

  8. 8

    Java date with different formats

  9. 9

    Parse Multiple Date Formats using RegEx in C#

  10. 10

    Using NodaTime to parse an input and output different dateTime formats

  11. 11

    Using NodaTime to parse an input and output different dateTime formats

  12. 12

    Parse output using PHP Simple HTML DOM parser

  13. 13

    Parse output using PHP Simple HTML DOM parser

  14. 14

    Swift parse string with different formats

  15. 15

    Not able to parse different parameters from a sentence using perl regex

  16. 16

    Parse using Jackson Parser

  17. 17

    Regex for capturing different date formats

  18. 18

    Transform and sort different date formats

  19. 19

    How to sort date of different formats

  20. 20

    Validate a date list with different formats

  21. 21

    Simple XLSX Parser: Date output

  22. 22

    A Java date parser which reads all British date formats

  23. 23

    Java or Scala fast way to parse dates with many different formats using java.time

  24. 24

    Using standard or custom date formats

  25. 25

    Using standard or custom date formats

  26. 26

    Parse website and save a specific DIV using 'PHP Simple HTML DOM Parser'

  27. 27

    Java 8 Date equivalent to Joda's DateTimeFormatterBuilder with multiple parser formats?

  28. 28

    pandas to_datetime formats date from same column in different formats

  29. 29

    Convert date (different formats) to day in r

HotTag

Archive