Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
fix(cli): fix progressbar
Browse files Browse the repository at this point in the history
  • Loading branch information
hanhxiao committed Sep 17, 2019
1 parent 8828535 commit 65fff1a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
20 changes: 3 additions & 17 deletions gnes/client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sys
import time
import zipfile
from typing import List, Generator
from typing import Generator

from termcolor import colored

Expand Down Expand Up @@ -62,22 +62,8 @@ def query_callback(self, req: 'gnes_pb2.Request', resp: 'gnes_pb2.Response'):
print(req)
print(resp)

def read_all(self) -> List[bytes]:
if self.args.txt_file:
all_bytes = [v.encode() for v in self.args.txt_file]
elif self.args.image_zip_file:
zipfile_ = zipfile.ZipFile(self.args.image_zip_file)
all_bytes = [zipfile_.open(v).read() for v in zipfile_.namelist()]
elif self.args.video_zip_file:
zipfile_ = zipfile.ZipFile(self.args.video_zip_file)
all_bytes = [zipfile_.open(v).read() for v in zipfile_.namelist()]
else:
raise AttributeError('--txt_file, --image_zip_file, --video_zip_file one must be given')

return all_bytes

@property
def bytes_generator(self) -> Generator[bytes]:
def bytes_generator(self) -> Generator[bytes, None, None]:
if self.args.txt_file:
all_bytes = (v.encode() for v in self.args.txt_file)
elif self.args.image_zip_file:
Expand Down Expand Up @@ -107,7 +93,7 @@ def update(self):
self.num_bars -= self.bar_len
sys.stdout.write('\n')
sys.stdout.write(
'{:>10} [{:<{}}] {:3.0f}% {:>8}: {:3.1f}s {:>8}: {:3.1f} batch/s'.format(
'{:>10} [{:<{}}] {:>8}: {:3.1f}s {:>8}: {:3.1f} batch/s'.format(
colored(self.task_name, 'cyan'),
colored('=' * self.num_bars, 'green'),
self.bar_len + 9,
Expand Down
22 changes: 22 additions & 0 deletions tests/test_progressbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import time
import unittest

from gnes.client.cli import ProgressBar


class TestProgessbar(unittest.TestCase):
def setUp(self):
self.bar_len = 20

def test_progressbar5(self):
# should be 5 line
with ProgressBar(task_name='test', bar_len=self.bar_len) as pb:
for j in range(5 * self.bar_len):
pb.update()

def test_progressbar1(self):
# should be single line
with ProgressBar(task_name='test', bar_len=self.bar_len) as pb:
for j in range(self.bar_len):
pb.update()
time.sleep(.1)

0 comments on commit 65fff1a

Please sign in to comment.