简体   繁体   English

奇怪的色彩行为Python

[英]Strange colour behavior Python

I have the following input: 我有以下输入:

localhost is alive
54.197.204.2 is alive
danezu4 172.31.24.178 Disk usage: 14602mb RAM available: 1837mb Uptime: 255mins CPULoad: 0.0% 
danezu1 172.31.32.230 Disk usage: 14962mb RAM available: 1837mb Uptime: 255mins CPULoad: 0.0% 
danezu4 172.31.24.178 Disk usage: 14602mb RAM available: 1837mb Uptime: 255mins CPULoad: 0.0% 
danezu1 172.31.32.230 Disk usage: 14962mb RAM available: 1837mb Uptime: 255mins CPULoad: 0.0%

And the following Python code: 以及以下Python代码:

import colour
import fileinput
class Colour:
   GREEN = '\033[92m'
   RED = '\033[91m'
   BOLD = '\033[1m'
   END = '\033[0m'
uptime="Uptime:"
cpuload="CPULoad:"
f=open('/home/ansible/ansible/playbooks/healthcheckv2/sysinfo/files/ping.txt', 'r')
filedata=f.read()
f.close()
with open('/home/ansible/ansible/playbooks/healthcheckv2/sysinfo/files/ping.txt', 'r') as f:
   for line in f:
       words=line.split()
       for i,w in enumerate(words):
          if w==uptime:
               if words[i+1]>86400:
                   filedata=filedata.replace(words[i+1], Colour.GREEN+words[i+1]+Colour.END)
               elif words[i+1]<86399:
                   filedata=filedata.replace(words[i+1], Colour.RED+words[i+1]+Colour.END)
          if w==cpuload:
               if words[i+1]>80:
                   filedata=filedata.replace(words[i+1], Colour.RED+words[i+1]+Colour.END)
               elif words[i+1]<79.99:
                   filedata=filedata.replace(words[i+1], Colour.GREEN+words[i+1]+Colour.END)
with open('/home/ansible/ansible/playbooks/healthcheckv2/sysinfo/files/ping.txt', 'w') as file:
     file.write(filedata)
file.close()

I do not understand the behavior for the color of the text after this Python code is applied. 应用此Python代码后,我不了解文本颜色的行为。 If the CPU is lower than 80 in the text file, the value will be RED. 如果文本文件中的CPU低于80,则该值为RED。 If the value is passing 80% in the text file, will be green. 如果该值在文本文件中超过80%,将为绿色。 The same thing is applying for the uptime value. 同样的事情是申请正常运行时间值。 What can be wrong here? 这里有什么问题?

The values that are in the text file are rounded in a Jinja2 template file(but I don't think that the behavior is related to this): 文本文件中的值在Jinja2模板文件中四舍五入(但我认为行为与此无关):

Uptime: {{(ansible_uptime_seconds/60)|round|int}}mins CPULoad: {{(cpuload.stdout)|float|round}}% 

Best regards, 最好的祝福,

Romain 罗曼

EDIT : I think i misinterpreted the question to mean you don't understand escape sequences. 编辑 :我认为我误解了这个问题,意味着您不理解转义序列。

If you don't understand why the colors are applied wrong here's why: 如果您不明白为什么将颜色应用错误,这是为什么:

if words[i+1]>86400:

You are comparing a string to an integer. 您正在将字符串与整数进行比较。

if float(words[i+1][:-4])>86400:

This will cut off the "mins" form the Uptime and convert the rest to a float number. 这将切断正常运行时间的“分钟”,并将其余时间转换为浮点数。

Likewise if float(words[i+1][:-1])>80: allowes for the percentagesto be compared to numbers. 同样, if float(words[i+1][:-1])>80:则将百分比与数字进行比较。

Also since 0.0% is loacted in multiple lines, the the replace function may not be best suited for your needs since ist formats every occurence of '0.0%' at once, and three times over due to the loop. 同样,由于0.0%位于多行中,因此replace函数可能并不最适合您的需求,因为ist一次将每次出现“ 0.0%”的格式设置为一次,由于循环而将其设置为三倍。


First answer 第一个答案

You never use the colour module here. 您永远不会在这里使用colour模块。

The defined class Colour has four values that are strings for terminal Escape Sequences (link) . 定义的类Colour具有四个值,这些值是终端转义序列(link)的字符串。

The ANSI/VT100 terminals and terminal emulators are not just able to display black and white text; ANSI / VT100终端和终端仿真器不仅可以显示黑白文本,还可以显示黑白文本。 they can display colors and formatted texts thanks to escape sequences. 由于使用了转义序列,它们可以显示颜色和格式化的文本。 Those sequences are composed of the Escape character (often represented by "^[" or "Esc") followed by some other characters: "Esc[FormatCodem". 这些序列由转义字符(通常由“ ^ [”或“ Esc”表示)以及其他一些字符组成:“ Esc [FormatCodem”。 (source) (资源)

Colour.RED is just a stand-in for the string '\\033[91m' , possibly to make the code more legible. Colour.RED只是字符串'\\033[91m'的替代者'\\033[91m' ,可能使代码更易读。

This string is interpreted by a terminal emulator and changes the following characters' color to red. 该字符串由终端仿真器解释,并将以下字符的颜色更改为红色。 The string represented by Colour.END reverts the output back to the default. Colour.END表示的字符串将输出恢复为默认值。

cat ping.txt after application of the script shows the interpreted file in the terminal, with the colors you obserevd. 应用脚本后, cat ping.txt在终端中显示已解释的文件,并带有您观察到的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM