watermark

This commit is contained in:
rasbt 2014-06-28 14:06:33 -04:00
parent e1364a11a9
commit b49d098bc2
4 changed files with 241 additions and 186 deletions

View File

@ -97,7 +97,7 @@ GitHub repository [One-Python-benchmark-per-day](https://github.com/rasbt/One-Py
###// Useful scripts and snippets ###// 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. - [Shell script](./useful_scripts/prepend_python_shebang.sh) for prepending Python-shebangs to .py files.

View File

@ -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)

View File

@ -1,19 +1,41 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:b0db931ae8afaadadc3173ffe809324b1b9396a4360f574e697b83ce32103015" "signature": "sha256:6a672b3872feb4ac09bbf846694bc0d4a04b3d7914e4afec8b2bd6398a5a57bd"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
"worksheets": [ "worksheets": [
{ {
"cells": [ "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",
"<hr>\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",
"<hr>"
]
},
{ {
"cell_type": "heading", "cell_type": "heading",
"level": 1, "level": 1,
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"#%install_ext https://raw.githubusercontent.com/rasbt/python_reference/master/ipython_magic/datemagic.py" "%install_ext https://raw.githubusercontent.com/rasbt/python_reference/master/ipython_magic/watermark.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"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -92,7 +94,7 @@
"level": 2, "level": 2,
"metadata": {}, "metadata": {},
"source": [ "source": [
"Loading the `%date` magic" "Loading the `%watermark` magic"
] ]
}, },
{ {
@ -106,7 +108,7 @@
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"%load_ext datemagic" "%load_ext watermark"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -133,7 +135,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"<pre>%date [-d] [-t] [-s] [-p] [-i]\n", "<pre>%watermark [-d] [-n] [-t] [-z] [-u] [-c CUSTOM_TIME] [-v] [-p PACKAGES]\n",
" [-m]\n",
"\n", "\n",
" \n", " \n",
"IPython magic function for printing the current date, time, Python,\n", "IPython magic function to print date/time stamps \n",
"and IPython version.\n", "and various system information.\n",
"\n", "\n",
"optional arguments:\n", "optional arguments:\n",
" -d, --date prints date (default)\n", " -d, --date prints current date\n",
" -t, --time print current time\n", " -n, --datename prints date with abbrv. day and month names\n",
" -s, --datetime print current time\n", " -t, --time prints current time\n",
" -p, --python prints Python version\n", " -z, --timezone appends the local time zone\n",
" -i, --ipython prints IPython version\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",
"</pre>" "</pre>"
] ]
}, },
@ -195,21 +205,22 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"28/06/2014 02:56:36\n", "28/06/2014 14:03:45\n",
"\n",
"Python 3.4.1\n", "Python 3.4.1\n",
"IPython 2.1.0\n", "IPython 2.1.0\n",
"\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", "system : Darwin\n",
"release : 13.2.0\n", "release : 13.2.0\n",
"machine : x86_64\n", "machine : x86_64\n",
"processor : i386\n", "processor : i386\n",
"CPU count : 2\n", "CPU cores : 2\n",
"interpreter: 64bit\n" "interpreter: 64bit\n"
] ]
} }
], ],
"prompt_number": 3 "prompt_number": 4
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -222,7 +233,7 @@
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"%date -t" "%watermark -d -t"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -231,7 +242,33 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"01:18:28\n" "28/06/2014 14:03:49 \n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>"
]
},
{
"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", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"%date -dt" "%watermark -v"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -257,7 +294,8 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "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", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"%date -p" "%watermark -m"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -283,7 +321,13 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "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", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"%date -i" "%watermark -v -m -p numpy,scipy"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -309,45 +353,23 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "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 "prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>"
]
},
{
"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": {} "metadata": {}

137
ipython_magic/watermark.py Normal file
View File

@ -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)