Skip to content

Commit df4d5f4

Browse files
MarkKoz64json
authored andcommitted
Replace default values with a new singleton named UNDEFINED
1 parent 011ec2a commit df4d5f4

File tree

8 files changed

+57
-69
lines changed

8 files changed

+57
-69
lines changed

algorithm_visualizer/commander.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import string
2-
from typing import Any, Dict, List, Optional
2+
from typing import Any, Dict, List, Optional, Union
33

44
from algorithm_visualizer import Randomize
5-
from algorithm_visualizer.types import Serializable
5+
from algorithm_visualizer.types import Serializable, Undefined
66

77
_MAX_COMMANDS = 1000000
88
_MAX_OBJECTS = 100
@@ -19,11 +19,11 @@ def __init__(self, *args):
1919
self.command(self.__class__.__name__, *args)
2020

2121
@classmethod
22-
def _command(cls, key: Optional[str], method: str, *args: Serializable):
22+
def _command(cls, key: Optional[str], method: str, *args: Union[Serializable, Undefined]):
2323
cmd = {
2424
"key": key,
2525
"method": method,
26-
"args": args
26+
"args": tuple(a for a in args if not isinstance(a, Undefined))
2727
}
2828
cls.commands.append(cmd)
2929

algorithm_visualizer/layouts.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import List
22

33
from algorithm_visualizer import Commander
4+
from .types import UNDEFINED
45

56

67
class Layout(Commander):
@@ -11,11 +12,8 @@ def __init__(self, children: List[Commander]):
1112
def setRoot(cls, child: Commander):
1213
cls._command(None, "setRoot", child.key)
1314

14-
def add(self, child: Commander, index: int = None):
15-
if index is None:
16-
self.command("add", child.key)
17-
else:
18-
self.command("add", child.key, index)
15+
def add(self, child: Commander, index: int = UNDEFINED):
16+
self.command("add", child.key, index)
1917

2018
def remove(self, child: Commander):
2119
self.command("remove", child.key)

algorithm_visualizer/tracers/array1d.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
from . import chart
44
from .array2d import Array2DTracer
5-
from algorithm_visualizer.types import Serializable
5+
from algorithm_visualizer.types import Serializable, UNDEFINED
66

77

88
class Array1DTracer(Array2DTracer):
9-
def set(self, array1d: List[Serializable] = None):
10-
super().set(array1d)
9+
def set(self, array1d: List[Serializable] = UNDEFINED):
10+
self.command("set", array1d)
1111

12-
def patch(self, x: int, v: Serializable = None):
12+
def patch(self, x: int, v: Serializable = UNDEFINED):
1313
self.command("patch", x, v)
1414

1515
def depatch(self, x: int):
1616
self.command("depatch", x)
1717

18-
def select(self, sx: int, ex: int = None):
19-
super().select(0, sx, 0, ex)
18+
def select(self, sx: int, ex: int = UNDEFINED):
19+
self.command("select", sx, ex)
2020

21-
def deselect(self, sx: int, ex: int = None):
22-
super().deselect(0, sx, 0, ex)
21+
def deselect(self, sx: int, ex: int = UNDEFINED):
22+
self.command("deselect", sx, ex)
2323

2424
def chart(self, chartTracer: "chart.ChartTracer"):
2525
self.command("chart", chartTracer.key)

algorithm_visualizer/tracers/array2d.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
from typing import List
22

33
from .tracer import Tracer
4-
from algorithm_visualizer.types import Serializable
4+
from algorithm_visualizer.types import Serializable, UNDEFINED
55

66

77
class Array2DTracer(Tracer):
8-
def set(self, array2d: List[List[Serializable]] = None):
9-
if array2d is None:
10-
array2d = []
8+
def set(self, array2d: List[List[Serializable]] = UNDEFINED):
119
self.command("set", array2d)
1210

13-
def patch(self, x: int, y: int, v: Serializable = None):
11+
def patch(self, x: int, y: int, v: Serializable = UNDEFINED):
1412
self.command("patch", x, y, v)
1513

1614
def depatch(self, x: int, y: int):
1715
self.command("depatch", x, y)
1816

19-
def select(self, sx: int, sy: int, ex: int = None, ey: int = None):
20-
if ex is None:
21-
ex = sx
22-
if ey is None:
23-
ey = sy
17+
def select(self, sx: int, sy: int, ex: int = UNDEFINED, ey: int = UNDEFINED):
2418
self.command("select", sx, sy, ex, ey)
2519

2620
def selectRow(self, x: int, sy: int, ey: int):
@@ -29,11 +23,7 @@ def selectRow(self, x: int, sy: int, ey: int):
2923
def selectCol(self, y: int, sx: int, ex: int):
3024
self.command("selectCol", y, sx, ex)
3125

32-
def deselect(self, sx: int, sy: int, ex: int = None, ey: int = None):
33-
if ex is None:
34-
ex = sx
35-
if ey is None:
36-
ey = sy
26+
def deselect(self, sx: int, sy: int, ex: int = UNDEFINED, ey: int = UNDEFINED):
3727
self.command("deselect", sx, sy, ex, ey)
3828

3929
def deselectRow(self, x: int, sy: int, ey: int):

algorithm_visualizer/tracers/graph.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
from typing import List, Optional
22

33
from .log import LogTracer, Tracer
4-
from algorithm_visualizer.types import Number, Serializable
4+
from algorithm_visualizer.types import Number, Serializable, UNDEFINED
55

