Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: name slugs from deterministic numbers #4

Open
asemic-horizon opened this issue Oct 6, 2019 · 5 comments
Open

Feature request: name slugs from deterministic numbers #4

asemic-horizon opened this issue Oct 6, 2019 · 5 comments

Comments

@asemic-horizon
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

(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
Copy link
Owner

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
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants