<p dir="ltr">"[Python-ideas] OrderedCounter and OrderedDefaultDict"<br>
<a href="https://mail.python.org/pipermail/python-ideas/2015-November/037163.html">https://mail.python.org/pipermail/python-ideas/2015-November/037163.html</a></p>
<p dir="ltr">- +2 for collections.ABC.Ordered<br>
- exception on initialization w/ an unordered collection /// __init__force_from_unordered<br>
</p>
<div class="gmail_quote">On Nov 9, 2015 7:21 PM, "Michael Selik" <<a href="mailto:mike@selik.org">mike@selik.org</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I found a use case, out in the wild!<br>
<br>
I've been searching through GitHub for "ordered" and similar phrases.<br>
Much of the usage is OrderedDict and OrderedSet, which really don't<br>
benefit from an essentially redundant inheritance from a hypothetical<br>
collections.Ordered. There are some tools that enable UI reordering,<br>
and a bunch of tree- and graph-traversal ordering functions. Again,<br>
not much benefit from a collections.Ordered.<br>
<br>
However, Django has a class factory that creates an attribute `can_order`:<br>
<br>
def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,<br>
                    can_delete=False, max_num=None, validate_max=False,<br>
                    min_num=None, validate_min=False):<br>
    """Return a FormSet for the given form class."""<br>
    if min_num is None:<br>
        min_num = DEFAULT_MIN_NUM<br>
    if max_num is None:<br>
        max_num = DEFAULT_MAX_NUM<br>
    # hard limit on forms instantiated, to prevent memory-exhaustion attacks<br>
    # limit is simply max_num + DEFAULT_MAX_NUM (which is 2*DEFAULT_MAX_NUM<br>
    # if max_num is None in the first place)<br>
    absolute_max = max_num + DEFAULT_MAX_NUM<br>
    attrs = {'form': form, 'extra': extra,<br>
             'can_order': can_order, 'can_delete': can_delete,<br>
             'min_num': min_num, 'max_num': max_num,<br>
             'absolute_max': absolute_max, 'validate_min': validate_min,<br>
             'validate_max': validate_max}<br>
    return type(form.__name__ + str('FormSet'), (formset,), attrs)<br>
<br>
<br>
<br>
This attribute gets set in several places, but checked only twice in<br>
the Django codebase. Unfortunately, I think switching to the proposed<br>
inheritance mechanism would make the code worse, not better:<br>
`self.can_order` would become `isinstance(self, collections.Ordered)`.<br>
The readability of the Django internal code would not be much<br>
different, but users would lose consistency of introspectability as<br>
the other features like `can_delete` are simple class attributes.<br>
<br>
<br>
<br>
On Mon, Nov 9, 2015 at 11:29 AM, Guido van Rossum <<a href="mailto:guido@python.org">guido@python.org</a>> wrote:<br>
> Well, that would still defeat the purpose, wouldn't it? The items are no<br>
> more ordered than the headers dict itself. Also, items() doesn't return a<br>
> sequence -- it's an ItemsView (which inherits from Set) and presumably it's<br>
> not Ordered.<br>
><br>
> I guess my question is not so much how to prevent getting an exception --<br>
> I'm trying to tease out what the right order for the headers would be. Or<br>
> perhaps I'm just trying to understand what the code is doing (the snippet<br>
> shown mostly looks like bad code to me).<br>
><br>
> On Mon, Nov 9, 2015 at 12:03 AM, Ram Rachum <<a href="mailto:ram@rachum.com">ram@rachum.com</a>> wrote:<br>
>><br>
>> I'm not Andrew, but I'm guessing simply writing<br>
>> `OrderedDict(headers.items())`.<br>
>><br>
>> On Mon, Nov 9, 2015 at 6:28 AM, Guido van Rossum <<a href="mailto:guido@python.org">guido@python.org</a>> wrote:<br>
>>><br>
>>> So if OrderedDict had always rejected construction from a dict, how would<br>
>>> you have written this?<br>
>>><br>
>>><br>
>>> On Sunday, November 8, 2015, Andrew Barnert via Python-ideas<br>
>>> <<a href="mailto:python-ideas@python.org">python-ideas@python.org</a>> wrote:<br>
>>>><br>
>>>> On Nov 8, 2015, at 14:10, Serhiy Storchaka <<a href="mailto:storchaka@gmail.com">storchaka@gmail.com</a>> wrote:<br>
>>>> ><br>
>>>> >> On 08.11.15 23:12, Sjoerd Job Postmus wrote:<br>
>>>> >> On 8 Nov 2015, at 20:06, Amir Rachum <<a href="mailto:amir@rachum.com">amir@rachum.com</a><br>
>>>> >> <mailto:<a href="mailto:amir@rachum.com">amir@rachum.com</a>>> wrote:<br>
>>>> >>> As part of BasicStruct I intend to allow the use of mapping types as<br>
>>>> >>> __slots__, with the semantics of default values..<br>
>>>> >>> So it'll look something like this:<br>
>>>> >>><br>
>>>> >>>    class Point(BasicStruct):<br>
>>>> >>>        __slots__ = {'x': 5, 'y': 7}<br>
>>>> >><br>
>>>> >> So instead they'll write<br>
>>>> >>     __slots__ = OrderedDict({'x': 5, 'y': 7})<br>
>>>> >> Causing the same issues?<br>
>>>> ><br>
>>>> > Perhaps OrderedDict should reject unordered sources. Hey, here is yet<br>
>>>> > one use case!<br>
>>>><br>
>>>> I've maintained code that does this:<br>
>>>><br>
>>>>     self.headers = OrderedDict(headers)<br>
>>>>     self.origheaders = len(headers)<br>
>>>><br>
>>>> … so it can later do this:<br>
>>>><br>
>>>>     altheaders = list(self.headers.items())[self.origheaders:]<br>
>>>><br>
>>>> Not a great design, but one that exists in the wild, and would be broken<br>
>>>> by OrderedDict not allowing a dict as an argument.<br>
>>>><br>
>>>> Also, this wouldn't allow creating an OrderedDict from an empty dict<br>
>>>> (which seems far less stupid, but I didn't lead with it because I can't<br>
>>>> remember seeing it in real code).<br>
>>>><br>
>>>> _______________________________________________<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/mailman/listinfo/python-ideas</a><br>
>>>> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
>>><br>
>>><br>
>>><br>
>>> --<br>
>>> --Guido (mobile)<br>
>>><br>
>>> _______________________________________________<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/mailman/listinfo/python-ideas</a><br>
>>> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
>><br>
>><br>
><br>
><br>
><br>
> --<br>
> --Guido van Rossum (<a href="http://python.org/~guido" rel="noreferrer" target="_blank">python.org/~guido</a>)<br>
><br>
> _______________________________________________<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/mailman/listinfo/python-ideas</a><br>
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
_______________________________________________<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/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a></blockquote></div>