Skip to content

Instantly share code, notes, and snippets.

@rhelmer
Created April 10, 2012 20:21
Show Gist options
  • Select an option

  • Save rhelmer/2354202 to your computer and use it in GitHub Desktop.

Select an option

Save rhelmer/2354202 to your computer and use it in GitHub Desktop.
lexer
import shlex
import re
from cStringIO import StringIO
c1 = 'jobname|1d|4:30'
c2 = 'jobname | 1d | 4:30'
def tokenize(t):
if re.match('(\d+)d', t): return 'DAY'
if re.match('(\d+)h', t): return 'HOUR'
if re.match('(\d+)m', t): return 'MIN'
if re.match('(\d+)s', t): return 'SEC'
if re.match('(\d+):(\d+)', t): return 'TIME'
if re.match('\w+', t): return 'JOB'
return 'UNKNOWN'
for job in [c1, c2]:
lex = shlex.shlex(StringIO(job))
lex.whitespace += '|'
lex.whitespace_split = True
while 1:
token = lex.get_token()
if token == '': break
print token
print tokenize(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment