Roll Your Own Name Generator with QPython3 on an Android Device

Roll Your Own Name Generator with QPython3 on an Android Device

Welcome to the first of a multi-part series of tutorials on how to develop, source control, and run scripts to control your Android device, using only mobile apps for development.

Dev On the Go

I recently went on a road trip, and wanted to experiment with a mobile-only workflow. The main components I needed were an interpreter, an IDE, and source control.

The suite of apps I discovered and used are as follows:

  • QPython3 (a python IDE and script runner)
  • SGit (source control)
  • LabCoat (GitLab repository viewer)
  • Dropbox (ad-hoc, easy-access file storage)
  • Dropsync (one- or two-way sync between directories on your mobile device)
  • Epsilon Notes (a markdown editor and renderer)

In this tutorial, I will cover my experience with QPython3.

Takeaways from this endeavor:

Plus:

  • It’s all in your pocket
  • Short feedback loop for testing code

Delta:

  • Difficult to view information across multiple apps
  • Editing code can be a chore
  • Not enough charging stations with comfortable seating in that one mall I went to

Recommendations:

  • Get a bluetooth keyboard
  • Bring the portable charger
  • Sit uncomfortably close to strangers so you can share the charging station and maybe they’ll leave (they didn’t leave, but offered to rearrange the seating so we could chum comfortably whilst staring silently at our phones)

In this tutorial, you will learn:

  1. How to run a ‘Hello World!’ script in QPython3
  2. How to load files into a QPython3 script

Install list

Run Qpython3 and Open hello_world.py

QPython3 Home

Select Programs > hello_world.py > Open to view the script contents.

QPython3 Open

QPython3 Hello World Contents

Super standard python3 here. The script uses the SL4A library (Scripting Layer for Android) to show an Android toast, then prints 'Hello world!' to the console.

Click the right-arrow on the bottom toolbar of the script editor screen to run the script. You can also back out of the editor, click hello_world.py again, and choose 'Run'.

QPython3 Hello World Execution

That’s it!

Play around with some of the other default scripts if you like. To see some of the SL4A library functionality in action, run test.py to run a series of tests on various sensors and UI elements. It will demo a few things like text to voice, various form input elements, and progress bars.

Demo Qpython3 App – Item Name Generator!

Now that you’re able to execute python scripts on your Android device, let’s do the only logical next thing: write a short fantasy weapon name generator script.

Loading the Files

First, we need to load the file contents. I wrote a quick loadfile function, which takes a filename and returns a list of strings contained in the file.

QPython3 Namegen Assets

weapons.txt

Each line in the loaded file will be stored as a separate string in the list by splitting on newline, or '\n'.

fileutils.py

QPython3 fileutils.py

That root_path is pretty ugly, but QPython3 does not work with relative paths. It can be improved by using the os library:

This will let you run the script on other devices, such as a Linux VM.

The Name Generator Script

This script will:

  • Import the loadfile function from our fileutils.py module
  • Read the contents from 3 text files into lists: prefixes, weapons, and suffixes
  • For each weapon in the weapons list:
    • Print a string which consists of the current weapon name and a random prefix and suffix

Step 1: Load the files. Import the loadfile function from our fileutils module.

Step 2: Invoke loadfile to create a weapons list, then print it out to verify.

If you run the script, you should see a list with an entry for each weapon printed to the console:

Step 3: Invoke loadfile for the prefixes and suffixes.

Step 4: Finally, loop through the weapon list, select a random prefix and suffix, and print out the combination. Don’t forget to import the random module.

QPython3 Namegen Code

You should see something similar to this in your console:

QPython3 Generated Names

Conclusion

You should now be familiar with writing and executing python modules on your Android device! Add your own prefixes, suffixes, and weapons to roll your equally awesome counterpart to such legendary names as satisfactory cuisses of pain, repressed flail of efficacy, and gesticulating bow of obscurity!

In the next installment, I’ll demonstrate how to set up file syncing so you can edit your source files from another device.

Thanks for reading!

Join the Discussion

%d bloggers like this: