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
Applications Semantic Web

IFTTT v Pinboard

Never mind Batman v Superman. Now it’s IFTTT v Pinboard and I’m caught in the cross-fire.

I use Pinboard daily in some form or another. I also use the great “connector” service, IFTTT, daily to automate a host of little tasks like adding Instapaper highlights to a text file in Dropbox and many more.

In particular, I use a number of IFTTT recipes that include Pinboard in various little workflows that make my life easier and now it seems that is going to come to an end in just a week or two. I received this alarming email from IFTTT:

IFTTT v Pinboard

I rushed over to the Pinboard blog to see what Pinboard’s creator, Maciej Cegłowski, has to say about the matter. It turns out, he has quite a bit to say in his blog post titled “My Heroic and Lazy Stand Against IFTTT”. He cited two reasons for this little impasse:

Because many of you rely on IFTTT, and because this email makes it sound like I’m the asshole, I feel I should explain myself.

In a nutshell:

  1. IFTTT wants me to do their job for them for free
  2. They have really squirrely terms of service

It’s entirely IFTTT’s decision to drop support for Pinboard (along with a bunch of other sites). They are the ones who are going to flip the switch on working code on April 4, and they could just as easily flip the switch back on (or even write an IFTTT recipe that does it for them). Weigh their claims about Pinboard being a beloved service accordingly.

I understand his concerns and I agree with him that he shouldn’t have to reinvent the wheel at his cost to satisfy IFTTT’s requirements to remain connected to the service. Surely IFTTT could have come up with a more developer-friendly way to migrate to their new platform and help developers make the transition at a lower cost?

I would also have reservations about the contract they want developers to agree to as part of their transition to the new platform. Requiring developers to agree to the sorts of terms Cegłowski quote seems pretty unreasonable given what the clauses would seem to be saying.

At the same time, I’m caught between these two providers I rely on for various tasks. I don’t like the approach IFTTT seems to be taking but I love the service they provide. It literally makes my life easier in so many ways. I have also been using Pinboard for a while now as my personal bookmarking service and I even pay for the archival service. It is a simple and effective service.

This IFTTT v Pinboard impasse between the two companies just hurts people like me who either have to switch to some IFTTT competitor to address the soon-to-be introduced gaps in my workflows or just abandon those workflows altogether. The effect of that is to diminish the value of both services for me, just enough to leave me with that sick, disappointed feeling you get in times like these.

I hope this situation is resolved in some form or another but, if it isn’t … well, that just sucks.

Postscript:

IFTTT v Pinboard Redux – Contracts and Condescension

Categories
Business and work

Automating unproductivity

I completely get this and have done it far more times that I would readily admit to.