[Zope-Coders] Patch for KeywordIndexes
Casey Duncan
casey@zope.com
Fri, 30 May 2003 23:00:28 -0400
All of the type tests in here are kludges.=20
How about something like this:
from types import StringType, UnicodeType
=2E..
if (isinstance(newKeywords, StringType)=20
or isinstance(newKeywords, UnicodeType)): #Python 2.1 compat isinstan=
ce
return (newKeywords,)
else:
try:
newKeywords =3D tuple(newKeywords)
except TypeError:
# Not a sequence
return (newKeywords,)
else:
... uniqueify ...
or as a variation of your code:
if (isinstance(newKeywords, StringType)=20
or isinstance(newKeywords, UnicodeType)): #Python 2.1 compat isinstan=
ce
return (newKeywords,)
else:
unique =3D {}
try:
for k in newKeywords:
unique[k] =3D None
except TypeError:
# Not a sequence
return (newKeywords,)
else:
return unique.keys()
I know we're stuck with this dwim for bw compatibility, but I hope that w=
e can=20
consent to simply require that the source be a sequence in future "keywor=
d"=20
index implementations.
-Casey
On Friday 30 May 2003 07:13 pm, Sidnei da Silva wrote:
> On Fri, May 30, 2003 at 05:34:30PM -0400, Tres Seaver wrote:
> | On Fri, 2003-05-30 at 17:21, Sidnei da Silva wrote:
> | > If no one object (or if someone provides a better way to do this) I
> | > would like to apply the following patch for KeywordIndexes on
> | > 2_6-branch and HEAD. Its just completely annoying that this worked
> | > before, and now its so stupidly broken :(
> |=20
> | I don't think your patch works correctly: the test for "string-like"=
is
> | not the same as a test for iterability. As a check, try indexing an
> | object with a "string" property (instead of "lines" or "tokens"). I'=
ll
> | bet you end up indexing each letter of the string as a "keyword".
>=20
> You're right. It needs to test for string-like *and* for
> iterability. Here's the fixed version.
>=20
> --=20
> Sidnei da Silva (dreamcatcher) <sidnei@x3ng.com.br>
> X3ng Web Technology <http://www.x3ng.com.br>
> GNU/Linux user 257852
> Debian GNU/Linux 3.0 (Sid) 2.4.20-powerpc ppc
>=20
> Real Programmers don't eat quiche. They eat Twinkies and Szechwan food=
=2E
>=20