From b49d098bc2ac48f533f631f1ecc6256cb6d9003c Mon Sep 17 00:00:00 2001 From: rasbt Date: Sat, 28 Jun 2014 14:06:33 -0400 Subject: [PATCH] watermark --- README.md | 2 +- ipython_magic/datemagic.py | 104 ---------- .../{datemagic.ipynb => watermark.ipynb} | 184 ++++++++++-------- ipython_magic/watermark.py | 137 +++++++++++++ 4 files changed, 241 insertions(+), 186 deletions(-) delete mode 100644 ipython_magic/datemagic.py rename ipython_magic/{datemagic.ipynb => watermark.ipynb} (59%) create mode 100644 ipython_magic/watermark.py diff --git a/README.md b/README.md index 0b71806..5c70dcb 100755 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ GitHub repository [One-Python-benchmark-per-day](https://github.com/rasbt/One-Py ###// Useful scripts and snippets -- [IPython magic function %date](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/ipython_magic/datemagic.ipynb) - for printing date stamps and/or the current Python/IPython version +- [IPython magic function %watermark](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/ipython_magic/watermark.ipynb) - for printing date- and time-stamps and various system info - [Shell script](./useful_scripts/prepend_python_shebang.sh) for prepending Python-shebangs to .py files. diff --git a/ipython_magic/datemagic.py b/ipython_magic/datemagic.py deleted file mode 100644 index e84b816..0000000 --- a/ipython_magic/datemagic.py +++ /dev/null @@ -1,104 +0,0 @@ -""" -Sebastian Raschka 2014 - -watermark.py -version 1.0.0 - - -IPython magic function for printing basic information, such as the current date, time, -Python, and IPython version. - -Installation: - -""" - -import platform -from time import strftime -from pkg_resources import get_distribution -from multiprocessing import cpu_count - -import IPython -from IPython.core.magic import Magics, magics_class, line_magic -from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring - -@magics_class -class WaterMark(Magics): - """ - IPython magic function for printing the current date, time, Python, - and IPython version. - - """ - @magic_arguments() - @argument('-d', '--date', action='store_true', help='prints current date') - @argument('-n', '--datename', action='store_true', help='prints date with abbrv. day and month names') - @argument('-t', '--datetime', action='store_true', help='prints currenttime') - @argument('-u', '--custom_time', type=str, help='prints a valid strftime() string') - @argument('-v', '--python', action='store_true', help='prints Python and IPython version') - @argument('-p', '--packages', type=str, help='prints versions of Python modules and packages') - @argument('-m', '--machine', action='store_true', help='prints system and machine info') - @line_magic - def watermark(self, line): - """ - IPython magic function for printing the current date, time, Python, - and IPython version. - - """ - args = parse_argstring(self.watermark, line) - - - if not any(vars(args).values()): - self._print_customtime('%d/%m/%Y %H:%M:%S') - self._print_pyver(args) - self._print_sysinfo() - - - def _print_customtime(self, ctime): - print(strftime(ctime)) - - def _print_pack(self): - out = '' - packages = args.packages.split(',') - for p in packages: - out += '\n%s' %get_distribution(p).version - print(out) - - - def _print_pyver(self, args): - print('\nPython %s\nIPython %s' %( - platform.python_version(), - IPython.__version__) - ) - - - def _print_datetime(self, args): - out = '' - if args.date: - out += strftime('%d/%m/%Y') - elif args.dateday: - out += strftime('%a %b %M %Y') - if args.time: - if out: - out += ' ' - out += strftime('%H:%M:%S') - if args.timezone: - if out: - out += ' ' - out += strftime('%Z') - if args.custom_time: - if out: - out += ' ' - out += strftime(args.custom_time) - print(out) - - def _print_sysinfo(self): - print('\ncompile : %s' %platform.python_compiler()) - print('system : %s' %platform.system()) - print('release : %s' %platform.release()) - print('machine : %s' %platform.machine()) - print('processor : %s' %platform.processor()) - print('CPU count : %s' %cpu_count()) - print('interpreter: %s' %platform.architecture()[0]) - - -def load_ipython_extension(ipython): - ipython.register_magics(WaterMark) diff --git a/ipython_magic/datemagic.ipynb b/ipython_magic/watermark.ipynb similarity index 59% rename from ipython_magic/datemagic.ipynb rename to ipython_magic/watermark.ipynb index fde6d92..bc2b250 100644 --- a/ipython_magic/datemagic.ipynb +++ b/ipython_magic/watermark.ipynb @@ -1,19 +1,41 @@ { "metadata": { "name": "", - "signature": "sha256:b0db931ae8afaadadc3173ffe809324b1b9396a4360f574e697b83ce32103015" + "signature": "sha256:6a672b3872feb4ac09bbf846694bc0d4a04b3d7914e4afec8b2bd6398a5a57bd" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[Sebastian Raschka](http://sebastianraschka.com) \n", + "\n", + "- [Link to the GitHub Repository python_reference](https://github.com/rasbt/python_reference/)\n", + "\n", + "
\n", + "I would be happy to hear your comments and suggestions. \n", + "Please feel free to drop me a note via\n", + "[twitter](https://twitter.com/rasbt), [email](mailto:bluewoodtree@gmail.com), or [google+](https://plus.google.com/+SebastianRaschka).\n", + "
" + ] + }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ - "IPython magic function documentation - `%date`" + "IPython magic function documentation - `%watermark`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "I wrote this simple `watermark` IPython magic function to conveniently add date- and time-stamps to my IPython notebooks. Also, I often want to document various system information, e.g., for my [Python benchmarks](https://github.com/rasbt/One-Python-benchmark-per-day) series.\n" ] }, { @@ -36,34 +58,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "You can install the magic function directly from my GitHub repository via:" + "The `watermark` line magic can be directly installed from my GitHub repository via" ] }, { "cell_type": "code", "collapsed": false, "input": [ - "#%install_ext https://raw.githubusercontent.com/rasbt/python_reference/master/ipython_magic/datemagic.py" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stdout", - "text": [ - "Installed datemagic.py. To use it, type:\n", - " %load_ext datemagic\n" - ] - } - ], - "prompt_number": 1 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "%install_ext ./datemagic.py" + "%install_ext https://raw.githubusercontent.com/rasbt/python_reference/master/ipython_magic/watermark.py" ], "language": "python", "metadata": {}, @@ -92,7 +94,7 @@ "level": 2, "metadata": {}, "source": [ - "Loading the `%date` magic" + "Loading the `%watermark` magic" ] }, { @@ -106,7 +108,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "%load_ext datemagic" + "%load_ext watermark" ], "language": "python", "metadata": {}, @@ -133,7 +135,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In order to display the available `magic` arguments, type:" + "In order to display the optional `watermark` arguments, type" ] }, { @@ -151,18 +153,26 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "
%date [-d] [-t] [-s] [-p] [-i]\n",
+      "
%watermark [-d] [-n] [-t] [-z] [-u] [-c CUSTOM_TIME] [-v] [-p PACKAGES]\n",
+      "                 [-m]\n",
       "\n",
       " \n",
-      "IPython magic function for printing the current date, time, Python,\n",
-      "and IPython version.\n",
+      "IPython magic function to print date/time stamps \n",
+      "and various system information.\n",
       "\n",
       "optional arguments:\n",
-      "  -d, --date      prints date (default)\n",
-      "  -t, --time      print current time\n",
-      "  -s, --datetime  print current time\n",
-      "  -p, --python    prints Python version\n",
-      "  -i, --ipython   prints IPython version\n",
+      "  -d, --date            prints current date\n",
+      "  -n, --datename        prints date with abbrv. day and month names\n",
+      "  -t, --time            prints current time\n",
+      "  -z, --timezone        appends the local time zone\n",
+      "  -u, --updated         appends a string \"Last updated: \"\n",
+      "  -c CUSTOM_TIME, --custom_time CUSTOM_TIME\n",
+      "                        prints a valid strftime() string\n",
+      "  -v, --python          prints Python and IPython version\n",
+      "  -p PACKAGES, --packages PACKAGES\n",
+      "                        prints versions of specified Python modules and\n",
+      "                        packages\n",
+      "  -m, --machine         prints system and machine info\n",
       "
" ] }, @@ -195,21 +205,22 @@ "output_type": "stream", "stream": "stdout", "text": [ - "28/06/2014 02:56:36\n", + "28/06/2014 14:03:45\n", + "\n", "Python 3.4.1\n", "IPython 2.1.0\n", "\n", - "compile : GCC 4.2.1 (Apple Inc. build 5577)\n", + "compiler : GCC 4.2.1 (Apple Inc. build 5577)\n", "system : Darwin\n", "release : 13.2.0\n", "machine : x86_64\n", "processor : i386\n", - "CPU count : 2\n", + "CPU cores : 2\n", "interpreter: 64bit\n" ] } ], - "prompt_number": 3 + "prompt_number": 4 }, { "cell_type": "markdown", @@ -222,7 +233,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "%date -t" + "%watermark -d -t" ], "language": "python", "metadata": {}, @@ -231,7 +242,33 @@ "output_type": "stream", "stream": "stdout", "text": [ - "01:18:28\n" + "28/06/2014 14:03:49 \n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%watermark -u -n -t -z" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Last updated: Sat Jun 03 2014 14:03:50 EDT\n" ] } ], @@ -248,7 +285,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "%date -dt" + "%watermark -v" ], "language": "python", "metadata": {}, @@ -257,7 +294,8 @@ "output_type": "stream", "stream": "stdout", "text": [ - "28/06/2014 01:18:33\n" + "Python 3.4.1\n", + "IPython 2.1.0\n" ] } ], @@ -274,7 +312,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "%date -p" + "%watermark -m" ], "language": "python", "metadata": {}, @@ -283,7 +321,13 @@ "output_type": "stream", "stream": "stdout", "text": [ - "Python 3.3.5\n" + "compiler : GCC 4.2.1 (Apple Inc. build 5577)\n", + "system : Darwin\n", + "release : 13.2.0\n", + "machine : x86_64\n", + "processor : i386\n", + "CPU cores : 2\n", + "interpreter: 64bit\n" ] } ], @@ -300,7 +344,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "%date -i" + "%watermark -v -m -p numpy,scipy" ], "language": "python", "metadata": {}, @@ -309,45 +353,23 @@ "output_type": "stream", "stream": "stdout", "text": [ - "IPython 2.1.0\n" + "Python 3.4.1\n", + "IPython 2.1.0\n", + "\n", + "numpy 1.8.1\n", + "scipy 0.14.0\n", + "\n", + "compiler : GCC 4.2.1 (Apple Inc. build 5577)\n", + "system : Darwin\n", + "release : 13.2.0\n", + "machine : x86_64\n", + "processor : i386\n", + "CPU cores : 2\n", + "interpreter: 64bit\n" ] } ], "prompt_number": 9 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "
" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "%date -d -i -z -p" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "output_type": "stream", - "stream": "stderr", - "text": [ - "UsageError: unrecognized arguments: -z" - ] - } - ], - "prompt_number": 11 - }, - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] } ], "metadata": {} diff --git a/ipython_magic/watermark.py b/ipython_magic/watermark.py new file mode 100644 index 0000000..d54a074 --- /dev/null +++ b/ipython_magic/watermark.py @@ -0,0 +1,137 @@ +""" +Sebastian Raschka 2014 + +watermark.py +version 1.0.0 + + +IPython magic function to print date/time stamps and various system information. + +Installation: + + %install_ext https://raw.githubusercontent.com/rasbt/python_reference/master/ipython_magic/watermark.py + +Usage: + + %load_ext watermark + + %watermark + + optional arguments: + -d, --date prints current date + -n, --datename prints date with abbrv. day and month names + -t, --time prints current time + -z, --timezone appends the local time zone + -u, --updated appends a string "Last updated: " + -c CUSTOM_TIME, --custom_time CUSTOM_TIME + prints a valid strftime() string + -v, --python prints Python and IPython version + -p PACKAGES, --packages PACKAGES + prints versions of specified Python modules and + packages + -m, --machine prints system and machine info + +Examples: + + %watermark -d -t + +""" +import platform +from time import strftime +from pkg_resources import get_distribution +from multiprocessing import cpu_count + +import IPython +from IPython.core.magic import Magics, magics_class, line_magic +from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring + +@magics_class +class WaterMark(Magics): + """ + IPython magic function to print date/time stamps + and various system information. + + """ + @magic_arguments() + @argument('-d', '--date', action='store_true', help='prints current date') + @argument('-n', '--datename', action='store_true', help='prints date with abbrv. day and month names') + @argument('-t', '--time', action='store_true', help='prints current time') + @argument('-z', '--timezone', action='store_true', help='appends the local time zone') + @argument('-u', '--updated', action='store_true', help='appends a string "Last updated: "') + @argument('-c', '--custom_time', type=str, help='prints a valid strftime() string') + @argument('-v', '--python', action='store_true', help='prints Python and IPython version') + @argument('-p', '--packages', type=str, help='prints versions of specified Python modules and packages') + @argument('-m', '--machine', action='store_true', help='prints system and machine info') + @line_magic + def watermark(self, line): + """ + IPython magic function to print date/time stamps + and various system information. + + """ + self.out = '' + args = parse_argstring(self.watermark, line) + + if not any(vars(args).values()): + self.out += strftime('%d/%m/%Y %H:%M:%S') + self._get_pyversions() + self._get_sysinfo() + + else: + if args.updated: + self.out += 'Last updated: ' + if args.custom_time: + self.out += '%s ' %strfime(args.custom_time) + if args.date: + self.out += '%s ' %strftime('%d/%m/%Y') + elif args.datename: + self.out += '%s ' %strftime('%a %b %M %Y') + if args.time: + self.out += '%s ' %strftime('%H:%M:%S') + if args.timezone: + self.out += strftime('%Z') + if args.python: + self._get_pyversions() + if args.packages: + self._get_packages(args.packages) + if args.machine: + self._get_sysinfo() + + print(self.out) + + + def _get_packages(self, pkgs): + if self.out: + self.out += '\n' + packages = pkgs.split(',') + for p in packages: + self.out += '\n%s %s' %(p, get_distribution(p).version) + + + def _get_pyversions(self): + if self.out: + self.out += '\n\n' + self.out += 'Python %s\nIPython %s' %( + platform.python_version(), + IPython.__version__ + ) + + + def _get_sysinfo(self): + if self.out: + self.out += '\n\n' + self.out += 'compiler : %s\nsystem : %s\n'\ + 'release : %s\nmachine : %s\n'\ + 'processor : %s\nCPU cores : %s\ninterpreter: %s'%( + platform.python_compiler(), + platform.system(), + platform.release(), + platform.machine(), + platform.processor(), + cpu_count(), + platform.architecture()[0] + ) + + +def load_ipython_extension(ipython): + ipython.register_magics(WaterMark) \ No newline at end of file