Skip to content

Commit a314e30

Browse files
add filter for game type in catalog.games()
1 parent a9dc597 commit a314e30

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

doc/tutorials/01_quickstart.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,23 +431,23 @@
431431
},
432432
{
433433
"cell_type": "code",
434-
"execution_count": 2,
434+
"execution_count": 3,
435435
"id": "701aa52a",
436436
"metadata": {},
437437
"outputs": [
438438
{
439439
"data": {
440440
"text/plain": [
441-
"['PrisonersDilemma', 'TwoStageMatchingPennies', 'PrisonersDilemmaTestgame']"
441+
"['PrisonersDilemma', 'PrisonersDilemmaTestgame']"
442442
]
443443
},
444-
"execution_count": 2,
444+
"execution_count": 3,
445445
"metadata": {},
446446
"output_type": "execute_result"
447447
}
448448
],
449449
"source": [
450-
"gbt.catalog.games()"
450+
"gbt.catalog.games(game_type=\"nfg\")"
451451
]
452452
},
453453
{

src/pygambit/catalog/catalog.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
from typing import Literal
23

34
import numpy as np
45

@@ -17,7 +18,7 @@ class CatalogGame:
1718

1819
title: str
1920
num_players: int
20-
game_type: str
21+
game_type: Literal["nfg", "efg"]
2122
description: str
2223
citation: str
2324

@@ -92,13 +93,30 @@ def __init_subclass__(cls, **kwargs):
9293
cls._extract_metadata_from_game(cls._cached_game)
9394

9495

95-
def games() -> list[str]:
96-
"""Return a list of all catalog game names."""
96+
def games(game_type: Literal["all", "nfg", "efg"] = "all") -> list[str]:
97+
"""
98+
Return a list of catalog game names.
99+
100+
Parameters
101+
----------
102+
game_type : {"all", "nfg", "efg"}, default "all"
103+
Filter games by type:
104+
- "all": return all games
105+
- "nfg": return only normal-form (strategic) games
106+
- "efg": return only extensive-form games
107+
108+
Returns
109+
-------
110+
list[str]
111+
List of game class names matching the specified type.
112+
"""
97113
def get_all_subclasses(cls):
98114
"""Recursively get all subclasses."""
99115
all_subclasses = []
100116
for subclass in cls.__subclasses__():
101-
if subclass.__name__ not in ["CatalogGameFromFile"]:
117+
if subclass.__name__ not in ["CatalogGameFromFile"] and (
118+
game_type == "all" or subclass.game_type == game_type
119+
):
102120
all_subclasses.append(subclass.__name__)
103121
all_subclasses.extend(get_all_subclasses(subclass))
104122
return all_subclasses

0 commit comments

Comments
 (0)