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
Blogs and blogging Miscellany

Are blogs still relevant as personal sharing platforms?

This is a repost from my Tumblr blog and was originally published on 11 February 2013

I’ve been thinking about blogs’ relevance again. My Squarespace subscription for my main blog is about to come to an end and as awesome as Squarespace is as a platform, I don’t use that site nearly often enough to justify the $80-something cost to renew it for another year. Particularly when I could set up a WordPress site for almost nothing on a server I already have available to me for hosting. Update: As you can see, I completed the migration to this blog anyway. There are a number of broken links and images after the export from Squarespace and I’ll sort those out in time, probably.

I have my blog archive going back to 2004 when I started blogging and I want to locate that somewhere so I started migrating my blog content yesterday and redirecting my domain to my hosted space. It’s a bit of a process and there are definitely glitches in the migration process. The hassle of the move has brought me back to a few stray thoughts I had about current social services like Google+ and Facebook and about a blog’s relevance as a personal sharing platform.

One of the things I love about using Facebook (and, to a lesser extent primarily because so few friends and family members are using it, Google+) is that it is possible to share selectively with specific groups of friends/connections. That makes Facebook and Google+ really useful. There are times when I just want to share something with friends and family and other times when I am happy to share stuff publicly (even though many of my connections probably wish I wouldn’t). The lists and circles functionalities in Facebook and Google+, respectively, make that really easy.

Blogs don’t really support that sort of selective sharing. You are either sharing publicly (and possibly replying on obscurity for some privacy) or you resort to posting stuff that users with passwords or access to restricted posts can see. Lists and circles are far more dynamic and flexible but the disadvantage of relying on Facebook and Google+ is that we really don’t have all that much control over those platforms and years of contributions and shares could disappear for a number of reasons ranging from a big crash to an exploit to the service provider cutting you off for even more reasons.

At least with your own blog, you can have more control over your content, back it up, move it around in a meaningful format and set up shop elsewhere. Sort of. That said, unless you have your own server in your house in a secure space, the risk of your data disappearing one day exists in varying degrees anyway. My host could take my site down one day. Tumblr could remove this blog and if I haven’t backed my data up, well I would be a bit upset at losing all my stuff.

So, if control is truly an illusion, are blogs still relevant as personal sharing platforms in a digital world which lends itself more to selective and flexible sharing? Is it worth going to the effort of relocating my blog or should I just archive my digital tracks from the last 8 years or so and share in the distributed moment?