Skip to content

Commit 8740916

Browse files
authored
Merge pull request #46 from twolodzko/master
POC: exposing the watermark function outside Jupyter
2 parents 9edc97c + c5a078e commit 8740916

File tree

7 files changed

+404
-255
lines changed

7 files changed

+404
-255
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ In line with [NEP 29][nep-29], this project supports:
125125

126126
[[top](#sections)]
127127

128+
#### v. 2.2.0 (February 17, 2021)
129+
130+
- 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))
131+
128132
#### v. 2.1.0 (November 23, 2020)
129133

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

docs/watermark.ipynb

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"name": "stdout",
7878
"output_type": "stream",
7979
"text": [
80-
"Watermark: 2.1.0\n",
80+
"Watermark: 2.2.0\n",
8181
"\n"
8282
]
8383
}
@@ -140,7 +140,7 @@
140140
" -b, --gitbranch prints current Git branch\n",
141141
" -w, --watermark prints the current version of watermark\n",
142142
" -iv, --iversions prints the name/version of all imported modules\n",
143-
"\u001b[0;31mFile:\u001b[0m ~/watermark/watermark.py\n"
143+
"\u001b[0;31mFile:\u001b[0m ~/Desktop/watermark/watermark/magic.py\n"
144144
]
145145
},
146146
"metadata": {},
@@ -208,18 +208,18 @@
208208
"name": "stdout",
209209
"output_type": "stream",
210210
"text": [
211-
"Last updated: 2020-11-20T15:27:00.378508+00:00\n",
211+
"Last updated: 2021-02-18T10:29:04.648127-06:00\n",
212212
"\n",
213213
"Python implementation: CPython\n",
214-
"Python version : 3.7.8\n",
215-
"IPython version : 7.16.1\n",
214+
"Python version : 3.8.2\n",
215+
"IPython version : 7.18.1\n",
216216
"\n",
217-
"Compiler : GCC 7.5.0\n",
218-
"OS : Linux\n",
219-
"Release : 4.19.112+\n",
217+
"Compiler : Clang 4.0.1 (tags/RELEASE_401/final)\n",
218+
"OS : Darwin\n",
219+
"Release : 20.3.0\n",
220220
"Machine : x86_64\n",
221-
"Processor : x86_64\n",
222-
"CPU cores : 8\n",
221+
"Processor : i386\n",
222+
"CPU cores : 12\n",
223223
"Architecture: 64bit\n",
224224
"\n"
225225
]
@@ -245,7 +245,7 @@
245245
"name": "stdout",
246246
"output_type": "stream",
247247
"text": [
248-
"Last updated: 2020-11-20 15:27:00\n",
248+
"Last updated: 2021-02-18 10:29:04\n",
249249
"\n"
250250
]
251251
}
@@ -263,7 +263,7 @@
263263
"name": "stdout",
264264
"output_type": "stream",
265265
"text": [
266-
"Last updated: 2020-11-20T15:27:00.417982+00:00\n",
266+
"Last updated: 2021-02-18T10:29:04.668981-06:00\n",
267267
"\n"
268268
]
269269
}
@@ -281,7 +281,7 @@
281281
"name": "stdout",
282282
"output_type": "stream",
283283
"text": [
284-
"Last updated: Fri Nov 20 2020 15:27:00UTC\n",
284+
"Last updated: Thu Feb 18 2021 10:29:04CST\n",
285285
"\n"
286286
]
287287
}
@@ -307,8 +307,8 @@
307307
"output_type": "stream",
308308
"text": [
309309
"Python implementation: CPython\n",
310-
"Python version : 3.7.8\n",
311-
"IPython version : 7.16.1\n",
310+
"Python version : 3.8.2\n",
311+
"IPython version : 7.18.1\n",
312312
"\n"
313313
]
314314
}
@@ -333,12 +333,12 @@
333333
"name": "stdout",
334334
"output_type": "stream",
335335
"text": [
336-
"Compiler : GCC 7.5.0\n",
337-
"OS : Linux\n",
338-
"Release : 4.19.112+\n",
336+
"Compiler : Clang 4.0.1 (tags/RELEASE_401/final)\n",
337+
"OS : Darwin\n",
338+
"Release : 20.3.0\n",
339339
"Machine : x86_64\n",
340-
"Processor : x86_64\n",
341-
"CPU cores : 8\n",
340+
"Processor : i386\n",
341+
"CPU cores : 12\n",
342342
"Architecture: 64bit\n",
343343
"\n"
344344
]
@@ -365,22 +365,22 @@
365365
"output_type": "stream",
366366
"text": [
367367
"Python implementation: CPython\n",
368-
"Python version : 3.7.8\n",
369-
"IPython version : 7.16.1\n",
368+
"Python version : 3.8.2\n",
369+
"IPython version : 7.18.1\n",
370370
"\n",
371-
"numpy : 1.19.4\n",
372-
"scipy : 1.5.4\n",
373-
"sklearn: 0.23.2\n",
371+
"numpy : 1.18.5\n",
372+
"scipy : 1.5.0\n",
373+
"sklearn: 0.24.1\n",
374374
"\n",
375-
"Compiler : GCC 7.5.0\n",
376-
"OS : Linux\n",
377-
"Release : 4.19.112+\n",
375+
"Compiler : Clang 4.0.1 (tags/RELEASE_401/final)\n",
376+
"OS : Darwin\n",
377+
"Release : 20.3.0\n",
378378
"Machine : x86_64\n",
379-
"Processor : x86_64\n",
380-
"CPU cores : 8\n",
379+
"Processor : i386\n",
380+
"CPU cores : 12\n",
381381
"Architecture: 64bit\n",
382382
"\n",
383-
"Git hash: b9768dfeebb4a079fc1a7b8d83f249ded3f31ad8\n",
383+
"Git hash: e0893cd2ead20c40402f32cb51876f8a87fb1eff\n",
384384
"\n"
385385
]
386386
}
@@ -407,18 +407,18 @@
407407
"text": [
408408
"Author: John Doe\n",
409409
"\n",
410-
"Last updated: 2020-11-20\n",
410+
"Last updated: 2021-02-18\n",
411411
"\n",
412412
"Python implementation: CPython\n",
413-
"Python version : 3.7.8\n",
414-
"IPython version : 7.16.1\n",
413+
"Python version : 3.8.2\n",
414+
"IPython version : 7.18.1\n",
415415
"\n",
416-
"Compiler : GCC 7.5.0\n",
417-
"OS : Linux\n",
418-
"Release : 4.19.112+\n",
416+
"Compiler : Clang 4.0.1 (tags/RELEASE_401/final)\n",
417+
"OS : Darwin\n",
418+
"Release : 20.3.0\n",
419419
"Machine : x86_64\n",
420-
"Processor : x86_64\n",
421-
"CPU cores : 8\n",
420+
"Processor : i386\n",
421+
"CPU cores : 12\n",
422422
"Architecture: 64bit\n",
423423
"\n"
424424
]
@@ -455,9 +455,9 @@
455455
"name": "stdout",
456456
"output_type": "stream",
457457
"text": [
458-
"numpy : 1.19.4\n",
459-
"sklearn: 0.23.2\n",
460-
"scipy : 1.5.4\n",
458+
"numpy : 1.18.5\n",
459+
"sklearn: 0.24.1\n",
460+
"scipy : 1.5.0\n",
461461
"\n"
462462
]
463463
}
@@ -483,7 +483,7 @@
483483
"name": "python",
484484
"nbconvert_exporter": "python",
485485
"pygments_lexer": "ipython3",
486-
"version": "3.7.8"
486+
"version": "3.8.2"
487487
},
488488
"toc": {
489489
"base_numbering": 1,

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[metadata]
2-
version = 2.1.0
2+
version = 2.2.0
33
license_file = LICENSE
44
classifiers =
55
Development Status :: 5 - Production/Stable

watermark/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from __future__ import absolute_import
99

1010
from .version import __version__
11-
from .watermark import *
1211

13-
__all__ = ["watermark"]
12+
from watermark.magic import *
13+
from watermark.watermark import watermark
14+
15+
__all__ = ["watermark", "magic"]

watermark/magic.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
IPython magic function to print date/time stamps and
4+
various system information.
5+
Author: Sebastian Raschka <sebastianraschka.com>
6+
License: BSD 3 clause
7+
"""
8+
9+
from IPython.core.magic import Magics
10+
from IPython.core.magic import magics_class
11+
from IPython.core.magic import line_magic
12+
from IPython.core.magic_arguments import argument
13+
from IPython.core.magic_arguments import magic_arguments
14+
from IPython.core.magic_arguments import parse_argstring
15+
16+
from watermark import watermark
17+
18+
19+
class PackageNotFoundError(Exception):
20+
pass
21+
22+
23+
@magics_class
24+
class WaterMark(Magics):
25+
"""
26+
IPython magic function to print date/time stamps
27+
and various system information.
28+
"""
29+
@magic_arguments()
30+
@argument('-a', '--author', type=str,
31+
help='prints author name')
32+
@argument('-d', '--date', action='store_true',
33+
help='prints current date as YYYY-mm-dd')
34+
@argument('-n', '--datename', action='store_true',
35+
help='prints date with abbrv. day and month names')
36+
@argument('-t', '--time', action='store_true',
37+
help='prints current time as HH-MM-SS')
38+
@argument('-i', '--iso8601', action='store_true',
39+
help='prints the combined date and time including the time zone'
40+
' in the ISO 8601 standard with UTC offset')
41+
@argument('-z', '--timezone', action='store_true',
42+
help='appends the local time zone')
43+
@argument('-u', '--updated', action='store_true',
44+
help='appends a string "Last updated: "')
45+
@argument('-c', '--custom_time', type=str,
46+
help='prints a valid strftime() string')
47+
@argument('-v', '--python', action='store_true',
48+
help='prints Python and IPython version')
49+
@argument('-p', '--packages', type=str,
50+
help='prints versions of specified Python modules and packages')
51+
@argument('-h', '--hostname', action='store_true',
52+
help='prints the host name')
53+
@argument('-m', '--machine', action='store_true',
54+
help='prints system and machine info')
55+
@argument('-g', '--githash', action='store_true',
56+
help='prints current Git commit hash')
57+
@argument('-r', '--gitrepo', action='store_true',
58+
help='prints current Git remote address')
59+
@argument('-b', '--gitbranch', action='store_true',
60+
help='prints current Git branch')
61+
@argument('-w', '--watermark', action='store_true',
62+
help='prints the current version of watermark')
63+
@argument('-iv', '--iversions', action='store_true',
64+
help='prints the name/version of all imported modules')
65+
@line_magic
66+
def watermark(self, line):
67+
"""
68+
IPython magic function to print date/time stamps
69+
and various system information.
70+
"""
71+
args = vars(parse_argstring(self.watermark, line))
72+
73+
# renaming not to pollute the namespace
74+
# while preserving backward compatibility
75+
args['current_date'] = args.pop('date')
76+
args['current_time'] = args.pop('time')
77+
args['watermark_self'] = self
78+
79+
formatted_text = watermark.watermark(**args)
80+
print(formatted_text)
81+
82+
83+
def load_ipython_extension(ipython):
84+
ipython.register_magics(WaterMark)

watermark/tests/test_watermark.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import watermark
4+
5+
6+
def test_defaults():
7+
a = watermark.watermark()
8+
txt = a.split('\n')
9+
clean_txt = []
10+
for t in txt:
11+
t = t.strip()
12+
if t:
13+
t = t.split(':')[0]
14+
clean_txt.append(t.strip())
15+
clean_txt = set(clean_txt)
16+
17+
expected = [
18+
'Last updated',
19+
'Python implementation',
20+
'Python version',
21+
'IPython version',
22+
'Compiler',
23+
'OS',
24+
'Release',
25+
'Machine',
26+
'Processor',
27+
'CPU cores',
28+
'Architecture']
29+
30+
for i in expected:
31+
assert i in clean_txt, print(f'{i} not in {clean_txt}')

0 commit comments

Comments
 (0)