Skip to content

Commit 057df86

Browse files
committed
Make Node a dataclass
Dataclasses make the code more compact and generate many convenience functions.
1 parent 9e32c26 commit 057df86

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

quoracle/expr.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from dataclasses import dataclass
12
from typing import Dict, Iterator, Generic, List, Optional, Set, TypeVar
23
import datetime
34
import itertools
@@ -89,14 +90,19 @@ def _dup_free_min_failures(self) -> int:
8990
raise NotImplementedError
9091

9192

93+
@dataclass
9294
class Node(Expr[T]):
93-
def __init__(self,
94-
x: T,
95-
capacity: Optional[float] = None,
96-
read_capacity: Optional[float] = None,
97-
write_capacity: Optional[float] = None,
98-
latency: datetime.timedelta = None) -> None:
99-
self.x = x
95+
x: T
96+
capacity: Optional[float] = None
97+
read_capacity: Optional[float] = None
98+
write_capacity: Optional[float] = None
99+
latency: Optional[datetime.timedelta] = None
100+
101+
def post_init(self):
102+
capacity = self.capacity
103+
read_capacity = self.read_capacity
104+
write_capacity = self.write_capacity
105+
latency = self.latency
100106

101107
# A user either specifies capacity or (read_capacity and
102108
# write_capacity), but not both.
@@ -124,6 +130,8 @@ def __init__(self,
124130
else:
125131
self.latency = latency
126132

133+
def __hash__(self):
134+
return self.x.__hash__()
127135

128136
def __str__(self) -> str:
129137
return str(self.x)

quoracle/quorum_system.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,17 @@ def fr_load(problem: pulp.LpProblem, fr: float) -> pulp.LpAffineExpression:
520520

521521
if x in x_to_read_quorum_vars:
522522
vs = x_to_read_quorum_vars[x]
523-
x_load += fr * sum(vs) / self.node(x).read_capacity
523+
xread_capacity = self.node(x).read_capacity
524+
if xread_capacity is None:
525+
xread_capacity = self.node(x).read_capacity = 1.0
526+
x_load += fr * sum(vs) / xread_capacity
524527

525528
if x in x_to_write_quorum_vars:
526529
vs = x_to_write_quorum_vars[x]
527-
x_load += (1 - fr) * sum(vs) / self.node(x).write_capacity
530+
xwrite_capacity = self.node(x).write_capacity
531+
if xwrite_capacity is None:
532+
xwrite_capacity = self.node(x).write_capacity = 1.0
533+
x_load += (1 - fr) * sum(vs) / xwrite_capacity
528534

529535
problem += (x_load <= l, f'{x}{fr}')
530536

0 commit comments

Comments
 (0)