Skip to content

Commit 97f0363

Browse files
committed
Cleaned up code and updated docstrings
Signed-off-by: Gabriele A. Ron <[email protected]>
1 parent 52a87c1 commit 97f0363

File tree

7 files changed

+30
-32
lines changed

7 files changed

+30
-32
lines changed

src/project_dilemma/interfaces/algorithm.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""
1616
from abc import abstractmethod
1717
from collections.abc import Sequence
18-
from typing import Optional, Self, Type
18+
from typing import Optional, Self
1919

2020
import project_dilemma.interfaces.base as pd_int_base
2121

@@ -37,9 +37,9 @@ class Algorithm(pd_int_base.Base):
3737
]
3838

3939
algorithm_id: str
40-
mutations: Optional[Sequence[Type[Self]]] = None
40+
mutations: Optional[Sequence[type[Self]]] = None
4141

42-
def __init__(self, mutations: Optional[Sequence[Type[Self]]] = None, **kwargs) -> None:
42+
def __init__(self, mutations: Optional[Sequence[type[Self]]] = None, **kwargs) -> None:
4343
self.mutations = mutations
4444

4545
def __eq__(self, other: Self):
@@ -51,7 +51,7 @@ def decide(rounds: pd_int_base.Rounds) -> bool:
5151
"""decide whether to cooperate or not
5252
5353
:param rounds: the list of moves
54-
:type rounds: MoveList
54+
:type rounds: Rounds
5555
:return: whether to cooperate or not
5656
:rtype: bool
5757
"""

src/project_dilemma/interfaces/generational_simulation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections.abc import MutableMapping, Sequence
33
from copy import deepcopy
44
import sys
5-
from typing import Any, Optional, Type
5+
from typing import Any, Optional
66

77
import project_dilemma.interfaces.base as pd_int_base
88
import project_dilemma.interfaces.node as pd_int_node
@@ -15,7 +15,7 @@ class GenerationalSimulation(pd_int_simulation.SimulationBase):
1515
:var generations: number of generations to run
1616
:vartype generations: int
1717
:var generational_simulation: simulation class to use
18-
:vartype simulation: Type[project_dilemma.interfaces.simulation.SimulationBase]
18+
:vartype simulation: type[SimulationBase]
1919
:var simulation_kwargs: keyword arguments to pass into the simulation
2020
:vartype simulation_kwargs: MutableMapping[str, Any]
2121
"""
@@ -27,15 +27,15 @@ class GenerationalSimulation(pd_int_simulation.SimulationBase):
2727
]
2828

2929
generations: int
30-
generational_simulation: Type[pd_int_simulation.SimulationBase]
30+
generational_simulation: type[pd_int_simulation.SimulationBase]
3131
simulation_kwargs: MutableMapping[str, Any]
3232
_simulation_data: pd_int_base.Generations
3333

3434
def __init__(self,
3535
simulation_id: str,
3636
nodes: Sequence[pd_int_node.Node],
3737
generations: int,
38-
generational_simulation: Type[pd_int_simulation.SimulationBase],
38+
generational_simulation: type[pd_int_simulation.SimulationBase],
3939
simulation_data: Optional[pd_int_base.Simulations] = None,
4040
**kwargs):
4141
super().__init__(nodes=nodes, simulation_id=simulation_id, simulation_data=simulation_data)

src/project_dilemma/interfaces/node.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
"""
1616
import random
17-
from typing import Self, Type
17+
from typing import Self
1818

