From b7b263639b1b56141d6d5919dfd4d9ee74f6a797 Mon Sep 17 00:00:00 2001 From: rasbt Date: Wed, 7 Sep 2016 21:35:58 -0400 Subject: [PATCH] pycharm support --- CHANGELOG.md | 4 ++ examples/pyprind_demo.ipynb | 140 +++++++++--------------------------- pyprind/__init__.py | 2 +- pyprind/prog_class.py | 23 +++--- 4 files changed, 54 insertions(+), 115 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f04ccb..68d3501 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +Version 2.9.9 +================ +- added PyCharm support + Version 2.9.8 ================ - raise `ValueError` if `monitor` is set to `True` and `psutils` is not installed diff --git a/examples/pyprind_demo.ipynb b/examples/pyprind_demo.ipynb index fb47a25..9f43cfe 100644 --- a/examples/pyprind_demo.ipynb +++ b/examples/pyprind_demo.ipynb @@ -23,7 +23,7 @@ "CPython 3.5.2\n", "IPython 5.1.0\n", "\n", - "pyprind 2.9.8\n" + "pyprind 2.9.9\n" ] } ], @@ -78,8 +78,7 @@ "- [Progress Bar/Percentage Indicator - Changing the output stream](#Progress-Bar/Percentage-Indicator---Setting-a-title)\n", "- [Stopping the Progress Bar/Percentage Indicator early](#Stopping-the-Progress-Bar/Percentage-Indicator-early)\n", "- [Choosing your own progress bar style](#Choosing-your-own-progress-bar-style)\n", - "- [Controlling the update frequency](#Controlling-the-update-frequency)\n", - "- [Pyprind in PyCharm](#Pyprind in PyCharm)" + "- [Controlling the update frequency](#Controlling-the-update-frequency)" ] }, { @@ -169,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -188,8 +187,8 @@ "output_type": "stream", "text": [ "Title: while example\n", - " Started: 09/07/2016 13:06:58\n", - " Finished: 09/07/2016 13:07:03\n", + " Started: 09/07/2016 21:32:36\n", + " Finished: 09/07/2016 21:32:41\n", " Total time elapsed: 00:00:05\n" ] } @@ -246,7 +245,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -299,7 +298,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -321,7 +320,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -372,7 +371,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": { "collapsed": false }, @@ -391,8 +390,8 @@ "output_type": "stream", "text": [ "Title: \n", - " Started: 03/05/2016 14:44:22\n", - " Finished: 03/05/2016 14:44:28\n", + " Started: 09/07/2016 21:32:58\n", + " Finished: 09/07/2016 21:33:03\n", " Total time elapsed: 00:00:05\n" ] } @@ -407,7 +406,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": { "collapsed": false, "scrolled": true @@ -428,11 +427,11 @@ "output_type": "stream", "text": [ "Title: Job_1\n", - " Started: 03/05/2016 14:44:28\n", - " Finished: 03/05/2016 14:44:33\n", + " Started: 09/07/2016 21:33:03\n", + " Finished: 09/07/2016 21:33:08\n", " Total time elapsed: 00:00:05\n", - " CPU %: 1.00\n", - " Memory %: 0.39\n" + " CPU %: 2.10\n", + " Memory %: 0.48\n" ] } ], @@ -504,8 +503,8 @@ "output_type": "stream", "text": [ "Title: \n", - " Started: 09/07/2016 13:14:09\n", - " Finished: 09/07/2016 13:14:14\n", + " Started: 09/07/2016 21:33:08\n", + " Finished: 09/07/2016 21:33:13\n", " Total time elapsed: 00:00:05\n", " CPU %: 1.90\n", " Memory %: 0.48\n" @@ -547,10 +546,10 @@ "output_type": "stream", "text": [ "Title: \n", - " Started: 09/07/2016 13:14:14\n", - " Finished: 09/07/2016 13:14:19\n", + " Started: 09/07/2016 21:33:13\n", + " Finished: 09/07/2016 21:33:19\n", " Total time elapsed: 00:00:05\n", - " CPU %: 3.40\n", + " CPU %: 2.90\n", " Memory %: 0.48\n" ] } @@ -595,7 +594,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 12, "metadata": { "collapsed": false }, @@ -620,7 +619,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 13, "metadata": { "collapsed": false }, @@ -674,7 +673,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "metadata": { "collapsed": false }, @@ -698,7 +697,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 15, "metadata": { "collapsed": false }, @@ -752,7 +751,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 16, "metadata": { "collapsed": false }, @@ -776,7 +775,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 17, "metadata": { "collapsed": false }, @@ -800,21 +799,11 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 18, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0% 100%\n", - "[##############################] | ETA: 00:00:00\n", - "Total time elapsed: 00:00:05\n" - ] - } - ], + "outputs": [], "source": [ "import sys\n", "\n", @@ -855,7 +844,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 19, "metadata": { "collapsed": false }, @@ -910,7 +899,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 20, "metadata": { "collapsed": false }, @@ -960,7 +949,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": { "collapsed": false }, @@ -1015,7 +1004,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 22, "metadata": { "collapsed": false }, @@ -1054,7 +1043,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 23, "metadata": { "collapsed": false }, @@ -1079,7 +1068,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 24, "metadata": { "collapsed": false }, @@ -1100,65 +1089,6 @@ " time.sleep(0.2) # do some computation\n", " bar.update()" ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "
\n", - "
" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Pyprind in PyCharm" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "[[back to section overview](#sections)]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you are using PyCharm, you need to pass the `stdout` or `stderr` from the `sys` module as a `stream` argument to the `ProgBar` or `ProgPercent` class as shown in the example below:" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0% 100%\n", - "[##############################] | ETA: 00:00:00\n", - "Total time elapsed: 00:00:05\n" - ] - } - ], - "source": [ - "import time\n", - "import sys\n", - "\n", - "n = 100\n", - "timesleep = 0.05\n", - "\n", - "bar = pyprind.ProgBar(n, stream=sys.stdout)\n", - "for i in range(n):\n", - " time.sleep(timesleep) # your computation here\n", - " bar.update()" - ] } ], "metadata": { diff --git a/pyprind/__init__.py b/pyprind/__init__.py index 08af3c3..27786bc 100644 --- a/pyprind/__init__.py +++ b/pyprind/__init__.py @@ -17,4 +17,4 @@ from .generator_factory import prog_bar -__version__ = '2.9.8' +__version__ = '2.9.9' diff --git a/pyprind/prog_class.py b/pyprind/prog_class.py index c0ce211..019e271 100644 --- a/pyprind/prog_class.py +++ b/pyprind/prog_class.py @@ -85,16 +85,21 @@ def stop(self): def _check_stream(self): """Determines which output stream (stdout, stderr, or custom) to use""" if self.stream: + try: - if self.stream == 1 and os.isatty(sys.stdout.fileno()): - self._stream_out = sys.stdout.write - self._stream_flush = sys.stdout.flush - elif self.stream == 2 and os.isatty(sys.stderr.fileno()): - self._stream_out = sys.stderr.write - self._stream_flush = sys.stderr.flush + supported = ('PYCHARM_HOSTED' in os.environ or + os.isatty(sys.stdout.fileno())) # a fix for IPython notebook "IOStream has no fileno." - except UnsupportedOperation: + except(UnsupportedOperation): + supported = True + + else: + if self.stream is not None and hasattr(self.stream, 'write'): + self._stream_out = self.stream.write + self._stream_flush = self.stream.flush + + if supported: if self.stream == 1: self._stream_out = sys.stdout.write self._stream_flush = sys.stdout.flush @@ -105,8 +110,8 @@ def _check_stream(self): if self.stream is not None and hasattr(self.stream, 'write'): self._stream_out = self.stream.write self._stream_flush = self.stream.flush - else: - print('Warning: No valid output stream.') + else: + print('Warning: No valid output stream.') def _elapsed(self): """ Returns elapsed time at update. """