Skip to content

Commit 6219320

Browse files
MarkKoz64json
authored andcommitted
Clean up type annotations
* Add SerializableSequence alias to support tuples in addition to lists * Replace list comprehension with a tuple * Add missing type annotations * Remove unused imports
1 parent a4c501a commit 6219320

File tree

6 files changed

+24
-29
lines changed

6 files changed

+24
-29
lines changed

algorithm_visualizer/commander.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import string
3-
from typing import Any, Dict, List, Optional, Union
3+
from typing import Dict, List, Optional, Union
44

55
from algorithm_visualizer import Randomize
66
from algorithm_visualizer.types import Serializable, Undefined
@@ -12,9 +12,9 @@
1212
class Commander:
1313
_keyRandomizer = Randomize.String(8, string.ascii_lowercase + string.digits)
1414
_objectCount = 0
15-
commands: List[Dict[str, Any]] = []
15+
commands: List[Dict[str, Serializable]] = []
1616

17-
def __init__(self, *args):
17+
def __init__(self, *args: Serializable):
1818
self._objectCount += 1
1919
self.key = self._keyRandomizer.create()
2020
self.command(self.__class__.__name__, *args)

algorithm_visualizer/layouts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from typing import List
1+
from typing import Iterable
22

33
from algorithm_visualizer import Commander
44
from .types import UNDEFINED
55

66

77
class Layout(Commander):
8-
def __init__(self, children: List[Commander]):
9-
super().__init__([c.key for c in children])
8+
def __init__(self, children: Iterable[Commander]):
9+
super().__init__(tuple(c.key for c in children))
1010

1111
@classmethod
1212
def setRoot(cls, child: Commander):

algorithm_visualizer/tracers/array1d.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
from typing import List
2-
31
from . import chart
42
from .array2d import Array2DTracer
5-
from algorithm_visualizer.types import Serializable, UNDEFINED
3+
from algorithm_visualizer.types import Serializable, SerializableSequence, UNDEFINED
64

75

86
class Array1DTracer(Array2DTracer):
9-
def set(self, array1d: List[Serializable] = UNDEFINED):
7+
def set(self, array1d: SerializableSequence[Serializable] = UNDEFINED):
108
self.command("set", array1d)
119

1210
def patch(self, x: int, v: Serializable = UNDEFINED):

algorithm_visualizer/tracers/array2d.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
from typing import List
2-
31
from .tracer import Tracer
4-
from algorithm_visualizer.types import Serializable, UNDEFINED
2+
from algorithm_visualizer.types import Serializable, SerializableSequence, UNDEFINED
53

64

75
class Array2DTracer(Tracer):
8-
def set(self, array2d: List[List[Serializable]] = UNDEFINED):
6+
def set(self, array2d: SerializableSequence[SerializableSequence[Serializable]] = UNDEFINED):
97
self.command("set", array2d)
108

119
def patch(self, x: int, y: int, v: Serializable = UNDEFINED):

algorithm_visualizer/tracers/graph.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
from typing import List, Optional
2-
31
from .log import LogTracer, Tracer
4-
from algorithm_visualizer.types import Number, Serializable, UNDEFINED
2+
from algorithm_visualizer.types import Number, Serializable, SerializableSequence, UNDEFINED
53

64

75
class GraphTracer(Tracer):
8-
def set(self, array2d: List[List[Serializable]] = UNDEFINED):
6+
def set(self, array2d: SerializableSequence[SerializableSequence[Serializable]] = UNDEFINED):
97
self.command("set", array2d)
108

119
def directed(self, isDirected: bool = UNDEFINED):
1210
self.command("directed", isDirected)
1311

14-
def weighted(self, isWeighted: bool = UNDEFINED):
12+
def weighted(self, isWeighted: bool = UNDEFINED) -> "GraphTracer":
1513
self.command("weighted", isWeighted)
1614
return self
1715

18-
def layoutCircle(self):
16+
def layoutCircle(self) -> "GraphTracer":
1917
self.command("layoutCircle")
2018
return self
2119

22-
def layoutTree(self, root: Serializable = UNDEFINED, sorted: bool = UNDEFINED):
20+
def layoutTree(self, root: Serializable = UNDEFINED, sorted: bool = UNDEFINED) -> "GraphTracer":
2321
self.command("layoutTree", root, sorted)
2422
return self
2523

26-
def layoutRandom(self):
24+
def layoutRandom(self) -> "GraphTracer":
2725
self.command("layoutRandom")
2826
return self
2927

3028
def addNode(
3129
self,
3230
id: Serializable,
33-
weight: Optional[Number] = UNDEFINED,
31+
weight: Number = UNDEFINED,
3432
x: int = 0,
3533
y: int = 0,
3634
visitedCount: int = 0,
@@ -41,7 +39,7 @@ def addNode(
4139
def updateNode(
4240
self,
4341
id: Serializable,
44-
weight: Optional[Number] = UNDEFINED,
42+
weight: Number = UNDEFINED,
4543
x: int = UNDEFINED,
4644
y: int = UNDEFINED,
4745
visitedCount: int = UNDEFINED,
@@ -56,7 +54,7 @@ def addEdge(
5654
self,
5755
source: Serializable,
5856
target: Serializable,
59-
weight: Optional[Number] = UNDEFINED,
57+
weight: Number = UNDEFINED,
6058
visitedCount: int = UNDEFINED,
6159
selectedCount: int = UNDEFINED
6260
):
@@ -66,7 +64,7 @@ def updateEdge(
6664
self,
6765
source: Serializable,
6866
target: Serializable,
69-
weight: Optional[Number] = UNDEFINED,
67+
weight: Number = UNDEFINED,
7068
visitedCount: int = UNDEFINED,
7169
selectedCount: int = UNDEFINED
7270
):
@@ -79,15 +77,15 @@ def visit(
7977
self,
8078
target: Serializable,
8179
source: Serializable = UNDEFINED,
82-
weight: Optional[Number] = UNDEFINED
80+
weight: Number = UNDEFINED
8381
):
8482
self.command("visit", target, source, weight)
8583

8684
def leave(
8785
self,
8886
target: Serializable,
8987
source: Serializable = UNDEFINED,
90-
weight: Optional[Number] = UNDEFINED
88+
weight: Number = UNDEFINED
9189
):
9290
self.command("leave", target, source, weight)
9391

algorithm_visualizer/types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from typing import Union
22

33
# Types which are serializable by the default JSONEncoder
4-
Serializable = Union[dict, list, str, int, float, bool, None]
4+
Serializable = Union[dict, list, tuple, str, int, float, bool, None]
5+
SerializableSequence = Union[list, tuple]
56
Number = Union[int, float]
67

78

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy