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 Github
How to post code using bbtags
Download my project scripts


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 4 4,492 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  How to change the datatype of list elements? mHosseinDS86 9 5,071 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  find some word in text list file and a bit change to them RolanRoll 3 3,139 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 3,356 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  change csv file into adjency list ainisyarifaah 0 2,236 Sep-21-2021, 02:49 AM
Last Post: ainisyarifaah
  How to change odd to even numbers in the list? plumberpy 8 6,963 Aug-08-2021, 11:07 AM
Last Post: plumberpy
  How to invoke a function with return statement in list comprehension? maiya 4 4,940 Jul-17-2021, 04:30 PM
Last Post: maiya
  An IF statement with a List variable dedesssse 3 27,191 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 3,764 May-24-2021, 08:48 AM
Last Post: bowlofred
  Feed List items with Integer euras 9 6,851 May-19-2021, 07:45 PM
Last Post: snippsat

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.