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


Messages In This Thread
Change a list to integer so I can use IF statement - by buckssg - Sep-21-2021, 12:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Change font in a list or tuple apffal 4 2,635 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  How to change the datatype of list elements? mHosseinDS86 9 1,910 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,603 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  change csv file into adjency list ainisyarifaah 0 1,480 Sep-21-2021, 02:49 AM
Last Post: ainisyarifaah
  How to change odd to even numbers in the list? plumberpy 8 3,622 Aug-08-2021, 11:07 AM
Last Post: plumberpy
  How to invoke a function with return statement in list comprehension? maiya 4 2,753 Jul-17-2021, 04:30 PM
Last Post: maiya
  An IF statement with a List variable dedesssse 3 7,956 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 2,490 May-24-2021, 08:48 AM
Last Post: bowlofred
  Feed List items with Integer euras 9 3,849 May-19-2021, 07:45 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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