Skip to content

Commit

Permalink
misc: minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bramhaag committed Jun 27, 2022
1 parent bb559de commit 7997786
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
generated_models/
test_sets/
benchmarks/

# Created by https://www.toptal.com/developers/gitignore/api/pycharm,python,tex
# Edit at https://www.toptal.com/developers/gitignore?templates=pycharm,python,tex
Expand Down
6 changes: 3 additions & 3 deletions src/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ def generate_model(self, level: Level) -> str:
pass


def _multi_range(start: int, end: int, offset: list[int], valid: set[int]):
def _multi_range(start: int, end: int, offset: list[int], valid: set[int]) -> list:
return [x for x in zip(range(start, end), *[range(start + o, end + o) for o in offset]) if set(x).issubset(valid)]


def _move_bounds(level: Level, offset: int):
def _move_bounds(level: Level, offset: int) -> (int, int):
if offset < 0:
return max(-offset, level.first_pos), level.last_pos + 1
else:
return level.first_pos, min(level.size - offset, level.last_pos + 1)


def _flatten(list_2d):
def _flatten(list_2d: list[list]) -> list:
return list(itertools.chain.from_iterable(list_2d))
2 changes: 1 addition & 1 deletion src/generator/jani_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Expr = Identifier | dict | Number | bool


def _binary_op(op: str, left: Expr, right: Expr):
def _binary_op(op: str, left: Expr, right: Expr) -> Expr:
return {
"op": op,
"left": left,
Expand Down
4 changes: 2 additions & 2 deletions src/generator/prism_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def generate_model(self, level: Level) -> str:

@staticmethod
def _generate_board(level: Level) -> str:
def to_variable(name: str, value: bool):
def to_variable(name: str, value: bool) -> str:
return f"{name}: bool init {str(value).lower()};"

return '\n'.join(to_variable(f"box_{i}", level.board[i] == TileType.BOX) for i in
Expand Down Expand Up @@ -190,7 +190,7 @@ def generate_model(self, level: Level) -> str:

@staticmethod
def _generate_board(level: Level) -> str:
def to_variable(name: str, value: bool):
def to_variable(name: str, value: bool) -> str:
return f"{name}: bool init {str(value).lower()};"

return '\n'.join(to_variable(f"box_{i}", level.board[i] == TileType.BOX) for i in
Expand Down
8 changes: 4 additions & 4 deletions src/parser/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ class Level(object):
columns: int

@property
def size(self):
def size(self) -> int:
return self.rows * self.columns

@cached_property
def first_pos(self):
def first_pos(self) -> int:
return min(self.reachable_tiles)

@cached_property
def last_pos(self):
def last_pos(self) -> int:
return max(self.reachable_tiles)

@cached_property
Expand Down Expand Up @@ -65,5 +65,5 @@ def _neighbors(pos: int) -> list[int]:
def boxes(self) -> set[int]:
return {i for i, t in enumerate(self.board) if t == TileType.BOX}

def is_wall(self, i: int):
def is_wall(self, i: int) -> bool:
return self.board[i] == TileType.WALL
Empty file added src/util/__init__.py
Empty file.
18 changes: 9 additions & 9 deletions src/util/util.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import logging

BYTE_UNITS = {
"B": 1024 ** 0,
"KB": 1024 ** 1,
"MB": 1024 ** 2,
"GB": 1024 ** 3,
}


def exit_with_error(message: str):
logging.error(message)
exit(-1)


def convert_size(size, from_unit, to_unit):
units = {
"B": 1024 ** 0,
"KB": 1024 ** 1,
"MB": 1024 ** 2,
"GB": 1024 ** 3,
}

return size * units[from_unit] // units[to_unit]
def convert_size(size: int, from_unit: str, to_unit: str) -> int:
return size * BYTE_UNITS[from_unit] // BYTE_UNITS[to_unit]

0 comments on commit 7997786

Please sign in to comment.