简体   繁体   中英

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:

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. If the CPU is lower than 80 in the text file, the value will be RED. If the value is passing 80% in the text file, will be green. 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):

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.

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.


First answer

You never use the colour module here.

The defined class Colour has four values that are strings for terminal Escape Sequences (link) .

The ANSI/VT100 terminals and terminal emulators are not just able to display black and white text; 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". (source)

Colour.RED is just a stand-in for the string '\\033[91m' , possibly to make the code more legible.

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.

cat ping.txt after application of the script shows the interpreted file in the terminal, with the colors you obserevd.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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