Three lines of Python to brighten your day

TL;DR: I wrote a short script that returns a random quote from a list of nice things people have said about me, to combat quarantine pants-ness while working from home.


Being an introvert by nature, I’m not suffering too badly in isolation (after an initial period of adjustment and complaining) but there is one big thing from the office that I am missing, and that’s the wall of reasons I’m great.

If I had a photo I would share it, but I do not. It’s a bunch of paper notes blutacked to a wall several miles away in the center of Bristol, and without it I am prone to feelings of worthlessness and pants.

Basically, I took all of the notes that colleagues wrote about me during the 2018 Christmas Appreciation Swap and put them on a wall next to my desk. Whenever I started to feel like I wasn’t getting anywhere, wasn’t good enough or that everyone secretly hated me, I would look up at that wall to remind myself that I’m actually very rad.

Illustration of me at my desk, affectionately touching a wall of hearts

I could have brought the notes home and set them up on my bedroom wall, if I’d had the foresight to do so before lockdown – but the need to do this reveals a great big flaw in this method of fighting Imposter Syndrome.

What if I go to work at a new office? Or from a cafe, or a car, or a train, or a field in the middle of nowhere in a post-apocalyptic wasteland?! I’ll still want to remember all the nice things my friends and colleagues said about me before they became zombie-mutants in the Infection Zone.

So the idea was to make a little program that I can easily run on my mac or transfer to another computer if I need to in the future. The criteria were thus:

  1. One file
  2. Returns a random quote from a list
  3. Run with custom bash command for ease of use

I decided to use Python because it’s supposed to be good for simple tasks, and I haven’t really used it since I made a soil classifier way back in 2015. (by this I mean that I told a programming friend that I wanted to do it, but at that time I had a horrible attitude towards learning – which resulted in him basically writing through my hands while I pretended to understand… I’ve learnt a lot about learning since then xD)

If you’d like to do something similar, follow along or you can copy-paste if you like. It’s legit just three lines of code to make yourself feel better at any time of day. You’ll need to have Python installed on your computer in order to run it though, for which you can find the official instructions here.

Compiling the list

The first thing I did was double-check how variables and lists are written in Python. I didn’t want to assume that it would be the same as JavaScript, but in this case we’re all good:

quote_list = ['Quote 1', 'Quote 2', 'Quote 3']

It doesn’t super matter since this is just for me, but I searched for Python naming conventions as well – ie. what format people have generally agreed to use when naming variables, classes, functions and files. I usually default to camelCase for everything but in Python it seems that lowercase_words_with_underscores is the norm, so I followed that.

Now I just needed to populate the list with a bunch of amazing quotes from people who love me. Which is everyone, obviously, because I am a lovable person.

Ideally, I’d have grabbed the ones I’m used to from the wall. They’re classics and have gotten me through some difficult Monday mornings in the last year. Unfortunately I didn’t have them, so it became a matter of trawling Slack and Twitter chats to find new things that stand out.

This was the most time-consuming part of the process, and I was kind of afraid that doing it would be super depressing, but it was actually more uplifting than anything else. xD

Sidenote: if you have something nice to say, feel free to email or DM me and I’ll add it in because I love feeling loved. And if you don’t have much for your own list, don’t be afraid to reach out to people and ask. It’s always a fear that you’re going to come across as super conceited by asking for a compliment, but I’ve found that the community – especially testers of the MOT clan – love nothing more than to let each other know how awesome and appreciated they are.

I switched out the single quotation marks for double ones because natural English has a lot of apostrophes in it. It occurred to me that I could (and should) probably keep the quotes as a separate file, and get the Python program to reference that file instead of having it all in one. But I really like the idea of emailing myself a single file for use on any other computer, ready to go. I could even put it on github I guess, and then I can add to it and pull the most recent version… But that’s too far ahead for today’s Bruce.

You’ll be glad to know that I’m not listing out all the nice things people have ever said about me in this blog post – not THAT much of a narcissist. At least, not quite.

Illustration of a snake with cute shiny eyes

The actual Python bit

Ok so: I did not learn Python in order to do this. I didn’t go and do a Python course, I just typed a few things into a search engine until I’d found all the bits I needed in order to put something simple together. I still feel pretty accomplished though, because a year and a half ago I didn’t have a good enough base of coding knowledge and experience to work from when trying to slip into new languages or tools. I now often find what I’m looking for within two to three attempts with a search engine. I gots me all the right words. _nods seriously_

I quickly found that there is already a Python module for making a random choice and it’d be silly not to use that. In fact, when I brought it up with a programmer friend, he told me: “Yeah, all you do with Python is import a bunch of modules where other people have worked it out for you, and then you write one line of code that does everything you want.” That’s his words, not mine, but in this case it checks out.

You can find a doc for the random module here. Always gotta read the docs, you know.

