Skip to content

Commit 011ec2a

Browse files
MarkKoz64json
authored andcommitted
Fix imports
* Move type aliases to a separate module
1 parent 2cf1c9d commit 011ec2a

File tree

10 files changed

+70
-59
lines changed

10 files changed

+70
-59
lines changed

algorithm_visualizer/__init__.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import atexit
22
import json
33
import os
4-
from typing import Union
54

6-
from algorithm_visualizer import randomize as Randomize
7-
from algorithm_visualizer.commander import Commander
8-
from algorithm_visualizer.layouts import *
9-
from algorithm_visualizer.tracers import *
5+
from . import randomize as Randomize
6+
from .commander import Commander
7+
from .layouts import *
8+
from .tracers import *
109

1110
__all__ = (
1211
"Randomize", "Commander",
1312
"Array1DTracer", "Array2DTracer", "ChartTracer", "GraphTracer", "LogTracer", "Tracer",
1413
"HorizontalLayout", "Layout", "VerticalLayout"
1514
)
1615

17-
# Types which are serializable by the default JSONEncoder
18-
_Serializable = Union[dict, list, str, int, float, bool, None]
19-
_Number = Union[int, float]
20-
2116

2217
@atexit.register
2318
def execute():

algorithm_visualizer/commander.py

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

4-
from algorithm_visualizer import Randomize, _Serializable
4+
from algorithm_visualizer import Randomize
5+
from algorithm_visualizer.types import Serializable
56

67
_MAX_COMMANDS = 1000000
78
_MAX_OBJECTS = 100
@@ -18,7 +19,7 @@ def __init__(self, *args):
1819
self.command(self.__class__.__name__, *args)
1920

