Categories
Applications Coding Tutorials

The missing piece in my Python scripts

My quest to automate parts of my workflows usually involves writing Python scripts to streamline some of my tasks as a Happiness Engineer.

One output has confused me for far longer than it should have, and I was shown the light today (in the form of a pretty simple solution).

My dilemma

Many of my scripts generate template documents. These include meeting agendas, hangout notes, and team reports. These documents include a couple components that I’ve largely automated using Python, such as –

  • Calculating dates and times in reports (sometimes including timezone conversions);
  • Taking inputs I provide in response to command line prompts, running calculations, and adding those to fields in my reports; and so on.

Many of my documents have a static list of team members that I ping much like Twitter mentions when I publish them as posts on internal blogs (we use internal blogs quite a lot, as you can imagine). Those lists each need to be updated manually every time there’s a change in the team composition, in each of the documents the list appears in.

Although the team composition doesn’t change frequently, I often find myself re-using a script for a team report, or hangout agenda, for example, in another group I work with. This means another set of documents where I need to manually create a list of people, and maintain that.

An example could be something like this:

Pinging: @john-doe, @maryjacobs, @davesmith, @janestuart, @tomwright, @steverobinson

The solution

Currently, the first part of generating this sort of list of people is creating a .csv file that looks something like this:

first_name,last_name,username
John,Doe,john-doe
Mary,Smith-Robinson,maryjacobs
Dave,Smith,davesmith
Jane,Stuart,janestuart
Tom,Wright,tomwright
Steve,Robinson,steverobinson

I was planning on using Python Classes to do this, but quickly realised that I don’t understand Classes nearly well enough to using this feature for this aspect of my documents.

I realised that there’s actually a simpler solution using .csv files and the csv module available in Python instead, after watching Corey Schafer‘s tutorial on this, here:

As an aside, Schafer’s tutorials are wonderful!

I was able to borrow from Schafer’s solutions to write a script that produced a list that looks something like this:

Pinging: ['@john-doe', '@maryjacobs', '@davesmith', '@janestuart', '@tomwright', '@steverobinson']

My script looked like this:

import csv

with open('people.csv', 'r') as csv_file:
    csv_reader = csv.DictReader(csv_file)

    members = []

    for line in csv_reader:
        wpuser = f"@{line['wp_user']}"
        members.append(wpuser)

print(f'Pinging: {members}')

I couldn’t work out how to just generate a list of usernames separated by spaces. I ran into a similar issue with other scripts where I similarly loop over a list of items to produce some sort of list-generated output.

In other words, I couldn’t figure out how to output the list of usernames without the []' characters you see in my initial output.

So, I asked for help on reddit where JohnnyJordaan pointed me to this StackOverflow post that includes a couple solutions to a similar question. I had searched for a solution to my challenge, but didn’t come across this answer (or anything like it).

Clearly, I need to work on my Googling skills when it comes to finding solutions to my coding challenges.

Anyway, the solution that JohnnyJordaan suggested is pretty simple. Instead of using something like print(f'Pinging: {members}'), I could rather join the items in the list with a statement like this:

print(f'Pinging: {", ".join(members)}')

As I suspected, the solution is pretty simple. It just eluded me, completely. So thank you to JohnnyJordaan for the pointer!

I also like the * that came up in one of the StackOverflow solutions, but it doesn’t work with the f strings that I tend to use. Still, there are other ways to use them. Trey Hunner has an interesting post about these operators here (if you’re interested).

Categories
Coding Policy issues Useful stuff

Creating good through open source

I really like videos like this:

Open source as a way of doing things has such amazing potential to make our world so much better.

Watching videos like this tend to prompt me to revisit my calendar and try find regular blocks of time I can dedicate to my dusty coding projects.

Categories
Coding Devices Events and Life Mindsets Useful stuff

My idea of a good time: coding on a Linux computer

Lately my idea of fun has been firmly rooted in coding, and playing around with Linux.

We’re planning to buy our son a new Linux PC after passing his (and before him, my) old Linux PC to our daughter.

I’m very tempted to extend my loan of my personal MacBook Air to him, and but myself a new laptop to install Linux on, and use that to explore what’s possibly my latest midlife crisis.

This article about Jason Evangelho’s switch to Linux just reinforces my temptation/idea.

Canonical’s Ubuntu seems to command a lot of mindshare when it comes to desktop Linux, so that was my next stop. I went through the same paces: download to a USB stick, boot up to the “Live” version of Ubuntu 18.04 (which includes 5 years of security patches and updates), have a look around, click “Install.” Ubuntu presented me with several options for partitioning the internal SSD, including blasting the entire drive. Tempting! I was feeling lucky so I took the plunge.

Categories
Coding Mindsets

Raising brave, imperfect daughters, and teaching them to code

Last week I came across a tweet sharing Reshma Saujani’s TED talk, titled “Teach girls bravery, not perfection“. I immediately bookmarked it to watch with my daughter (and tweeted my plan to do that).

Saujani replied to my tweet, and asked me to let her know what my daughter thought of the talk.

So, I watched the talk on Saturday morning with my 7 year old (along with my son). Afterwards, I asked her what she thought about what Saujani said about how important it is to be brave, rather than being perfect, and how the quest for perfection is so self-defeating.

My daughter said she liked the video. I asked her to elaborate, and she commented on this talk has inspired her to try to learn to code again. She said that she stopped trying the first time around because she kept making mistakes.

I noticed this when I introduced her to coding on Code.org last year. She started off really excited to see what she could create after watching me learn front-end web development for most of last year. But she soon gave up when the exercises became trickier and she found herself making mistakes.

Since watching the talk, she’s been asking me when she can get back to learning to code. It also helps that my son has also returned to learning to code after seeing me return to Python (I’ve started at the beginning with Python 3).

Now all I need to do is pick a learning platform for her to learn with. So far, Code.org and Scratch look like good options for her.

Photo by Jelleke Vanooteghem on Unsplash

Categories
Business and work Coding

An awesome thread about developers’ careers

Stephanie Hurlburt asked for stories from developers about their career paths on Twitter, and it quickly became one of the best threads I’ve read in a while. As you can imagine, I have a particular interest in stories about other developers’ careers given my journey this year. This thread didn’t disappoint.

I started reading the thread when I woke up this morning and, boy, what a great way to start a day.

Categories
Mindsets Policy issues

Celebrating Women in Tech with the awesome #WITBragDay meme

My favourite meme at the moment is the awesome #WITBragDay meme on Twitter that celebrates women in tech. It seems to have been started by Alice Goldfuss with her tweet:

The result is tweet after tweet of pure inspiration from women in the technology industry. I spent some time reading tweets this morning when I woke up and I found myself smiling because these stories are just awesome.

These women, and others like them, are the perfect response to the odious Damore memo. These stories are also the stories I want both our kids to know, especially our daughter. Heck, these stories inspire me as I learn to code. Here is a selection of some of my favourites:

I’ve created a Twitter Moment for the tweets I love the most. You can find that here too (it may be more complete and up to date):

Image credit: The #WOCinTech collection on Flickr, licensed CC BY 4.0

You can read more about the #WOCinTech project here too: “#WOCinTechChat – Promoting diversity in tech through stock photos