<commit_before>from collections import namedtuple

from modules.core.PlayerAssessment import PlayerAssessments
from modules.core.Games import Games

import numpy as np

class Game(namedtuple('Game', ['id', 'pgn', 'emts'])):

  def isEmtOutlier(self, emt):
    abs(emt - self.mean) >= self.outlierCutoff

  def emtsNoOutliers(self):
    return [e for e in self.emts if not self.isEmtOutlier(e)]

  def emtStd(self):
    return np.std(self.emtsNoOutliers())

  def emtMean(self):
    return np.mean(self.emtsNoOutliers())

  def __init__(self, *args, **kvargs):
    super().__init__(*args, **kvargs)
    self.mean = np.mean(self.emts)
    self.outlierCutoff = 0.5 * np.std(self.emts)

class GameBSONHandler:
  @staticmethod
  def reads(bson):
    return Game(
      id = bson['_id'],
      pgn = bson['pgn'],
      emts = bson['emts'])

  @staticmethod
  def writes(game):
    return {
      '_id': game.id,
      'pgn': game.pgn,
      'emts': game.emts
    }

class GameDB(namedtuple('GameDB', ['gameColl'])):
  def byId(self, _id):
    try:
      return GameBSONHandler.reads(self.gameColl.find_one({'_id': _id}))
    except:
      return None

  def byIds(self, ids): # List[Ids]
    return Games(list([GameBSONHandler.reads(g) for g in self.gameColl.find({'_id': {'$in': [i for i in ids]}})]))

  def write(self, game): # Game<commit_msg>Fix subclass namedtuple function<commit_after>from collections import namedtuple

from modules.core.PlayerAssessment import PlayerAssessments
from modules.core.Games import Games

import numpy as np

class Game(namedtuple('Game', ['id', 'pgn', 'emts'])):

  def isEmtOutlier(self, emt):
    abs(emt - self.mean) >= self.outlierCutoff

  def emtsNoOutliers(self):
    return [e for e in self.emts if not self.isEmtOutlier(e)]

  def emtStd(self):
    return np.std(self.emtsNoOutliers())

  def emtMean(self):
    return np.mean(self.emtsNoOutliers())

  def __new__(cls, *args, **kvargs):
    self = super().__new__(cls, *args, **kvargs)
    self.mean = np.mean(self.emts)
    self.outlierCutoff = 0.5 * np.std(self.emts)
    return self


class GameBSONHandler:
  @staticmethod
  def reads(bson):
    return Game(
      id = bson['_id'],
      pgn = bson['pgn'],
      emts = bson['emts'])

  @staticmethod
  def writes(game):
    return {
      '_id': game.id,
      'pgn': game.pgn,
      'emts': game.emts
    }

class GameDB(namedtuple('GameDB', ['gameColl'])):
  def byId(self, _id):
    try:
      return GameBSONHandler.reads(self.gameColl.find_one({'_id': _id}))
    except:
      return None

  def byIds(self, ids): # List[Ids]
    return Games(list([GameBSONHandler.reads(g) for g in self.gameColl.find({'_id': {'$in': [i for i in ids]}})]))

  def write(self, game): # Game
{'fraction_non_alphanumeric': 0.15181518151815182, 'fraction_numerical': 0.0014668133480014668, 'mean_word_length': 3.577181208053691, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1162095', 'n_tokens_mistral': 1029, 'n_tokens_neox': 988, 'n_words': 232}