[python-uk] memoize & ordering of kwargs.items()
Jonathan
tartley at tartley.com
Fri Nov 11 12:18:36 CET 2011
On 11/11/2011 11:14, Jonathan wrote:
> On 11/11/2011 09:35, Jonathan wrote:
>> On 11/11/2011 09:24, Duncan Booth wrote:
>>> pick keys that that hash to the same value modulo the size of the
>>> dictionary. Since the dictionary copy copies the keys in the order
>>> they are stored you will get the same hash conflict in the copy as
>>> the original.
>>
>> Brilliant, thank-you. So I was just being 'lucky' in my choices of
>> dict keys. I believe this is the answer I am looking for. René &
>> Ross's point about being mindful of different implementations is
>> still very pertinent in my mind, but I'm happy for now.
>>
>> Thanks everyone!
>>
>> Jonathan
>>
>
> For completeness, my final test is therefore:
>
> def test_not_dependant_on_order_of_kwargs(self):
> calls = []
>
> @memoize
> def counter(**kwargs):
> calls.append(1)
>
> for first in ascii_letters:
> for second in ascii_letters:
> forward = {first:0, second:0}
> reverse = {second:0, first:0}
>
> del calls[:]
> counter(**forward)
> counter(**reverse)
> self.assertEqual(len(calls), 1,
> '%d for %s,%s' % (len(calls), first, second))
>
oops, but that doesn't pass with the 'sorted'. This does:
for index, first in enumerate(ascii_letters):
for second in ascii_letters[index:]:
(i.e. I was testing each combination forwards and backwards, but then
later in the iteration also testing them again, in the opposite order,
which resulted in 0 extra calls to counter.)
--
Jonathan Hartley tartley at tartley.com http://tartley.com
Made of meat. +44 7737 062 225 twitter/skype: tartley
More information about the python-uk
mailing list