def get(self, key, default=defaultdict, force_set=False): """ Change get with setdefault :param force_set: :type key: object :type default: object """ if default == defaultdict: default = BetterDict() if isinstance(default, BaseException) and key not in self: raise default if force_set: value = self.setdefault(key, default) else: value = defaultdict.get(self, key, default) if isinstance(value, string_types): if isinstance( value, str): # this is a trick for python v2/v3 compatibility return value else: return text_type(value) else: return value
def get(self, key, default=defaultdict, force_set=False): """ Change get with setdefault :param force_set: :type key: object :type default: object """ if default == defaultdict: default = BetterDict() if isinstance(default, BaseException) and key not in self: raise default if force_set: value = self.setdefault(key, default) else: value = defaultdict.get(self, key, default) if isinstance(value, string_types): if isinstance(value, str): # this is a trick for python v2/v3 compatibility return value else: return text_type(value) else: return value
def relativeSortArray(self, arr1: list, arr2): size = len(arr1) d = {} for i in arr1: d[i] = d.get(i, 0) + 1 output = [] for i in arr2: temp = [i] * d[i] output.extend(temp) d2 = [] for i in arr1: if i not in arr2: d2.append(i) output.extend(d2) return output
def __delitem__(self, key): """also update parent if given""" if self.parent is not None: self.parent[key] -= defaultdict.get(self, key, 0) defaultdict.__delitem__(self, key)
def __setitem__(self, key, value): """also update parent if given""" if self.parent is not None: self.parent[key] += value - defaultdict.get(self, key, 0) defaultdict.__setitem__(self, key, value)
def count(self, countFilter=None): """how many tiles defined by countFilter do we hold? countFilter is an iterator of element names. No countFilter: Take all So count(['we','ws']) should return 8""" return sum((defaultdict.get(self, x) or 0) for x in countFilter or self)
def get(self, key, default=0.0): return defaultdict.get(self, key, default)