Python Regex Methods – A Short Overview

Python’s re module comes with a number of regular expression methods that help you achieve more with less. Think of those methods as the framework connecting regular expressions with the Python programming language. Every programming language comes with its own way of handling regular expressions. For example, the Perl programming language has many built-in mechanisms for regular expressions—you don’t need to import a regular expression library—while the Java programming language provides regular expressions only within a library. This is also the approach of Python.  These are the most important regular…

Read More

Slow and fast methods for generating random integers in Python

The other day, while playing with a simple program involving randomness, I noticed something strange. Python’s random.randint() function feels quite slow, in comparison to other randomness-generating functions. Since randint() is the canonical answer for "give me a random integer" in Python, I decided to dig deeper to understand what’s going on. This is a brief post that dives into the implementation of the random module, and discusses some alternative methods for generating pseudo-random integers. First, a basic benchmark (Python 3.6): $ python3 -m timeit -s 'import random' 'random.random()' 10000000 loops,…

Read More