API reference

Pub’lish Launcher

Provide the main menu for the Oxford pub tracking application.

class beer_app.run.MenuState(map_server: BackgroundMapServer | None = None, loaded_pubs: list[Pub] = <factory>)

Keep track of resources shared by the menu choices.

beer_app.run.create_pub_crawl(state: MenuState) → None

Create a crawl from the pubs currently loaded on the map.

beer_app.run.handle_choice(choice: str, state: MenuState) → bool

Run one menu choice and return whether the menu should stay open.

beer_app.run.launch_map(state: MenuState) → None

Launch the Oxford map unless it is already running.

beer_app.run.load_map_pubs(state: MenuState, choice: str) → None

Load the selected pubs onto the running map.

beer_app.run.main() → int

Show the welcome menu and run the selected feature.

beer_app.run.search_map_pubs(state: MenuState) → None

Search the pubs currently loaded on the map.

beer_app.run.show_help(choice: str) → bool

Print help when choice is a general or command help request.

beer_app.run.show_stats() → None

Open the visit statistics flow.

beer_app.run.unload_map_pubs(state: MenuState) → None

Remove all pubs from the running map.

beer_app.run.visit_pubs(state: MenuState) → None

Open the visit tracker with the current or packaged pub list.

Generating the Pub Map

Run a small local web server that displays an interactive map of Oxford.

class beer_app.oxford_map.BackgroundMapServer(server: ThreadingHTTPServer, thread: Thread, url: str, state: MapState)

A running map server and its background thread.

clear_pubs() → None

Remove all pub markers exposed to the browser map.

set_pubs(pubs: list[Pub]) → None

Replace the pub markers exposed to the browser map.

stop() → None

Stop the server and wait briefly for its thread to finish.

class beer_app.oxford_map.MapRequestHandler(request, client_address, server)

Serve the map page and a lightweight health check.

do_GET() → None

Handle a browser request (HTTP GET request).

do_POST() → None

Record a visit submitted by a map popup.

log_message(format: str, *args: object) → None

Suppress routine browser request logs.

class beer_app.oxford_map.MapState(pubs: list[Pub] = <factory>, version: int = 0, visited_statuses: dict[int, bool]=<factory>, lock: lock = <factory>)

Thread-safe pub data shared by the terminal and browser map.

record_visit_today(pub_id: int) → bool

Record today’s visit for a pub currently displayed on the map.

replace_pubs(pubs: list[Pub]) → None

Replace the pubs displayed by the map.

snapshot() → dict[str, int | list[dict[str, int | float | str | bool]]]

Return a consistent JSON-ready snapshot of the map state.

beer_app.oxford_map.build_map_page() → str

Return the HTML page used by the local Oxford map server.

beer_app.oxford_map.build_parser() → ArgumentParser

Create the command-line argument parser.

beer_app.oxford_map.build_static_asset(filename: str) → bytes

Return a static image asset used by the map.

beer_app.oxford_map.build_template_asset(filename: str) → bytes

Return a template asset used by the local Oxford map server.

beer_app.oxford_map.main(argv: list[str] | None = None) → int

Run the Oxford map command.

beer_app.oxford_map.run_map_server(host: str = '127.0.0.1', port: int = 8000, open_browser: bool = True) → None

Serve the map until the user stops the process.

beer_app.oxford_map.start_map_server(host: str = '127.0.0.1', port: int = 8000, open_browser: bool = True) → BackgroundMapServer

Start the map server in the background and return its controller.

Load Oxford pub data onto the interactive map.

beer_app.pub_loader.load_pubs_onto_map(map_server: BackgroundMapServer, selection: str = 'all') → list[Pub]

Load the selected pubs, display them on the map, and return them.

beer_app.pub_loader.main(map_server: BackgroundMapServer | None = None, selection: str | None = None) → list[Pub] | None

Load pubs onto the supplied map, or launch a map when run as a script.

beer_app.pub_loader.unload_pubs_from_map(map_server: BackgroundMapServer) → None

Remove all currently displayed pubs from the map.

beer_app.pub_search.format_pub_values(field: str, values: Iterable[str]) → str

Format list-command results for terminal display.

beer_app.pub_search.is_help_query(query: str) → bool

Return whether a query requests search guidance.

beer_app.pub_search.list_pub_values(pubs: Iterable[Pub], field: str) → list[str]

Return sorted unique values for a listable pub field.

beer_app.pub_search.list_query_field(query: str) → str | None

Return the requested field for a --list command, if present.

beer_app.pub_search.main(argv: list[str] | None = None) → int

Search the packaged pub list from the command line.

beer_app.pub_search.search_loaded_pubs(pubs: Iterable[Pub], on_results: Callable[[list[Pub]], None]) → int

Prompt for searches, show list/help commands, and send matches to the caller.

beer_app.pub_search.search_pubs(pubs: Iterable[Pub], query: str) → list[Pub]