2021
@classmethod
21-
def _command(cls, key: Optional[str], method: str, *args: _Serializable):
22+
def _command(cls, key: Optional[str], method: str, *args: Serializable):
2223
cmd = {
2324
"key": key,
2425
"method": method,

algorithm_visualizer/layouts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from algorithm_visualizer import Commander
44

5+
56
class Layout(Commander):
67
def __init__(self, children: List[Commander]):
78
super().__init__([c.key for c in children])

algorithm_visualizer/randomize.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import string
44
from typing import List, NoReturn, Sequence
55

6-
from algorithm_visualizer import _Number
6+
from algorithm_visualizer.types import Number
77

88

9-
class Randomizer(metaclass=abc.ABCMeta):
9+
class _Randomizer(metaclass=abc.ABCMeta):
1010
@abc.abstractmethod
1111
def create(self) -> NoReturn:
1212
raise NotImplementedError
1313

1414

15-
class Integer(Randomizer):
15+
class Integer(_Randomizer):
1616
def __init__(self, min: int = 1, max: int = 9):
1717
self._min = min
1818
self._max = max
@@ -21,16 +21,16 @@ def create(self) -> int:
2121
return random.randint(self._min, self._max)
2222

2323

24-
class Double(Randomizer):
25-
def __init__(self, min: _Number = 0, max: _Number = 1):
24+
class Double(_Randomizer):
25+
def __init__(self, min: Number = 0, max: Number = 1):
2626
self._min = min
2727
self._max = max
2828

2929
def create(self) -> float:
3030
return random.uniform(self._min, self._max)
3131

3232

33-
class String(Randomizer):
33+
class String(_Randomizer):
3434
def __init__(self, length: int = 16, letters: Sequence[str] = string.ascii_lowercase):
3535
self._length = length
3636
self._letters = letters
@@ -40,8 +40,8 @@ def create(self) -> str:
4040
return "".join(text)
4141

4242

43-
class Array1D(Randomizer):
44-
def __init__(self, N: int = 10, randomizer: Randomizer = Integer()):
43+
class Array1D(_Randomizer):
44+
def __init__(self, N: int = 10, randomizer: _Randomizer = Integer()):
4545
self._N = N
4646
self._randomizer = randomizer
4747
self._sorted = False
@@ -59,7 +59,7 @@ def create(self) -> List:
5959

6060

6161
class Array2D(Array1D):
62-
def __init__(self, N: int = 10, M: int = 10, randomizer: Randomizer = Integer()):
62+
def __init__(self, N: int = 10, M: int = 10, randomizer: _Randomizer = Integer()):
6363
super().__init__(N, randomizer)
6464
self._M = M
6565

@@ -71,8 +71,8 @@ def create(self) -> List[List]:
7171
return [super().create() for _ in range(self._N)]
7272

7373

74-
class Graph(Randomizer):
75-
def __init__(self, N: int = 5, ratio: _Number = 0.3, randomizer: Randomizer = Integer()):
74+
class Graph(_Randomizer):
75+
def __init__(self, N: int = 5, ratio: Number = 0.3, randomizer: _Randomizer = Integer()):
7676
self._N = N
7777
self._ratio = ratio
7878
self._randomizer = randomizer
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from typing import List
22

3-
import algorithm_visualizer
4-
from algorithm_visualizer import Array2DTracer, _Serializable
3+
from . import chart
4+
from .array2d import Array2DTracer
5+
from algorithm_visualizer.types import Serializable
6+
57

68
class Array1DTracer(Array2DTracer):
7-
def set(self, array1d: List[_Serializable] = None):
9+
def set(self, array1d: List[Serializable] = None):
810
super().set(array1d)
911

10-
def patch(self, x: int, v: _Serializable = None):
12+
def patch(self, x: int, v: Serializable = None):
1113
self.command("patch", x, v)
1214

1315
def depatch(self, x: int):
@@ -19,5 +21,5 @@ def select(self, sx: int, ex: int = None):
1921
def deselect(self, sx: int, ex: int = None):
2022
super().deselect(0, sx, 0, ex)
2123

22-
def chart(self, chartTracer: "algorithm_visualizer.ChartTracer"):
24+
def chart(self, chartTracer: "chart.ChartTracer"):
2325
self.command("chart", chartTracer.key)

algorithm_visualizer/tracers/array2d.py

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

3-
from algorithm_visualizer import Tracer, _Serializable
3+
from .tracer import Tracer
4+
from algorithm_visualizer.types import Serializable
5+
46

57
class Array2DTracer(Tracer):
6-
def set(self, array2d: List[List[_Serializable]] = None):
8+
def set(self, array2d: List[List[Serializable]] = None):
79
if array2d is None:
810
array2d = []
911
self.command("set", array2d)
1012

11-
def patch(self, x: int, y: int, v: _Serializable = None):
13+
def patch(self, x: int, y: int, v: Serializable = None):
1214
self.command("patch", x, y, v)
1315

1416
def depatch(self, x: int, y: int):

algorithm_visualizer/tracers/chart.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from algorithm_visualizer import Array1DTracer
1+
from .array1d import Array1DTracer
2+
23

34
class ChartTracer(Array1DTracer):
45
pass

algorithm_visualizer/tracers/graph.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from typing import Any, List, Optional
1+
from typing import List, Optional
2+
3+
from .log import LogTracer, Tracer
4+
from algorithm_visualizer.types import Number, Serializable
25

3-
from algorithm_visualizer import LogTracer, Tracer, _Number, _Serializable
46

57
class GraphTracer(Tracer):
6-
def set(self, array2d: List[List[_Serializable]] = None):
8+
def set(self, array2d: List[List[Serializable]] = None):
79
if array2d is None:
810
array2d = []
911
self.command("set", array2d)
@@ -29,8 +31,8 @@ def layoutRandom(self):
2931

3032
def addNode(
3133
self,
32-
id: _Serializable,
33-
weight: Optional[_Number] = None,
34+
id: Serializable,
35+
weight: Optional[Number] = None,
3436
x: int = 0,
3537
y: int = 0,
3638
visitedCount: int = 0,
@@ -40,61 +42,61 @@ def addNode(
4042

4143
def updateNode(
4244
self,
43-
id: _Serializable,
44-
weight: Optional[_Number] = None,
45+
id: Serializable,
46+
weight: Optional[Number] = None,
4547
x: int = 0,
4648
y: int = 0,
4749
visitedCount: int = 0,
4850
selectedCount: int = 0
4951
):
5052
self.command("updateNode", id, weight, x, y, visitedCount, selectedCount)
5153

52-
def removeNode(self, id: _Serializable):
54+
def removeNode(self, id: Serializable):
5355
self.command("removeNode", id)
5456

5557
def addEdge(
5658
self,
57-
source: _Serializable,
58-
target: _Serializable,
59-
weight: Optional[_Number] = None,
59+
source: Serializable,
60+
target: Serializable,
61+
weight: Optional[Number] = None,
6062
visitedCount: int = 0,
6163
selectedCount: int = 0
6264
):
6365
self.command("addEdge", source, target, weight, visitedCount, selectedCount)
6466

6567
def updateEdge(
6668
self,
67-
source: _Serializable,
68-
target: _Serializable,
69-
weight: Optional[_Number] = None,
69+
source: Serializable,
70+
target: Serializable,
71+
weight: Optional[Number] = None,
7072
visitedCount: int = 0,
7173
selectedCount: int = 0
7274
):
7375
self.command("updateEdge", source, target, weight, visitedCount, selectedCount)
7476

75-
def removeEdge(self, source: _Serializable, target: _Serializable):
77+
def removeEdge(self, source: Serializable, target: Serializable):
7678
self.command("removeEdge", source, target)
7779

7880
def visit(
7981
self,
80-
target: _Serializable,
81-
source: _Serializable = None,
82-
weight: Optional[_Number] = None
82+
target: Serializable,
83+
source: Serializable = None,
84+
weight: Optional[Number] = None
8385
):
8486
self.command("visit", target, source, weight)
8587

8688
def leave(
8789
self,
88-
target: _Serializable,
89-
source: _Serializable = None,
90-
weight: Optional[_Number] = None
90+
target: Serializable,
91+
source: Serializable = None,
92+
weight: Optional[Number] = None
9193
):
9294
self.command("leave", target, source, weight)
9395

94-
def select(self, target: _Serializable, source: _Serializable = None):
96+
def select(self, target: Serializable, source: Serializable = None):
9597
self.command("select", target, source)
9698

97-
def deselect(self, target: _Serializable, source: _Serializable = None):
99+
def deselect(self, target: Serializable, source: Serializable = None):
98100
self.command("deselect", target, source)
99101

100102
def log(self, log: LogTracer):

algorithm_visualizer/tracers/log.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
from algorithm_visualizer import Tracer, _Serializable
1+
from .tracer import Tracer
2+
from algorithm_visualizer.types import Serializable
3+
24

35
class LogTracer(Tracer):
4-
def set(self, log: _Serializable):
6+
def set(self, log: Serializable):
57
self.command("set", log)
68

7-
def print(self, message: _Serializable):
9+
def print(self, message: Serializable):
810
self.command("print", message)
911

10-
def println(self, message: _Serializable):
12+
def println(self, message: Serializable):
1113
self.command("println", message)
1214

13-
def printf(self, format: str, *args: _Serializable):
15+
def printf(self, format: str, *args: Serializable):
1416
self.command("printf", format, *args)

algorithm_visualizer/types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from typing import Union
2+
3+
# Types which are serializable by the default JSONEncoder
4+
Serializable = Union[dict, list, str, int, float, bool, None]
5+
Number = Union[int, float]

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