Python Forum
Change a list to integer so I can use IF statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change a list to integer so I can use IF statement
#1
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()
Reply
#2
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]
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply
#3
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?
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Change font in a list or tuple apffal 3 363 Oct-14-2021, 05:26 AM
Last Post: ndc85430
  change csv file into adjency list ainisyarifaah 0 264 Sep-21-2021, 02:49 AM
Last Post: ainisyarifaah
  How to change odd to even numbers in the list? plumberpy 8 761 Aug-08-2021, 11:07 AM
Last Post: plumberpy
  How to invoke a function with return statement in list comprehension? maiya 4 676 Jul-17-2021, 04:30 PM
Last Post: maiya
  An IF statement with a List variable dedesssse 3 583 Jul-08-2021, 05:58 PM
Last Post: perfringo
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 782 May-24-2021, 08:48 AM
Last Post: bowlofred
  Feed List items with Integer euras 9 1,274 May-19-2021, 07:45 PM
Last Post: snippsat
  LIST or ARRAY Comparison and change of value nio74maz 0 657 Dec-21-2020, 05:52 PM
Last Post: nio74maz
Question dict value, how to change type from int to list? swissjoker 3 1,078 Dec-09-2020, 09:50 AM
Last Post: perfringo
  using element on a list as condition statement glennford49 11 2,149 May-21-2020, 12:53 PM
Last Post: hussainmujtaba

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020