An it harm none, code as thou wilt

The other day I had a little problem to solve. It was not a complex problem. I wanted to generate a set of coupon codes to hand out for something. I wanted them to be in an alphanumeric format like A3YH-UB4S. I often do things like that in a LibreOffice spreadsheet, because I’m sort of lazy that way. I tinkered for a while at getting the randomness to work the way I wanted and to generate the numbers and letters the way I wanted, then realized I was going to have to write some sort of macro code to make it do what I wanted. At that point I decided that if I was going to write code, rather than write it in the spreadsheet tool I would do something external that could generate my list. I decided to use Python. I’ve been interested in doing something with Python for some time, mostly because there seems to be a lot of integration for it in tools like GIMP and Blender. The logic involved was not really rocket surgery. I did some searches to find out how to do the basic loops and output and soon had a trivial little program that did the job.

#makecodes.py
import random
import sys

for x in range(0,100):
    for y in range(0,9):
        z=random.randint(0,35)
        if z<10:
            z+=48
        else:
            z+=55
        
        if y<>4:
            sys.stdout.write(chr(z))
        else:
            sys.stdout.write("-")
    sys.stdout.write("\n")

If I work with this often there are a number of things I’ll probably do with it. For example, there is not real uniqueness check that is happening. It’s possible to generate duplicate codes. Ideally I should track each code generated and make sure there are no repeated values. I could also have a way to change the number of entries generated, and their format. However, for the time and task I had, this worked fine. I piped the output to a text file, pulled that into my LibreOffice base and generated the coupons.

Feeling proud, like the Brave Little Tailor, I made a social post about my first Python program. Of course, I immediately received feedback from some of my technical friends. Some praised my choice of Python. Others questioned it and suggested languages and approaches that would have been  a better accomplishment. However, my job was done. This was not something I plan to release (though I guess I just did by posting it here). It was a way to quickly solve a problem and let me see what coding Python was like.

There are many situations where we want to use technology to get something done. Sometimes these are critical circumstances that have far-reaching impact. In those cases it is very important that we understand the moving parts and how our actions and choices impact the rest of the environment (especially as we are more cloud-facing). However, there are also many situations where something just needs to be done and it’s fine to reach for the nearest technical duct tape for the job.

Depending on your role, it may be that your duct tape gets moved into production fairly often, so you should probably always consider that and not do things that are too weird. But technology is also about curiosity and finding new skills and ways to do things. If the duct tape will do, don’t sweat it. You solve your problem. You learn some things. The world continues to spin. Explore and experiment and be proud of your creations.

(I used Bluefish as my code editor. I’m becoming rather fond of it.)

Leave a Reply

Your email address will not be published. Required fields are marked *