class Game: """ Container and manager for GameEngine instances @author: James Heslin (PROGRAM_IX) """ def __init__(self, width, height): """ Constructs a new Game, whose screen has the specified width and height @type width: int @param width: Width of the screen @type height: int @param height: Height of the screen @author: James Heslin (PROGRAM_IX) """ self.width = width self.height = height self.screen = None self.engine = None self.engines = [] self.i_e = InputEngine() self.e_e = EventEngine(self.i_e) def start(self): """ Set up the GameEngine and run the game @author: James Heslin (PROGRAM_IX) """ pygame.init() self.screen = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption("PyStroke") self.engines = [GameEngine(self.screen, self.e_e)] # add others here self.engine = self.engines[0] self.run() def run(self): """ Runs the GameEngine, switches to another GameEngine, or quits, based on returned flags from GameEngine @author: James Heslin (PROGRAM_IX) """ r = self.engine.run() while r != 1: if r == 0: if self.engines.index(self.engine) < len(self.engines) - 1: self.engine = self.engines[self.engines.index(self.engine) + 1] print self.engines.index(self.engine) self.e_e.reset_input() else: self.engine = self.engines[0] r = self.engine.run() pygame.quit() raise SystemExit
class TrafficSimulation: def __init__(self, traffic_alpha, debug=False): self.engine = EventEngine() # self.inter_arrival_mu = inter_arrival_mu # all parameters will be in a config file later # self.inter_arrival_sigma = inter_arrival_sigma self.traffic_alpha = traffic_alpha self.am_pm = 'PM' self.section_occupancy = [0, 0, 0] self.section_queueing = [0, 0, 0, 0] self.section_data = np.empty((0, 4)) self.travel_times = [] self.alpha_cdf = [] for a in [0.5, 0.75, 1, 1.25, 1.5]: fname = 'interarrival_times/interarrival-{:.2f}-cdf.dat'.format(a) cdf = np.genfromtxt(fname, delimiter=',') self.alpha_cdf.append(cdf) if debug: logging.basicConfig(level=logging.DEBUG) def run_simulation(self, sim_time: int, print_info=False, queue_initial=True): self.print_info = print_info time = 0 if queue_initial: self.engine.queue_event(Arrival(self, time, 0, 0)) while time <= sim_time: try: e = self.engine.get_next_event() except IndexError: print('No more events: t = {}'.format(time)) return time = e.time e.handle() if type(e).__name__ == 'Departure' and print_info: print(self.section_occupancy, time)
def __init__(self, traffic_alpha, debug=False): self.engine = EventEngine() # self.inter_arrival_mu = inter_arrival_mu # all parameters will be in a config file later # self.inter_arrival_sigma = inter_arrival_sigma self.traffic_alpha = traffic_alpha self.am_pm = 'PM' self.section_occupancy = [0, 0, 0] self.section_queueing = [0, 0, 0, 0] self.section_data = np.empty((0, 4)) self.travel_times = [] self.alpha_cdf = [] for a in [0.5, 0.75, 1, 1.25, 1.5]: fname = 'interarrival_times/interarrival-{:.2f}-cdf.dat'.format(a) cdf = np.genfromtxt(fname, delimiter=',') self.alpha_cdf.append(cdf) if debug: logging.basicConfig(level=logging.DEBUG)
def __init__(self, width, height): """ Constructs a new Game, whose screen has the specified width and height @type width: int @param width: Width of the screen @type height: int @param height: Height of the screen @author: James Heslin (PROGRAM_IX) """ self.width = width self.height = height self.screen = None self.engine = None self.engines = [] self.i_e = InputEngine() self.e_e = EventEngine(self.i_e)
class Data(pw.Model): last_price = pw.DecimalField(null=False, default=0.0) timestamp = pw.DateTimeField(null=False) created_at = pw.DateTimeField(default=datetime.datetime.now) class Meta: database = db db_table = 'tick_data' def init(): db.create_tables([ Data, ]) def on_tick(e): print 'On tick' print e.dict_ if __name__ == '__main__': from main import register_all_handlers ee = EventEngine() register_all_handlers(ee) ee.start() time.sleep(1) ee.put(Event('on_tick', id=1))
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # add formatter to ch ch.setFormatter(formatter) # add ch to logger logger.addHandler(ch) client = pm.MongoClient('localhost', 27017) db = client['data-db'] event_handlers = [{ 'name': 'on_tick', 'func': 'test.on_tick' }] orders = Queue() # order manager main_engine = EventEngine() def register_all_handlers(ee, handlers): if not isinstance(ee, EventEngine): raise Exception('Parameter must be the instance of EventEngine') def parse_func(full_name): p = re.compile(r'^(\S+)\.([\S^.]+)$') m = p.match(full_name) return m.groups() if m else (__name__, full_name) for handler in handlers: event_name = handler.get('name') module, func = parse_func(handler.get('func')) try: