def post(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) votes_to_graduate = self.request.get('votes_to_graduate') name = self.request.get('name') bandid = urllib.quote(name.replace(' ', '-').lower()) if not name or not votes_to_graduate or Bands.get_by_id(bandid): msg = "Requested group exists already" else: msg = "New band created successfully" Bands(id=bandid).put() namespace_manager.set_namespace(bandid) conf = Configuration(id="singleton") conf.name = name conf.votes_to_graduate = int(votes_to_graduate) conf.put() template_values = { 'bandid': bandid, 'name': name, 'votes_to_graduate': votes_to_graduate, 'msg': msg } template = JINJA_ENVIRONMENT.get_template('confirm.html') self.response.write(template.render(template_values))
def get(self, bandid): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return conf = Configuration.get_by_id("singleton") if user not in conf.users: conf.users.append(user) # TODO: For now users are authenticated by default. This should # probably be a configuration parameter. conf.auth_users.append(user) conf.put() ranking_query = SongNode.query().order(-SongNode.vote_cnt) ranking_query = ranking_query.filter(SongNode.vote_cnt < conf.votes_to_graduate) songs = ranking_query.fetch(50) template_values = { 'is_admin': conf.admin == user, 'songs': songs, 'band_name': conf.name, 'bandid': bandid, 'logout_uri': users.create_login_url(self.request.uri), 'votes_to_graduate': conf.votes_to_graduate } template = JINJA_ENVIRONMENT.get_template('ranking.html') self.response.write(template.render(template_values))
def get(self, bandid): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return conf = Configuration.get_by_id("singleton") if conf.admin == None: conf.admin = user conf.put() if conf.admin != user: self.error(401); return template_values = { 'admin': conf.admin, 'users': conf.users, 'auth_users': conf.auth_users, 'bandid': bandid, } template = JINJA_ENVIRONMENT.get_template('config.html') self.response.write(template.render(template_values))
def post(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) bandid = self.request.get('bandid') logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return conf = Configuration.get_by_id("singleton") if user not in conf.auth_users: logging.info("letting an unauthed users vote... for now") votes = self.request.get_all('votes') for songid in votes: logging.info('songid:' + songid + 'len: ' + str(len(songid))) song = SongNode.get_by_id(songid) song_url=urllib.quote('/' + bandid + '/song/' + song.name + '/' + song.interpreter) if user not in song.votes: send_notifications(song,'http://' + self.request.host + song_url, 'vote', user) song.votes.append(user) song.vote_cnt += 1 song.graduated = song.vote_cnt >= VOTES_TO_GRADUATE song.put() self.redirect('/' + bandid + '/thanks')
def _create_fixture_configurations(self, config): ''' Create configurations. ''' session = app.database.get_session(self._db) for key, value in config.items('config_table'): session.add(Configuration(key, value)) session.commit()
def post(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) bandid = self.request.get('bandid') logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return conf = Configuration.get_by_id("singleton") if user not in conf.auth_users: logging.info("letting an unauthed users vote... for now") name = self.request.get('name').strip() interpreter = self.request.get('interpreter').strip() songid = name + interpreter song = SongNode.get_by_id(songid) if not song: song = SongNode(id=name+interpreter) song.name = name song.interpreter = interpreter song.vote_cnt = 0 song.comments = [] song.links = [] song.votes = [] song.graduated = False song_url=urllib.quote('/' + bandid + '/song/' + name + '/' + interpreter) unvote = self.request.get('undo', default_value=False) if user not in song.votes and not unvote: logging.info(str(user) + ' voted for ' + song.name) song.votes.append(user) song.vote_cnt += 1 send_notifications(song,'http://' + self.request.host + song_url, 'vote', user) elif user in song.votes and unvote == 'true': logging.info(str(user) + ' unvoted for ' + song.name) song.votes.remove(user) song.vote_cnt -= 1 send_notifications(song,'http://' + self.request.host + song_url, 'unvote', user) else: logging.error(str(user) + ' failed to vote/unvote for ' + song.name) return song.graduated = song.vote_cnt >= VOTES_TO_GRADUATE song.put() self.redirect(song_url)
def addConfiguration(): configData = request.get_json() newConfiguration = Configuration( id=configData["id"], hand=configData["hand"], gesture=configData["gesture"], action=configData["action"], alias=configData["alias"], ) db.session.add(newConfiguration) db.session.commit() return "Added", 201
def __init__(self): Configuration.load() self.db = production_session()
def load_configuration(self): if not Configuration.instance: Configuration.load()
def data_directory(self): return Configuration.data_directory()
def main(): max_depth = None while max_depth is None: print("Choose difficulty") print("\t1 = Easy") print("\t2 = Medium") print("\t3 = Hard") difficulty = int(input()) if difficulty == 1: max_depth = 3 elif difficulty == 2: max_depth = 4 elif difficulty == 3: max_depth = 5 else: print("Invalid difficulty") prune = None while prune is None: print("Choose algorithm:") print("\t1 = minimax") print("\t2 = minimax with alpha-beta pruning") algorithm_choice = int(input()) if algorithm_choice == 1: prune = False elif algorithm_choice == 2: prune = True else: print("Invalid algorithm") human_player = None while human_player is None: print("Choose player:") print("\t1 = Black") print("\t2 = White") player_choice = int(input()) if player_choice == 1: human_player = Player.BLACK elif player_choice == 2: human_player = Player.WHITE else: print("Invalid player") computer_player = human_player.opposite() print("Human plays as", human_player) print("Computer AI plays as", computer_player) ui = None while ui is None: print("Choose user interface type:") print("1 = Text") print("2 = Graphical") ui_choice = int(input()) if ui_choice == 1: ui = TUI() elif ui_choice == 2: ui = GUI() else: print("Invalid user interface") winner = None current_config = Configuration.initial() current_player = Player.BLACK move_count = 0 game_start_time = time.time() while True: move_count += 1 print("Black's score:", current_config.score()) ui.render(current_config) if not current_config.has_pieces(computer_player): # the other player won winner = human_player break if not current_config.has_pieces(human_player): # the other player won winner = computer_player break possible_moves = current_config.possible_moves(current_player) if not possible_moves: # leave winner as `None`, indicating a draw break if current_player == human_player: move = ui.get_next_move(current_config, current_player, possible_moves) # provide an option to quit the game if move is None: break current_config = current_config.apply_move(move) else: print("Computer is thinking...") start_time = time.perf_counter() current_config = best_move(current_config, computer_player, max_depth, prune) end_time = time.perf_counter() thinking_time = end_time - start_time print(f"Computer spent {thinking_time:.2f} seconds thinking") if not current_config.capture_chain: current_player = current_player.opposite() game_end_time = time.time() game_duration = game_end_time - game_start_time if winner is None: print("The game is a draw") else: print("Winner:", winner) print(f"Game finished after {move_count} moves") print(f"Took {int(game_duration)} seconds")
def get_database_connection(cls): url = Configuration.database_url(test=True) engine, connection = SessionManager.initialize(url) return engine, connection
SessionManager, temp_config, ) import logging import mock import model import os import shutil import tempfile #os.environ['TESTING'] = 'true' from sqlalchemy.orm.session import Session from sqlalchemy.ext.declarative import declarative_base Configuration.load() Base = declarative_base() def package_setup(): """Make sure the database schema is initialized and initial data is in place. """ engine, connection = DatabaseTest.get_database_connection() # First, recreate the schema. # # Base.metadata.drop_all(connection) doesn't work here, so we # approximate by dropping everything except the materialized # views. for table in reversed(Base.metadata.sorted_tables):
def config(self): return Configuration.get_instance()
def load_configuration(config_file) -> Configuration: with open(config_file, "r") as f: return Configuration(yaml.load(f, Loader=EnvVarLoader))