I used regex in a meaningful way for the first time yesterday.

Regex is short for Regular Expressions and are used to define search/match patterns in programming. For example, Python has the re standard library module which can be used to construct regular expressions and use them to match/search strings.

I have known of regular expressions for a while now but I have never actually used them in any meaningful way. Until yesterday.

I've been working on trivial contributions to open source Python projects. One of the trivial changes was updating how the super builtin function is called. On Python 3, arguments are optional. On Python 2, arguments have to be passed and specifically, super(ClassName, self) is how the builtin function needs to be called.

For Python packages that are completely dropping support for Python 2, they can get rid of the old way of calling super. But, manually finding and updating the usage is a little tedious, especially for large codebases which heavily rely on objects and inheritance.

This is the perfect place where regex shines. As I mentioned above, I can define all uses of super on Python 2 using super(ClassName, self) and I can just update that to super().

I used the online regex editor regex101 to experiment with building the regular expression and I ended up with (super)+\(+(\w+)(,)+\s(self)+\)+.

Let's break it down.

  • (super)+ - I am being lenient in saying that there can be one or more exact matches of "super"
  • \(+ - One or more matches of "("
  • (\w+) - One or more words
  • (,)+ - One or more ","
  • \s - One whitespace character
  • (self)+ - One or more "self"
  • \)+ - One or more ")"

Note that I could have constructed a stricter/better regular expression. I am expecting the codebase to conform to PEP8 when I say that there should be a "," after the word and exactly one whitespace after "," and before "self". But, this works and it works well for my needs.

And this led to a Pull Request on Traits.

Popular posts from this blog

Farewell to Enthought

Arxiv author affiliations using Python

Elementary (particle physics), my dear Watson