66

77
class GraphTracer(Tracer):
8-
def set(self, array2d: List[List[Serializable]] = None):
9-
if array2d is None:
10-
array2d = []
8+
def set(self, array2d: List[List[Serializable]] = UNDEFINED):
119
self.command("set", array2d)
1210

13-
def directed(self, isDirected: bool = True):
11+
def directed(self, isDirected: bool = UNDEFINED):
1412
self.command("directed", isDirected)
1513

16-
def weighted(self, isWeighted: bool = True):
14+
def weighted(self, isWeighted: bool = UNDEFINED):
1715
self.command("weighted", isWeighted)
1816
return self
1917

2018
def layoutCircle(self):
2119
self.command("layoutCircle")
2220
return self
2321

24-
def layoutTree(self, root: Any = 0, sorted: bool = False):
22+
def layoutTree(self, root: Serializable = UNDEFINED, sorted: bool = UNDEFINED):
2523
self.command("layoutTree", root, sorted)
2624
return self
2725

@@ -32,7 +30,7 @@ def layoutRandom(self):
3230
def addNode(
3331
self,
3432
id: Serializable,
35-
weight: Optional[Number] = None,
33+
weight: Optional[Number] = UNDEFINED,
3634
x: int = 0,
3735
y: int = 0,
3836
visitedCount: int = 0,
@@ -43,11 +41,11 @@ def addNode(
4341
def updateNode(
4442
self,
4543
id: Serializable,
46-
weight: Optional[Number] = None,
47-
x: int = 0,
48-
y: int = 0,
49-
visitedCount: int = 0,
50-
selectedCount: int = 0
44+
weight: Optional[Number] = UNDEFINED,
45+
x: int = UNDEFINED,
46+
y: int = UNDEFINED,
47+
visitedCount: int = UNDEFINED,
48+
selectedCount: int = UNDEFINED
5149
):
5250
self.command("updateNode", id, weight, x, y, visitedCount, selectedCount)
5351

@@ -58,19 +56,19 @@ def addEdge(
5856
self,
5957
source: Serializable,
6058
target: Serializable,
61-
weight: Optional[Number] = None,
62-
visitedCount: int = 0,
63-
selectedCount: int = 0
59+
weight: Optional[Number] = UNDEFINED,
60+
visitedCount: int = UNDEFINED,
61+
selectedCount: int = UNDEFINED
6462
):
6563
self.command("addEdge", source, target, weight, visitedCount, selectedCount)
6664

6765
def updateEdge(
6866
self,
6967
source: Serializable,
7068
target: Serializable,
71-
weight: Optional[Number] = None,
72-
visitedCount: int = 0,
73-
selectedCount: int = 0
69+
weight: Optional[Number] = UNDEFINED,
70+
visitedCount: int = UNDEFINED,
71+
selectedCount: int = UNDEFINED
7472
):
7573
self.command("updateEdge", source, target, weight, visitedCount, selectedCount)
7674

@@ -80,23 +78,23 @@ def removeEdge(self, source: Serializable, target: Serializable):
8078
def visit(
8179
self,
8280
target: Serializable,
83-
source: Serializable = None,
84-
weight: Optional[Number] = None
81+
source: Serializable = UNDEFINED,
82+
weight: Optional[Number] = UNDEFINED
8583
):
8684
self.command("visit", target, source, weight)
8785

8886
def leave(
8987
self,
9088
target: Serializable,
91-
source: Serializable = None,
92-
weight: Optional[Number] = None
89+
source: Serializable = UNDEFINED,
90+
weight: Optional[Number] = UNDEFINED
9391
):
9492
self.command("leave", target, source, weight)
9593

96-
def select(self, target: Serializable, source: Serializable = None):
94+
def select(self, target: Serializable, source: Serializable = UNDEFINED):
9795
self.command("select", target, source)
9896

99-
def deselect(self, target: Serializable, source: Serializable = None):
97+
def deselect(self, target: Serializable, source: Serializable = UNDEFINED):
10098
self.command("deselect", target, source)
10199

102100
def log(self, log: LogTracer):

algorithm_visualizer/tracers/log.py

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

44

55
class LogTracer(Tracer):
6-
def set(self, log: Serializable):
6+
def set(self, log: Serializable = UNDEFINED):
77
self.command("set", log)
88

99
def print(self, message: Serializable):

algorithm_visualizer/tracers/tracer.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
from algorithm_visualizer import Commander
2+
from algorithm_visualizer.types import UNDEFINED
23

34

45
class Tracer(Commander):
5-
def __init__(self, title: str = None):
6-
if title is None:
7-
super().__init__()
8-
else:
9-
super().__init__(title)
6+
def __init__(self, title: str = UNDEFINED):
7+
super().__init__(title)
108

119
@classmethod
12-
def delay(cls, lineNumber: int = None):
13-
if lineNumber is None:
14-
cls._command(None, "delay")
15-
else:
16-
cls._command(None, "delay", lineNumber)
10+
def delay(cls, lineNumber: int = UNDEFINED):
11+
cls._command(None, "delay", lineNumber)
1712

1813
def set(self):
1914
self.command("set")

algorithm_visualizer/types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@
33
# Types which are serializable by the default JSONEncoder
44
Serializable = Union[dict, list, str, int, float, bool, None]
55
Number = Union[int, float]
6+
7+
8+
class Undefined:
9+
pass
10+
11+
12+
UNDEFINED = Undefined()

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