Skip to content

Commit 92db076

Browse files
committed
- Added a method to get CPU and RAM utilization by the system
- Added [typehinting](https://peps.python.org/pep-0484/) and docstring support to all the functions, increasing code quality - Ensured that the CPU script works for multiple CPUs as well - Updated README file
1 parent 2717a2c commit 92db076

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

CPU temperature/Readme.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,41 @@ This Python script is used to retrieve the CPU temperature using the psutil libr
66
## Prerequisites
77
- Python installed on your machine
88
- `psutil` library installed. You can install it by running the following command:
9-
```bash
9+
10+
```bash
1011
pip install psutil
11-
12+
```
1213

1314
## Explanation of the Script
1415

15-
The script utilizes the `psutil` library's `sensors_temperatures()` function to measure the temperature of the CPU.
16+
The script utilizes the `psutil` library's `sensors_temperatures()`, `virtual_memory()` and `cpu_percent()` functions to output data that tells us about the system's resource consumption.
1617

1718
## Setup Instructions
1819

1920
1. Clone the repository to your local machine.
20-
2. Navigate to the "Cpu Temperature" folder.
21+
2. Navigate to the "CPU Temperature" folder.
2122
3. Install the `psutil` library if you haven't already by running the following command:
22-
```bash
23-
pip install psutil
24-
```
23+
24+
```bash
25+
pip install psutil
26+
```
2527
## Output
2628

27-
The script will display the current CPU temperature in Celsius.
29+
The script will display the current CPU temperature in Celsius alongwith CPU Percentage Used.
30+
In addition, it'll display the percentage used and total RAM of the system.
2831
32+
Sample Output:
33+
34+
```
35+
Current CPU Temperature (Celsius): 60, with percentage utilized being at 3.1%,
36+
Current RAM utilization is 75.3% of 512 MB
37+
```
2938
3039
3140
## Compatibility
3241
3342
Please note that the script is primarily designed for Linux-based systems. While it may work on other platforms, the availability and format of temperature information can vary. Ensure that your system supports the `psutil` library and has the necessary sensors for CPU temperature measurement.
43+
However, the CPU and RAM utilization functionalities would work regaurdless of the Operating System.
3444
3545
## Contributing
3646

CPU temperature/temp.py

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
import psutil
22

33
# Function to retrieve CPU temperature
4-
def get_cpu_temperature():
4+
def get_cpu_temperature() -> float | str:
5+
"""A function which returns the temperature of the CPU
6+
7+
Returns:
8+
float | str: The temperature value in Celsius or 'NA' if temperature info. is unavailable
9+
"""
510
try:
611
# Retrieve temperature information using psutil
712
# and access the first temperature value from the 'coretemp' key
813
temperature = psutil.sensors_temperatures()['coretemp'][0].current
914
return temperature
1015
except (KeyError, IndexError):
1116
# Handle cases where temperature information is not available
12-
return "CPU temperature information not available."
17+
return "NA"
18+
19+
def get_ram_and_cpu_util() -> list:
20+
"""Get the utilization of system RAM and CPU
21+
22+
Returns:
23+
list: Contains total memory of system, percentage utilized and percantage of CPU utilized
24+
"""
25+
memory_stats = psutil.virtual_memory()
26+
return [
27+
memory_stats[0]//(1024*1024), #Total memory available in the system (MB)
28+
memory_stats[2], #Percentage of memory utilized
29+
psutil.cpu_percent(interval=4, percpu=True) #Percentage of CPU utilized
30+
]
31+
1332

1433
# Call the get_cpu_temperature() function to get the CPU temperature
1534
cpu_temperature = get_cpu_temperature()
1635

17-
# Print the CPU temperature
18-
print("Current CPU Temperature (Celsius):", cpu_temperature)
36+
#Call the get_ram_and_cpu_util() function to get data on system resource utilization
37+
system_utils = get_ram_and_cpu_util()
38+
39+
# Print the system info
40+
if(type(system_utils[2]) == list):
41+
cpu_percentage = ""
42+
for i in system_utils[2]:
43+
cpu_percentage += "{}%, ".format(i)
44+
else:
45+
cpu_percentage = '{}%'.format(system_utils[2])
46+
47+
print(f"Current CPU Temperature in Celsius is {cpu_temperature}°C, with percentage utilized being at {cpu_percentage}")
48+
print(f"Current RAM utilization is {system_utils[1]}% of {system_utils[0]} MB")
1949

0 commit comments

Comments
 (0)