Amending Commits, Matplotlib, and More Python

I’ve been on vacation and spend the last two days catching up and not doing a lot of learning, so I’ve been lazy in putting up TIL posts. That is over. (I did, however, push some updates to my Apple Photos Analysis project.) Here is a small collection of things I learned in the last week.

Amending commits

Say you forgot to add a file to your last commit or you made a typo in your commit message. You can amend it!

Make the necessary changes, then do this:

git commit --amend -m "Commit message here"

If you’ve already pushed it to an external repository, you’ll need to force the push since the external repo will look like it is ahead. If branch protection is turned on, you’ll need to make a new commit. Make sure you aren’t overwriting anything important!

git push origin master --force

Here are the docs.

Adding data labels to the top of bar charts in Matplotlib

Matplotlib is a great plotting library for Python.

def autolabel(rects): # attach some text labels for rect in rects: height = rect.get_height() plt.text(rect.get_x() + rect.get_width()/2., 5+height, '%d' % int(height), ha='center', va='bottom') rect = plt.bar(xs, counted_hours, color=color) # To use: autolabel(rect)

Saving images in matplotlib

plt.savefig('directory/filename.png')

Counting items that match a regex pattern

def hour_finder(regex,lines): time_counter = 0 for l in lines: if re.match(regex, l): time_counter = time_counter + 1 return time_counter # To use hour_finder('^8:[0-9]{2,}:[0-9]{2,}sPM',time_csv)

Splitting!

Splitting by a space ' ' and choose the item after the split ([1] because counting starts at 0)

list = [i.split(' ')[1] for i in time_csv]