goofy_grothendieck
, ecstatic_lederberg
, quizzical_wu
, ...
You want to give it a random but easy-to-read name, like a Docker container, right? In moby / moby [Go language code](https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator. I found go), so I ported it to Python and made it a library called mong.
2019/12/22 postscript: Follow-up I wrote
We have confirmed the operation with Python 3.6 and 3.8. I think it will work with Python3.5 or later, but Python2 will not work due to Type Hinting etc.
Since we are only using the standard library, the installation should finish soon.
$ pip install git+https://github.com/toshihikoyanase/mong.git
Let's generate a name at random.
(Fixed 22/12/2019: Changed from instantiation method to function call)
import mong
mong.get_random_name() # 'goofy_robinson'
mong.get_random_name() # 'stoic_feynman'
Jupyter Notebook is available on Google Colab. You can try it immediately from:
Looking at the original code, the dictionary of the words that are the source of the name shows most of it. .. If you write a dictionary in the code of the port destination, maintenance seems to be difficult, so I chose the method of extracting from the original code.
Specifically, it is as follows.
--- mong / create_dict.py
extracts the dictionary of the words that are the source of the name from the original code.
--The extracted dictionary is saved in mong / moby_dict.json
.
--The name generation logic is ported to NameGenerator
in mong / name_generator.py
.
Docker name generation code Is located in moby / moby and is written in Go language. Moby seems to be a collection of tools for container systems. It is briefly summarized in this slide.
The name consists of two words, an adjective and the names of famous scientists and hackers. They are connected by _
.
Example
#Angry (adjective)_Turing (personal name)
angry_turing
Adjectives are managed in the list left
, and personal names are managed in the list right
.
left = [...]string{
"admiring",
"adoring",
"affectionate",
"agitated",
right = [...]string{
// Muhammad ibn Jābir al-Ḥarrānī al-Battānī was a founding father of astronomy. https://en.wikipedia.org/wiki/Mu%E1%B8%A5ammad_ibn_J%C4%81bir_al-%E1%B8%A4arr%C4%81n%C4%AB_al-Batt%C4%81n%C4%AB
"albattani",
// Frances E. Allen, became the first female IBM Fellow in 1989. In 2006, she became the first female recipient of the ACM's Turing Award. https://en.wikipedia.org/wiki/Frances_E._Allen
"allen",
The name generation process is basically just random selection and connection.
// GetRandomName generates a random name from the list of adjectives and surnames in this package
// formatted as "adjective_surname". For example 'focused_turing'. If retry is non-zero, a random
// integer between 0 and 10 will be added to the end of the name, e.g `focused_turing3`
func GetRandomName(retry int) string {
begin:
name := fmt.Sprintf("%s_%s", left[rand.Intn(len(left))], right[rand.Intn(len(right))])
if name == "boring_wozniak" /* Steve Wozniak is not boring */ {
goto begin
}
if retry > 0 {
name = fmt.Sprintf("%s%d", name, rand.Intn(10))
}
return name
}
As you can see in This Qiita article, boring_wozniak
is not generated. (When it comes out, it will be remade again.)
As of December 21, 2019, there were 108 words for left
and 235 words for right
, so a total of 25,379 names can be generated. I feel that there are surprisingly few.
Note that the retry
argument is not well understood, and even if you look at the process, it does not seem to be retry
. If retry> 0
, integers between 0
and 9
are randomly selected and added to the end of the name. You may not know this without reading the user's code. Is it simply to increase the number of single digit types?
I made a library called mong that can generate Docker container-like random names with Python.
For the time being, I wrote up to the point where it works, but there is room for improvement in various ways, such as low test coverage, omission of error handling, and no CI. Also, since it is not registered in pypi, installation is a little troublesome.
If there is a need (including myself), I think I'll arrange it a little more.
Recommended Posts