Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __mul__(self, other):
if isinstance(other, (int, long, float)):
return Counter((key, value * other) for (key, value) in self.iteritems())
keys = set(self.iterkeys())
keys.update(other.iterkeys())
lval = Counter((key, self.d_get(key) * other.d_get(key)) for key in keys)
lval.default = self.default * other.default
return lval
def __div__(self, other):
if isinstance(other, (int, long, float)):
return Counter((key, value / other) for (key, value) in self.iteritems())
keys = set(self.iterkeys())
keys.update(other.iterkeys())
lval = Counter((key, self.d_get(key) / other.d_get(key)) for key in keys)
if other.default:
lval.default = self.default / other.default
else:
lval.default = self.default
return lval
def __add__(self, other):
if isinstance(other, (int, long, float)):
return Counter((key, value + other) for (key, value) in self.iteritems())
new = Counter()
keys = set(self.iterkeys())
keys.update(other.iterkeys())
for key in keys:
new[key] = self.d_get(key) + other.d_get(key)
new.default = self.default + other.default
return new
def __sub__(self, other):
if isinstance(other, (int, long, float)):
return Counter((key, value - other) for (key, value) in self.iteritems())
keys = set(self.iterkeys())
keys.update(other.iterkeys())
new = Counter((key, self.d_get(key) - other.d_get(key)) for key in keys)
new.default = self.default - other.default
return new
def __add__(self, other):
if isinstance(other, (int, long, float)):
return Counter((key, value + other) for (key, value) in self.iteritems())
new = Counter()
keys = set(self.iterkeys())
keys.update(other.iterkeys())
for key in keys:
new[key] = self.d_get(key) + other.d_get(key)
new.default = self.default + other.default
return new
def __div__(self, other):
if isinstance(other, (int, long, float)):
return Counter((key, value / other) for (key, value) in self.iteritems())
keys = set(self.iterkeys())
keys.update(other.iterkeys())
lval = Counter((key, self.d_get(key) / other.d_get(key)) for key in keys)
if other.default:
lval.default = self.default / other.default
else:
lval.default = self.default
return lval
def __setitem__(self, key, value):
if not isinstance(value, (int, long, float)):
raise ValueError("Counters can only hold numeric types")
return super(Counter, self).__setitem__(key, value)
def __sub__(self, other):
if isinstance(other, (int, long, float)):
return Counter((key, value - other) for (key, value) in self.iteritems())
keys = set(self.iterkeys())
keys.update(other.iterkeys())
new = Counter((key, self.d_get(key) - other.d_get(key)) for key in keys)
new.default = self.default - other.default
return new
def __mul__(self, other):
if isinstance(other, (int, long, float)):
return Counter((key, value * other) for (key, value) in self.iteritems())
keys = set(self.iterkeys())
keys.update(other.iterkeys())
lval = Counter((key, self.d_get(key) * other.d_get(key)) for key in keys)
lval.default = self.default * other.default
return lval