async def startup(self, app): self.app["highscore"] = {variant: ValueSortedDict(neg) for variant in VARIANTS} self.app["highscore"]["crazyhouse960"] = ValueSortedDict(neg, ZH960) self.wplayer = User(self.app, username="******", perfs=PERFS["user7"]) self.bplayer = User(self.app, username="******", perfs=PERFS["newplayer"]) self.strong_player = User(self.app, username="******", perfs=PERFS["strongplayer"]) self.weak_player = User(self.app, username="******", perfs=PERFS["weakplayer"])
def knn_search(self, q=None, k=1): self.q = q self.visitedset = set() self.candidates = ValueSortedDict() self.result = ValueSortedDict() count = 0 for i in range(self.m): v_ep = self.corpus[np.random.choice(list(self.corpus.keys()))] if self.dmat is None: cost = self.switch_metric(self.q.values, v_ep.values) else: cost = self.dmat[q.index][v_ep.index] count += 1 self.candidates[v_ep.index] = cost self.visitedset.add(v_ep.index) tempres = ValueSortedDict() while True: # get element c closest from candidates to q, and remove c # from candidates if len(self.candidates) > 0: c = self.get_closest() else: break # check stop condition if len(self.result) >= k: if self.check_stop_condition(c, k): break tempres.update(c) # add neighbors of c if not in visitedset c = self.corpus[list(c.keys())[0]] for key in list(c.neighbors.keys()): if key not in self.visitedset: if self.dmat is None: cost = self.switch_metric(self.q.values, v_ep.values) else: cost = self.dmat[q.index][v_ep.index] count += 1 self.visitedset.add(key) self.candidates[key] = cost tempres[key] = cost # add tempres to result self.result.update(tempres) # return k neighbors/result return self.result, count
def __init__(self, vertices: Iterable[any], compute_fills=True, compute_degrees=True): """ Create a Primal graph representing the interactions between vertices. For min-fill or min-induced-width, use remove_and_process_node() instead of use remove_node() :param vertices: The vertices of the graph :param compute_fills; Whether this will be used to compute fills. :param compute_degrees: Whether this will be used to compute degrees. """ self.connected_to: Dict[any, Set[any]] = {vertex: set() for vertex in vertices} self._fills: ValueSortedDict[any, Tuple[int, List[Tuple[any, Set[any]]]]] = ValueSortedDict(lambda n: n[0]) if \ compute_fills else None self._degrees: ValueSortedDict[any, int] = ValueSortedDict() if compute_degrees else None
def update_cpu_burn(name, count, t, l): burn = burners.get(name, {}) burn['count'] = burn.get('count', 0) + count burn['time'] = burn.get('time', 0.0) + t if l is not None: l = ValueSortedDict(l) burn['list'] = burn.get('list', ValueSortedDict()) for k in l: # XXX replace this loop with .update() burn['list'][k] = l[k] length = len(burn['list']) for _ in range(10, length): burn['list'].popitem() burners[name] = burn
def initialize(self, parent=None): # one model per cluster in current_state self._models = {} for cluster in parent.current_state.labels(): self._models[cluster] = self.compute_model(cluster, parent=parent) # list of clusters clusters = list(self._models) try: self._similarity = self.compute_similarity_matrix(parent=parent) except NotImplementedError as e: n_clusters = len(clusters) self._similarity = ValueSortedDict() for i, j in combinations(clusters, 2): # compute similarity if (and only if) clusters are mergeable if not parent.constraint.mergeable([i, j], parent=parent): self._similarity[i, j] = -np.inf self._similarity[j, i] = -np.inf continue similarity = self.compute_similarity(i, j, parent=parent) self._similarity[i, j] = similarity if not self.is_symmetric: similarity = self.compute_similarity(j, i, parent=parent) self._similarity[j, i] = similarity
def initialize_sentences(self, sentences, concept_weights): '''Initializes sentences based on metric "concept density". creates: sentences_dict, concept_to_sentences, ranks_to_sentences ''' self.sentences_dict = {} self.concept_to_sentences = defaultdict(set) # Highest value --> first rank self.ranks_to_sentences = ValueSortedDict(lambda x: -x) for sent in sentences: # Create sentences_dict sent_id = (sent.doc_id, sent.position) self.sentences_dict[sent_id] = sent # Calculate concept density per sentence concept_density = 0 for concept in sent.concepts: concept_density += concept_weights[concept] # Create concept2sent dic self.concept_to_sentences[concept].add(sent_id) concept_density /= float(sent.length) # create ranks_to_sentences self.ranks_to_sentences[sent_id] = concept_density
def __init__(self, id, car, gui): super(Field, self).__init__(target=self.dispatch, name='Field_{}_proc'.format(id)) self.exit = mp.Event() self.id = id self.car = car self.gui = gui # type: FieldGUI self.width = 150 self.height = 90 self.houses = dict() self.xes = ValueSortedDict() self.houseupdates = ValueSortedDict() # Essentially a priority queue with more functionality self.update_idx = 0 self.next_new_house = 0 self.potentials = np.zeros((H, W), dtype = float) self.que = mp.Queue(4) self.path = list() self.start()
def __init__(self, fname, restart=False): if '/' not in fname: fname = os.path.join(DIR, fname) if not os.path.exists(os.path.dirname(fname)): os.makedirs(os.path.dirname(fname)) self.fname = fname print "Loading Recorder from", self.fname self.record = ValueSortedDict(lambda x: -x[0]) self.write_freq = 10 self.read(restart) self.write()
async def create_new_pairings(self, waiting_players): pairing = self.create_pairing(waiting_players) if self.first_pairing: self.first_pairing = False # Before tournament starts leaderboard is ordered by ratings # After first pairing it will be sorted by score points and performance # so we have to make a clear (all 0) leaderboard here new_leaderboard = [(user, 0) for user in self.leaderboard] self.leaderboard = ValueSortedDict(neg, new_leaderboard) self.leaderboard_keys_view = SortedKeysView(self.leaderboard) games = await self.create_games(pairing) return (pairing, games)
def load_scores(self) -> None: """ Loads all of the scores from the flairs on Reddit. """ self.scores = ValueSortedDict({}) for flair in self.bot.reddit.main_subreddit.flair(limit=None): try: self.scores[flair["user"].name.lower()] = int( "".join([char for char in flair["flair_text"].split(" ")[0] if char.isnumeric()]) ) except Exception as e: print(e) pass print("POINTS: Loaded scores.")
def setUp(self): self.loop = asyncio.get_event_loop() self.app = {} self.app["db"] = None self.app["users"] = {} self.app["games"] = {} self.app["tasks"] = weakref.WeakSet() self.app["crosstable"] = {} self.app["highscore"] = { variant: ValueSortedDict(neg) for variant in VARIANTS } self.app["highscore"]["crazyhouse960"] = ValueSortedDict(neg, ZH960) self.wplayer = User(self.app, username="******", perfs=PERFS["Doooovid"]) self.bplayer = User(self.app, username="******", perfs=PERFS["pepellou"]) self.splayer = User(self.app, username="******", perfs=PERFS["strongplayer"])
def compute_similarity_matrix(self, parent=None): clusters = list(self._models) n_clusters = len(clusters) X = np.vstack([self[cluster][0] for cluster in clusters]) nX = l2_normalize(X) similarities = -squareform(pdist(nX, metric=self.distance)) matrix = ValueSortedDict() for i, j in itertools.combinations(range(n_clusters), 2): matrix[clusters[i], clusters[j]] = similarities[i, j] matrix[clusters[j], clusters[i]] = similarities[j, i] return matrix
def compute_similarities(self, cluster, clusters, parent=None): x = self[cluster][0].reshape((1, -1)) X = np.vstack([self[c][0] for c in clusters]) # L2 normalization nx = l2_normalize(x) nX = l2_normalize(X) similarities = -cdist(nx, nX, metric=self.distance) matrix = ValueSortedDict() for i, cluster_ in enumerate(clusters): matrix[cluster, cluster_] = similarities[0, i] matrix[cluster_, cluster] = similarities[0, i] return matrix
def __init__(self, palettes: List[OrderedSet], num_palettes: int, colors_per_palette: int): """ :param palettes: A list of ordered sets of palettes :param palette_relations: A list of lists of possible palettes for tiles :param num_palettes: The number of palettes to reduce down to :param colors_per_palette: The number of colors per palette """ self.was_run = False self._palettes = [p.copy() for p in palettes] self._current_number_of_palettes = len(palettes) self._num_palettes = num_palettes self._colors_per_palette = colors_per_palette self._merges_performed: List[Tuple[int, int]] = [] self._count_per_pal = ValueSortedDict( {pidx: -len(p) for pidx, p in enumerate(self._palettes)})
def record_a_burn(name, start, url=None): if isinstance(url, URL): url = url.url elapsed = time.process_time() - start burn = burners.get(name, {}) burn['count'] = burn.get('count', 0) + 1 burn['time'] = burn.get('time', 0.0) + elapsed avg = burn.get('avg', 10000000.) # are we exceptional? 10x current average and significant if elapsed > avg * 10 and elapsed > 0.015: if 'list' not in burn: burn['list'] = ValueSortedDict() url = url or 'none' burn['list'][url] = -elapsed length = len(burn['list']) for _ in range(10, length): burn['list'].popitem() burn['avg'] = burn['time'] / burn['count'] burners[name] = burn
def identity_words(df): # Analyze words, derive stats good_counter = Counter() good_count = 0 for batch in range(int(len(df) / 100000)): sum_identities = np.sum(df.iloc[(batch) * 100000:(batch + 1) * 100000][REAL_IDENTITY_COLUMNS].values, axis=1).astype('bool') good_comments = ' '.join(df.iloc[(batch) * 100000:(batch + 1) * 100000][sum_identities == 0].apply( lambda row: row['comment_text'], axis=1)).split(' ') good_counter.update(good_comments) good_count += len(good_comments) del good_comments sum_identities = np.sum(df[REAL_IDENTITY_COLUMNS].values, axis=1).astype('bool') foul_comments = ' '.join(df[sum_identities > 0].apply( lambda row: row['comment_text'], axis=1)).split(' ') foul_counter = Counter(foul_comments) foul_count = len(foul_comments) del foul_comments word_foulness = ValueSortedDict() for word, count in good_counter.most_common() + foul_counter.most_common(): if count > MIN_FOUL_COUNT: bad_freq = foul_counter[word] / foul_count good_freq = (good_counter[word] + 1) / good_count bad_prob = bad_freq / (good_freq) word_foulness[word + '_'] = bad_prob # Because of a weird bug we had to add an underscore to every word, let's remove it foul_words = [ word[:-1] for word in list( word_foulness.islice(word_foulness.bisect_key(FOUL_FREQ_TO_DROP))) ] return foul_words
def record_a_latency(name, start, url=None, elapsedmin=10.0): if isinstance(url, URL): url = url.url elapsed = time.time() - start latency = latencies.get(name, {}) latency['count'] = latency.get('count', 0) + 1 latency['time'] = latency.get('time', 0.0) + elapsed if 'hist' not in latency: latency['hist'] = HdrHistogram(1, 30 * 1000, 2) # 1ms-30sec, 2 sig figs latency['hist'].record_value(elapsed * 1000) # ms if elapsed > elapsedmin: if 'list' not in latency: latency['list'] = ValueSortedDict() url = url or 'none' length = len(latency['list']) if length > 9: for u in itertools.islice(latency['list'], 9, length): del latency['list'][u] latency['list'][url] = -elapsed latencies[name] = latency
def compute_similarity_matrix(self, parent=None): # name and number of clusters clusters = list(self._models) n_clusters = len(clusters) # precompute pairwise embedding distance data = parent.features X = np.array(data[data.columns[2:]]) self.precomputed_ = -squareform(pdist(X, metric='euclidean')) matrix = ValueSortedDict() for i, j in itertools.combinations(range(n_clusters), 2): # indices of embedding in ith cluster indices_i = self[clusters[i]] # indices of embedding in jth cluster indices_j = self[clusters[j]] # mean of all pairwise euclidean distances similarity = np.mean(self.precomputed_[indices_i][:, indices_j]) matrix[clusters[i], clusters[j]] = similarity matrix[clusters[j], clusters[i]] = similarity return matrix
def record_a_latency(name, start, url=None, elapsedmin=10.0): if isinstance(url, URL): url = url.url elapsed = time.time() - start latency = latencies.get(name, {}) latency['count'] = latency.get('count', 0) + 1 latency['time'] = latency.get('time', 0.0) + elapsed if 'hist' not in latency: latency['hist'] = HdrHistogram(1, 30 * 1000, 2) # 1ms-30sec, 2 sig figs latency['hist'].record_value(elapsed * 1000) # ms # show the 10 most recent latencies > 10 seconds if elapsed > elapsedmin: if 'list' not in latency: latency['list'] = ValueSortedDict() url = url or 'none' length = len(latency['list']) for _ in range(9, length): latency['list'].popitem( last=False) # throwing away biggest value(s) latency['list'][url] = -elapsed latencies[name] = latency
def __init__( self, app, tournamentId, variant="chess", chess960=False, rated=True, before_start=5, minutes=45, name="", description="", fen="", base=1, inc=0, byoyomi_period=0, rounds=0, created_by="", created_at=None, starts_at=None, status=None, with_clock=True, frequency="", ): self.app = app self.id = tournamentId self.name = name self.description = description self.variant = variant self.rated = rated self.before_start = before_start # in minutes self.minutes = minutes # in minutes self.fen = fen self.base = base self.inc = inc self.byoyomi_period = byoyomi_period self.chess960 = chess960 self.rounds = rounds self.frequency = frequency self.created_by = created_by self.created_at = datetime.now(timezone.utc) if created_at is None else created_at if starts_at == "" or starts_at is None: self.starts_at = self.created_at + timedelta(seconds=int(before_start * 60)) else: self.starts_at = starts_at # TODO: calculate wave from TC, variant, number of players self.wave = timedelta(seconds=3) self.wave_delta = timedelta(seconds=1) self.current_round = 0 self.prev_pairing = None self.messages = collections.deque([], MAX_CHAT_LINES) self.spectators = set() self.players: dict[User, PlayerData] = {} self.leaderboard = ValueSortedDict(neg) self.leaderboard_keys_view = SortedKeysView(self.leaderboard) self.status = T_CREATED if status is None else status self.ongoing_games = 0 self.nb_players = 0 self.nb_games_finished = 0 self.w_win = 0 self.b_win = 0 self.draw = 0 self.nb_berserk = 0 self.nb_games_cached = -1 self.leaderboard_cache = {} self.first_pairing = False self.top_player = None self.top_game = None self.notify1 = False self.notify2 = False if minutes is None: self.ends_at = self.starts_at + timedelta(days=1) else: self.ends_at = self.starts_at + timedelta(minutes=minutes) if with_clock: self.clock_task = asyncio.create_task(self.clock())
def boring_to_burners(d): global burners for k in d: burners[k] = d[k] burners[k]['list'] = ValueSortedDict(d[k].get('list', dict()))
from sortedcollections import ValueSortedDict sdram_tracker = ValueSortedDict(lambda x: -x) sdram_tracker["one"] = 1 sdram_tracker["two"] = 2 print(sdram_tracker.items())
async def init_state(app): # We have to put "kill" into a dict to prevent getting: # DeprecationWarning: Changing state of started or joined application is deprecated app["data"] = {"kill": False} if "db" not in app: app["db"] = None app["users"] = { "Random-Mover": User(app, bot=True, username="******"), "Fairy-Stockfish": User(app, bot=True, username="******"), "Discord-Relay": User(app, anon=True, username="******"), } app["users"]["Random-Mover"].online = True app["lobbysockets"] = {} app["seeks"] = {} app["games"] = {} app["invites"] = {} app["chat"] = collections.deque([], 100) app["game_channels"] = set() app["invite_channels"] = set() app["highscore"] = {variant: ValueSortedDict(neg) for variant in VARIANTS} app["crosstable"] = {} app["stats"] = {} # counters for games app["g_cnt"] = 0 # last game played app["tv"] = None # fishnet active workers app["workers"] = set() # fishnet works app["works"] = {} # fishnet worker tasks app["fishnet"] = asyncio.PriorityQueue() # fishnet workers monitor app["fishnet_monitor"] = {} app["fishnet_versions"] = {} for key in FISHNET_KEYS: app["fishnet_monitor"][FISHNET_KEYS[key]] = collections.deque([], 50) rm = app["users"]["Random-Mover"] for variant in VARIANTS: variant960 = variant.endswith("960") variant_name = variant[:-3] if variant960 else variant byoyomi = variant.endswith("shogi") or variant in ("dobutsu", "gorogoro", "janggi", "shogun") seek = Seek(rm, variant_name, base=5, inc=30 if byoyomi else 3, level=0, chess960=variant960, byoyomi_period=1 if byoyomi else 0) app["seeks"][seek.id] = seek rm.seeks[seek.id] = seek ai = app["users"]["Fairy-Stockfish"] asyncio.create_task(BOT_task(ai, app)) asyncio.create_task(BOT_task(rm, app)) # Configure templating. app["jinja"] = {} base = os.path.dirname(__file__) for lang in LANGUAGES: # Generate compiled mo file folder = os.path.join(base, "../lang/", lang, "LC_MESSAGES") poname = os.path.join(folder, "server.po") moname = os.path.join(folder, "server.mo") try: with open(poname, 'rb') as po_file: po_lines = [ line for line in po_file if line[:8] != b"#, fuzzy" ] mo = Msgfmt(po_lines).get() with open(moname, 'wb') as mo_file: mo_file.write(mo) except PoSyntaxError: log.error("PoSyntaxError in %s", poname) # Create translation class try: translation = gettext.translation("server", localedir="lang", languages=[lang]) except FileNotFoundError: log.warning("Missing translations file for lang %s", lang) translation = gettext.NullTranslations() env = jinja2.Environment(enable_async=True, extensions=['jinja2.ext.i18n'], loader=jinja2.FileSystemLoader("templates"), autoescape=jinja2.select_autoescape(["html"])) env.install_gettext_translations(translation, newstyle=True) env.globals["static"] = static_url app["jinja"][lang] = env if app["db"] is None: return # Read users and highscore from db try: cursor = app["db"].user.find() async for doc in cursor: if doc["_id"] not in app["users"]: perfs = doc.get("perfs") if perfs is None: perfs = {variant: DEFAULT_PERF for variant in VARIANTS} app["users"][doc["_id"]] = User( app, username=doc["_id"], title=doc.get("title"), first_name=doc.get("first_name"), last_name=doc.get("last_name"), country=doc.get("country"), bot=doc.get("title") == "BOT", perfs=perfs, enabled=doc.get("enabled", True)) db_collections = await app["db"].list_collection_names() if "highscore" not in db_collections: await generate_highscore(app["db"]) cursor = app["db"].highscore.find() async for doc in cursor: app["highscore"][doc["_id"]] = ValueSortedDict(neg, doc["scores"]) if "crosstable" not in db_collections: await generate_crosstable(app["db"]) cursor = app["db"].crosstable.find() async for doc in cursor: app["crosstable"][doc["_id"]] = doc await app["db"].game.create_index("us") await app["db"].game.create_index("v") await app["db"].game.create_index("y") await app["db"].game.create_index("by") except Exception: print("Maybe mongodb is not running...") raise
async def make_app(loop): app = web.Application(loop=loop) setup(app, EncryptedCookieStorage(SECRET_KEY, max_age=MAX_AGE)) app["client"] = ma.AsyncIOMotorClient(MONGO_HOST) app["db"] = app["client"][MONGO_DB_NAME] app["users"] = { "Random-Mover": User(app, bot=True, username="******"), "Fairy-Stockfish": User(app, bot=True, username="******"), "Discord-Relay": User(app, anon=True, username="******"), } app["users"]["Random-Mover"].bot_online = True app["websockets"] = {} app["seeks"] = {} app["games"] = {} app["tasks"] = weakref.WeakSet() app["chat"] = collections.deque([], 200) app["channels"] = set() app["highscore"] = {variant: ValueSortedDict(neg) for variant in VARIANTS} app["crosstable"] = {} # counters for games and users app["g_cnt"] = 0 app["u_cnt"] = 0 # last game played app["tv"] = None # fishnet active workers app["workers"] = set() # fishnet works app["works"] = {} # fishnet worker tasks app["fishnet"] = asyncio.PriorityQueue() # fishnet workers monitor app["fishnet_monitor"] = {} app["fishnet_versions"] = {} for key in FISHNET_KEYS: app["fishnet_monitor"][FISHNET_KEYS[key]] = collections.deque([], 50) bot = app["users"]["Random-Mover"] for variant in VARIANTS: variant960 = variant.endswith("960") variant_name = variant[:-3] if variant960 else variant seek = Seek(bot, variant_name, base=5, inc=3, level=0, chess960=variant960) app["seeks"][seek.id] = seek bot.seeks[seek.id] = seek ai = app["users"]["Fairy-Stockfish"] app["tasks"].add(loop.create_task(AI_task(ai, app))) # Read users and highscore from db cursor = app["db"].user.find() try: async for doc in cursor: if doc["_id"] not in app["users"]: perfs = doc.get("perfs") if perfs is None: perfs = {variant: DEFAULT_PERF for variant in VARIANTS} app["users"][doc["_id"]] = User( app, username=doc["_id"], title=doc.get("title"), first_name=doc.get("first_name"), last_name=doc.get("last_name"), country=doc.get("country"), bot=doc.get("title") == "BOT", perfs=perfs, enabled=doc.get("enabled", True) ) db_collections = await app["db"].list_collection_names() if "highscore" not in db_collections: await generate_highscore(app["db"]) cursor = app["db"].highscore.find() async for doc in cursor: app["highscore"][doc["_id"]] = ValueSortedDict(neg, doc["scores"]) if "crosstable" not in db_collections: await generate_crosstable(app["db"]) cursor = app["db"].crosstable.find() async for doc in cursor: app["crosstable"][doc["_id"]] = doc except Exception: print("Maybe mongodb is not running...") raise app.on_shutdown.append(shutdown) # Configure templating. app["jinja"] = jinja2.Environment( loader=jinja2.FileSystemLoader("templates"), autoescape=jinja2.select_autoescape(["html"])) # Setup routes. for route in get_routes: app.router.add_get(route[0], route[1]) for route in post_routes: app.router.add_post(route[0], route[1]) app.router.add_static("/static", "static") return app
async def init_state(app): # We have to put "kill" into a dict to prevent getting: # DeprecationWarning: Changing state of started or joined application is deprecated app["data"] = {"kill": False} if "db" not in app: app["db"] = None app["users"] = { "Random-Mover": User(app, bot=True, username="******"), "Fairy-Stockfish": User(app, bot=True, username="******"), "Discord-Relay": User(app, anon=True, username="******"), } app["users"]["Random-Mover"].bot_online = True app["websockets"] = {} app["seeks"] = {} app["games"] = {} app["chat"] = collections.deque([], 200) app["channels"] = set() app["highscore"] = {variant: ValueSortedDict(neg) for variant in VARIANTS} app["crosstable"] = {} # counters for games and users app["g_cnt"] = 0 app["u_cnt"] = 0 # last game played app["tv"] = None # fishnet active workers app["workers"] = set() # fishnet works app["works"] = {} # fishnet worker tasks app["fishnet"] = asyncio.PriorityQueue() # fishnet workers monitor app["fishnet_monitor"] = {} app["fishnet_versions"] = {} for key in FISHNET_KEYS: app["fishnet_monitor"][FISHNET_KEYS[key]] = collections.deque([], 50) rm = app["users"]["Random-Mover"] for variant in VARIANTS: variant960 = variant.endswith("960") variant_name = variant[:-3] if variant960 else variant byoyomi = variant == "janggi" or variant.endswith( "shogi") or variant == "shogun" seek = Seek(rm, variant_name, base=5, inc=3, level=0, chess960=variant960, byoyomi_period=1 if byoyomi else 0) app["seeks"][seek.id] = seek rm.seeks[seek.id] = seek ai = app["users"]["Fairy-Stockfish"] loop = asyncio.get_event_loop() loop.create_task(BOT_task(ai, app)) loop.create_task(BOT_task(rm, app)) # Configure templating. app["jinja"] = jinja2.Environment( loader=jinja2.FileSystemLoader("templates"), autoescape=jinja2.select_autoescape(["html"])) if app["db"] is None: return # Read users and highscore from db try: cursor = app["db"].user.find() async for doc in cursor: if doc["_id"] not in app["users"]: perfs = doc.get("perfs") if perfs is None: perfs = {variant: DEFAULT_PERF for variant in VARIANTS} app["users"][doc["_id"]] = User( app, username=doc["_id"], title=doc.get("title"), first_name=doc.get("first_name"), last_name=doc.get("last_name"), country=doc.get("country"), bot=doc.get("title") == "BOT", perfs=perfs, enabled=doc.get("enabled", True)) db_collections = await app["db"].list_collection_names() if "highscore" not in db_collections: await generate_highscore(app["db"]) cursor = app["db"].highscore.find() async for doc in cursor: app["highscore"][doc["_id"]] = ValueSortedDict(neg, doc["scores"]) if "crosstable" not in db_collections: await generate_crosstable(app["db"]) cursor = app["db"].crosstable.find() async for doc in cursor: app["crosstable"][doc["_id"]] = doc except Exception: print("Maybe mongodb is not running...") raise
def __init__(self, bin_count, history_length): self.history = ValueSortedDict() self.step = 0 self.thresholds = np.linspace(0, 1, bin_count - 1) self.history_length = history_length self.saturated = False
from random import random # 1. Выставляем всем вершинам метки 0 для lead, для остальных inf # 2. Выбираем непокрытую вершину с наименьшей меткой # 3. Для всех ее соседей пересчитываем метки # 4. Как соседей нет считаем вершину покрытой # 5. Переходим на 2 если есть непокрытые вершины MAGIC_ZEROS = 4 # Особенности реализации: # Пути хранятся в словаре labels => [:метка]: [:вершина] # Словарь с сортировкой # Ключ словаря '<label>_<id>' таким образом избегаем одинаковых ключей (ограничение словаря) и # сравниваем даже между одинаковыми лейблами, для # WARNING связано через замыкание v_dict = ValueSortedDict() def dejkstra(input): labels = init_dejkstra(input) while bool(v_dict.__len__()): v_id = pop_min_label() if labels[v_id] == float('inf'): # Если есть недостижимые вершины return labels edges = get_edges(input, v_id) labels = calc_labels(labels, edges, v_id) return labels def calc_labels(labels, edges, v_id): result_labels = labels
def pull_foreign_keys(self, dim: DimensionName) -> None: select_statement = self.foreign_key_lookups[dim] # type: Select from db import fetch self._foreign_keys[dim] = ValueSortedDict( {row[0]: str(row[1]) for row in fetch(select_statement)})
def __init__(self, index: int, values: list, label=None): self.index = index self.values = values self.label = label self.neighbors = ValueSortedDict()
async def init_state(app): # We have to put "kill" into a dict to prevent getting: # DeprecationWarning: Changing state of started or joined application is deprecated app["data"] = {"kill": False} app["date"] = {"startedAt": datetime.now(timezone.utc)} if "db" not in app: app["db"] = None app["users"] = { "Random-Mover": User(app, bot=True, username="******"), "Fairy-Stockfish": User(app, bot=True, username="******"), "Discord-Relay": User(app, anon=True, username="******"), } app["users"]["Random-Mover"].online = True app["lobbysockets"] = { } # one dict only! {user.username: user.tournament_sockets, ...} app["lobbychat"] = collections.deque([], MAX_CHAT_LINES) app["tourneysockets"] = { } # one dict per tournament! {tournamentId: {user.username: user.tournament_sockets, ...}, ...} app["tourneynames"] = { } # cache for profile game list page {tournamentId: tournament.name, ...} app["tournaments"] = {} app["tourneychat"] = { } # one deque per tournament! {tournamentId: collections.deque([], MAX_CHAT_LINES), ...} app["seeks"] = {} app["games"] = {} app["invites"] = {} app["game_channels"] = set() app["invite_channels"] = set() app["highscore"] = {variant: ValueSortedDict(neg) for variant in VARIANTS} app["crosstable"] = {} app["shield"] = {} app["shield_owners"] = {} # {variant: username, ...} app["stats"] = {} app["stats_humans"] = {} # counters for games app["g_cnt"] = [0] # last game played app["tv"] = None app["twitch"] = Twitch(app) if not DEV: await app["twitch"].init_subscriptions() # fishnet active workers app["workers"] = set() # fishnet works app["works"] = {} # fishnet worker tasks app["fishnet"] = asyncio.PriorityQueue() # fishnet workers monitor app["fishnet_monitor"] = {} app["fishnet_versions"] = {} for key in FISHNET_KEYS: app["fishnet_monitor"][FISHNET_KEYS[key]] = collections.deque([], 50) rm = app["users"]["Random-Mover"] ai = app["users"]["Fairy-Stockfish"] asyncio.create_task(BOT_task(ai, app)) asyncio.create_task(BOT_task(rm, app)) # Configure templating. app["jinja"] = {} base = os.path.dirname(__file__) for lang in LANGUAGES: # Generate compiled mo file folder = os.path.join(base, "../lang/", lang, "LC_MESSAGES") poname = os.path.join(folder, "server.po") moname = os.path.join(folder, "server.mo") try: with open(poname, 'rb') as po_file: po_lines = [ line for line in po_file if line[:8] != b"#, fuzzy" ] mo = Msgfmt(po_lines).get() with open(moname, 'wb') as mo_file: mo_file.write(mo) except PoSyntaxError: log.error("PoSyntaxError in %s", poname) # Create translation class try: translation = gettext.translation("server", localedir="lang", languages=[lang]) except FileNotFoundError: log.warning("Missing translations file for lang %s", lang) translation = gettext.NullTranslations() env = jinja2.Environment(enable_async=True, extensions=['jinja2.ext.i18n'], loader=jinja2.FileSystemLoader("templates"), autoescape=jinja2.select_autoescape(["html"])) env.install_gettext_translations(translation, newstyle=True) env.globals["static"] = static_url app["jinja"][lang] = env if app["db"] is None: return # Read tournaments, users and highscore from db try: cursor = app["db"].user.find() async for doc in cursor: if doc["_id"] not in app["users"]: perfs = doc.get("perfs") if perfs is None: perfs = {variant: DEFAULT_PERF for variant in VARIANTS} app["users"][doc["_id"]] = User(app, username=doc["_id"], title=doc.get("title"), bot=doc.get("title") == "BOT", perfs=perfs, enabled=doc.get( "enabled", True)) cursor = app["db"].tournament.find() cursor.sort('startsAt', -1) counter = 0 async for doc in cursor: if doc["status"] in (T_CREATED, T_STARTED): await load_tournament(app, doc["_id"]) counter += 1 if counter > 3: break await generate_shield(app) db_collections = await app["db"].list_collection_names() # if "highscore" not in db_collections: # Always create new highscore lists on server start await generate_highscore(app["db"]) cursor = app["db"].highscore.find() async for doc in cursor: app["highscore"][doc["_id"]] = ValueSortedDict(neg, doc["scores"]) if "crosstable" not in db_collections: await generate_crosstable(app["db"]) cursor = app["db"].crosstable.find() async for doc in cursor: app["crosstable"][doc["_id"]] = doc await app["db"].game.create_index("us") await app["db"].game.create_index("v") await app["db"].game.create_index("y") await app["db"].game.create_index("by") except Exception: print("Maybe mongodb is not running...") raise # create test tournament if 1: pass