Python Forum

Full Version: Change a list to integer so I can use IF statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings fellow pythoners, I got a huge favor to ask. I have tried everyway imaginable to turn psutil.cpu_percent() results into an integer so I can use with my custom resource monitor. It always comes back as list so I cannot use it in the IF statment. Everything else works. This is the last piece Shocked

Here is the error I get:
if cpu_percent > max_pct :
TypeError: '>' not supported between instances of 'map' and 'int'
here is the whole code, but again, it is just the psutil.cpu_percent() format.

from datetime import datetime
from os.path import dirname
import os.path
import csv
import psutil
import time
max_pct = 90
#sets the sketch to run for time 60 sec x 15; we can replace with chron
t_end = time.time() + 60 * 1
while time.time() < t_end:
    def main():
        #my attempt at a universal path
        expand = (dirname(__file__))
        folder = os.path.join(expand, 'monitor.csv')
        # date time stamp
        dt = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
        # load average for 1 minute
        #cpu_load = psutil.getloadavg()
        # grabs the CPU percent from psutil
        cpu_percent = [psutil.cpu_percent()]
        #change cpu_percent from list to int However it definatley is not working
        cpu_percent = map(int,cpu_percent)
        # grabs the RAM usage from psutil
        mem_usage = [psutil.virtual_memory().percent]
        # setup parameter output
        params = [dt]
        params.extend(cpu_percent)
        params.extend(mem_usage)
        time.sleep(2)

        # sets up the line for input into the CSV
        line = ','.join([str(x) for x in params])
        # starts the CSV writing
        with open(folder, 'a') as f:
            f.write(line + '\n')
        # gives some user feedback for debugging
        print(line)

        if cpu_percent > max_pct :
            print('finally!!!!!!!!!!!!!')
            time.sleep(3)

    if __name__ == "__main__":
        main()
Have you had a look at this doc?
I don't understand why you are putting the cpu_percent in a list bracket.

#! /usr/bin/env python3
import psutil
from time import sleep

max_pct = 90

cpu_pct = psutil.cpu_percent(interval=0.1)
cpu_pct2 = psutil.cpu_percent(interval=0.1, percpu=True)
counter = 10
while counter != 0:
    print(f'cpu_pct: {cpu_pct} \ncpu_pct2: {cpu_pct2}\n')
    sleep(1)
    counter -= 1
Output:
cpu_pct: 8.5 cpu_pct2: [14.3, 0.0, 14.3, 0.0, 14.3, 0.0, 0.0, 14.3, 0.0, 0.0, 0.0, 0.0] cpu_pct: 8.5 cpu_pct2: [14.3, 0.0, 14.3, 0.0, 14.3, 0.0, 0.0, 14.3, 0.0, 0.0, 0.0, 0.0] cpu_pct: 8.5 cpu_pct2: [14.3, 0.0, 14.3, 0.0, 14.3, 0.0, 0.0, 14.3, 0.0, 0.0, 0.0, 0.0] cpu_pct: 8.5 cpu_pct2: [14.3, 0.0, 14.3, 0.0, 14.3, 0.0, 0.0, 14.3, 0.0, 0.0, 0.0, 0.0] cpu_pct: 8.5 cpu_pct2: [14.3, 0.0, 14.3, 0.0, 14.3, 0.0, 0.0, 14.3, 0.0, 0.0, 0.0, 0.0] cpu_pct: 8.5 cpu_pct2: [14.3, 0.0, 14.3, 0.0, 14.3, 0.0, 0.0, 14.3, 0.0, 0.0, 0.0, 0.0] cpu_pct: 8.5 cpu_pct2: [14.3, 0.0, 14.3, 0.0, 14.3, 0.0, 0.0, 14.3, 0.0, 0.0, 0.0, 0.0] cpu_pct: 8.5 cpu_pct2: [14.3, 0.0, 14.3, 0.0, 14.3, 0.0, 0.0, 14.3, 0.0, 0.0, 0.0, 0.0] cpu_pct: 8.5 cpu_pct2: [14.3, 0.0, 14.3, 0.0, 14.3, 0.0, 0.0, 14.3, 0.0, 0.0, 0.0, 0.0] cpu_pct: 8.5 cpu_pct2: [14.3, 0.0, 14.3, 0.0, 14.3, 0.0, 0.0, 14.3, 0.0, 0.0, 0.0, 0.0]
Pure Genius; I can't tell you how many hours I have spent on that (19+). I am new to psutil and....well just about everything python. So I guess I have to change my while loop to a for loop because of this error:

TypeError: 'int' object is not iterable
Any suggestions?
If you want it to be a number, why are you putting it into a list?

On line 20, you are calling psutil.cpu_percent(), which returns a floating point number. But when you put it in brackets, you put that value into a new list.

>>> type( psutil.cpu_percent() )
<class 'float'>
>>> type( [psutil.cpu_percent()] )
<class 'list'>
Take off the brackets and you have a number that you can compare against an integer.

>>> psutil.cpu_percent()
6.3
>>> psutil.cpu_percent() > 90
False