def check(cls): """check cache consistency""" for key, value in cls.cache.items(): assert key == value.key or key == str(value), 'cache wrong: cachekey=%s realkey=%s value=%s' % ( key, value.key, value) assert value.key == 1 + value.hashTable.index(value) / 2 assert value.key == TileList.key(value), \ 'static key:%s current key:%s, static value:%s, current value:%s ' % ( value.key, TileList.key(value), value.original, value)
def check(cls): """check cache consistency""" for key, value in cls.cache.items(): assert key == value.key or key == str( value), 'cache wrong: cachekey=%s realkey=%s value=%s' % ( key, value.key, value) assert value.key == 1 + value.hashTable.index(value) / 2 assert value.key == TileList.key(value), \ 'static key:%s current key:%s, static value:%s, current value:%s ' % ( value.key, TileList.key(value), value.original, value)
def __new__(cls, newContent=None): """try to use cache""" if isinstance(newContent, str) and newContent in cls.cache: return cls.cache[newContent] if isinstance(newContent, Meld): return newContent tiles = TileList(newContent) cacheKey = tiles.key() if cacheKey in cls.cache: return cls.cache[cacheKey] return TileList.__new__(cls, tiles)
def __new__(cls, newContent=None): """try to use cache""" if isinstance(newContent, str) and newContent in cls.cache: return cls.cache[newContent] if isinstance(newContent, Meld): return newContent tiles = TileList(newContent) cacheKey = tiles.key() if cacheKey in cls.cache: return cls.cache[cacheKey] return TileList.__new__(cls, tiles)
def __init__(self, newContent=None): """init the meld: content can be either - a single string with 2 chars for every tile - a list containing such strings - another meld. Its tiles are not passed. - a list of Tile objects""" if not hasattr(self, '_fixed'): # already defined if I am from cache TileList.__init__(self, newContent) self.case = ''.join('a' if x.islower() else 'A' for x in self) self.key = TileList.key(self) if self.key not in self.cache: self.cache[self.key] = self self.cache[str(self)] = self self.isExposed = self.__isExposed() self.isConcealed = not self.isExposed self.isSingle = self.isPair = self.isChow = self.isPung = False self.isKong = self.isClaimedKong = self.isKnitted = False self.isDragonMeld = len(self) and self[0].isDragon self.isWindMeld = len(self) and self[0].isWind self.isHonorMeld = self.isDragonMeld or self.isWindMeld self.isBonus = len(self) == 1 and self[0].isBonus self.isKnown = len(self) and self[0].isKnown self.__setMeldType() self.isPungKong = self.isPung or self.isKong self.isDeclared = self.isExposed or self.isKong groups = set(x.group.lower() for x in self) if len(groups) == 1: self.group = self[0].group self.lowerGroup = self.group.lower() else: self.group = 'X' self.lowerGroup = 'x' self.isRest = False self.__staticRules = {} # ruleset is key self.__dynamicRules = {} # ruleset is key self.__staticDoublingRules = {} # ruleset is key self.__dynamicDoublingRules = {} # ruleset is key self.__hasRules = None # unknown yet self.__hasDoublingRules = None # unknown yet self.concealed = self.exposed = self.declared = self.exposedClaimed = None # to satisfy pylint self._fixed = True if len(self) < 4: TileList.__setattr__(self, 'concealed', Meld(TileList(x.concealed for x in self))) TileList.__setattr__(self, 'declared', self.concealed) TileList.__setattr__(self, 'exposed', Meld(TileList(x.exposed for x in self))) TileList.__setattr__(self, 'exposedClaimed', self.exposed) else: TileList.__setattr__(self, 'concealed', Meld(TileList(x.concealed for x in self))) TileList.__setattr__( self, 'declared', Meld( TileList([ self[0].exposed, self[1].concealed, self[2].concealed, self[3].exposed ]))) TileList.__setattr__(self, 'exposed', Meld(TileList(x.exposed for x in self))) TileList.__setattr__( self, 'exposedClaimed', Meld( TileList([ self[0].exposed, self[1].exposed, self[2].exposed, self[3].concealed ])))
def __init__(self, newContent=None): """init the meld: content can be either - a single string with 2 chars for every tile - a list containing such strings - another meld. Its tiles are not passed. - a list of Tile objects""" if not hasattr(self, '_fixed'): # already defined if I am from cache TileList.__init__(self, newContent) self.case = ''.join('a' if x.islower() else 'A' for x in self) self.key = TileList.key(self) if self.key not in self.cache: self.cache[self.key] = self self.cache[str(self)] = self self.isExposed = self.__isExposed() self.isConcealed = not self.isExposed self.isSingle = self.isPair = self.isChow = self.isPung = False self.isKong = self.isClaimedKong = self.isKnitted = False self.isDragonMeld = len(self) and self[0].isDragon self.isWindMeld = len(self) and self[0].isWind self.isHonorMeld = self.isDragonMeld or self.isWindMeld self.isBonus = len(self) == 1 and self[0].isBonus self.isKnown = len(self) and self[0].isKnown self.__setMeldType() self.isPungKong = self.isPung or self.isKong self.isDeclared = self.isExposed or self.isKong groups = set(x.group.lower() for x in self) if len(groups) == 1: self.group = self[0].group self.lowerGroup = self.group.lower() else: self.group = 'X' self.lowerGroup = 'x' self.isRest = False self.__staticRules = {} # ruleset is key self.__dynamicRules = {} # ruleset is key self.__staticDoublingRules = {} # ruleset is key self.__dynamicDoublingRules = {} # ruleset is key self.__hasRules = None # unknown yet self.__hasDoublingRules = None # unknown yet self.concealed = self.exposed = self.declared = self.exposedClaimed = None # to satisfy pylint self._fixed = True if len(self) < 4: TileList.__setattr__( self, 'concealed', Meld(TileList(x.concealed for x in self))) TileList.__setattr__(self, 'declared', self.concealed) TileList.__setattr__( self, 'exposed', Meld(TileList(x.exposed for x in self))) TileList.__setattr__(self, 'exposedClaimed', self.exposed) else: TileList.__setattr__( self, 'concealed', Meld(TileList(x.concealed for x in self))) TileList.__setattr__( self, 'declared', Meld(TileList([self[0].exposed, self[1].concealed, self[2].concealed, self[3].exposed]))) TileList.__setattr__( self, 'exposed', Meld(TileList(x.exposed for x in self))) TileList.__setattr__( self, 'exposedClaimed', Meld(TileList([self[0].exposed, self[1].exposed, self[2].exposed, self[3].concealed])))