標籤:疊加 sel cut com where font gif def tle
連續添加資料到明細:
def onchange_product_id(self, cr, uid,ids, product_id,line_id, context=None): result={} if product_id: sql="select product_id, sum (qty) qty,lot_id ,max(in_date) in_date ,location_id from stock_quant where product_id=%d GROUP by product_id,lot_id ,location_id;"%(product_id) cr.execute(sql) dict=cr.dictfetchall()
# 這一句將原開資料,一直疊加進去 line_ids=line_id num=len(dict) i=0 for i in range(num): line_ids.append(({ ‘sequence‘:i+1, ‘product_id‘:dict[i][‘product_id‘], ‘lot_id‘:dict[i][‘lot_id‘], ‘wl_qty‘:dict[i][‘qty‘], ‘realy_qty‘:dict[i][‘qty‘], ‘date_in‘:dict[i][‘in_date‘], ‘location_id‘:dict[i][‘location_id‘], })) i+=1 result[‘line_id‘]=line_ids return {‘value‘:result}
做odoo一年多了,今天碰到學習交流群裡的一些問題:這裡將它記錄下來
當函數的參數不確定時,可以使用*args 和**kwargs,*args 沒有key值,**kwargs有key值。
def fun_var_args(farg, *args):
print "arg:", farg
for value in args:
print "another arg:", value
fun_var_args(1, "two", 3) # *args可以當作可容納多個變數組成的list
result:
[python]
arg: 1
another arg: two
another arg: 3
**kwargs:
[python]
def fun_var_kwargs(farg, **kwargs):
print "arg:", farg
for key in kwargs:
print "another keyword arg: %s: %s" % (key, kwargs[key])
fun_var_kwargs(farg=1, myarg2="two", myarg3=3) # myarg2和myarg3被視為key, 感覺**kwargs可以當作容納多個key和value的dictionary
result:
[python]
arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
也可以用下面的形式:
[python]
def fun_var_args_call(arg1, arg2, arg3):
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
args = ["two", 3] #list
fun_var_args_call(1, *args)
result:
[python]
arg1: 1
arg2: two
arg3: 3
#這裡的**kwargs可以是字典型別參數
#簡單的例子
[python]
def fun_var_args_call(arg1, arg2, arg3):
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
#命名變數
kwargs = {"arg3": 3, "arg2": "two"} # dictionary
#調用函數
fun_var_args_call(1, **kwargs)
result:
[python] view plaincopyprint?
arg1: 1
arg2:"two"
arg3:3
python中的單例模式:
單例模式(Singleton Pattern)是一種常用的軟體設計模式,該模式的主要目的是確保某一個類只有一個執行個體存在。當你希望在整個系統中,某個類只能出現一個執行個體時,單例對象就能派上用場。
實現單例模式的幾種形式:
1.使用模組
其實,Python 的模組就是天然的單例模式
模組在第一次匯入時,會產生 .pyc 檔案,當第二次匯入時,就會直接載入 .pyc 檔案,
而不會再次執行模組代碼。因此,我們只需把相關的函數和資料定義在一個模組中,就可以獲得一個單例對象了
mysingleton.py
class Singleton(object): def foo(self): passsingleton = Singleton()
將上面的代碼儲存在檔案 mysingleton.py 中,要使用時,直接在其他檔案中匯入此檔案中的對象,這個對象即是單例模式的對象
調用時用: from a import singleton
2.使用裝飾器
def Singleton(cls): _instance = {} def _singleton(*args, **kargs): if cls not in _instance: _instance[cls] = cls(*args, **kargs) return _instance[cls] return _singleton#運行時,會現載入他@Singletonclass A(object): a = 1 def __init__(self, x=0): self.x = xa1 = A(2)a2 = A(3)
3.使用類
class Singleton(object): def __init__(self): pass @classmethod #類方法 def instance(cls, *args, **kwargs): if not hasattr(Singleton, "_instance"): Singleton._instance = Singleton(*args, **kwargs) return Singleton._instance
python學習總結---學習交流群裡的問題總結