order of supported

1 view
Skip to first unread message

Ed Summers

unread,
May 20, 2009, 9:47:40 PM5/20/09
to Mimeparse-dev
Here is a patch [1] that makes best_match in mimeparse.py respect the
order of the list of supplied supported mime types. I have a situation
where multiple representations are available for a resource. When
clients request both mime types with equal preference, best_match
returns the one that sorted alphabetically last...and I actually want
to prefer the one that sorts alphabetically earlier.

For example, I have application/json and text/html representations
available for a resource. If the cilent doesn't prefer one over the
other I want to return the application/json. Here's what I saw before
the patch:

>>> best_match(['text/html', 'application/json'], '*/*')
'text/html'

With this patch the supported mime types are in order from least
desirable to most desirable, so you'll see:

>>> best_match(['text/html', 'application/json'], '*/*')
'application/json'

Not sure if it's worth patching mimeparse.py since I guess this will
be divergent behavior with the other language libraries. But I figured
it was at least worth sharing with y'all to see if I was doing
something wrong.

//Ed

[1] http://groups.google.com/group/mimeparse-dev/web/best_match_order.patch

mca

unread,
May 20, 2009, 10:58:28 PM5/20/09
to Mimeparse-dev
sounds (and looks) very similar to the mod i made to my C# port of
mimeparse.py.

i, too, found that the best strategy was to return the last item in
the list if no other pref was indicated (*/*).

MCA

Steve Vinoski

unread,
May 21, 2009, 12:09:47 AM5/21/09
to mimepa...@googlegroups.com
Interesting. My Erlang version already always returns the last one in
the list under these circumstances, because it folds over the list
from left to right and since each choice is as good as any other, the
rightmost one -- the last one in the list -- wins.

I would call the Python sorting behavior in this case a bug,
especially since it's probably my fault. I'm reasonably sure the idea
was to sort only by the weighting, not the alphabetic ordering of the
mime types, but Joe can correct me if I'm wrong.

I vote for adopting the return the last in the list behavior here,
adding a test for this, and making sure all languages return the same
result for this case.

BTW Mike, did your C# port ever make it into this repository? If not
we should probably remedy that as well.

my 2 cents,
--steve

mike amundsen

unread,
May 21, 2009, 2:53:42 AM5/21/09
to mimepa...@googlegroups.com
Steve:

I've just added the mimeparse-csharp.zip to the group's file collection.
The archive contains MimeParse.cs, a small test app, and a readme.
The complete project source is on github (see the readme for details).

mca
http://amundsen.com/blog/

Chris Anderson

unread,
Aug 4, 2009, 3:56:30 AM8/4/09
to mimepa...@googlegroups.com
It looks like this is cropping up for me. I've ported the failing test
case from Ed's patch to my copy of the JavaScript library. Once I have
it passing I think I should push it to the repository.

I think this is worth pushing into all the languages, not having it
has turned out to cause some trouble in CouchDB's usage.

Chris

> //Ed
>
> [1] http://groups.google.com/group/mimeparse-dev/web/best_match_order.patch
> >
>



--
Chris Anderson
http://jchrisa.net
http://couch.io

Chris Anderson

unread,
Aug 4, 2009, 4:05:22 AM8/4/09
to mimepa...@googlegroups.com
That was easier than I expected. The fix has been pushed to the
JavaScript version.

Ed Summers

unread,
Aug 4, 2009, 9:11:16 AM8/4/09
to mimepa...@googlegroups.com
I'd be happy to push the patch into Python if I can get commit bits.

//Ed

Joe Gregorio

unread,
Aug 26, 2009, 4:42:05 PM8/26/09
to mimepa...@googlegroups.com
On Tue, Aug 4, 2009 at 9:11 AM, Ed Summers <e...@pobox.com> wrote:

I'd be happy to push the patch into Python if I can get commit bits.
 
Done. Commit away.

  Thanks,
  -joe

Reply all
Reply to author
Forward
0 new messages