This commit is contained in:
2023-04-13 15:38:06 -04:00
parent 90bfaa6ae7
commit 54ca1c76c4
6 changed files with 134 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
from abc import ABC, abstractmethod
class Simulation(ABC):
@abstractmethod
def start(self):
"Start the simulated object."
pass
@abstractmethod
def tick(self) -> bool:
"""
Simulate one clock tick.
Returns:
bool: True if the simulation could advance one clock tick, or False if the object is expired.
"""
pass
@abstractmethod
def get_uses(self):
"""
Return the uses of the simulated object. These are verb strings which are passed to the `use()` method.
Returns:
tuple: A dict of key-value pairs, where the keys are strings of
verbs to be passed to `use`, and the values are strings explaining
what the verb does in English.
"""
@abstractmethod
def use(self, *args, **kwargs):
"""
Use the simulated object.
Args:
args (tuple): A tuple of verbs to be used on the object.
kwargs (dict): A dictionary of key-value pairs of arguments to the verb(s).
Returns:
True if the action succeeded, False if the action did not succeed and the object needs to be expired.
"""
pass
@abstractmethod
def stop(self):
"Stop the simulated object."
pass
+42
View File
@@ -0,0 +1,42 @@
from importlib import import_module
import tkinter as tk
import os
import sys
from random import choice
from time import sleep
from tkinter import filedialog
from abc import ABC, abstractmethod
class SimulationRunner:
def __init__(self, simulation) -> None:
self.simulation = simulation()
self.simulation.start()
uses = list(self.simulation.get_uses().keys())
while self.simulation.tick() and self.simulation.use(choice(uses)):
pass
self.simulation.stop()
if __name__ == '__main__':
if len(sys.argv) == 1:
# Create a hidden root window
root = tk.Tk()
root.withdraw()
# Open the file chooser dialog
file_path = filedialog.askopenfilename()
print(f"Selected file: {file_path}")
else:
file_path = sys.argv[1]
# Get the python file's directory and filename
module_dir, module_file = os.path.split(file_path)
# Get the python file's name without any extension
module_name, _ = os.path.splitext(module_file)
# Add the module directory to sys.path so we can import it unqualified
sys.path.append(module_dir)
simulation = import_module(module_name)
# Load the simulation module's simulation class, which should be the same
# name as the module name.
simulationrunner = SimulationRunner(getattr(simulation, module_name))
Binary file not shown.