Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some type hints #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pycrfsuite/_dumpparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self):
self.result = ParsedDump()

def feed(self, line):
# type: (str) -> None
# Strip initial ws and line terminator, but allow for ws at the end of feature names.
line = line.lstrip().rstrip('\r\n')
if not line:
Expand All @@ -62,25 +63,30 @@ def feed(self, line):
getattr(self, 'parse_%s' % self.state)(line)

def parse_FILEHEADER(self, line):
# type: (str) -> None
m = re.match(r"(\w+): (.*)", line)
self.result.header[m.group(1)] = m.group(2)

def parse_LABELS(self, line):
# type: (str) -> None
m = re.match(r"(\d+): (.*)", line)
self.result.labels[m.group(2)] = m.group(1)

def parse_ATTRIBUTES(self, line):
# type: (str) -> None
m = re.match(r"(\d+): (.*)", line)
self.result.attributes[m.group(2)] = m.group(1)

def parse_TRANSITIONS(self, line):
# type: (str) -> None
m = re.match(r"\(\d+\) (.+) --> (.+): ([+-]?\d+\.\d+)", line)
from_, to_ = m.group(1), m.group(2)
assert from_ in self.result.labels
assert to_ in self.result.labels
self.result.transitions[(from_, to_)] = float(m.group(3))

def parse_STATE_FEATURES(self, line):
# type: (str) -> None
m = re.match(r"\(\d+\) (.+) --> (.+): ([+-]?\d+\.\d+)", line)
attr, label = m.group(1), m.group(2)
assert attr in self.result.attributes
Expand Down
10 changes: 10 additions & 0 deletions pycrfsuite/_logparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self):
self.events = []

def feed(self, line):
# type: (str) -> None
# if line != '\n':
self.log.append(line)
if self.state is None:
Expand All @@ -44,10 +45,12 @@ def last_log(self):
return ''.join(self.log[start:end])

def handle_STARTING(self, line):
# type: (str) -> None
if line.startswith('Feature generation'):
self.state = 'FEATGEN'

def handle_FEATGEN(self, line):
# type: (str) -> str
if line in "0123456789.10":
self.featgen_percent += 2
return 'featgen_progress'
Expand All @@ -63,6 +66,7 @@ def handle_FEATGEN(self, line):
return 'featgen_end'

def handle_AFTER_FEATGEN(self, line):
# type: (str) -> str
if self._iteration_head(line) is not None:
self.state = 'ITERATION'
self.handle_ITERATION(line)
Expand All @@ -73,6 +77,7 @@ def handle_AFTER_FEATGEN(self, line):
return 'prepare_error'

def handle_ITERATION(self, line):
# type: (str) -> None
if self._iteration_head(line) is not None:
self.last_iteration = {
'num': self._iteration_head(line),
Expand All @@ -84,6 +89,7 @@ def handle_ITERATION(self, line):
return 'iteration'

def add_re(key, pattern, typ):
# type: (str,str,str) -> None
m = re.match(pattern, line)
if m:
self.last_iteration[key] = typ(m.group(1))
Expand Down Expand Up @@ -137,6 +143,7 @@ def add_re(key, pattern, typ):
})

def handle_AFTER_ITERATION(self, line):
# type: (str) -> None
if self._iteration_head(line) is not None:
self.state = 'ITERATION'
return self.handle_ITERATION(line)
Expand All @@ -150,17 +157,20 @@ def handle_AFTER_ITERATION(self, line):
return 'optimization_end'

def handle_STORING(self, line):
# type: (str) -> None
if line == '\n':
return 'end'
elif self._seconds(line):
self.storing_seconds = self._seconds(line)

def _iteration_head(self, line):
# type: (str) -> None
m = re.match(r'\*{5} (?:Iteration|Epoch) #(\d+) \*{5}\n', line)
if m:
return int(m.group(1))

def _seconds(self, line):
# type: (str) -> float
m = re.match(r'Seconds required: (\d+\.\d+)', line)
if m:
return float(m.group(1))