|
#!/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,这样可以多个脚本调用
#调用脚本的参数为变化的数值,每次调用输入想计算的数值,时间间隔为调用时间间隔,
#这样得出就是在调用间隔内数值变化的平均数。 |
|
|