[IPython-user] Using argparse to implement %magics
Robert Kern
robert.kern@gmail....
Mon Oct 27 19:08:40 CDT 2008
argparse is my command-line option parser of choice these days, and I always
thought it would be a nice way to implement some of the IPython %magics that
have such options. I recently had the occasion to write some of my own %magics,
so I tried it out. Here are the results:
In [1]: text = 'Foo!'
In [2]: %fwrite?
Type: Magic function
Base Class: <type 'instancemethod'>
Namespace: IPython internal
File: /Users/rkern/.ipython/mymagics.py
Definition: %fwrite(self, arg)
Docstring:
Write text out to a file.
%fwrite [-e ENCODING] [-m MODE] variable [filename]
positional arguments:
variable the name of the variable
filename the filename to write [default: the variable's name]
optional arguments:
-e ENCODING, --encoding ENCODING
the encoding to use for unicode objects; no effect on
str objects [default: utf-8]
-m MODE, --mode MODE the file mode to use when opening the file for writing
In [3]: %fwrite text somefile.txt
In [4]: !cat somefile.txt
IPython system call: cat somefile.txt
Foo!
In [5]: %fread?
Type: Magic function
Base Class: <type 'instancemethod'>
Namespace: IPython internal
File: /Users/rkern/.ipython/mymagics.py
Definition: %fread(self, arg)
Docstring:
Read text from a file into a variable.
%fread [-e ENCODING] [-m MODE] variable filename
positional arguments:
variable the name of the variable
filename the filename to read from
optional arguments:
-e ENCODING, --encoding ENCODING
decode the text using this encoding [default: do not
attempt to decode]
-m MODE, --mode MODE the file mode to use when opening the file for reading
In [7]: %fread x somefile.txt
In [8]: x
Out[8]: 'Foo!'
In [9]: %sym?
Type: Magic function
Base Class: <type 'instancemethod'>
Namespace: IPython internal
File: /Users/rkern/.ipython/mymagics.py
Definition: %sym(self, arg)
Docstring:
Create Sympy variables easily.
%sym [-r] [-i] [-c] [-f] [-q] names [names ...]
positional arguments:
names the names of the variables to create
optional arguments:
-r, --real symbols are real variables
-i, --int symbols are integer variables
-c, --complex symbols are complex variables
-f, --function symbols are functions
-q, --quiet do not print out verbose information
In [10]: %sym x y z
Adding variables:
x
y
z
In [13]: x+y
Out[13]: x + y
In [14]: %sym -c z
Adding complex variables:
z
In [15]: z.is_complex
Out[15]: True
In [17]: %sym -i i j k
Adding integer variables:
i
j
k
In [18]: i.is_integer
Out[18]: True
In [20]: %sym -f f g h
Adding function variables:
f
g
h
In [25]: type(f)
Out[25]: <class 'sympy.core.function.FunctionClass'>
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mymagics.py
Type: application/x-python
Size: 5562 bytes
Desc: not available
Url : http://lists.ipython.scipy.org/pipermail/ipython-user/attachments/20081027/2f4035c8/attachment-0001.bin
More information about the IPython-user
mailing list