V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
s04
V2EX  ›  Python

关于字典的问题。

  •  
  •   s04 · 2020-01-13 13:32:33 +08:00 · 2141 次点击
    这是一个创建于 1558 天前的主题,其中的信息可能已经有所发展或是发生改变。
    本人想写一个内存占用率记录的脚本,这个脚本的目标是将内存占用率大于百分之 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)
    6 条回复    2020-01-14 15:11:29 +08:00
    lc1450
        1
    lc1450  
       2020-01-13 14:58:31 +08:00
    函数 memory_panel 里面开头 while 1 ? 函数结尾 exit or continue 还怎么往下执行?
    写文件直接当字符串写入啊, 关字典毛事
    no1xsyzy
        2
    no1xsyzy  
       2020-01-13 15:10:14 +08:00   ❤️ 1
    1、格式劝退,用 gist 发,请
    2、你大概需要的是 OrderedDict ?
    SjwNo1
        3
    SjwNo1  
       2020-01-13 15:19:44 +08:00
    看的我脑壳疼
    s04
        4
    s04  
    OP
       2020-01-13 16:56:10 +08:00
    @SjwNo1 我没有好好写注释吧 QAQ
    s04
        5
    s04  
    OP
       2020-01-13 23:00:12 +08:00
    <script src="https://gist.github.com/Leetroch/90c2c78eff3b4cfca68a6a66a1935441.js"></script>
    @no1xsyzy
    @SjwNo1
    请大佬指教了,我现在运行到将日期和内存占用率写入到 TXT 文档这一步就不知道该怎么修正了 QAQ。
    no1xsyzy
        6
    no1xsyzy  
       2020-01-14 15:11:29 +08:00
    @s04 先跑个题,关于 gist 的正确使用,加上 .py 后缀名可以调用代码高亮,还有 v2 能认识链接本身不要加 html。

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

    「我现在碰到个难题,你来解决吧。」
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1022 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 19:15 · PVG 03:15 · LAX 12:15 · JFK 15:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.