FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python_reference/ipython_magic/watermark.py at master · codetasks/python_reference · GitHub
codetasks
/
python_reference
Public
forked from
rasbt/python_reference
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python_reference
/
ipython_magic
/
watermark.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
172 lines (137 loc) · 5.67 KB
Breadcrumbs
python_reference
/
ipython_magic
/
watermark.py
Copy path
File metadata and controls
172 lines (137 loc) · 5.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Sebastian Raschka 2014
watermark.py
version 1.1.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:
-a AUTHOR, --author AUTHOR
prints author name
-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
-h, --hostname prints the host name
-m, --machine prints system and machine info
-g, --githash prints current Git commit hash
Examples:
%watermark -d -t
"""
import
platform
import
subprocess
from
time
import
strftime
from
socket
import
gethostname
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
(
'-a'
,
'--author'
,
type
=
str
,
help
=
'prints author name'
)
@
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
(
'-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'
)
@
line_magic
def
watermark
(
self
,
line
):
"""
IPython magic function to print date/time stamps
and various system information.
watermark version 1.1.0
"""
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
.
author
:
self
.
out
+=
'% s '
%
args
.
author
.
strip
(
'
\'
"'
)
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
()
if
args
.
hostname
:
space
=
''
if
args
.
machine
:
space
=
' '
self
.
out
+=
'
\n
host name%s: %s'
%
(
space
,
gethostname
())
if
args
.
githash
:
self
.
_get_commit_hash
(
bool
(
args
.
machine
))
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
+=
'%s %s
\n
IPython %s'
%
(
platform
.
python_implementation
(),
platform
.
python_version
(),
IPython
.
__version__
)
def
_get_sysinfo
(
self
):
if
self
.
out
:
self
.
out
+=
'
\n
\n
'
self
.
out
+=
'compiler : %s
\n
system : %s
\n
'
\
'release : %s
\n
machine : %s
\n
'
\
'processor : %s
\n
CPU cores : %s
\n
interpreter: %s'
%
(
platform
.
python_compiler
(),
platform
.
system
(),
platform
.
release
(),
platform
.
machine
(),
platform
.
processor
(),
cpu_count
(),
platform
.
architecture
()[
0
]
)
def
_get_commit_hash
(
self
,
machine
):
process
=
subprocess
.
Popen
([
'git'
,
'rev-parse'
,
'HEAD'
],
shell
=
False
,
stdout
=
subprocess
.
PIPE
)
git_head_hash
=
process
.
communicate
()[
0
].
strip
()
space
=
''
if
machine
:
space
=
' '
self
.
out
+=
'
\n
Git hash%s: %s'
%
(
space
,
git_head_hash
.
decode
(
"utf-8"
))
def
load_ipython_extension
(
ipython
):
ipython
.
register_magics
(
WaterMark
)
Back
|
FazBrowse Home
|
New Git URL