Object-oriented Python, one subway system at a time.

A small, dependency-free Python 3 codebase that models a city subway with classes. Plan a route below — your browser runs the actual .py files from this repo — then clone it, read it, extend it.

Plan a route

Pick two stations. Nothing is reimplemented in JavaScript: on first use this page loads Pyodide (CPython compiled to WebAssembly), fetches station.py, connection.py, subway.py, subway_loader.py, subway_printer.py and objectville_subway.txt from this site, and calls Subway.get_directions() exactly as you would locally.

Loading the station list…


    

Learn by building it

Reading finished code only takes you so far. The Learn page rebuilds this program with you, starting from the passenger problem and adding one idea at a time. Pick a path. Build it step by step guides the whole construction. Test-driven development builds the same code test first, so every step starts with a failing check. Explore and extend starts from the finished repo and asks it new questions. Every step runs Python in your browser and checks your code.

Run it locally

Standard library only — nothing to install beyond Python 3.10 or newer.

git clone https://github.com/michael-borck/objectville.git
cd objectville
python3 main.py                              # sanity checks + sample routes
python3 main.py "Ajax Rapids" JavaRanch     # plan your own route
python3 -m unittest discover -s tests -v    # run the tests

The classes

Six small files, each with one job — the object-oriented idea in miniature:

The city map lives in objectville_subway.txt — edit it, add a line, and rerun. Objectville itself is a tip of the hat to the town where the Head First books taught a generation of programmers objects. The tests in tests/test_subway.py show each class being exercised on its own.

Why learn this when AI can write it?

An AI tool can write this code in seconds. It can also get it wrong in ways that look right. This repo is proof. Its old README described methods that never existed and claimed Python 3 support for Python 2 code. Nobody noticed, because nobody ran it.

The fix was not more code. It was reading the five classes, deciding what they should do, and checking the output against tests. That part is still yours. Classes, equality, and graph search are how you check work, whether you wrote it or a machine did. Use AI on the exercises below if you want. Then run the tests and read what it gave you. If you can say why the answer is right, you have learned the thing this site is here to teach.

Try extending it

Good first exercises: add fares per connection and price a journey; write a second loader that reads JSON instead of text — and notice how little else has to change; make get_directions prefer fewer line changes when two routes have the same number of hops.