-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
30 lines (27 loc) · 1.01 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from typing import Any, TextIO, List, Dict
def read_lines(textio: TextIO, spec: Dict[str, type]) -> List[Dict[str, Any]]:
output = []
lines = textio.readlines()
for line in lines:
blah = dict()
pieces = line.strip().split(' ')
for index, (piece_name, piece_type) in enumerate(spec.items()):
blah[piece_name] = piece_type(pieces[index])
output.append(blah)
return output
def lookahead(iterable):
"""Pass through all values from the given iterable, augmented by the
information if there are more values to come after the current one
(True), or if it is the last value (False).
https://stackoverflow.com/a/1630350/111777
"""
# Get an iterator and pull the first value.
it = iter(iterable)
last = next(it)
# Run the iterator to exhaustion (starting from the second value).
for val in it:
# Report the *previous* value (more to come).
yield last, True
last = val
# Report the last value.
yield last, False