Return pubs matching every word in a case-insensitive query.

Visiting Pubs

Load and save the list of pubs marked as visited.

beer_app.visited_pubs.load_visit_history(path: str | Path | None = None) → tuple[dict[int, bool], dict[int, list[str]]]

Return current visited statuses and visit dates from the CSV.

beer_app.visited_pubs.main(pubs: list[Pub] | None = None) → int

Run the interactive visit manager for the supplied or packaged pub list.

beer_app.visited_pubs.manage_visited(pubs: list[Pub]) → None

Show pub visit status and record each visit entered by the user.

beer_app.visited_pubs.record_visit_today(pub_id: int, path: str | Path | None = None) → bool

Record one visit for today, returning whether a new visit was added.

beer_app.visited_pubs.save_visit_history(statuses: dict[int, bool], dates: dict[int, list[str]], path: str | Path | None = None) → None

Write current status and visit dates for each pub to CSV.

Generating a Pub Crawl

Generate pub crawl routes.

Users specify a starting pub, an ending pub, and the number of pubs to visit.

beer_app.pub_crawl.crawl_file_path() → Path

Return the path used to save the latest pub crawl.

beer_app.pub_crawl.create_pub_crawl(pubs: list[Pub], start: Pub, end: Pub, total_pubs: int, buffer_metres: float = 100) → list[Pub]

Select intermediate pubs for a crawl.

Randomly choose the requested number of pubs within the corridor between the starting and ending pubs.

Parameters:
  • pubs (list) – A list of pubs that sit in the corridor, created by pubs_in_buffer() above

  • start ('Pub' object) – The starting pub, specified by the user

  • end ('Pub' object) – The ending pub, specified by the user

  • total_pubs (integer) – The total number of pubs on the pub crawl, specified by the user

  • buffer_metres (float) – The buffer distance to create the corridor between start and end

beer_app.pub_crawl.distance_to_line(pub: Pub, start: Pub, end: Pub) → float

Calculate the distance between a pub and the line between start and end pub.

beer_app.pub_crawl.find_pub(pubs: list[Pub], query: str) → Pub

Find exactly one pub by a case-insensitive name fragment.

beer_app.pub_crawl.main(pubs: list[Pub] | None = None) → int

Create a crawl from the supplied pubs, or all pubs when run directly.

beer_app.pub_crawl.pubs_in_buffer(pubs: list[Pub], start: Pub, end: Pub, buffer_metres: float = 100) → list[Pub]

Create a list of pubs that fall within the corridor created above.

Parameters:
  • pubs (list) – A list of pubs generated using pub_data.py

  • start (Pub object) – The starting pub, specified by the user

  • end (Pub object) – The ending pub, specified by the user

  • buffer_metres (float) – The buffer distance to create the corridor between start and end

beer_app.pub_crawl.relative_xy(pub: Pub, start: Pub) → tuple[float, float]

Set East-West and North-south displacement in metres.

beer_app.pub_crawl.save_pub_crawl(crawl: list[Pub], path: str | Path | None = None) → Path

Save a crawl in route order using the standard pub CSV format.

Summary Statistics

Summary statistics for the uploaded pub database.

beer_app.stats.pub_counts()

Return a panda dataframe including Pub ID, Name, and number of total visits.

beer_app.stats.visited_pub_count(visited_pubs_path)

Count visited trips to each pub as a panda df.

beer_app.stats.visited_pub_path()

Define file path for recorded pub visits.

beer_app.stats.visited_pub_times()

Return list of pubs with names and dates.

Plotting Statistics

Visualizing Script for Pub STATS bby.

beer_app.visualization.format_pub_chart(fig: Figure, ax: Axes, *, title: str, xlabel: str, ylabel: str) → None

Apply the shared Pub’lish chart style to a figure and axes.

beer_app.visualization.launch_stats_graph(target)

Launch the stats graph.

beer_app.visualization.main() → int

Prompt for and display one of the stats graphs.

beer_app.visualization.p_visits_over_time(visited_pub_times: DataFrame) → Figure

Plot monthly pub visits as a cozy stacked bar chart.

beer_app.visualization.p_visits_per_pub(pub_count)

Plot the number of Pub’lishes per pub.

beer_app.visualization.show_visits_over_time()

Plot pub visits overtime.

beer_app.visualization.show_visits_per_pub()

Visit per pub.

Handling Pub Master Data

Load and validate Oxford pub data from CSV files.

class beer_app.pub_data.Pub(id: int, name: str, road: str, location: str, sponsor: str, notes: str, latitude: float, longitude: float)

A pub that can be displayed on the Oxford map.

as_dict() → dict[str, int | float | str]

Return a JSON-ready representation of the pub.

beer_app.pub_data.load_pubs(path: str | Path | None = None) → list[Pub]

Load pubs from a CSV file, using the packaged list by default.