Skip to content

Commit

Permalink
Merge pull request #30 from ouuan/accurate-graph
Browse files Browse the repository at this point in the history
feat: more accurate graph
  • Loading branch information
athul authored Aug 16, 2020
2 parents b37bede + 215367a commit aaff45b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
11 changes: 7 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ def this_week() -> str:

def make_graph(percent: float) -> str:
'''Make progress graph from API graph'''
done_block = '█'
empty_block = '░'
pc_rnd = round(percent)
return f"{done_block*int(pc_rnd/4)}{empty_block*int(25-int(pc_rnd/4))}"
blocks = "░▒▓█"
graph = blocks[3] * int(percent / 4 + 1 / 6)
remainder_block = int((percent + 2 / 3) % 4 * 3 / 4)
if remainder_block > 0:
graph += blocks[remainder_block]
graph += blocks[0] * (25 - len(graph))
return graph


def get_stats() -> str:
Expand Down
30 changes: 14 additions & 16 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ class TestMain(unittest.TestCase):

def test_make_graph(self):
'''Tests the make_graph function'''
self.assertEqual(make_graph(0), "░░░░░░░░░░░░░░░░░░░░░░░░░",
"0% should return ░░░░░░░░░░░░░░░░░░░░░░░░░")
self.assertEqual(make_graph(100), "█████████████████████████",
"100% should return █████████████████████████")
self.assertEqual(make_graph(50), "████████████░░░░░░░░░░░░░",
"50% should return ████████████░░░░░░░░░░░░░")
self.assertEqual(make_graph(25), "██████░░░░░░░░░░░░░░░░░░░",
"25% should return ██████░░░░░░░░░░░░░░░░░░░")
self.assertEqual(make_graph(75), "██████████████████░░░░░░░",
"75% should return ██████████████████░░░░░░░")
self.assertEqual(make_graph(3.14), "░░░░░░░░░░░░░░░░░░░░░░░░░",
"3.14% should return ░░░░░░░░░░░░░░░░░░░░░░░░░")
self.assertEqual(make_graph(9.901), "██░░░░░░░░░░░░░░░░░░░░░░░",
"9.901% should return ██░░░░░░░░░░░░░░░░░░░░░░░")
self.assertEqual(make_graph(87.5), "██████████████████████░░░",
"87.5% should return ██████████████████████░░░")
def test(percent: float, result: str):
self.assertEqual(make_graph(percent), result, f"{percent}% should return {result}")
test(0, "░░░░░░░░░░░░░░░░░░░░░░░░░")
test(100, "█████████████████████████")
test(50, "████████████▒░░░░░░░░░░░░")
test(50.001, "████████████▓░░░░░░░░░░░░")
test(25, "██████▒░░░░░░░░░░░░░░░░░░")
test(75, "██████████████████▓░░░░░░")
test(3.14, "▓░░░░░░░░░░░░░░░░░░░░░░░░")
test(9.901, "██▒░░░░░░░░░░░░░░░░░░░░░░")
test(87.334, "██████████████████████░░░")
test(87.333, "█████████████████████▓░░░")
test(4.666, "█░░░░░░░░░░░░░░░░░░░░░░░░")
test(4.667, "█▒░░░░░░░░░░░░░░░░░░░░░░░")


if __name__ == '__main__':
Expand Down

0 comments on commit aaff45b

Please sign in to comment.