python计算平均速率模块
2017-02-07 15:42
232 查看
#!/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中使用time模块计算代码执行效率的精度测试(转)
- Python性能提升一 —— 高计算模块使用C++编写
- Python写一个根据日期计算是星期几的模块
- Python并行计算pp模块实践笔记
- Python之科学计算模块学习
- Python十进制数学计算模块decimal
- 使用python中openpyxl模块操作excel文件,计算单元格分子式的相对分子质量(一)
- Python科学计算包模块的安装(ubuntu)
- python3 科学计算模块 numpy、pandas、sns、seaborn等模块安装遇到的问题
- Python实现一些简单的算法(2)—计算余数与random模块的使用
- windows x64 安装python科学计算模块numpy
- python-time模块计算程序运行的时间
- python operator计算模块
- [Python]计算豆瓣电影TOP250的平均得分
- python科学计算数据可视化模块-seaborn
- python数值计算模块NumPy scipy安装
- Python 分布式计算模块 Parallel
- python安装numpy科学计算模块
- 解决Python中浮点数计算结果不够精确的问题:decimal模块
- 初学python-练习_1使用python编写计算班级学生平均分程序