All pastes #608959 Raw Edit

kestaz

public python v1 · immutable
·published 2007-07-08 16:21 UTC
rendered paste body
from __future__ import divisionimport reimport osfrom math import floorprint '''	Paprasta programa parodo kiek % yra baterijos.	Testuota ant HP nx6325 + linux	by kestaz 2007 @ priekule'''class get_battery_info:	def __init__(self, p):		self.__last_full_capacity = 0        	self.__last_remaining_capacity = 0		self.__battery_present = 0		self.path = p	def is_battery_present(self, s, line):		p = re.compile(s + '\s + (.+)$')		m = p.match(line)		if m:			self.__battery_present = m.group(1)				def find_last_full_capacity(self, s,line):		p = re.compile(s + '\s+([1-9]+)')		m = p.match(line)		if m:			self.__last_full_capacity = int(m.group(1))	def find_remaining_capacity(self, s, line):		p = re.compile(s + '\s+([1-9]+)')		m = p.match(line)		if m:			self.__last_remaining_capacity = int(m.group(1))						def get(self):		f = open(self.path + "info")		while True:			k = f.readline()			if not k: break			self.is_battery_present("present:", k)			self.find_last_full_capacity("last full capacity:",k)		f1 = open(self.path + "state")		while True:			k = f1.readline()			if self.__battery_present == 'no': break			if not k: break			self.find_remaining_capacity("remaining capacity:", k)	def get_percentage(self):		k = self.__last_remaining_capacity		p = self.__last_full_capacity		if not self.__battery_present == 'no':			if p == k: print "100%"			else:				m = ((p-1) / k) * 100.0 				self.__battery_present = 0				m = floor(m)				print "%.0f" % m + "%"		else: print "Baterija neprijungta!" class list_bat_files:	def __init__(self, p):		self.path = p 		self.get_dirs()	def get_dirs(self):		for root, dirs, files in os.walk(self.path):			if 'state' in files and 'info' in files:				print "Bat: " + root				f = get_battery_info(root + "/")				f.get()				m = f.get_percentage()			f = list_bat_files("/proc/acpi/battery")