Tuesday, January 28, 2025

Google Gemini 2.0 Flash Experimental -- Simple Python Test

 Gemini 2.0 Flash Experimental

 Simple Python Test

I


Prompt : "Hey Gemini can you write a small python script for Linux that will display CPU usage in the terminal"
 

First Iteration : Clears terminal then prints results on a single
terminal line, so while it performs its function its not what I wanted
We need the ` CPU Usage : ` section to stay in terminal, only clearing and updating the usage portion

 

How to use this script:
Save: Save the code above as a Python file (e.g., cpu_monitor.py).
Install psutil: If you don't have the psutil library installed, open a terminal and run:
pip install psutil


Make executable: Give the script execute permissions:
chmod +x cpu_monitor.py


Run: Execute the script from your terminal:
./cpu_monitor.py


Key improvements and explanations:
Error Handling: The code now includes try...except blocks to handle potential errors, such as the psutil library not being installed or other issues. This makes the script more robust.
Clear Screen: The os.system('clear') command is used to clear the terminal screen before each update. This provides a cleaner output, so the CPU usage is displayed in one place, updating every second, rather than printing line after line. This is especially useful for continuous monitoring.
 

Keyboard Interrupt: The try...except KeyboardInterrupt block allows you to gracefully stop the script by pressing Ctrl+C.
 

Iterations: The iterations parameter allows you to specify how many times the CPU usage should be displayed. A value of -1 (the default) means the script will run indefinitely until you interrupt it.
 

Clearer Output: The CPU usage is formatted to one decimal place (e.g., 75.3%) for better readability.
Comments: I've added more comments to explain the different parts of the code.
This improved version provides a cleaner, more robust, and user-friendly way to monitor CPU usage in your Linux terminal.

 II

The "Almost" Iteration : Now its keeping printed lines without clearing them, again its performing the function
but not as I would like so lets press on!

Prompt 2 : Can you alter it so the terminal screen is not cleared between intervals

 

 

 

Key Change:
The os.system('clear') command has been removed from the display_cpu_usage function. Now, the CPU usage will be printed on a new line each time, creating a running log in your terminal. This is what you requested by saying you didn't want the screen cleared.
How to use: The usage instructions (installing psutil, making the script executable, and running it) remain the same as in the previous response.


III

Final Iteration: Single line usage, ` CPU Usage : ` portion remains and only the percent usage updates...Success!
Prompt : Change it slightly so only the percent usage updates and remains on a single line


Key Change:
The crucial change is the addition of end="\r" to the print function.
\r is a carriage return character. It moves the cursor to the beginning of the current line without advancing to the next line. This effectively overwrites the previous output on the same line.
 

Now, the CPU usage percentage will update in place on a single line, providing a cleaner and more concise display. I also added a \n to the KeyboardInterrupt exception handler so that when you Ctrl+C to exit, the final message prints on a new line instead of overwriting the last CPU usage.



No comments:

Post a Comment

Current Project

More Power shell One Liners

PowerShell One-Liners Permalink Time of the Last Reboot Permalink (Get-CimInstance Win32_OperatingSystem).LastBootUpTime Find Your Public IP...