So the first thing to do is to create a python file – name it what you like, and use the file extension .py at the end.

At the top of the file, you import any modules you need. In this case, just ‘random’, like so:

import random

Beneath that we can write some code. Let’s add an example list to start with:

import random

pick_me_up_list = ['Quote 1', 'Quote 2', 'Quote 3']

Items in a list have something called an index, which tells you which position that element is in the list. For example, in the list [‘banana’, ‘carrot’, ‘bruce is the best’], carrot is the second item – and has an index of (1) because computers start counting at 0. I originally thought that I’d need to generate a random number and then have the program select the quote with that number as its index.

But the random module makes it easier than that. It already has a method for the exact purpose of selecting a random choice from a list! huzzah! We can use the random.choice() method to select a quote for us. Let’s assign the result to a variable, then print that out:

import random

pick_me_up_list = ['Quote 1', 'Quote 2', 'Quote 3']

random_pickup = random.choice(pick_me_up_list)
print random_pickup

After checking that works, we can combine the last two lines together so that it prints the random quote without assigning it to a variable first:

import random

pick_me_up_list = ['Quote 1', 'Quote 2', 'Quote 3']

print random.choice(pick_me_up_list)

That’s pretty much it. That’s everything.

To run a python file from the terminal, you use the python command:

> python your_file_name.py

(so long as you’re in the same directory. To run it from anywhere else, you need to use the full path to the python file.)

And here is it working:

Image shows a screenshot of the computer terminal. The program is run four times, returning "Quote 3", "Quote 1", "Quote 1" and "Quote 2".
Running the program from terminal returns a random quote

Awesome.

Creating a custom terminal command

OK so the above is all well and good, loving it so far, but I don’t want to have to type in

> python /Users/brucehughes/Projects/pyFortune/pick_me_up.py

every time I need an uplifting message of appreciation. I just wanna use one word that will do this for me. For that, we need to add an alias to the bash config file. I love aliases by the way, and I gleefully misuse them for silly purposes instead of creating shortcuts to make my life easier. I have way too many of them, and they’re all One-Punch Man themed.

I mean, why git commit -m "comment" when you can CONSECUTIVENORMALPUNCHES "comment"?

If you haven’t played about with this stuff before, the chances are you don’t have a bash profile to add aliases to so that’s the first step. You can find instructions for your operating system by typing something like “add bash alias for <os>” into a search engine of your choice. I’m on mac, and the process of adding a profile and some aliases is pretty straightforward:

  1. Open terminal and navigate to your home directory with the command ‘cd’ and nothing after it:
> cd

2. Create a .bash_profile file (if you’re not on mac, this will be called something else, eg .bashrc on Linux):

> touch .bash_profile

3. Open this file in a text editor of your choice, and add an alias in the following format:

alias commandName='python /Full/path/to/your/python/file/file_name.py'

The important things to note are that there are no spaces to either side of the = sign, and that you need to use single quotation marks. Oh, and your command is just one word, no spaces. You can come back and add as many as you like by adding them to new lines (without commas or anything between them). Save the file and then make sure your terminal window pulls in the changes using the command

> source .bash_profile

Now you can use your new command from anywhere, with just a single word!

Results

Screenshot from terminal. By typing the word "pickup", a random quote is returned. The quotes in the example are:
"While I appreciate the innocence and sincerity of that, I'm afraid you've been misinformed, as I have it on good authority that you are actually the best";
"It's going to be all good, I believe in it and I believe in you.";
"Bruce your creativity + work massively inspire me. You rock!";
"I have to tell you again because you might have forgotten - you're fucking remarkable."; and
"I really appreciated you taking a time out to talk to me".

There we have it. No matter where I go, I can now carry all the nice things people have said with me, and call upon them whenever they are needed just by typing the word “pickup”.

I’m not sure I learnt much when it comes to Python, or any technical skill at all, but the really interesting insight I got from this was from trawling chats for nice things. There were some quotes I already remembered, ones that super impacted me at the time they were said; and there were some conversations I remembered feeling good about without knowing the precise quotes. I went right for these at first.

Then there were also certain people that I went to automatically, thinking something like: all I have to do it scroll up a few ticks and I’m guaranteed to find something nice from ma boii Stan. I realised the massive effect that just a few individuals have on the way I view myself. There is a core group of powerfully positive people that I talk to, who go out of their way to lift me up in every single interaction we have, without fail.

Illustration of me floating on a bunch of speech bubbles with hearts inside
_singing_ you raise me upppppppp

Those people are amazing, and I am endeavouring to be more like them by lifting up the people around me and making sure they know that they’re awesome and appreciated as well.

1 thought on “Three lines of Python to brighten your day”

  1. Hi Bruce, this is such a great article for beginners who are just starting with coding. I am definitely going to share this with the trainees who are working remotely in my team.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.