[IPython-dev] cell magics

Jason Grout jason-sage@creativetrax....
Sat Feb 16 16:54:32 CST 2013


On 2/16/13 4:39 PM, Thomas Kluyver wrote:
> That's quite a lot to read, but while I think about it in more detail, a
> couple of bits that jump out at me:


It was quite a lot to write :).


>
> On 16 February 2013 22:07, Jason Grout <jason-sage@creativetrax.com
> <mailto:jason-sage@creativetrax.com>> wrote:
>
>     %time some_function
>
>     now I want to add a few more things to the time run.  All I have to do
>     is change where the string is:
>
>     %time
>     some_function
>     some_other function
>
>     and the lack of a string after %time tells me that I should look below
>     %time for the string.  I don't have to constantly keep adjusting the %
>     character(s).
>
>
> For %%timeit, however, we use the remainder of the line as a setup
> statement, before timing the body. Now you can argue that that's a bad
> idea, but it's useful to have that distinction.


%timeit('setup code', runs=4)
some function
some other function

would work fine, right?

Or (more explicitly)

%timeit(runs=4, setup='setup code')
blah
blah



>
>     P.S. Actually, after thinking about it more, since a single % is
>     ambiguous syntax, I think I would prefer all string decorators be
>     invoked with %%, which is invalid python syntax.  Then we won't have
>     this problem:
>
>     cd=5
>     a=4\
>     %cd
>
>     or this problem:
>
>     cd=4
>     a="time: %d"\
>     %cd
>
>
> My input transformation work should handle such cases. Arguably it could
> be done more easily if we used a syntax like %% that could never appear
> in a valid Python statement, but it still wouldn't be trivial:
>
> a = """
> %%foo
> """

But if it's a token-based transformer, it will know %%foo is inside a 
string, and it won't see that as a token.



>
>  > ...
>
>     I think it's pretty clear which statement is more readable.  This focus
>     on readability over brevity (remember, "Readability counts") is part of
>     why python is so good in general.
>
>
> And part of the reason IPython exists is to provide for the cases when
> you do want brevity. Because there are times when you want to type '%run
> -d foo' than 'pdb.runcall(execfile, "foo.py")'.
>
> Of course, IPython has evolved - when it was only a shell, most of what
> you entered was just for single use, so readability wasn't hugely
> important. When you're writing & publishing a notebook, readability is a
> much bigger deal. But I've yet to see a particular problem with people
> abusing magic syntax and making notebooks hard to read.

And it's only the *very*, *very* beginning....



>
>  > * backwards compatibility (with IPP and previous IPython versions, as
>  > well as IDL and matlab apparently?).  But I'll point out that we can
>  > easily support this too, in almost exactly the same invocation:
>  >
>  > %timeit('-r 5 -and -other -options') 2+3
>
> Honestly, I wouldn't call that 'almost the same invocation'. Moreover,
> it sacrifices the brevity without enforcing any corresponding gain in
> readability.

I wasn't advocating for that, of course, I was advocating for the 
readable version below.

Thanks,

Jason



More information about the IPython-dev mailing list