Python implementation of nim