jazzband / jsonmodels

jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
http://jsonmodels.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
334 stars 51 forks source link

Add __eq__ to models that compare based on fields #76

Closed dimakuz closed 7 years ago

dimakuz commented 7 years ago

This patch adds the ability to compare model instances based on their type and fields (rather than their reference).

This patch also adds _cache_key member and changes Field.memory access to be by obj._cache_key rather than obj itself. This had to be done because memory's __getitem__ called obj's __eq__ and caused stack overflow.

dimakuz commented 7 years ago

@beregond , take a look please

beregond commented 7 years ago

@dimakuz looks nice. How did you run into memory leak with weak key dictionary?

dimakuz commented 7 years ago

@beregond I did not run into memory leak, but there is a stack overflow due to infinite recursion in __eq__ if we don't use cache keys. This happens because in __eq__ if types are the same and we go on to compare field values, we have to lookup in dict (Field.memory) with object itself as key, this access calls __hash__ and __eq__ on object and thus we have an infinite recursion

beregond commented 7 years ago

Ok, nice idea with CacheKey object!

beregond commented 7 years ago

Released in 2.1.4