kerrickstaley / genanki

A Python 3 library for generating Anki decks
MIT License
1.99k stars 150 forks source link

Generate model ID based on hash of its parameters #43

Open ddevault opened 4 years ago

remiberthoz commented 4 years ago

@kerrickstaley and I explain why it would not be wise to do so in https://github.com/kerrickstaley/genanki/issues/49#issuecomment-698996326. Basically it would mean that one cannot update the model without generating obsolete models in user databases.

skunne commented 1 year ago

Hello,

Thank you for this awesome and simple library.

Would it be wise for the user to generate a model id and deck id with the hash of a string hardcoded in the program?

I generated 12 decks using the same model of cards. For the model I used id hash(hardcodeddate+hardcodedprojectname+'model') % (1 << 31) and for each deck I used id hash(hardcodeddate+hardcodedprojectname+deckname) % (1 << 31).

This somewhat contradicts your advice to use random numbers, but not too much. Is that OK or is there some counter-argument?

kerrickstaley commented 1 year ago

The return value of hash() for a given input changes across runs of your program. So no, that isn’t a good approach because the deck ID will change every time you run the program. You could consider using a SHA256 hash though.

skunne commented 1 year ago

Thank you for the quick response! I'm using hashlib.sha256 now.