Friday, June 26, 2015

Python Regular Expressions

Regular expressions (regex's) are a language for describing patterns in text. Although Python functions like string.startswith() and string.endswith() can search for fixed substrings, a regex can recognize string patterns where the exact value is not known.

Books

Mastering Regular Expressions (O'Reilly Publishing)

This is truly the only book you will ever need. A massive encyclopedia (over 500 pages) covering every aspect of regular expressions in all the major programming languages. Affiliate link


Online Testers

Regex101

This is one of the best regular expression testers. It supports several regex dialects (PHP, JavaScript, and Python). You can build your expressions interactively and test them against sample text. Additional features include
  • online regex reference guide
  • display regular expressions in plain English
  • display match groups
  • automatically generate Python code for any regular expression


Command Line Tools

grep

Written 40 years ago, grep is one of the oldest regular expression tools. You can find free versions for all the major operating systems (Linux, Windows, Mac). With grep you can search through large amounts of text files using regular expressions to narrow your search. If you work with text files a lot, grep will probably end up being your main search tool. If you need a quick overview of grep's options, look at this short tutorial. Read the full article


awk

This command line tool is very similar to grep, but it's optimized for searching program files. It's written in Perl, which means you'll need to install Perl if you don't already have it on your system. Read the full article


Puzzles and Games

RegEx Crossword

Test your regular expression skills! This is an online game similar to Sudoku, but all the clues are written as regular expressions. Start at the "beginner" level and see how far you can go. Play now


Videos

Python for Informatics : Regular Expressions

This 35 minute lecture is Lesson 11 in the Python for Informatics course by Charles Severance. Dr. Severance explains how to use the Python regular expression library to clean up "dirty" data. And he'll show you his tattoo. Watch the video


Libraries

Python "re" module

A detailed explanation of Python's standard "re" module with examples. Read the full article


Sunday, June 21, 2015

Python State Machines

State machines allow you to describe the high level logic of your program while ignoring low level implementation details. They are especially useful for modeling event-driven systems.

Theory and Concepts

Statecharts

Here is the original paper written by David Harel in 1986. This gave birth to the UML state diagrams we have today. Read the full article

UML State Diagrams

This Wikipedia article explains the basics of Events, States, Guards, and Actions. Read this if you need to get up to speed on basic concepts and terminology. Read the full article

State Diagram Crash Course

This 30 page whitepaper makes a strong argument for using state machines instead of giant if-else statements. It uses the Visual Basic calculator program to illustrate how state machines can simplify your code and eliminate bugs. Read the full article


Videos

UML State Diagrams

Derek Banas teaches you the UML notation in this 13 minute YouTube video. Watch the video

Coding a State Machine by Hand

You probably don't want to do this. Watch the video


Libraries and Tools

pystatemachine 1.2

A clever idea. This package uses Python @decorators to turn any class into a state machine. Read the full article

State Machine Compiler (SMC)

Writing state machine code is dull, repetitive, and error prone. Let the computer do it for you. This tool generates Python code from a high-level description of your state machine. Read the full article