From 54ca1c76c46b31e9bcbac92242e63d4bb85eef29 Mon Sep 17 00:00:00 2001 From: Karen Zeng Date: Thu, 13 Apr 2023 15:38:06 -0400 Subject: [PATCH] update --- Simulation/Simulation.py | 42 ++++++++++++++++++ Simulation/SimulationRunner.py | 42 ++++++++++++++++++ .../__pycache__/Simulation.cpython-311.pyc | Bin 0 -> 2233 bytes assessment1.py | 18 ++++++++ hello.py | 1 + test1.py | 31 +++++++++++++ 6 files changed, 134 insertions(+) create mode 100644 Simulation/Simulation.py create mode 100644 Simulation/SimulationRunner.py create mode 100644 Simulation/__pycache__/Simulation.cpython-311.pyc create mode 100644 assessment1.py create mode 100644 test1.py diff --git a/Simulation/Simulation.py b/Simulation/Simulation.py new file mode 100644 index 0000000..113d5a5 --- /dev/null +++ b/Simulation/Simulation.py @@ -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 \ No newline at end of file diff --git a/Simulation/SimulationRunner.py b/Simulation/SimulationRunner.py new file mode 100644 index 0000000..43145d6 --- /dev/null +++ b/Simulation/SimulationRunner.py @@ -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)) diff --git a/Simulation/__pycache__/Simulation.cpython-311.pyc b/Simulation/__pycache__/Simulation.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f876df5f534d95cf4e7ffb6935de594399839bbf GIT binary patch literal 2233 zcmb7FOK%%D5azyQS+$&pgA{EJCD21Gz;S^bjUtFsq&WmIkUBY3FuJCew6yhVsghhf z)+s1_=&iZs7bHQDKhtBd*PeP);GS~o40pAX>^0i1q*-z}Go0@m4)wO(ZV`BX-~NU_ z3JCcVC({S!_Bp!?uUCWjeue2PB1qTar0WWgJE!Cg=-yY>T_3cr2o7BC zo%-k28nEVH@K^$Ea6#JyZR3J=nJ+=+&Ahc653_`sSf-IvZB7V|G262S2;js4Yj=EW z*Dz(M83?KiKf$T&{VXE0x$};^b8=scFF(750D>YUbUe9UYa#0xTMkmdGo{uxY2-cZ71>c1d)-J<1F#uikj zf<}pq4ylQw!_CUz2OCpwRSI6REVrg_f?go{Le$NtJ zITrlUSX1$Q6f41JTkohQz*2~`;8cBvFnvsLd}WRmR9LH$lvPWLS_SKWFDrWj^{UVn za4n=v3r*z$yji^|eL4_;0!mAdgz8Z^^>NzQ^mq_Q1FIe}t---a8*LxLt#|fm(HAq2 zwfvfFl!z_5O?ey{%;8X++&N+i@I7L&(i^aifSvIu!jiW8O*4=O6&kD+W~VWW9y(7` z!gTgGC`%zh2x4O|hJkVynW6ToHCqZ+CIm#ago~){QTLtr Uw;kB-qIkLX5w(BUEZM~V0UQh~G5`Po literal 0 HcmV?d00001 diff --git a/assessment1.py b/assessment1.py new file mode 100644 index 0000000..52ac5cf --- /dev/null +++ b/assessment1.py @@ -0,0 +1,18 @@ +from Simulation import Simulation +class Vault: + def __init__(self): + self._treasure = "gold" + self._attributes = dict(self.__dict__) + + def __setattr__(self, name, value): + if name != "_attributes" and name not in self._attributes: + print("attribute deleted!") + self._attributes[name] = value + super().__setattr__("_attributes", dict(self.__dict__)) + + @property + def treasure(self): + if self.__dict__ != self._attributes: + print("attribute deleted!") + return self._treasure + diff --git a/hello.py b/hello.py index e69de29..ce47b77 100644 --- a/hello.py +++ b/hello.py @@ -0,0 +1 @@ +print("hello") \ No newline at end of file diff --git a/test1.py b/test1.py new file mode 100644 index 0000000..5ad4b94 --- /dev/null +++ b/test1.py @@ -0,0 +1,31 @@ +class Count: + def __init__(self,mymin,mymax): + self.mymin=mymin + self.mymax=mymax + + def __getattr__(self, item): + self.__dict__[item]=0 + return 0 + +obj1 = Count(1,10) +print(obj1.mymin) +print(obj1.mymax) +print(obj1.mycurrent1) + +class Count: + + def __init__(self,mymin,mymax): + self.mymin=mymin + self.mymax=mymax + self.current=None + + def __getattribute__(self, item): + if item.startswith('cur'): + raise AttributeError + return object.__getattribute__(self,item) + # or you can use ---return super().__getattribute__(item) + +obj1 = Count(1,10) +print(obj1.mymin) +print(obj1.mymax) +print(obj1.current) \ No newline at end of file