Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7072673

Browse files
MarkKoz64json
authored andcommitted
Add Commander
* Capitalise module names for consistency with other tracers
1 parent 55db114 commit 7072673

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

tracer/Commander.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import string
2+
from typing import Any, Dict, List
3+
4+
from tracer import Randomize
5+
6+
_MAX_COMMANDS = 1000000
7+
_MAX_OBJECTS = 100
8+
9+
10+
class Commander:
11+
_keyRandomizer = Randomize.String(8, string.ascii_lowercase + string.digits)
12+
_objectCount = 0
13+
commands: List[Dict[str, Any]] = []
14+
15+
def __init__(self, **kwargs):
16+
self._objectCount += 1
17+
self.command(self.__class__.__name__, **kwargs)
18+
19+
@classmethod
20+
def command(cls, method: str, key: str = _keyRandomizer.create(), **kwargs):
21+
cmd = {
22+
"key": key,
23+
"method": method
24+
}
25+
cmd.update(kwargs)
26+
cls.commands.append(cmd)
27+
28+
if len(cls.commands) > _MAX_COMMANDS:
29+
raise RuntimeError("Too Many Commands")
30+
elif cls._objectCount > _MAX_OBJECTS:
31+
raise RuntimeError("Too Many Objects")
32+
33+
@classmethod
34+
def destroy(cls):
35+
cls._objectCount -= 1
36+
cls.command("destroy")
File renamed without changes.

tracer/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import atexit
2+
import json
3+
import os
4+
5+
from tracer.Commander import Commander
6+
7+
8+
@atexit.register
9+
def execute():
10+
if os.getenv("ALGORITHM_VISUALIZER"):
11+
with open("visualization.json", "w", encoding="UTF-8") as file:
12+
json.dump(Commander.commands, file)
13+
else:
14+
import requests
15+
import webbrowser
16+
17+
response = requests.post(
18+
"https://algorithm-visualizer.org/api/visualizations",
19+
json=Commander.commands
20+
)
21+
22+
if response.status_code == 200:
23+
webbrowser.open(response.text)
24+
else:
25+
raise requests.HTTPError(response=response)

0 commit comments

Comments
 (0)