コード例 #1
0
ファイル: progressbar.py プロジェクト: e-koch/BaSiCs
    def __init__(self, total_or_items, ipython_widget=False, file=None):
        """
        Parameters
        ----------
        total_or_items : int or sequence
            If an int, the number of increments in the process being
            tracked.  If a sequence, the items to iterate over.

        ipython_widget : bool, optional
            If `True`, the progress bar will display as an IPython
            notebook widget.

        file : writable file-like object, optional
            The file to write the progress bar to.  Defaults to
            `sys.stdout`.  If `file` is not a tty (as determined by
            calling its `isatty` member, if any, or special case hacks
            to detect the IPython console), the progress bar will be
            completely silent.
        """

        ipython_widget = False

        # if ipython_widget:
        #     # Import only if ipython_widget, i.e., widget in IPython
        #     # notebook
        #     if ipython_major_version < 4:
        #         from IPython.html import widgets
        #     else:
        #         from ipywidgets import widgets
        #     from IPython.display import display

        if file is None:
            file = _get_stdout()

        if not isatty(file) and not ipython_widget:
            self.update = self._silent_update
            self._silent = True
        else:
            self._silent = False

        if isiterable(total_or_items):
            self._items = iter(total_or_items)
            self._total = len(total_or_items)
        else:
            try:
                self._total = int(total_or_items)
            except TypeError:
                raise TypeError("First argument must be int or sequence")
            else:
                self._items = iter(range(self._total))

        self._file = file
        self._start_time = time.time()
        self._human_total = human_file_size(self._total)
        self._ipython_widget = ipython_widget

        self._signal_set = False
        if not ipython_widget:
            self._should_handle_resize = (
                _CAN_RESIZE_TERMINAL and self._file.isatty())
            self._handle_resize()
            if self._should_handle_resize:
                signal.signal(signal.SIGWINCH, self._handle_resize)
                self._signal_set = True

        self.update(0)
コード例 #2
0
ファイル: progressbar.py プロジェクト: hongliliu/BaSiCs
    def __init__(self, total_or_items, ipython_widget=False, file=None):
        """
        Parameters
        ----------
        total_or_items : int or sequence
            If an int, the number of increments in the process being
            tracked.  If a sequence, the items to iterate over.

        ipython_widget : bool, optional
            If `True`, the progress bar will display as an IPython
            notebook widget.

        file : writable file-like object, optional
            The file to write the progress bar to.  Defaults to
            `sys.stdout`.  If `file` is not a tty (as determined by
            calling its `isatty` member, if any, or special case hacks
            to detect the IPython console), the progress bar will be
            completely silent.
        """

        ipython_widget = False

        # if ipython_widget:
        #     # Import only if ipython_widget, i.e., widget in IPython
        #     # notebook
        #     if ipython_major_version < 4:
        #         from IPython.html import widgets
        #     else:
        #         from ipywidgets import widgets
        #     from IPython.display import display

        if file is None:
            file = _get_stdout()

        if not isatty(file) and not ipython_widget:
            self.update = self._silent_update
            self._silent = True
        else:
            self._silent = False

        if isiterable(total_or_items):
            self._items = iter(total_or_items)
            self._total = len(total_or_items)
        else:
            try:
                self._total = int(total_or_items)
            except TypeError:
                raise TypeError("First argument must be int or sequence")
            else:
                self._items = iter(range(self._total))

        self._file = file
        self._start_time = time.time()
        self._human_total = human_file_size(self._total)
        self._ipython_widget = ipython_widget

        self._signal_set = False
        if not ipython_widget:
            self._should_handle_resize = (_CAN_RESIZE_TERMINAL
                                          and self._file.isatty())
            self._handle_resize()
            if self._should_handle_resize:
                signal.signal(signal.SIGWINCH, self._handle_resize)
                self._signal_set = True

        self.update(0)
