rstripping Simon Pegg: Don’t use rstrip for file extension removal

March 12, 2018 Leave a comment

In Python, what does '/path/to/my/simon_pegg.jpeg'.rstrip('.jpeg') yield?

If you guessed '/path/to/my/simon_pegg' – good try, but not quite right. The real answer is '/path/to/my/simon_'.

Despite what you might intuitively think, rstrip does NOT strip off a substring from the end of a string. Instead, it eliminates all of the characters from the end of the string that are in the argument. (Note that this is not mutating the string; it returns a copy of the string.)

Since pegg contains the characters that are in .jpeg, it is eliminated as well.

While this behavior is documented, it may be surprising.

Why does it matter? There are many instances of people attempting to use this rstrip approach to strip off a file extension. For instance, you might be converting an image from one filetype to another, and need to construct the final path. Or you might want to rename a bunch of files to have consistent extensions (jpegjpg).

This buggy implementation of stripping a file extension is tricky because most of the time it works – but it works by coincidence (the file name happens not to end with the characters in the file extension).

Github is rife with examples of people making this same mistake. For instance, name = str(name).rstrip(".tif"),
i.path.rstrip('.gzip').rstrip('.gz') + '.gzip') = [i.rstrip(".jpg") for i in k]

The Go language has the same semantics for its TrimRight function. This leads to the same sort of mistakes when people use it to trim file names. For instance,

filehelper.go: filename := strings.TrimRight(f.Name( ".pdf")

latest_images.go: idStr := strings.TrimRight(f.Name(), ".jpg")

The lessons to be learned from this are,

  1. Read the documentation of the library functions you use.
  2. Test your code, and not just of the happy paths. Good tests should try to break your implementation and exercise edge cases.


(Hat tip to my colleague Fredrik Lundh who alerted me to this problem and inspired this post)


Video: “Meet Spot, the robot dog that can run, hop and open doors”

August 9, 2017 Leave a comment

Still from TED talk video of

This is an incredible video. When I worked on a robotics team in college, we had a hard enough time with the four legged Sony AIBO robots on a fixed field with fixed landmarks. In this live demo, they show the dog pathfinding and adapting to changing terrain in real time. Inspiring engineering

I made a game – Rocket Runner is live now!

May 22, 2017 Leave a comment

After months of Coursera classes in game design, I just finished the capstone project – an eight week course to build a game from scratch. I built the game Rocket Runner using Unity. You can play it now on Kongregate. I hope to put it in the iOS and Android app stores in the next few weeks.


I hope you enjoy! Let me know what you think in the comments.

Categories: Uncategorized Tags: , ,

Link: graphics analysis of Deus Ex: Human Revolution

March 21, 2015 Leave a comment

Screenshot from the article showing normal map generation

This is one of the best presentations I’ve ever seen. Each step of the rendering pipeline is explained clearly, and the animated transitions between the screenshots are incredible. I can’t wait to read more from this author.

Categories: Uncategorized

Your update is not more important than my work

March 5, 2015 Leave a comment

I love TextMate, but I just saw the most user-hostile, infuriating thing. I’m doing work when all of a sudden I get the pop-up:


TextMate forced update

TextMate forced update

It closed my document (thankfully giving me a chance to save), and now the program refuses to launch until updated.


Link: At Chipotle, How Many Calories Do People Really Eat?

February 20, 2015 Leave a comment

This article makes good use of histograms to display distributions rather than just standard descriptive statistics like “average”. For those who haven’t taken much math or haven’t been exposed to these sorts of distributions, the author also picks out certain points along the cumulative frequency distribution chart to explain what they mean; for instance only 10 percent of meals have less than 625 calories.

For many data sets (especially non-normal ones that arise in social networks, arithmetic mean (sum and divide by the number of elements) is a gross approximation of the real central tendency. See Ed Chi’s great article about this subject.  (Full disclosure: I have worked with Ed at Google)

Categories: Uncategorized

Link: “A female computer science major at Stanford: “Floored” by the sexism”

February 19, 2015 Leave a comment article

Choice quote:

When a boy – let’s call him Rush (like Rush Limbaugh) — heard my friend had interned at Facebook, his mouth dropped. “Wow! Facebook! You must be really smart!” He then turned to me and asked the exact same question: What did you do this summer?

Except when I responded the same — “Facebook” — I got a completely different response. “Oh… well then I should have applied for that internship.”

Terrible. Also terrible is the treatment she received during her internships:

My high-pitched voice also became an unexpected source of frustration as team meetings became small battlegrounds for respect. At another company (which I prefer not to name), I noticed that management listened more to what my male counterparts had to say even though I was offering insightful feedback. Managers asked my male coworkers about the status of projects, although I was touching all the same files. The guys were praised more on their progress although I was pushing the same amount of code.

We as an industry have to stop this nonsense.

Categories: Uncategorized