def expand_sum(self): sfile = open(self.destcmp, 'r') dfile = open(self.destsum, 'w') sum_format = Keyword(KEYWORDS['sum']) + '(' + Word(nums)("start") + "," + Word(nums)("end") + "," + Word(nums)("jump") + ')' \ + '(' + Regex(r'[^\s\(\)]*')("var") + ')' line_sum_format = Regex(r'[^\@]*')("before") + sum_format + Regex( r'[^\@]*')("after") for line in sfile: if KEYWORDS['sum'] in line: # import pdb; pdb.set_trace() res = line_sum_format.parseString(line) start = int(res.start) end = int(res.end) jump = int(res.jump) var = res.var replacement = res.before for i in range(start, end, jump): replacement += var.replace("$i", str(i)) + " + " replacement = replacement[:-3] + res.after #FIX MEE !!! if len(res.after.split()) == 0: replacement += "\n" dfile.write(replacement) else: dfile.write(line) sfile.close() dfile.close()
def parseDate(self, dateString): dateGrammar = Regex("\d{4}")("year") + Regex("\d{2}")("month") +\ Regex("\d{2}")("day") + Regex("\d{2}")("hours") +\ Suppress(":") + Regex("\d{2}")("minutes") results = dateGrammar.parseString(dateString) return { "year": results["year"], "month": results["month"], "day": results["day"], "hours": results["hours"], "minutes": results["minutes"], }
def parseDate(self, dateString): dateGrammar = Regex("\d{4}")("year") + Regex("\d{2}")("month") +\ Regex("\d{2}")("day") + Regex("\d{2}")("hours") +\ Suppress(":") + Regex("\d{2}")("minutes") results = dateGrammar.parseString(dateString) return { "year" : results["year"], "month" : results["month"], "day" : results["day"], "hours" : results["hours"], "minutes" : results["minutes"], }
def expand_bool(self): sfile = open(self.destsum, 'r') dfile = open(self.destbool, 'w') bool_format = Keyword( KEYWORDS['bool']) + '(' + Word(nums)("start") + "," + Word(nums)( "end") + "," + Word(nums)("jump") + ')' + '(' + Regex( r'[^\s\(\)]*')('op') + ')' + '(' + Regex(r'[^\s\(\),]*')( "logical_op") + ')' + '(' + Regex(r'[^\s\(\),]*')( "var") + "," + Regex(r'[^\s\(\),]*')( "operand") + ')' line_bool_format = Regex(r'[^\@]*')("before") + \ bool_format + Regex(r'[^\@]*')("after") for line in sfile: if KEYWORDS['bool'] in line: # import pdb; pdb.set_trace() res = line_bool_format.parseString(line) start = int(res.start) end = int(res.end) jump = int(res.jump) logical_op = res.logical_op var = res.var operand = res.operand op = res.op replacement = res.before for i in range(start, end, jump): replacement += "%s %s %s %s " % (var.replace( "$i", str(i)), op, operand, logical_op) replacement = replacement[:-2 - len(logical_op)] + res.after dfile.write(replacement) else: dfile.write(line) sfile.close() dfile.close()
def email_address_parser(text): emailExpr = Regex( r"(?P<user>[A-Za-z0-9._%+-]+)@(?P<hostname>[A-Za-z0-9.-]+)\.(?P<domain>[A-Za-z]{2,4})" ) print emailExpr.parseString(text).dump()
def test_raise_field_error(self): parser = Regex(r'foo')\ .setFailAction(AssetNameParser._raise_field_error('foo', 'bar')) expected = 'Illegal foo field bar in "baz". Expecting: foo' with self.assertRaisesRegexp(ParseException, expected): parser.parseString('baz')
def validate_args(docopt_args): """Validate the user provided information.""" # validate priority if docopt_args['-p']: docopt_args['-p'] = docopt_args['-p'].upper() if docopt_args['-p'] not in ['L', 'M', 'H']: print '\nThe priority given is not valid.\n' sys.exit(-4) # validate date if docopt_args['-d']: pattern = Regex('^(1[0-2]|0?[1-9])(?P<Sep>(/|-|.))(3[01]|[12][0-9]|0?[1-9])(?P=Sep)(?:[0-9]{2})?[0-9]{2}$') try: pattern.parseString(docopt_args['-d']) except ParseException: print '\n' + docopt_args['-d'] + ' is not a valid date\n' sys.exit(-3) else: # figure out what format the user provided... if '/' in docopt_args['-d']: date_sep = '/' elif '-' in docopt_args['-d']: date_sep = '-' elif '.' in docopt_args['-d']: date_sep = '.' date = docopt_args['-d'].split(date_sep) if len(date[0]) == 1: month_format = 'M' else: month_format = 'MM' if len(date[1]) == 1: day_format = 'D' else: day_format = 'DD' if len(date[2]) == 2: year_format = 'YY' else: year_format = 'YYYY' # get the time zone # offset = time.strftime('%z', time.localtime()) <--- This doesn't work on windows offset = ("-" if time.timezone > 0 else "+") + time.strftime("%H:%M", time.gmtime(abs(time.timezone))) # parse the date date_format = date_sep.join([month_format, day_format, year_format]) date_format = date_format + ' h:mm A Z' if '--time' in docopt_args and docopt_args['--time']: pattern = Regex('^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$') try: pattern.parseString(docopt_args['--time']) except ParseException: print '\nInvalid time format. Dropping Due Time.\n' else: # parse the date to make sure it is in the correct format: dtime = docopt_args['--time'][:-2].strip() period = docopt_args['--time'][-2:] dtime = dtime + ' ' + period + ' ' + str(offset) date = docopt_args['-d'] + ' ' + dtime else: date = docopt_args['-d'] + ' 11:59 PM ' + offset date = arrow.get(date, date_format) # validate due date occurs in the future if date.date() < date.date().today(): print '\nWarning: The due date provided occurs in the past.\n' docopt_args['-d'] = [date.format(date_format), date_format] return docopt_args
def email_address_parser(text): emailExpr = Regex(r"(?P<user>[A-Za-z0-9._%+-]+)@(?P<hostname>[A-Za-z0-9.-]+)\.(?P<domain>[A-Za-z]{2,4})") print emailExpr.parseString(text).dump()
encodingreference = Word(A_to_Z_latin, A_to_Z_latin + hyphen_minus + non_breaking_hyphen) # 12.37 Single character lexical items LEFT_CURLY_BRACKET = Literal('{').suppress() RIGHT_CURLY_BRACKET = Literal('}').suppress() LESS_THAN_SIGN = Literal('<') GREATER_THAN_SIGN = Literal('>') COMMA = Literal(',').suppress() FULL_STOP = Literal('.') SOLIDUS = Literal('/') LEFT_PARENTHESIS = Literal('(').suppress() RIGHT_PARENTHESIS = Literal(')').suppress() LEFT_SQUARE_BRACKET = Literal('[') RIGHT_SQUARE_BRACKET = Literal(']') HYPHEN_MINUS = Literal('-') COLON = Literal(':') EQUALS_SIGN = Literal('=') QUOTATION_MARK = Literal('"') APOSTROPHE = Literal("'") SPACE = Literal(' ') SEMICOLON = Literal(';') COMMERCIAL_AT = Literal('@') VERTICAL_LINE = Literal('|') EXCLAMATION_MARK = Literal('!') CIRCUMFLEX_ACCENT = Literal('^') if __name__ == '__main__': s = "000'B" print(bstring.parseString(s, parseAll=True))