1919
import project_dilemma.interfaces.algorithm as pd_int_algorithm
2020
import project_dilemma.interfaces.base as pd_int_base
@@ -28,7 +28,7 @@ class Node(pd_int_base.Base):
2828
:var node_id: id of the node
2929
:vartype node_id: str
3030
:var algorithm: cooperation algorithm
31-
:vartype algorithm: Type[Algorithm]
31+
:vartype algorithm: type[Algorithm]
3232
"""
3333
_required_attributes = [
3434
'algorithm',
@@ -37,9 +37,9 @@ class Node(pd_int_base.Base):
3737
]
3838

3939
node_id: str
40-
algorithm: Type[pd_int_algorithm.Algorithm]
40+
algorithm: type[pd_int_algorithm.Algorithm]
4141

42-
def __init__(self, node_id: str, algorithm: Type[pd_int_algorithm.Algorithm], **kwargs):
42+
def __init__(self, node_id: str, algorithm: type[pd_int_algorithm.Algorithm], **kwargs):
4343
self.node_id = node_id
4444
self.algorithm = algorithm
4545

src/project_dilemma/interfaces/simulation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SimulationBase(pd_int_base.Base):
2929
all the nodes must have unique node ids
3030
3131
:var nodes: node data for the simulation
32-
:vartype nodes: Sequence[Node]
32+
:vartype nodes: Sequence[Type[Node]]
3333
:var simulation_id: id of the simulation
3434
:vartype simulation_id: str
3535
:var simulation_data: simulation round data
@@ -45,7 +45,7 @@ class SimulationBase(pd_int_base.Base):
4545

4646
simulation_id: str
4747
_simulation_data: pd_int_base.Generations | pd_int_base.Simulations
48-
_nodes: Sequence[pd_int_node.Node]
48+
_nodes: Sequence[type[pd_int_node.Node]]
4949

5050
@abstractmethod
5151
def __init__(self,
@@ -59,11 +59,11 @@ def __init__(self,
5959
self.simulation_data = simulation_data
6060

6161
@property
62-
def nodes(self) -> Sequence[pd_int_node.Node]:
62+
def nodes(self) -> Sequence[type[pd_int_node.Node]]:
6363
return self._nodes
6464

6565
@nodes.setter
66-
def nodes(self, nodes: Sequence[pd_int_node.Node]):
66+
def nodes(self, nodes: Sequence[type[pd_int_node.Node]]):
6767
if max(Counter([node.node_id for node in nodes]).values()) > 1:
6868
raise ValueError('All node ids provided must be unique')
6969

src/project_dilemma/object_loaders.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
import json
33
import os.path
44
import sys
5-
from typing import Dict, List, Type
5+
from typing import Dict, List
66

77
from project_dilemma.config import ProjectDilemmaConfig
88
from project_dilemma.interfaces import Algorithm, Generations, Node, SimulationBase, Simulations
99
from project_dilemma.simulations import simulations_map
1010

1111

12-
def create_nodes(config: ProjectDilemmaConfig, algorithms_map: Dict[str, Type[Algorithm]]) -> List[Node]:
12+
def create_nodes(config: ProjectDilemmaConfig, algorithms_map: Dict[str, type[Algorithm]]) -> List[type[Node]]:
1313
"""create the simulation nodes
1414
1515
:param config: configuration data
1616
:type config: ProjectDilemmaConfig
1717
:param algorithms_map: map of algorithm class names to algorithms
18-
:type algorithms_map: Dict[str, Type[Algorithm]]
19-
:return:
18+
:type algorithms_map: Dict[str, type[Algorithm]]
19+
:return: list of nodes
20+
:rtype: List[type[Node]]
2021
"""
2122
nodes = []
2223

@@ -26,18 +27,18 @@ def create_nodes(config: ProjectDilemmaConfig, algorithms_map: Dict[str, Type[Al
2627
return nodes
2728

2829

29-
def load_algorithms(config: ProjectDilemmaConfig) -> Dict[str, Type[Algorithm]]:
30+
def load_algorithms(config: ProjectDilemmaConfig) -> Dict[str, type[Algorithm]]:
3031
"""load all algorithms used
3132
3233
:param config: configuration data
3334
:type config: ProjectDilemmaConfig
3435
:return: map of algorithm class names to algorithms
35-
:rtype: Dict[str, Type[Algorithm]]
36+
:rtype: Dict[str, type[Algorithm]]
3637
"""
3738
sys.path.append(config['algorithms_directory'])
3839

3940
algorithms = [node['algorithm'] for node in config['nodes']]
40-
algorithm_map: Dict[str, Type[Algorithm]] = {}
41+
algorithm_map: Dict[str, type[Algorithm]] = {}
4142

4243
for algorithm in algorithms:
4344
if algorithm_map.get(algorithm['object']):
@@ -76,15 +77,15 @@ def load_simulation_data(config: ProjectDilemmaConfig) -> Generations | Simulati
7677
return data
7778

7879

79-
def load_simulation(config: ProjectDilemmaConfig, *, generational: bool = False) -> Type[SimulationBase]:
80+
def load_simulation(config: ProjectDilemmaConfig, *, generational: bool = False) -> type[SimulationBase]:
8081
"""load the simulation
8182
8283
:param config: configuration data
8384
:type config: ProjectDilemmaConfig
8485
:param generational: if the generational simulation should be loaded
8586
:type generational: bool
8687
:return: the configured simulation
87-
:rtype: Type[SimulationBase]
88+
:rtype: type[SimulationBase]
8889
"""
8990
key = 'simulation'
9091
if generational:

src/project_dilemma/simulations/basic_simulation.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,23 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16-
from abc import abstractmethod
1716
from collections.abc import Sequence
1817
import random
1918
from typing import Optional
2019

2120
from project_dilemma.interfaces import Node, Round, Rounds, Simulation, Simulations
2221

2322

24-
def play_round(nodes: Sequence[Node],
23+
def play_round(nodes: Sequence[type[Node]],
2524
rounds: Rounds,
2625
*,
2726
mutations_per_mille: int,
2827
round_mutations: bool = False,) -> Round:
2928
"""run a round of prisoners dilemma with each node
3029
3130
:param nodes: nodes to run
32-
:type nodes: Sequence[Node]
33-
:param rounds: lsit of rounds
31+
:type nodes: Sequence[type[Node]]
32+
:param rounds: list of rounds
3433
:type rounds: RoundList
3534
:param mutations_per_mille: rate that mutations should occur per mille
3635
:type mutations_per_mille: int
@@ -94,7 +93,7 @@ def run_simulation(self) -> Simulations:
9493
"""run the simulation
9594
9695
:return: simulation results
97-
:rtype: RoundList
96+
:rtype: Rounds
9897
"""
9998
game_id = ':'.join(sorted(node.node_id for node in self.nodes))
10099

src/project_dilemma/simulations/standard_generational_simulation.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import math
2-
31
from project_dilemma.interfaces import GenerationalSimulation, Node
42

53

0 commit comments

Comments
 (0)