Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Progress bar (See related posts)

This is a simple class to generate char-based progress bars.

Example:
[++++++      ]


class Bar:
	def __init__(self):
		self.len = 80
		self.chars = (' ', '+')
		self.wrap = ('[', ']')
		self.filledc = 0

	def fill(self, i):
		assert not (i > 100) or (i < 0)
		self._setP(i)

	def _setP(self, p):
		self.filledc = int(round(float(self.len*p)/100))

	def show(self):
		out = []
		out.append(self.wrap[0])
		out.append(self.filledc*self.chars[1])
		out.append((self.len-self.filledc)*self.chars[0])
		out.append(self.wrap[1])
		return "".join(out)

b = Bar()
b.len = 20
b.fill(20)
print b.show()

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts