Skip to content

About

Teaches object-oriented programming in Python through interactive exercises and simulations using a subway system as a practical learning context.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

objectville

▶ objectville.borck.education — plan a route in your browser; the page runs this repo's actual Python files via Pyodide. The Learn page rebuilds the program step by step, also in your browser, with automatic checks.

Part of programming.borck.education.

educational learning-resources object-oriented-programming python tutorial curtin edtech education exercises interactive-learning

A small Python 3 codebase for learning object-oriented programming. It models a city subway — stations, the connections between them, and the lines they run on — then finds the shortest route between any two stations and prints directions a human can follow.

Objectville is the town from the Head First books (the subway example is adapted from Head First Object-Oriented Analysis & Design). The station names give it away: Ajax Rapids, EJB Estates, SRP Square, the Mighty Gumball, Inc. Lines are named for OO people: Booch, Wirfs-Brock, Rumbaugh, Jacobson, Liskov, Meyer, Gamma.

Requirements

  • Python 3.10 or newer
  • Nothing else. Standard library only, tests included.

Run it

git clone https://github.com/michael-borck/objectville.git
cd objectville

python3 main.py                              # sanity checks + three sample routes
python3 main.py "Ajax Rapids" JavaRanch     # plan your own route
python3 main.py --stations                   # list all 33 stations
python3 -m unittest discover -s tests -v    # run the tests

output.txt shows what the no-argument run prints.

Use it as a library

from subway_loader import load_subway
from subway_printer import SubwayPrinter

subway = load_subway("objectville_subway.txt")

route = subway.get_directions("Ajax Rapids", "JavaRanch")
SubwayPrinter().print_directions(route)
from station import Station
from connection import Connection
from subway import Subway

Station("DRY Drive") == Station("dry drive")   # True — names ignore case

subway = Subway()
subway.add_station("University Campus")
subway.add_station("DRY Drive")
subway.add_connection("University Campus", "DRY Drive", "Campus Line")
subway.has_connection("DRY Drive", "University Campus", "Campus Line")  # True (both directions)

Notes on the API:

  • Subway.get_directions(start, destination) returns a list of Connection objects — the shortest route by number of hops (breadth-first search). It returns [] when start and destination are the same station or no route connects them, and raises ValueError for a station name that does not exist.
  • Subway.add_connection(...) also raises ValueError for unknown station names, so a typo in the map file fails loudly instead of silently dropping part of a line.
  • Attributes are plain Python attributes: station.name, connection.station1, connection.station2, connection.line. There are no Java-style getters.
  • SubwayPrinter.format_directions(route) returns the directions as a string (that is what the website displays); print_directions(route) prints them.

Project structure

objectville/
├── station.py               # Station: a named stop, case-insensitive equality
├── connection.py            # Connection: dataclass linking two stations on a line
├── subway.py                # Subway: the network + breadth-first route finding
├── subway_loader.py         # SubwayLoader: builds a Subway from the text map
├── subway_printer.py        # SubwayPrinter: route -> readable directions
├── main.py                  # Command-line entry point (demo + route planner)
├── objectville_subway.txt   # The map: 33 stations, 7 lines
├── output.txt               # Expected output of `python3 main.py`
├── tests/test_subway.py     # unittest suite (stdlib only)
├── index.html               # The website, incl. the in-browser route planner
├── learn.html / learn.js    # Guided learning paths: build, TDD, explore (in-browser)
└── CNAME                    # objectville.borck.education (GitHub Pages)

The map format

objectville_subway.txt is blocks separated by blank lines. The first block lists every station, one per line. Each later block is one line: the line's name first, then its stations in order — consecutive stations are connected. Edit it, add a station or a whole line, and rerun. SubwayLoader.load_from_text() parses the same format from a string, which is how the website feeds it the map it fetched over HTTP.

The website

index.html is served by GitHub Pages at objectville.borck.education. Its route planner is genuinely interactive but contains no route-finding logic of its own: on first use it loads Pyodide (CPython in WebAssembly), fetches this repo's five model/loader/printer modules plus the map from the same site, and calls Subway.get_directions() in the browser.

learn.html (with learn.js) is a guided course over this same codebase. Three paths share one in-browser Python runner with per-step automatic checks: Build it step by step starts from the passenger problem and adds one class at a time; Test-driven development builds the same program test-first, red then green; Explore and extend loads the finished modules and the real map, then asks questions of the network and extends it. Progress is kept in localStorage. Each step's setup, starter, checks, and solution live in learn.js, which also runs under Node for verification: every step's solution must pass its checks and its starter must not.

To try it locally (it needs HTTP, not file://, because it fetches those files):

python3 -m http.server 8000
# then open http://localhost:8000 in a browser

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 repo is here to teach.

What it teaches

  • Class design — one small class per file, each with a single job.
  • Objects holding objects — a Connection holds two Stations; a Subway holds all of them.
  • Equality and hashing — Station.__eq__/__hash__ define what "the same station" means, and the rest of the code relies on it.
  • Separating concerns — the model (subway.py) knows nothing about files or printing; the loader and printer handle those, so each can change independently.
  • Searching a graph — route finding as breadth-first search over the connection network.

Good follow-on exercises: add a fare per connection and price a journey; write a loader that reads JSON and notice nothing else changes; make get_directions prefer fewer line changes when two routes tie on hops.

Contributing

This is a teaching repo. Small, readable changes that keep that focus are welcome — open a pull request and say what a learner gets out of it.

License

MIT. See LICENSE.

About

Teaches object-oriented programming in Python through interactive exercises and simulations using a subway system as a practical learning context.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages