[IPython-user] Using argparse to implement %magics

Ondrej Certik ondrej@certik...
Tue Oct 28 12:20:11 CDT 2008


On Mon, Oct 27, 2008 at 5:08 PM, Robert Kern <robert.kern@gmail.com> wrote:
> 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'>

Brian just pointed me to this email, that's cool. We can ship this
with sympy. I usually use var("x y z") for this kind of stuff.

Ondrej


More information about the IPython-user mailing list