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 Design Education Tutorials

A CSS Grid refresher with Mozilla

I really like CSS Grid for layouts. My challenge is that I tend not to use it all that much in my day-to-day work (I provide a fair amount of CSS support, but it tends to be focused on narrower issues), so I don’t practice it all that much. Then, when I return to CSS Grid, I don’t remember any of the syntax beyond display: grid; 😜

Fortunately there are loads of resources online to learn CSS Grid. The latest is a video by Miriam Suzanne on the Mozilla Developer channel, titled “Build a Classic Layout FAST in CSS Grid” –

My favourite CSS Grid resources also include the MDN guide “CSS Grid Layout – CSS: Cascading Style Sheets“, and Wes Bos’ terrific CSS Grid YouTube series:

Categories
Education Tutorials Useful stuff

Teaching kids fractions

Our son is learning fractions at school. He’s finding them a little challenging, so I’ve been trying to help him. On one hand, my math knowledge still seems to be sufficient at his level. On the other, I don’t remember doing this stuff like he does it at school.

I found a couple links that will hopefully be helpful to him (well, aside from the examples I worked through with him, some artful diagrams with blocks, and loads of patience), so I thought I’d share them –

I also found the Khan Academy videos on YouTube (also worthwhile if you just want the videos):

Kids these days have such awesome resources available … (and, thankfully, so do we parents!)

unsplash-logoFeatured image by Dawid Małecki
Categories
Blogs and blogging Publishing Semantic Web Tutorials Useful stuff

Post Kinds, in depth

If you’re looking for a detailed guide to Post Kinds, then read Chris Aldrich’s “Post Kinds Plugin for WordPress“. I’ve been meaning to read this article properly for a little while now to help me better understand how to use Post Kinds more effectively on my site.

Categories
Applications Blogs and blogging Tutorials

Now even more IndieWebified

I just watched Chris Aldrich’s tutorial on how to configure a WordPress site for IndieWeb use. In other words, how to setup your WordPress site as pretty dynamic hub on the Web using a variety of IndieWeb technologies and plugins.

The tutorial runs to about two hours, but it was worth watching. It certainly helped me figure out how to make better use of the plugins I’d installed.

One aspect of this that really impresses me is the Post Kinds plugin. It’s become so much more useful to me. At the same time, it’s only really useful if I publish posts using the WP Admin dashboard on my site.

I’d love to be able to map selected Post Kinds to WordPress’ default post formats so I could take advantage of more Post Kinds when publishing posts from other WordPress interfaces (such as the mobile WordPress.com app).

Categories
Tutorials Writing

A reminder how to use a semicolon

How to use a semicolon by The Oatmeal
How to use a semicolon by The Oatmeal

Now and then I feel like I need a refresher on how to use certain forms of punctuation. Today it was the semicolon, which I have abused on multiple occasions.

The most feared punctuation on earth.

I did a little Googling and found this awesome guide on The Oatmeal titled “How to use a semicolon“. Not only is this guide really useful and worth bookmarking, it is a reminder of just how awesome The Oatmeal is, generally.

Other terrific grammar guides include:

Even people who write for a living could use a reminder now and then. I certainly do.

Source: How to use a semicolon – The Oatmeal

Categories
Applications Tutorials

Tips for capturing screenshots on your Mac

Capturing screenshots on your Mac may be old hat for most Mac users. If, like me, you are a little unsure about how to do this on your Mac, definitely read Macworld’s article titled “How to take screenshots on your Mac“. It is really useful.

I usually use Evernote’s Skitch to capture them because it’s easy to annotate them and drag them into whichever app I am working in.

Lately, though, it is frustrating to use it. Something changed and I often need to restart the app to get it to recognise the keyboard hotkeys to launch the clipping windows.

I knew that Mac OS had a built-in capability to take screenshots but I didn’t take the time to learn the various options so I just stuck with Skitch.

Article link: How to take screenshots on your Mac

Photo credit: Pixabay

Categories
Design Tutorials

Rethinking strong passwords

Here is a great xkcd comic about strong passwords. I have tended to go for the longer, random passwords which I store in LastPass. I usually pick 20 random characters with text, numbers and special characters. I then update them now and then to keep things interesting.

xkcd's recommendation for strong passwords
Password Strength by xkcd

If I understand this comic, it looks like I may be safer with a couple random words instead. Or, a couple random words with mixed characters within them!

Remembering strong passwords is challenging because they need to be considerably more complex than “1234” (if you are using that as your password, you really need to change it). Like I said earlier, I use LastPass to manage my passwords.

There are other options if LastPass isn’t for you and you should investigate if you aren’t already using a password manager. I don’t know how people manage multiple passwords securely without a password manager these days.