efn阿克说 发表于 2018-8-15 11:57:06

python计算平均速率模块

#!/usr/bin/python  
#this is model
  
import dbm
  
import time
  
import os
  
classcomputation_rate:
  
      def __init__(self,defind_namedb,value):
  
                self.path =os.path.split(os.path.realpath(__file__)) + '/' + 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(self.value)
  
                        db.close()
  
                        return sys.exit(0)
  
                elif len(checklistdbm) != 0:
  
                        db = dbm.open(self.path, 'c')
  
                        old_time = float(checklistdbm)
  
                        old_val = float(db])
  
                        del db]
  
                        db = 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__)) + '/' + inspect.stack().replace('.py','.tempfile')
  
                        self.value = value
  

  
#call model
  

  
#!/usr/bin/python
  
import mean_rate
  
import sys
  

  
p = mean_rate.ccomputation_rate(int(sys.argv))
  

  
print p.computation_rate()
  

  

  
#调用模块和模块文件在相同目录下
  
#模块目录下会生出 以调用脚本命名的 dbm 临时文件目录,
  
#例如调用脚本名字为 example.py ,则在当前目录下会生成 example.tempfile.dir,example.tempfile.pag,这样可以多个脚本调用
  
#调用脚本的参数为变化的数值,每次调用输入想计算的数值,时间间隔为调用时间间隔,
  
#这样得出就是在调用间隔内数值变化的平均数。
页: [1]
查看完整版本: python计算平均速率模块