def play(): # game = [[1 if random() > .3 else 0 for x in range(10)] for y in range(10)] patterns = Patterns() game = patterns.toad() for step in range(100): print_board(game, step) sleep(.5) game = progress(game)
def __init__(self, credential_path, database_url="https://ledypie.firebaseio.com/", debug=False, tracker=None, thread_name="FireBaseConnectorThread"): """ :param credential_path: str, path to the firebase credential json file :param database_url: str, URL of the firebase :param debug: bool, set to true to allow debug output :param tracker: func, function to be called when the database has been updated with new values """ # init thread class super().__init__(name=thread_name) # define local attributes self.tracker = tracker if tracker is not None else lambda: None self.stop = False self.rgba = None self.random_colors = False self.local_db = None if debug: fire_logger.setLevel(logging.DEBUG) # connect to firebase cred = credentials.Certificate(credential_path) try: firebase_admin.initialize_app( credential=cred, options={'databaseURL': database_url}, ) except ValueError: firebase_admin.initialize_app( credential=cred, options={'databaseURL': database_url}, name=socket.gethostname()) # update db and get references self.root = db.reference('/') self.init_db() self.db_refs = dict(pattern_attributes={ pt: db.reference(f"/pattern_attributes/{pt}") for pt in Patterns.keys() }, cur_pattern=db.reference('/cur_pattern'), rate=db.reference('/rate'), RGBA=db.reference('/RGBA')) # add listener and sleep to wait for the first call where self.local_db is initialized self.listener = self.root.listen(self.listener_method) time.sleep(1) fire_logger.debug("FireBaseConnector initialized")
def init_pattern_attributes(): """ Add all the attributes from the default modifier dictionary to the remote database """ # get the attributes from the remote data = self.get("pattern_attributes", {}) pattern_attributes = {} # for every pattern in the pattern dict for pt_name, pt_class in Patterns.items(): remote_att = {} # get the local modifier dictionary local_att = pt_class(handler=None, rate=1, pixels=1).modifiers # if there are none, then set to NA if len(local_att) == 0: pattern_attributes[pt_name] = "NA" continue # get the remote attributes try: remote_att = data[pt_name] except KeyError: if len(local_att) > 0: fire_logger.warning(f"Patter '{pt_name}' not found in pattern dict") pattern_attributes[pt_name] = {} # for every attribute for att_name, modifier in local_att.items(): # generate a dictionary with the corresponding values att_dict = dict( value=modifier.value, name=modifier.name, type=modifier.type.__name__ ) if modifier.type == list: att_dict['options'] = modifier.options # try to override the default value with the one from the db try: att_dict['value'] = remote_att[att_name]['value'] except (TypeError, KeyError): pass # set the value pattern_attributes[pt_name][att_name] = att_dict # update the database self.root.update(dict(pattern_attributes=pattern_attributes)) return pattern_attributes
def draw_selected_pattern_forever(choice): if (choice == 1): Patterns.blinker().draw_matrix_forever() elif (choice == 2): Patterns.toad().draw_matrix_forever() elif (choice == 3): Patterns.beacon().draw_matrix_forever() else: raise ValueError("there is no suitable pattern for the given number")
def update_patterns(filename=None): if filename: ServiceProxy.patterns_filename = filename ServiceProxy.patterns = Patterns(ServiceProxy.patterns_filename)
# setup chuck osc communication chuck_in = liblo.ServerThread(CHUCK_IN_PORT, reg_methods=False) chuck_out = liblo.Address(CHUCK_HOST, CHUCK_OUT_PORT) # setup monome monome = Monome() # setup ui rows = [] def chuck_send(path, loopId, *values): liblo.send(chuck_out, path, loopId, *values) patterns = Patterns(chuck_send) for i in range(LOOPS_COUNT): row = Row(i, i + 1, monome, patterns.on_msg) chuck_in.add_method("/status/" + str(row.id), "siiffffiff", row.on_osc_msg) chuck_in.add_method("/status_granular/" + str(row.id), "ffiii", row.on_osc_msg) rows.append(row) topbar = Topbar(rows, patterns, monome) # clean exit def signal_handler(signal, frame): print("Nótt UI Cleanup")
try: print_menu() choice = raw_input() # review: I'd probably cast the choice into an int and check the `if` statement against the singleton value of the int. This is a bit more consistent with PEP 8 # http://www.python.org/dev/peps/pep-0008/#programming-recommendations # For example # choice = int( choice ) # if choice is 1: # ... # # review: consider moving this logic to a more generic method that will allow running the game separately from the `main` block # (for example via the interteractive shell) if (choice == '1'): Patterns.blinker().draw_matrix_forever() elif (choice == '2'): Patterns.toad().draw_matrix_forever() elif (choice == '3'): Patterns.beacon().draw_matrix_forever() else: # review: consider re-using this block of code to a separate method as it's used in the `catch` block as well print('bye...') # review: you didn't import sys, interrupting causes an error: # NameError: name 'sys' is not defined sys.exit() except KeyboardInterrupt: