Thursday, March 27, 2025

Python script to show Laptop Battery Percentage

Source: https://www.geeksforgeeks.org/python-script-to-show-laptop-battery-percentage/


# python script showing battery details 

import psutil 


# function returning time in hh:mm:ss 

def convertTime(seconds): 

minutes, seconds = divmod(seconds, 60) 

hours, minutes = divmod(minutes, 60) 

return "%d:%02d:%02d" % (hours, minutes, seconds) 


# returns a tuple 

battery = psutil.sensors_battery() 


print("Battery percentage : ", battery.percent) 

print("Power plugged in : ", battery.power_plugged) 


# converting seconds to hh:mm:ss 

print("Battery left : ", convertTime(battery.secsleft)) 


Battery Notifier : A Python program

Source:

  (i) www.geeksforgeeks.org/create-battery-notifier-for-laptop-using-python

  (ii) stackoverflow.com - question - doing-something-before-program-exit.

NOTE: First enable 'Notifications' in your Windows PC. (Click on the Clock icon, at the right corner in the status bar and turn off the 'Do not disturb' button.  Web link: plyer-notifications-are-not-visible

#Save this  Python program as 'BatteryNotifier.py' in your 'Users' folder, and run this program in DOS #prompt, and keep the DOS window open as long as you are working on your PC.

# Install and Import the 'psutil' module, for accessing info on running processes and system utilization.

# Install and Import the 'plyer' module, for accessing features of the Hardware, like displaying Notifications on the Desktop.

 ------------------------------------------------------------------------------------------------------------

import psutil

from plyer import notification

import time


import sys

import atexit

import signal


#To handle killing the process with kill or Ctrl+C.

def kill_handler(*args):

    print("BatteryNotifier is turned OFF")

    sys.exit(0)


signal.signal(signal.SIGINT, kill_handler)

signal.signal(signal.SIGTERM, kill_handler)

 

print("Battery Notifier is ON!!")

 

#from psutil we use the sensors_battery.


while(True):


    battery = psutil.sensors_battery()

    percent = battery.percent

    if percent < 70:

        notification.notify(title="Recharge battery!! ", message=str(percent)+"% Battery remaining", timeout=30)

        

        time.sleep(35)

        

        if battery.power_plugged:

            print("Power is plugged IN")

        else:

            notification.notify(message = "Power is plugged OUT", timeout=30)

        

    if percent > 97:

        notification.notify(title="Battery Full!! ", message=str(percent)+"% Battery remaining", timeout=30)

        

    


    #after every 5 mins this loop will iterate & show the respective notifications as long as anyone of the battery conditions is met.

    time.sleep(60*5)

     

    continue



------------------------------------------------------------------------------------------------------------------

Output: 


Console output at beginning of the program:


Console output when you end the program by 'Ctr + C' on command prompt:




Sunday, March 23, 2025

Trending Sectors

 

  1. Auto & Aviation - Tata Motors, M&M, Hero, TVS, Bajaj Auto, Inter globe aviation.
  2. Banking, Finance, Broking & Insurance - Axis bk, ICICI bk, HDFC bk, SBI, AU small bk, M&M Finance, BSE, MCX, CDSL, Dixon.
  3. Computer (IT & Software) - Infy, TCS, Wipro, Persistent.
  4. Metals - Vedanta, Hindalco, Tata Steel.
  5. Pharma - Sun, Lupin, Aurobindo, Glenmark, Divi's labs
  6. FMCG - TCP, GCP, Britannia, Colgate, HUL, ITC.
  7. PSU - NTPC, CIL, ONGC, HAl, BEL, BHEL, REC, PFC.
  8. CG - ABB, Siemens, Cummins, Polycab, Havells, Voltas.
  9. MISC - Indian Hotels, Trent, Bharti Airtel, LIC, RIL.

Python Certification

 Python course from codebasics  https://codebasics.io/courses/python-beginner-to-advanced?utm_source=infocard&utm_medium=yt&utm_camp...