12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- # -*- coding: utf-8 -*-
- from odoo.exceptions import ValidationError
- class SumNumberHelper(object):
- def __init__(self):
- self.dic = {}
- def add(self, key, number):
- if not number:
- number = 0
- if not key:
- key = '__none__'
- if key in self.dic:
- value = self.dic[key]
- self.dic[key] = value + number
- else:
- self.dic[key] = number
- def get(self, key):
- if not key:
- key = '__none__'
- if key not in self.dic:
- return 0
- return self.dic[key]
- class SumNumberConditionHelper(object):
- def __init__(self):
- self.dic = {}
- def add(self, key, unit, number):
- if not number:
- number = 0
- if not key:
- key = '__none__'
- if key in self.dic and unit == self.dic[key][1]:
- value = self.dic[key][0]
- self.dic[key][0] = value + number
- else:
- self.dic[key] = [number, unit if unit else '']
- def get(self, key, unit):
- if not key:
- key = '__none__'
- if key not in self.dic:
- return 0
- if self.dic[key][1] == unit:
- print self.dic[key][0]
- return self.dic[key][0]
- # 合计相同个数的数量。
- # 例如:
- # a = [m,1,2,3,4]
- # b = [m,5,6,7,8]
- # c = [n,11,12,13,14]
- # 操作:按第1列分组,对其余列求和
- # 则:结果为{m:[6,8,10,12], n:[11,12,13,14]}
- # =====用法:=====
- # helper = SumNumbersHelper(4)
- # helper.add_numbers(m, [1,2,3,4])
- # helper.add_numbers(m, [5,6,7,8])
- # helper.add_numbers(n, [11,12,13,14])
- # 得到m的结果:helper.get(m)
- # 得到n的结果:helper.get(n)
- class SumNumbersHelper(object):
- def __init__(self, number_count):
- self.dic = {}
- self.number_count = number_count
- def add_numbers(self, key, numbers):
- if not numbers:
- raise ValidationError(u'第2个参数不合法,应该为数组,元素个数为:' + str(self.number_count))
- if len(numbers) != self.number_count:
- raise ValidationError(u'第2个参数个数不对,应该为' + str(self.number_count))
- if not key:
- key = '__none__'
- if key in self.dic:
- value = self.dic[key]
- self.dic[key] = self._sum(value, list(numbers))
- else:
- self.dic[key] = list(numbers)
- def get(self, key):
- if not key:
- key = '__none__'
- if key not in self.dic:
- return 0
- return self.dic[key]
- def _sum(self, arr1, arr2):
- for i in range(self.number_count):
- arr1[i] += arr2[i]
- return arr1
|