关于字典的问题。

2020-01-13 13:32:33 +08:00
 s04
本人想写一个内存占用率记录的脚本,这个脚本的目标是将内存占用率大于百分之 90 的时间戳用 txt 文本记录下来。但本人目前用列表写出来的并不能执行记录功能(由 warning_recorder 函数实现),而且我觉得用字典以时间为键,占用率为值来建立字典比用列表更合适,求大佬指教。
代码如下所示:

from typing import List

import psutil
import math
import time

def occ_rate() ->float:
memory = psutil.virtual_memory()
occ_rate = float(memory.used) / float(memory.total) * 100
return float(occ_rate)
'_______________________________________________________________________________'
def memory_panel():
while 1:
memory = psutil.virtual_memory()
ratio = math.pow(1024, 3) # X Byte = X GB / 1024^3
print('已使用内存:%.2f GB' % (memory.used / ratio))
'''需要一个利用上行数据进行统计得出最大值和最小值以及超出设定阈值并记录次数。'''
print('总内存:%.2f GB' % (memory.total / ratio))
'occ_rate = float(memory.used) / float(memory.total) * 100' # 需要抽出来单独做一个函数?
'''occ_rate 代表内存占用率'''
m_rate = occ_rate()
print("内存占用率: %.2f%%" % m_rate) # .2f 是表示保留两位小数。
print(psutil.swap_memory())
print("退出请输入 exit(请区分大小写): ")
chosen = input() # input()方法将所有输入值转换为 string 类型
if chosen == "exit":
print('再见')
exit()
else:
continue
'_______________________________________________________________________________'
def occ_warning_recorder(rate):
"""
:type rate: float
"""
global file
global now2
occ_warning: List[float] = []
while 1:
rate = occ_rate()
if rate < 0.95:
time.sleep(10)
pass
elif rate >= 0.95:
rate: float = rate
print('/a')
# length_of_w = len(occ_warning)
for i in range(0,100):
occ_warning[i] = rate
i += 1
now = int(round(time.time() * 1000))
now2 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now / 1000))
"上面这两行检测通过,数据正常;occ_warning 为警告值"
file = open('G:/python 练习册 /memory_float_log.txt',
'a') # 写入警告值到 log.txt 当中,在'w'写入模式下,当我们下次写入变量时,会覆盖 txt,有一个追加模式'a',可以实现多次写入
output_data = [now2, occ_warning[i], '%']
"—".join(output_data)
file.write(str(output_data))
# 最傻瓜的连接字符串方式,但不推荐用这种方式 file.write(str(now02) + '|' + str(abv) + '%')
file.write('/n')
file.close()
'_________________________________________________________________________________________________'

print(memory_panel())
occupied: float = occ_rate()
occ_warning_recorder(occupied)
2149 次点击
所在节点    Python
6 条回复
lc1450
2020-01-13 14:58:31 +08:00
函数 memory_panel 里面开头 while 1 ? 函数结尾 exit or continue 还怎么往下执行?
写文件直接当字符串写入啊, 关字典毛事
no1xsyzy
2020-01-13 15:10:14 +08:00
1、格式劝退,用 gist 发,请
2、你大概需要的是 OrderedDict ?
SjwNo1
2020-01-13 15:19:44 +08:00
看的我脑壳疼
s04
2020-01-13 16:56:10 +08:00
@SjwNo1 我没有好好写注释吧 QAQ
s04
2020-01-13 23:00:12 +08:00
<script src="https://gist.github.com/Leetroch/90c2c78eff3b4cfca68a6a66a1935441.js"></script>
@no1xsyzy
@SjwNo1
请大佬指教了,我现在运行到将日期和内存占用率写入到 TXT 文档这一步就不知道该怎么修正了 QAQ。
no1xsyzy
2020-01-14 15:11:29 +08:00
@s04 先跑个题,关于 gist 的正确使用,加上 .py 后缀名可以调用代码高亮,还有 v2 能认识链接本身不要加 html。

然后,思维混乱,三分之二的代码是执行不到的
还有花式 premature optimization 和 premature design。
我不知道从哪开始说,先起手廖雪峰吧。
先把问题说清楚,不然就像这样:

「我现在碰到个难题,你来解决吧。」

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/637465

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX