Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: exposing the watermark function outside Jupyter #46

Merged
merged 7 commits into from Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -125,6 +125,10 @@ In line with [NEP 29][nep-29], this project supports:

[[top](#sections)]

#### v. 2.2.0 (February 17, 2021)

- Refactoring such that a `watermark()` function now also works outside IPython and Jupyter. Now, the magic `%watermark` calls `watermark()`. Calling `%watermark` via IPython and Juypter still works as usual. However, in addition the `watermark()` function can be used within regular Python for unit testing purposes etc. ([#46](https://github.com/rasbt/watermark/pull/46), via contribution by [Tymoteusz Wołodźko](https://github.com/twolodzko))

#### v. 2.1.0 (November 23, 2020)

- Adopt [NEP 29][nep-29] and require Python version 3.7 or newer. ([#63](https://github.com/rasbt/watermark/pull/63), via contribution by [James Myatt](https://github.com/jamesmyatt))
Expand Down
86 changes: 43 additions & 43 deletions docs/watermark.ipynb
Expand Up @@ -77,7 +77,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Watermark: 2.1.0\n",
"Watermark: 2.2.0\n",
"\n"
]
}
Expand Down Expand Up @@ -140,7 +140,7 @@
" -b, --gitbranch prints current Git branch\n",
" -w, --watermark prints the current version of watermark\n",
" -iv, --iversions prints the name/version of all imported modules\n",
"\u001b[0;31mFile:\u001b[0m ~/watermark/watermark.py\n"
"\u001b[0;31mFile:\u001b[0m ~/Desktop/watermark/watermark/magic.py\n"
]
},
"metadata": {},
Expand Down Expand Up @@ -208,18 +208,18 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Last updated: 2020-11-20T15:27:00.378508+00:00\n",
"Last updated: 2021-02-18T10:29:04.648127-06:00\n",
"\n",
"Python implementation: CPython\n",
"Python version : 3.7.8\n",
"IPython version : 7.16.1\n",
"Python version : 3.8.2\n",
"IPython version : 7.18.1\n",
"\n",
"Compiler : GCC 7.5.0\n",
"OS : Linux\n",
"Release : 4.19.112+\n",
"Compiler : Clang 4.0.1 (tags/RELEASE_401/final)\n",
"OS : Darwin\n",
"Release : 20.3.0\n",
"Machine : x86_64\n",
"Processor : x86_64\n",
"CPU cores : 8\n",
"Processor : i386\n",
"CPU cores : 12\n",
"Architecture: 64bit\n",
"\n"
]
Expand All @@ -245,7 +245,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Last updated: 2020-11-20 15:27:00\n",
"Last updated: 2021-02-18 10:29:04\n",
"\n"
]
}
Expand All @@ -263,7 +263,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Last updated: 2020-11-20T15:27:00.417982+00:00\n",
"Last updated: 2021-02-18T10:29:04.668981-06:00\n",
"\n"
]
}
Expand All @@ -281,7 +281,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Last updated: Fri Nov 20 2020 15:27:00UTC\n",
"Last updated: Thu Feb 18 2021 10:29:04CST\n",
"\n"
]
}
Expand All @@ -307,8 +307,8 @@
"output_type": "stream",
"text": [
"Python implementation: CPython\n",
"Python version : 3.7.8\n",
"IPython version : 7.16.1\n",
"Python version : 3.8.2\n",
"IPython version : 7.18.1\n",
"\n"
]
}
Expand All @@ -333,12 +333,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Compiler : GCC 7.5.0\n",
"OS : Linux\n",
"Release : 4.19.112+\n",
"Compiler : Clang 4.0.1 (tags/RELEASE_401/final)\n",
"OS : Darwin\n",
"Release : 20.3.0\n",
"Machine : x86_64\n",
"Processor : x86_64\n",
"CPU cores : 8\n",
"Processor : i386\n",
"CPU cores : 12\n",
"Architecture: 64bit\n",
"\n"
]
Expand All @@ -365,22 +365,22 @@
"output_type": "stream",
"text": [
"Python implementation: CPython\n",
"Python version : 3.7.8\n",
"IPython version : 7.16.1\n",
"Python version : 3.8.2\n",
"IPython version : 7.18.1\n",
"\n",
"numpy : 1.19.4\n",
"scipy : 1.5.4\n",
"sklearn: 0.23.2\n",
"numpy : 1.18.5\n",
"scipy : 1.5.0\n",
"sklearn: 0.24.1\n",
"\n",
"Compiler : GCC 7.5.0\n",
"OS : Linux\n",
"Release : 4.19.112+\n",
"Compiler : Clang 4.0.1 (tags/RELEASE_401/final)\n",
"OS : Darwin\n",
"Release : 20.3.0\n",
"Machine : x86_64\n",
"Processor : x86_64\n",
"CPU cores : 8\n",
"Processor : i386\n",
"CPU cores : 12\n",
"Architecture: 64bit\n",
"\n",
"Git hash: b9768dfeebb4a079fc1a7b8d83f249ded3f31ad8\n",
"Git hash: e0893cd2ead20c40402f32cb51876f8a87fb1eff\n",
"\n"
]
}
Expand All @@ -407,18 +407,18 @@
"text": [
"Author: John Doe\n",
"\n",
"Last updated: 2020-11-20\n",
"Last updated: 2021-02-18\n",
"\n",
"Python implementation: CPython\n",
"Python version : 3.7.8\n",
"IPython version : 7.16.1\n",
"Python version : 3.8.2\n",
"IPython version : 7.18.1\n",
"\n",
"Compiler : GCC 7.5.0\n",
"OS : Linux\n",
"Release : 4.19.112+\n",
"Compiler : Clang 4.0.1 (tags/RELEASE_401/final)\n",
"OS : Darwin\n",
"Release : 20.3.0\n",
"Machine : x86_64\n",
"Processor : x86_64\n",
"CPU cores : 8\n",
"Processor : i386\n",
"CPU cores : 12\n",
"Architecture: 64bit\n",
"\n"
]
Expand Down Expand Up @@ -455,9 +455,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"numpy : 1.19.4\n",
"sklearn: 0.23.2\n",
"scipy : 1.5.4\n",
"numpy : 1.18.5\n",
"sklearn: 0.24.1\n",
"scipy : 1.5.0\n",
"\n"
]
}
Expand All @@ -483,7 +483,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.8"
"version": "3.8.2"
},
"toc": {
"base_numbering": 1,
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
@@ -1,5 +1,5 @@
[metadata]
version = 2.1.0
version = 2.2.0
license_file = LICENSE
classifiers =
Development Status :: 5 - Production/Stable
Expand Down
6 changes: 4 additions & 2 deletions watermark/__init__.py
Expand Up @@ -8,6 +8,8 @@
from __future__ import absolute_import

from .version import __version__
from .watermark import *

__all__ = ["watermark"]
from watermark.magic import *
from watermark.watermark import watermark

__all__ = ["watermark", "magic"]
84 changes: 84 additions & 0 deletions watermark/magic.py
@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
"""
IPython magic function to print date/time stamps and
various system information.
Author: Sebastian Raschka <sebastianraschka.com>
License: BSD 3 clause
"""

from IPython.core.magic import Magics
from IPython.core.magic import magics_class
from IPython.core.magic import line_magic
from IPython.core.magic_arguments import argument
from IPython.core.magic_arguments import magic_arguments
from IPython.core.magic_arguments import parse_argstring

from watermark import watermark


class PackageNotFoundError(Exception):
pass


@magics_class
class WaterMark(Magics):
"""
IPython magic function to print date/time stamps
and various system information.
"""
@magic_arguments()
@argument('-a', '--author', type=str,
help='prints author name')
@argument('-d', '--date', action='store_true',
help='prints current date as YYYY-mm-dd')
@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 as HH-MM-SS')
@argument('-i', '--iso8601', action='store_true',
help='prints the combined date and time including the time zone'
' in the ISO 8601 standard with UTC offset')
@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('-h', '--hostname', action='store_true',
help='prints the host name')
@argument('-m', '--machine', action='store_true',
help='prints system and machine info')
@argument('-g', '--githash', action='store_true',
help='prints current Git commit hash')
@argument('-r', '--gitrepo', action='store_true',
help='prints current Git remote address')
@argument('-b', '--gitbranch', action='store_true',
help='prints current Git branch')
@argument('-w', '--watermark', action='store_true',
help='prints the current version of watermark')
@argument('-iv', '--iversions', action='store_true',
help='prints the name/version of all imported modules')
@line_magic
def watermark(self, line):
"""
IPython magic function to print date/time stamps
and various system information.
"""
args = vars(parse_argstring(self.watermark, line))

# renaming not to pollute the namespace
# while preserving backward compatibility
args['current_date'] = args.pop('date')
args['current_time'] = args.pop('time')
args['watermark_self'] = self

formatted_text = watermark.watermark(**args)
print(formatted_text)


def load_ipython_extension(ipython):
ipython.register_magics(WaterMark)
31 changes: 31 additions & 0 deletions watermark/tests/test_watermark.py
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-

import watermark


def test_defaults():
a = watermark.watermark()
txt = a.split('\n')
clean_txt = []
for t in txt:
t = t.strip()
if t:
t = t.split(':')[0]
clean_txt.append(t.strip())
clean_txt = set(clean_txt)

expected = [
'Last updated',
'Python implementation',
'Python version',
'IPython version',
'Compiler',
'OS',
'Release',
'Machine',
'Processor',
'CPU cores',
'Architecture']

for i in expected:
assert i in clean_txt, print(f'{i} not in {clean_txt}')