Skip to content

Commit 2c30cac

Browse files
committed
datemagic
1 parent d732c99 commit 2c30cac

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

ipython_magic/datemagic.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
IPython magic function for printing basic information, such as the current date, time,
3+
Python, and IPython version.
4+
5+
Installation:
6+
%install_ext https://raw.githubusercontent.com/rasbt/python_reference/master/ipython_magic/datemagic.py
7+
8+
Usage:
9+
%load_ext datemagic
10+
11+
12+
%date
13+
14+
optional arguments:
15+
-d, --date prints date (default)
16+
-t, --time print current time
17+
-s, --datetime print current time
18+
-p, --python prints Python version
19+
-i, --ipython prints IPython version
20+
21+
"""
22+
23+
import platform
24+
from time import strftime
25+
from platform import python_version
26+
27+
import IPython
28+
from IPython.core.magic import Magics, magics_class, line_magic
29+
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
30+
31+
@magics_class
32+
class DateMagic(Magics):
33+
"""
34+
IPython magic function for printing the current date, time, Python,
35+
and IPython version.
36+
37+
"""
38+
@magic_arguments()
39+
@argument('-d', '--date', action='store_true', help='prints date (default)')
40+
@argument('-t', '--time', action='store_true', help='print current time')
41+
@argument('-s', '--datetime', action='store_true', help='print current time')
42+
@argument('-p', '--python', action='store_true', help='prints Python version')
43+
@argument('-i', '--ipython', action='store_true', help='prints IPython version')
44+
@line_magic
45+
def date(self, line):
46+
"""
47+
IPython magic function for printing the current date, time, Python,
48+
and IPython version.
49+
50+
"""
51+
args = parse_argstring(self.date, line)
52+
out = ''
53+
if args.date:
54+
out += strftime('%d/%m/%Y')
55+
if args.time:
56+
if out:
57+
out += ' '
58+
out += strftime('%H:%M:%S')
59+
if args.python:
60+
if out:
61+
out += '\n'
62+
out += 'Python %s' %python_version()
63+
if args.ipython:
64+
if out:
65+
out += '\n'
66+
out += 'IPython %s' %IPython.__version__
67+
if not out:
68+
out += strftime('%d/%m/%Y')
69+
print(out)
70+
71+
def load_ipython_extension(ipython):
72+
ipython.register_magics(DateMagic)

0 commit comments

Comments
 (0)