コード例 #1
0
ファイル: __init__.py プロジェクト: canassa/minibar
class Minibar(object):
    def __init__(self, total, template='{i}/{total} {bar:fill}', out=sys.stdout):
        self.total = total
        self.template = text_type(template)
        self.enabled_widgets = list(self._get_widgets())
        self.terminal_width = get_terminal_width()
        self.fmt = Formatter(self.terminal_width)
        self.start_time = time.time()
        self.counter = 1
        self.out=out
        self.render()

    def _get_widgets(self):
        avaliable_widgets = dict([(w.name, w) for w in Widget.__subclasses__()])
        for _, field_name, _, _ in string.Formatter().parse(self.template):
            if field_name in avaliable_widgets:
                yield avaliable_widgets[field_name]

    def iter(self, iterator):
        for value in iterator:
            self.render()
            self.counter += 1
            yield value

    def render(self):
        if self.counter <= self.total:
            elapsed = time.time() - self.start_time
            kwargs = dict([(w.name, w(self.counter, self.total, elapsed)) for w in self.enabled_widgets])
            iprint(self.fmt.format(self.template, **kwargs),self.out)

    def inc(self, increment=1):
        self.render()
        self.counter += increment
コード例 #2
0
ファイル: __init__.py プロジェクト: lowks/minibar
def bar(iterator, template='{i}/{total} {bar:fill}', total=None):
    if total is None:
        total = len(iterator)

    # Make sure that template is unicode
    template = str(template)

    enabled_widgets = list(get_widgets(template, iterator, total))
    terminal_width = get_terminal_width()
    fmt = Formatter(terminal_width)
    start_time = time.time()

    for i, value in enumerate(iterator):
        elapsed = time.time() - start_time
        kwargs = {w.name: w(i + 1, total, elapsed) for w in enabled_widgets}
        iprint(fmt.format(template, **kwargs))

        yield value
コード例 #3
0
ファイル: __init__.py プロジェクト: lowks/minibar
def bar(iterator, template='{i}/{total} {bar:fill}', total=None):
    if total is None:
        total = len(iterator)

    # Make sure that template is unicode
    template = str(template)

    enabled_widgets = list(get_widgets(template, iterator, total))
    terminal_width = get_terminal_width()
    fmt = Formatter(terminal_width)
    start_time = time.time()

    for i, value in enumerate(iterator):
        elapsed = time.time() - start_time
        kwargs = {w.name: w(i + 1, total, elapsed) for w in enabled_widgets}
        iprint(fmt.format(template, **kwargs))

        yield value
コード例 #4
0
ファイル: __init__.py プロジェクト: sirlark/minibar
class Minibar(object):
    def __init__(self,
                 total,
                 template='{i}/{total} {bar:fill}',
                 out=sys.stdout):
        self.total = total
        self.template = text_type(template)
        self.enabled_widgets = list(self._get_widgets())
        self.terminal_width = get_terminal_width()
        self.fmt = Formatter(self.terminal_width)
        self.start_time = time.time()
        self.counter = 1
        self.out = out
        self.render()

    def _get_widgets(self):
        avaliable_widgets = {w.name: w for w in Widget.__subclasses__()}
        for _, field_name, _, _ in string.Formatter().parse(self.template):
            if field_name in avaliable_widgets:
                yield avaliable_widgets[field_name]

    def iter(self, iterator):
        for value in iterator:
            self.render()
            self.counter += 1
            yield value

    def render(self):
        if self.counter <= self.total:
            elapsed = time.time() - self.start_time
            kwargs = {
                w.name: w(self.counter, self.total, elapsed)
                for w in self.enabled_widgets
            }
            iprint(self.fmt.format(self.template, **kwargs), self.out)

    def inc(self, increment=1):
        self.render()
        self.counter += increment