Giter Site home page Giter Site logo

genkey output is not consistent about das HOT 6 CLOSED

DMWMBot avatar DMWMBot commented on August 16, 2024
genkey output is not consistent

from das.

Comments (6)

DMWMBot avatar DMWMBot commented on August 16, 2024

gfball: I've added a possible solution, plus a unittest that demonstrates the problem (at least on this machine).

The solution passes the unittest, but is probably significantly more expensive. It recurses through a nested structure, finding all the primitive types, converting them to unicode strings then finally sorts the list of all the resulting strings. The ordered list of strings is then fed into md5 to generate the hash.

As an added side effect, cache, merge and analytics will need to be emptied since the new value is not any of the previous possibilities.

from das.

vkuznet avatar vkuznet commented on August 16, 2024

valya: Hmm, you're right. I miss that. But now my fear is that your solution may not work in general case. It rely on a fact that dict will have"key" and "value" and I use genkey in different places. How about this way:

{{{
def genkey(query):
if isinstance(query, dict):
squery = str(query)
lquery = list(squery)
lquery.sort()
query = ''.join(lquery)

 keyhash.update(query)
 return keyhash.hexdigest()

}}}

I convert query, if it is a dict, into a list, sort the list and create a new string out of it. Seems to work, but I didnt' test with unicode/base strings mixture.

from das.

DMWMBot avatar DMWMBot commented on August 16, 2024

gfball: Yes, on the way home I realised there were lots of things wrong with it, eg

['a', 'b'] and ['b', 'a']
['abc', 'def'] and ['abcdef']
{'a': 'b'} and {'b': 'a'}
{1: {2: 3}} and 1abac61 2eb6285 2]]

would all compare the same, which is clearly broken.

I think the right way to do this is to do it as genkey() already does (using json.dumps, which deals with unicode issues for us and works fine with everything but dict) but use a subclass of JSONDecoder that represents dict as [(k1, v1), (k2, v2) ...], sorted by key. I think this should have 1) relatively low overhead and 2) doesn't require us to write any recursive code.

from das.

vkuznet avatar vkuznet commented on August 16, 2024

valya: Sounds good to me. Give it a shot and send a patch.

from das.

DMWMBot avatar DMWMBot commented on August 16, 2024

gfball: Turns out to be really quite simple. If you use JSONEncoder directly, it accepts a kwarg "sort_keys" which resolves this problem without having to even change the dictionary representation. In many cases the qhash will be the same as before.

Had to also make JSONEncoder available in jsonwrapper, since we can only get it from json, not cjson.

from das.

vkuznet avatar vkuznet commented on August 16, 2024

valya: Patch has been applied, closing the ticket.

from das.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.