Skip to content
Snippets Groups Projects
Unverified Commit 96ac13b9 authored by Björn Ludwig's avatar Björn Ludwig
Browse files

refactor(type_aliases): rename some type aliases, the module itself and include all in __all__

parent fb813af0
No related branches found
No related tags found
1 merge request!1Introduce Chars
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
...@@ -2,9 +2,11 @@ ...@@ -2,9 +2,11 @@
__all__ = ["Chars"] __all__ = ["Chars"]
from typing import Optional, Union import string
from itertools import product
from typing import Optional, Tuple, Union
from src.ilp_keyboard_layout_optimization.type_aliases import CharTuple from ..type_aliases import Bigram, CharTuple
class Chars: class Chars:
......
...@@ -14,6 +14,17 @@ from .type_aliases import ( ...@@ -14,6 +14,17 @@ from .type_aliases import (
) )
from pyscipopt import Model, quicksum from pyscipopt import Model, quicksum
from .data_aquisition.chars import Chars
from .type_aliases import (
CharPosPair,
CharPosQuadruple,
LinCosts,
LinVars,
PosTuple,
QuadCosts,
QuadVars,
)
class KeyboardOptimization: class KeyboardOptimization:
"""Instances of this class represent instances of the keyboard layout QAP """Instances of this class represent instances of the keyboard layout QAP
...@@ -179,12 +190,12 @@ class KeyboardOptimization: ...@@ -179,12 +190,12 @@ class KeyboardOptimization:
print(f"{str(solution_assignments)}") print(f"{str(solution_assignments)}")
@property @property
def char_key_assigns_keys(self) -> Iterable[CharKeyPair]: def char_key_assigns_keys(self) -> Iterable[CharPosPair]:
"""An iterator for the pairs of (special) characters and corresponding keys""" """An iterator for the pairs of (special) characters and corresponding keys"""
return product(self.chars, self.keys) return product(self.chars, self.keys)
@property @property
def quad_char_key_assigns_keys(self) -> Iterable[CharKeyQuadruple]: def quad_char_key_assigns_keys(self) -> Iterable[CharPosQuadruple]:
"""An iterator for quadruples of character pairs and corresponding key pairs""" """An iterator for quadruples of character pairs and corresponding key pairs"""
flattened_tuple_of_quads = chain.from_iterable( flattened_tuple_of_quads = chain.from_iterable(
chain.from_iterable( chain.from_iterable(
......
"""This module contains custom types for type hints and thus more convenient coding""" """This module contains type aliases for type hints and thus more convenient coding"""
__all__ = [ __all__ = [
"CharKeyPair", "Bigram",
"CharKeyQuadruple", "Char",
"CharTuple", "CharPosPair",
"CharPosQuadruple",
"CharSet", "CharSet",
"KeyTuple", "CharTuple",
"LinCosts", "LinCosts",
"LinVars", "LinVars",
"Pos",
"PosPair",
"PosTuple",
"QuadCosts", "QuadCosts",
"QuadVars", "QuadVars",
] ]
Char = str Char = str
"""A (special) character""" """A (special) character"""
Key = str Pos = str
"""A key""" """A position"""
CharKeyPair = tuple[Char, Key] CharPosPair = tuple[Char, Pos]
"""A pair of a (special) character and a key""" """A pair of a (special) character and a position"""
Bigram = tuple[Char, Char] Bigram = str
"""A tuple of two (special) characters""" """A length-two string of (special) characters"""
KeyPair = tuple[Key, Key] PosPair = tuple[Pos, Pos]
"""A tuple of two keys""" """A tuple of two positions"""
CharTuple = tuple[Char, ...] CharTuple = tuple[Char, ...]
"""A tuple of several (special) characters""" """A tuple of several (special) characters"""
CharSet = set[Char] CharSet = set[Char]
"""A set of several (special) characters""" """A set of several (special) characters"""
KeyTuple = tuple[Key, ...] PosTuple = tuple[Pos, ...]
"""A tuple of several keys""" """A tuple of several positions"""
LinCosts = dict[CharKeyPair, float] LinCosts = dict[CharPosPair, float]
"""A dictionary assigning costs to (special) character bigrams""" """A dictionary assigning costs to (special) character bigrams"""
CharKeyQuadruple = tuple[Char, Key, Char, Key] CharPosQuadruple = tuple[Char, Char, Pos, Pos]
"""A four-tuple: (special) character, key, another (special) character, another key""" """A four-tuple: two (special) characters and their respective positions"""
QuadCosts = dict[CharKeyQuadruple, float] QuadCosts = dict[CharPosQuadruple, float]
"""A dictionary assigning costs to (special) character, key quadruples""" """A dictionary assigning costs to (special) character, position quadruples"""
LinVars = dict[CharKeyPair, bool] LinVars = dict[CharPosPair, bool]
"""A dictionary of binary decisions of assigning (special) characters to keys""" """A dictionary of binary decisions of assigning (special) characters to positions"""
QuadVars = dict[CharKeyQuadruple, bool] QuadVars = dict[CharPosQuadruple, bool]
"""A dictionary of binary decisions of assigning two (special) characters to two keys""" """A dictionary of binary vars assigning two (special) characters to two positions"""
from ilp_keyboard_layout_optimization.costs import FreqTuple from ilp_keyboard_layout_optimization.costs import FreqTuple
from ilp_keyboard_layout_optimization.types import ( from ilp_keyboard_layout_optimization.type_aliases import (
Bigram, Bigram,
Char, Char,
CharKeyPair, CharPosPair,
CharKeyQuadruple, CharPosQuadruple,
CharTuple, CharTuple,
Key, Pos,
KeyPair, PosPair,
KeyTuple, PosTuple,
LinCosts, LinCosts,
LinVars, LinVars,
QuadCosts, QuadCosts,
...@@ -25,23 +25,23 @@ def test_char(): ...@@ -25,23 +25,23 @@ def test_char():
def test_key(): def test_key():
assert Key == str assert Pos == str
def test_char_key_pair(): def test_char_key_pair():
assert CharKeyPair == tuple[Char, Key] assert CharPosPair == tuple[Char, Pos]
def test_bigram(): def test_bigram():
assert Bigram == tuple[Char, Char] assert Bigram == str
def test_pos_pair(): def test_pos_pair():
assert KeyPair == tuple[Key, Key] assert PosPair == tuple[Pos, Pos]
def test_lin_costs(): def test_lin_costs():
assert LinCosts == dict[CharKeyPair, float] assert LinCosts == dict[CharPosPair, float]
def test_char_tuple(): def test_char_tuple():
...@@ -49,20 +49,20 @@ def test_char_tuple(): ...@@ -49,20 +49,20 @@ def test_char_tuple():
def test_pos_tuple(): def test_pos_tuple():
assert KeyTuple == tuple[Key, ...] assert PosTuple == tuple[Pos, ...]
def test_lin_vars(): def test_lin_vars():
assert LinVars == dict[CharKeyPair, bool] assert LinVars == dict[CharPosPair, bool]
def test_quad_pos_quadruple(): def test_quad_pos_quadruple():
assert CharKeyQuadruple == tuple[Char, Key, Char, Key] assert CharPosQuadruple == tuple[Char, Pos, Char, Pos]
def test_quad_costs(): def test_quad_costs():
assert QuadCosts == dict[CharKeyQuadruple, float] assert QuadCosts == dict[CharPosQuadruple, float]
def test_quad_vars(): def test_quad_vars():
assert QuadVars == dict[CharKeyQuadruple, bool] assert QuadVars == dict[CharPosQuadruple, bool]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment