Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Matt Hamilton
On 11 Nov 2011, at 12:54, Alexander Harrowell wrote: > This is surely inherent in the fact that python dictionaries are not > deterministically ordered. The dox don't say that you will always get key-val > pairs back in *different* orders - just that you cannot rely on them being in > the *sam

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Alexander Harrowell
On Friday 11 Nov 2011 09:30:23 Jonathan wrote: > I agree that 'to be sure' is enough of a justification for including the 'sorted' call anyway. But it irks me that I can't write a test to show it. This is surely inherent in the fact that python dictionaries are not deterministically ordered. Th

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Duncan Booth
On Fri, Nov 11, 2011 at 10:14 AM, Jonathan wrote: > Hey René, > Thanks for that! I guess I was getting distracted because I was interpreting > the situation as the fact that .items() for an *arbitrary* dictionary may > well be non-deterministic, but in this particular case, for dictionaries > newl

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Jonathan
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 con

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Jonathan
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.

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Jonathan
On 11/11/2011 09:34, René Dudfield wrote: On Fri, Nov 11, 2011 at 10:23 AM, Jonathan > wrote: That's good to know René, but I *think* it's orthogonal to the question. Please correct me if I'm wrong. If PyPy returns items in a different order than CPython,

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Jonathan
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 be

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread René Dudfield
On Fri, Nov 11, 2011 at 10:23 AM, Jonathan wrote: > That's good to know René, but I *think* it's orthogonal to the question. > Please correct me if I'm wrong. > > If PyPy returns items in a different order than CPython, that doesn't > matter to me, so long as every invocation of my function in a

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Jonathan
Thanks Ross. The thing is, the 'kwargs' dict that callers pass in is not the same dict as the kwargs that my wrapper function receives. My 'wrapper' function always receives a brand-new dictionary, created from scratch as the function is invoked. Hence, the kwargs never has any 'history' - th

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Duncan Booth
On Fri, Nov 11, 2011 at 8:50 AM, Ross Lawley wrote: >> However, I'm unable to come up with a test that proves this is necessary. >> I'm can create two equal dictionaries which return their .items() in a >> different order: >> >>        # The intent is that 'small.items()' comes out in a different

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Jonathan
That's good to know René, but I *think* it's orthogonal to the question. Please correct me if I'm wrong. If PyPy returns items in a different order than CPython, that doesn't matter to me, so long as every invocation of my function in a particular process ends up receiving a particular order,

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Ross Lawley
Hi, >From the docs: http://docs.python.org/library/stdtypes.html#dict.items "CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions." Given that

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread René Dudfield
Good morning, 1. Run it on different versions of python, or on different machines/OSes. That usually results in different dictionary ordering. 2. create a dict subclass that returns items always randomised. class my_fun_randomising_item_dict_subclass(dict): def items(self):

Re: [python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Jonathan
oops. wrong list. sorry all. The question still stands though, if anyone's interested. On 11/11/2011 08:42, Jonathan wrote: Hey, I've been writing my own 'memoize' function a lot recently - I'm using it as an interview question. Here's what I've got (with a corresponding series of tests):

[python-uk] memoize & ordering of kwargs.items()

2011-11-11 Thread Jonathan
Hey, I've been writing my own 'memoize' function a lot recently - I'm using it as an interview question. Here's what I've got (with a corresponding series of tests): def memoize(wrapped): cache = {} @wraps(wrapped) def wrapper(*args, **kwargs): key =