Skip to content

Commit c030b59

Browse files
authored
Merge pull request #355 from Nibba2018/battery_info
Battery Information Script
2 parents ef70488 + ebbbf8c commit c030b59

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

Python/Battery-Info/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Battery Information of a Device.
2+
The following script provides necessary battery information.
3+
4+
## Running Script
5+
* Install dependencies `pip install -r requirements.txt`
6+
* Execute script `python info.py`
7+
8+
## Output
9+
10+
![output](output.png)

Python/Battery-Info/info.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
import psutil
3+
4+
# Helper method to convert seconds to hours.
5+
def secs_to_hours(secs):
6+
mm, ss = secs//60, secs%60
7+
hh, mm = mm//60, mm%60
8+
return "%d:%02d:%02d" % (hh, mm, ss)
9+
10+
# Check if the OS is supported or not.
11+
if not hasattr(psutil, "sensors_battery"):
12+
sys.exit("platform not supported")
13+
14+
# Create battery instance.
15+
batt = psutil.sensors_battery()
16+
17+
# Check if battery is installed or not.
18+
if batt is None:
19+
sys.exit("no battery is installed")
20+
21+
# Print the current battery charge.
22+
print(f"charge: {round(batt.percent, 2)}")
23+
24+
# Check if battery is plugged in or not.
25+
if batt.power_plugged:
26+
print(f"status: {'charging' if batt.percent < 100 else 'fully charged'}")
27+
print("plugged in: yes")
28+
else:
29+
print(f"left: {secs_to_hours(batt.secsleft)}")
30+
print(f"status: discharging")
31+
print(f"plugged in: no")

Python/Battery-Info/output.png

20.9 KB
Loading

Python/Battery-Info/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
psutil

0 commit comments

Comments
 (0)