コード例 #3
0
ファイル: progressbar.py プロジェクト: e-koch/BaSiCs
    def map(cls, function, items, multiprocess=False, file=None, step=100,
            item_len=None, nprocesses=None):
        """
        Does a `map` operation while displaying a progress bar with
        percentage complete.

        ::

            def work(i):
                print(i)

            ProgressBar.map(work, range(50))

        Parameters
        ----------
        function : function
            Function to call for each step

        items : sequence
            Sequence where each element is a tuple of arguments to pass to
            *function*.

        multiprocess : bool, optional
            If `True`, use the `multiprocessing` module to distribute each
            task to a different processor core.

        file : writeable file-like object, optional
            The file to write the progress bar to.  Defaults to
            `sys.stdout`.  If `file` is not a tty (as determined by
            calling its `isatty` member, if any), the scrollbar will
            be completely silent.

        step : int, optional
            Update the progress bar at least every *step* steps (default: 100).
            If ``multiprocess`` is `True`, this will affect the size
            of the chunks of ``items`` that are submitted as separate tasks
            to the process pool.  A large step size may make the job
            complete faster if ``items`` is very long.
        """

        results = []

        if file is None:
            file = _get_stdout()

        if item_len is not None:
            assert isinstance(item_len, int)
            if hasattr(items, "__len__"):
                assert item_len == len(items)
        else:
            if hasattr(items, "__len__"):
                item_len = len(items)
            else:
                # Will convert to iterable. Not a good thing to do with
                # large inputs.
                items = list(items)
                item_len = len(items)

        with cls(item_len, file=file) as bar:
            default_step = max(int(float(item_len) / bar._bar_length), 1)
            chunksize = min(default_step, step)
            if not multiprocess:
                for i, item in enumerate(items):
                    results.append(function(item))
                    if (i % chunksize) == 0:
                        bar.update(i)
            else:
                max_proc = multiprocessing.cpu_count()
                if nprocesses is None:
                    nprocesses = max_proc
                elif nprocesses > max_proc:
                    nprocesses = max_proc

                p = multiprocessing.Pool(nprocesses)
                for i, out in enumerate(p.imap_unordered(function,
                                                         items,
                                                         chunksize=1)):
                    bar.update(i)
                    results.append(out)
                p.close()
                p.join()

        return results
コード例 #4
0
ファイル: progressbar.py プロジェクト: hongliliu/BaSiCs
    def map(cls,
            function,
            items,
            multiprocess=False,
            file=None,
            step=100,
            item_len=None,
            nprocesses=None):
        """
        Does a `map` operation while displaying a progress bar with
        percentage complete.

        ::

            def work(i):
                print(i)

            ProgressBar.map(work, range(50))

        Parameters
        ----------
        function : function
            Function to call for each step

        items : sequence
            Sequence where each element is a tuple of arguments to pass to
            *function*.

        multiprocess : bool, optional
            If `True`, use the `multiprocessing` module to distribute each
            task to a different processor core.

        file : writeable file-like object, optional
            The file to write the progress bar to.  Defaults to
            `sys.stdout`.  If `file` is not a tty (as determined by
            calling its `isatty` member, if any), the scrollbar will
            be completely silent.

        step : int, optional
            Update the progress bar at least every *step* steps (default: 100).
            If ``multiprocess`` is `True`, this will affect the size
            of the chunks of ``items`` that are submitted as separate tasks
            to the process pool.  A large step size may make the job
            complete faster if ``items`` is very long.
        """

        results = []

        if file is None:
            file = _get_stdout()

        if item_len is not None:
            assert isinstance(item_len, int)
            if hasattr(items, "__len__"):
                assert item_len == len(items)
        else:
            if hasattr(items, "__len__"):
                item_len = len(items)
            else:
                # Will convert to iterable. Not a good thing to do with
                # large inputs.
                items = list(items)
                item_len = len(items)

        with cls(item_len, file=file) as bar:
            default_step = max(int(float(item_len) / bar._bar_length), 1)
            chunksize = min(default_step, step)
            if not multiprocess:
                for i, item in enumerate(items):
                    results.append(function(item))
                    if (i % chunksize) == 0:
                        bar.update(i)
            else:
                max_proc = multiprocessing.cpu_count()
                if nprocesses is None:
                    nprocesses = max_proc
                elif nprocesses > max_proc:
                    nprocesses = max_proc

                p = multiprocessing.Pool(nprocesses)
                for i, out in enumerate(
                        p.imap_unordered(function, items, chunksize=1)):
                    bar.update(i)
                    results.append(out)
                p.close()
                p.join()

        return results