IndexError when parsing lscpu output
Eboreg opened this issue · 6 comments
Version 0.9, hwmon.py:162, method data()
subprocess.getstatusoutput(f"lscpu | grep 'CPU MHz'")[1].split("\n")[1]
throws IndexError because output of lscpu | grep 'CPU MHz'
contains no newline.
klaatu@gort-ubuntu:~/Downloads/Hwmon-python$ lscpu -V
lscpu from util-linux 2.34
klaatu@gort-ubuntu:~/Downloads/Hwmon-python$ lscpu | grep 'CPU MHz'
CPU MHz: 1597.124
I'm running Ubuntu 20.04.1.
ipdb> subprocess.getstatusoutput(f"lscpu | grep 'CPU MHz'")[1]
'CPU MHz: 1877.460'
Also, I'm not sure what the following .split(":")[1]
is meant to accomplish. But I guess the end goal is to get the float at the end of the string and cast it to int? In the case of my lscpu output, that would be accomplished by:
int(float(subprocess.getstatusoutput(f"lscpu | grep 'CPU MHz'")[1].split()[-1]))
Or something nicer looking :)
Good afternoon Eboreg.
First of all, thank you for taking the time to see, use and report a bug. We are very happy to see that people are interested in our project :D
As far as the error is concerned, our code may not be universal and this type of failure may be present, because we only have two computers to test the code (mainly systems based on AMD Ryzen). In addition, we also know that depending on the kernel or the version of Linux some functions may stop working due to changes in folders or files.
Anyway, we are going to retest it on our computers and see if the solution you have indicated works. In a few hours I'll get back to you with the results.
We have already implemented your suggestion (we have had to add a "replace" since it returns the value with "," instead of "."). In addition, we have realized that it read the wrong data (instead of reading the maximum MHz, it read the current ones) in addition to correcting it we have added the minimum value.
I hope the code works well for you now. Write us again if you find any other error or bug: D
We added:
try:
# Try to get the name in spanish
info["Max MHz"] = int(float(subprocess.getstatusoutput(f"lscpu | grep 'CPU MHz máx'")[1].split()[-1].replace(",",".")))
except IndexError:
# Get the english name
info["Max MHz"] = int(float(subprocess.getstatusoutput(f"lscpu | grep 'CPU MHz max'")[1].split()[-1].replace(",",".")))
except:
info["Max MHz"] = None
try:
# Try to get the name in spanish
info["Min MHz"] = int(float(subprocess.getstatusoutput(f"lscpu | grep 'CPU MHz mín'")[1].split()[-1].replace(",",".")))
except IndexError:
# Get the english name
info["Min MHz"] = int(float(subprocess.getstatusoutput(f"lscpu | grep 'CPU MHz min'")[1].split()[-1].replace(",",".")))
except:
info["Min MHz"] = None
Well, that was quick =)
Thanks, I'll check it out and return if I find any more strange stuff.
Ah, seems it didn't quite work anyway, but nevermind, I issue a pull request instead.
I'm very sorry that the code didn't work. I think it doesn't work for you because you have the language of the system in English and I have it in Spanish.
Basically in English the adjectives are on the left and in Spanish they are on the right, so when doing the grep it does not make an exact mach and you get the error.
In spanish format:
CPU MHz: 1796.338
CPU MHz máx.: 3800,0000
CPU MHz mín.: 1550,0000
In english format
CPU MHz: 1796.338
CPU max MHz: 3800,0000
CPU min MHz: 1550,0000
So my code worked and was designed to work on code outputs in Spanish and not in English, that's why it works for you by changing the position of the max and min to the left.
Anyway, your code change seems correct and knowing that it works for you in English, I accept it and attach it to my code :D.
Apart from the CPU class, do the other classes work for you?
Thanks again.
Yeah, at least I tried hwmon-cli with all the arguments and nothing went wrong.
Sorry for all the closing and reopening of this issue, I'm not used to this. Will close it now though. :)