mong --I tried porting the code that randomly generates Docker container names to Python -

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

How to use

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: Open In Colab

Implementation policy

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.

Original (moby) implementation

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?

Summary and future issues

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.

References

Recommended Posts

mong --I tried porting the code that randomly generates Docker container names to Python -
I felt that I ported the Python code to C ++ 98.
I tried porting the code written for TensorFlow to Theano
I tried to make a generator that generates a C # container class from CSV with Python
I tried to get the authentication code of Qiita API with Python.
I tried using the Python library "pykakasi" that can convert kanji to romaji.
I tried to refactor the code of Python beginner (junior high school student)
I tried to graph the packages installed in Python
I tried to solve the soma cube with python
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to solve the problem with Python Vol.1
I tried to summarize the string operations of Python
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
[Python] I tried to visualize the follow relationship of Twitter
I tried to implement the mail sending function in Python
Miscellaneous notes that I tried using python for the matter
I tried to enumerate the differences between java and python
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to divide the file into folders with Python
I tried to remodel the code of Python beginner (junior high school student) into object-oriented crunchy
I tried to solve the ant book beginner's edition with python
I tried running the Python Package Repository (Warehouse) that supports PyPI
I tried to touch Python (installation)
Movement that changes direction in the coordinate system I tried Python 3
[Python] A memo that I tried to get started with asyncio
I wrote the code to write the code of Brainf * ck in python
I tried to display the video playback time (OpenCV: Python version)
[Python] I tried to make a simple program that works on the command line using argparse.
A story that didn't work when I tried to log in with the Python requests module
I tried to improve the efficiency of daily work with Python
I tried to move the ball
I tried to estimate the interval.
I tried to refactor the template code posted in "Getting images from Flickr API with Python" (Part 2)
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
When I tried to run Python, it was skipped to the Microsoft Store
Python -I tried to restore the dictionary comprehensive notation to its original form-
[Python] I wrote a simple code that automatically generates AA (ASCII art)
[Python] I tried to analyze the pitcher who achieved no hit no run
I tried to develop a Formatter that outputs Python logs in JSON
(Python) I tried to analyze 1 million hands ~ I tried to estimate the number of AA ~
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I tried to verify and analyze the acceleration of Python by Cython
I tried to understand the decision tree (CART) that makes the classification carefully
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
I tried to analyze the New Year's card by myself using python
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to summarize the umask command
I tried to implement permutation in Python
I tried to recognize the wake word
Python3 standard input I tried to summarize
I tried to summarize the graphical modeling.
I tried to implement ADALINE in Python
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried to implement PPO in Python