<div dir="ltr">+100<div><br></div><div>I like this idea of giving `slice` a metaclass that defines a `.__getitem__()` allowing us to construct slices on the slice type itself.</div><div><br></div><div>FWIW, this is exactly what pandas.IndexSlice does. E.g., from <a href="http://pandas.pydata.org/pandas-docs/stable/advanced.html">http://pandas.pydata.org/pandas-docs/stable/advanced.html</a>:</div><div><pre style="overflow-x:auto;overflow-y:hidden;padding:10px;background-color:rgb(250,250,250);line-height:1.2em;border:1px solid rgb(201,201,201);font-size:1.1em;margin-top:1.5em;margin-bottom:1.5em"><span class="gmail-gp" style="color:rgb(198,93,9);font-weight:bold">In [51]: </span><span class="gmail-n">dfmi</span><span class="gmail-o" style="color:rgb(102,102,102)">.</span><span class="gmail-n">loc</span><span class="gmail-p">[(</span><span class="gmail-nb" style="color:rgb(0,112,32)">slice</span><span class="gmail-p">(</span><span class="gmail-s1" style="color:rgb(64,112,160)">'A1'</span><span class="gmail-p">,</span><span class="gmail-s1" style="color:rgb(64,112,160)">'A3'</span><span class="gmail-p">),</span><span class="gmail-nb" style="color:rgb(0,112,32)">slice</span><span class="gmail-p">(</span><span class="gmail-bp" style="color:rgb(0,112,32)">None</span><span class="gmail-p">),</span> <span class="gmail-p">[</span><span class="gmail-s1" style="color:rgb(64,112,160)">'C1'</span><span class="gmail-p">,</span><span class="gmail-s1" style="color:rgb(64,112,160)">'C3'</span><span class="gmail-p">]),:]</span>
</pre><pre style="overflow-x:auto;overflow-y:hidden;padding:10px;background-color:rgb(250,250,250);line-height:1.2em;border:1px solid rgb(201,201,201);font-size:1.1em;margin-top:1.5em;margin-bottom:1.5em"><span class="gmail-gp" style="color:rgb(198,93,9);font-weight:bold">In [52]: </span><span class="gmail-n">idx</span> <span class="gmail-o" style="color:rgb(102,102,102)">=</span> <span class="gmail-n">pd</span><span class="gmail-o" style="color:rgb(102,102,102)">.</span><span class="gmail-n">IndexSlice</span>
<span class="gmail-gp" style="color:rgb(198,93,9);font-weight:bold">In [53]: </span><span class="gmail-n">dfmi</span><span class="gmail-o" style="color:rgb(102,102,102)">.</span><span class="gmail-n">loc</span><span class="gmail-p">[</span><span class="gmail-n">idx</span><span class="gmail-p">[:,:,[</span><span class="gmail-s1" style="color:rgb(64,112,160)">'C1'</span><span class="gmail-p">,</span><span class="gmail-s1" style="color:rgb(64,112,160)">'C3'</span><span class="gmail-p">]],</span><span class="gmail-n">idx</span><span class="gmail-p">[:,</span><span class="gmail-s1" style="color:rgb(64,112,160)">'foo'</span><span class="gmail-p">]]</span></pre><div class="gmail_extra">This is one of those nifty things that's buried in Pandas but not well documented. I'd rather spell the above simply as:</div><div class="gmail_extra"><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div class="gmail_extra"><font face="monospace, monospace">dfmi.loc[slice[:,:,['C1','C3']], slice[:,'foo']]</font></div></div></blockquote><div><div class="gmail_extra"><br></div><div class="gmail_extra">I like the change proposed to `str(slice(10))` also... and it would be way better if `slice[:10]` were actual "syntax." In fact, in that case it could even be the repr().</div><div class="gmail_extra"><br></div><div class="gmail_extra">Note: Notwithstanding my scare quotes, Steven isn't actually asking for new syntax. "slice" is already a name, and names can already be followed by square brackets. He's just asking for a new method on a metaclass.</div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Nov 12, 2016 at 1:26 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail-HOEnZb"><div class="gmail-h5">On Thu, Oct 06, 2016 at 04:19:17PM -0700, Neil Girdhar wrote:<br>
> Currently str(slice(10)) returns "slice(None, 10, None)"<br>
><br>
> If the start and step are None, consider not emitting them. Similarly<br>
> slice(None) is rendered slice(None, None, None).<br>
><br>
> When you're printing a lot of slices, it's a lot of extra noise.<br>
<br>
</div></div>I have an alternative suggestion. Wouldn't it be nice if slice objects<br>
looked something like the usual slice syntax?<br>
<br>
If you think the answer is No, then you'll hate my suggestion :-)<br>
<br>
Let's keep the current repr() of slice objects as they are, using the<br>
full function-call syntax complete with all three arguments show<br>
explicitly:<br>
<br>
repr(slice(None, None, None)) => "slice(None, None, None)"<br>
<br>
But let's make str() of a slice more suggestive of actual slicing, and<br>
as a bonus, make slices easier to create too.<br>
<br>
str(slice(None, None, None)) => "slice[:]"<br>
<br>
Let the slice type itself be sliceable, as an alternate constuctor:<br>
<br>
slice[:] => returns slice(None)<br>
slice[start:] => returns slice(start, None)<br>
slice[:end] => returns slice(None, end)<br>
slice[start::step] => returns slice(start, None, step)<br>
<br>
and so forth. (This probably would require changing the type of slice to<br>
a new metaclass.)<br>
<br>
And then have str() return the compact slice syntax.<br>
<br>
At worst, the compact slice syntax is one character longer than the<br>
optimal function syntax:<br>
<br>
# proposed slice str()<br>
slice[:7] # 9 characters<br>
<br>
# proposed compact str()<br>
slice(7) # 8 characters<br>
<br>
# current str()<br>
slice(None, 7, None) # 20 characters<br>
<br>
<br>
but it will be more compact more often:<br>
<br>
slice[1:] # 9 characters<br>
<br>
versus:<br>
<br>
slice(1, None) # 14 characters<br>
slice(None, 1, None) # 20 characters<br>
<span class="gmail-HOEnZb"><font color="#888888"><br>
<br>
<br>
<br>
--<br>
Steve<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div></div></div>