alexanderlukanin13 / coolname

Random Name and Slug Generator
BSD 2-Clause "Simplified" License
140 stars 12 forks source link

Feature request: name slugs from deterministic numbers #4

Open asemic-horizon opened 4 years ago

asemic-horizon commented 4 years ago

Is it difficult to extend this algorithm to handle nonrandom inputs? E.g. something like

 >> deterministic_slug(4360)
 quirky-thing-easy-memorize

 >> deterministic_slug(4360)
 quirky-thing-easy-memorize

If unfeasible -- did you take inspiration for this concept from another project so I can bother them instead?

alexanderlukanin13 commented 4 years ago

That's an interesting idea. You are essentially talking about a weak pseudo-hash function. I see how it can be extended from just integers to arbitrary objects implementing __hash__(), such as strings.

It's not hard to implement. But one important caveat is that function output for a given input won't be stable between releases, because adding one new word completely alters the sequence. In other words, every change in word lists will break backward compatibility. And while it's not that bad in default generator - I can make a major release for every change in word lists - it can be a pitfall for custom generators.

Anyway, I'll think about it over the next week. (If you want this feature ASAP for commercial project, contact me via gmail, I can give it a priority).

In current version, you can use a simple hack to produce similar result:

>>> import random
>>> from coolname import generate_slug
>>> def deterministic_slug(x):
...     random.seed(x)
...     return generate_slug()
... 
>>> deterministic_slug(123)
'lumpy-analytic-jerboa-of-chaos'
>>> deterministic_slug(123)
'lumpy-analytic-jerboa-of-chaos'
>>> deterministic_slug(456)
'smoky-satisfied-teal-of-democracy'

Obviously, this messes with global random number generator and with ordinary generate_slug() sequence. So I don't recommend this approach beyond simple scripts.

asemic-horizon commented 4 years ago

But one important caveat is that function output for a given input won't be stable between releases, because adding one new word completely alters the sequence.

One way of getting around this is carrying around previous word lists (maybe downloading them on demand in the style of nltk) so they can be fixed in the call.

asemic-horizon commented 4 years ago

(An alternative I might be able to use today is either joblib.memcache on a function like lambda num: generate_slug() (so known arguments are not reevaluated -- or simply a wrapper that stores known values in a dict or sqlite/mongo collection. These are all ideas that could be internalized in coolnames.)

alexanderlukanin13 commented 4 years ago

No, I want to keep it lightweight and simple.

I'll probably just add something like this:

from coolname import pseudohash_slug_v2

With every word lists change, I'll make a new major release, and change version of the function accordingly, to minimize possibility of people upgrading to incompatible version.

For custom generators, I'll add a warning in docs: use at your own risk. Probably only a few people, if any, will use it with custom generators.

felipemoran commented 1 year ago

Hey, any updates on this feature? This is exactly what I need, a stable and deterministic way of generating human-readable names from hash values.

From what I understand from this note on the bottom of this page simply locking the word list isn't enough since the order of the combinations could also change, correct?

If so, is my only option to pin this library to a specific version and hope I won't have to update it in the future or is there any other viable workarounds?

Thanks!