首页 > 编程语言 > 详细

python计算平均速率模块

时间:2017-02-08 00:39:31      阅读:387      评论:0      收藏:0      [点我收藏+]
#!/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,这样可以多个脚本调用
#调用脚本的参数为变化的数值,每次调用输入想计算的数值,时间间隔为调用时间间隔,
#这样得出就是在调用间隔内数值变化的平均数。

python计算平均速率模块

原文:http://bobo123.blog.51cto.com/3133979/1895692

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!