python計算平均速率模塊
#!/usr/bin/python
#this is model
import dbm
import time
import os
class computation_rate:
def __init__(self,defind_namedb,value):
self.path = os.path.split(os.path.realpath(__file__))[0] + '/' + defind_namedb
self.value = value
def computation_rate(self):
db = dbm.open(self.path, 'c')
checklistdbm = []
for key in db.keys():
checklistdbm.append(key)
db.close()
now_time = time.time()
if len(checklistdbm) == 0:
db = dbm.open(self.path, 'c')
db[str(now_time)] = str(self.value)
db.close()
return sys.exit(0)
elif len(checklistdbm) != 0:
db = dbm.open(self.path, 'c')
old_time = float(checklistdbm[0])
old_val = float(db[checklistdbm[0]])
del db[checklistdbm[0]]
db[str(now_time)] = str(self.value)
db.close()
mean_rate = (self.value - old_val) / (now_time - old_time)
return mean_rate
if __name__ != '__main__':
import inspect
import sys
class ccomputation_rate(computation_rate):
def __init__(self,value):
self.path = os.path.split(os.path.realpath(__file__))[0] + '/' + inspect.stack()[1][1].replace('.py','.tempfile')
self.value = value
#call model
#!/usr/bin/python
import mean_rate
import sys
p = mean_rate.ccomputation_rate(int(sys.argv[1]))
print p.computation_rate()
#調用模塊和模塊文件在相同目錄下
#模塊目錄下會生出 以調用腳本命名的 dbm 臨時文件目錄,
#例如調用腳本名字爲 example.py ,則在當前目錄下會生成 example.tempfile.dir,example.tempfile.pag,這樣可以多個腳本調用
#調用腳本的參數爲變化的數值,每次調用輸入想計算的數值,時間間隔爲調用時間間隔,
#這樣得出就是在調用間隔內數值變化的平均數。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.