Database & Search API Reference
This module covers PGN archive and Scid binary database indexing, multi-criteria header queries, and filter bitsets.
Database
libscid.Database
Chess game database supporting fast header indexing and subset queries.
A Database manages an indexed collection of chess games, supporting
high-speed header queries, tag extraction, game deserialisation, filter
subset management via DatabaseFilters, and
search execution via DatabaseSearch.
Direct instantiation of Database is disallowed; instances are opened via
factory class methods such as
open_pgn_read_only().
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "Match"]\n[White "Capablanca"]\n[Black "Lasker"]'
... '\n\n1. e4 e5 1-0\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> database = libscid.Database.open_pgn_read_only(path)
>>> database.game_count
1
>>> database.get_tag(0, "White")
'Capablanca'
>>> game = database.get_game(0)
>>> game.mainline_move_count
2
>>> database.close()
>>> pathlib.Path(path).unlink()
Disallow direct database instantiation.
Raises:
| Type | Description |
|---|---|
TypeError
|
Always raised if instantiated directly. |
Source code in src/libscid/_database.py
Attributes
type
property
Database backend format identifier (e.g. 'PGN', 'Scid5', 'Memory').
Examples:
>>> import tempfile, pathlib, libscid
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write('[Event "E"]\n[White "W"]\n\n1. e4 1-0\n')
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.type
'PGN'
>>> db.close()
>>> pathlib.Path(path).unlink()
read_only
property
True if the database was opened in read-only mode.
Examples:
>>> import tempfile, pathlib, libscid
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write('[Event "E"]\n[White "W"]\n\n1. e4 1-0\n')
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.read_only
True
>>> db.close()
>>> pathlib.Path(path).unlink()
game_count
property
Total number of games indexed in the database.
Examples:
>>> import tempfile, pathlib, libscid
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write('[Event "E"]\n[White "W"]\n\n1. e4 1-0\n')
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.game_count
1
>>> db.close()
>>> pathlib.Path(path).unlink()
filters
property
filters: DatabaseFilters
Filter manager for accessing and creating game subset views.
Examples:
>>> import tempfile, pathlib, libscid
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write('[Event "E"]\n[White "W"]\n\n1. e4 1-0\n')
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.filters.all_games.game_count
1
>>> db.close()
>>> pathlib.Path(path).unlink()
search
property
search: DatabaseSearch
Query engine for executing header, board, and position searches.
Examples:
>>> import tempfile, pathlib, libscid
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(
... '[Event "E"]\n[White "Capablanca"]\n\n1. e4 1-0\n'
... )
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> criteria = libscid.HeaderCriteria(white="Capablanca")
>>> filter_view = db.search.headers(criteria)
>>> filter_view.game_count
1
>>> db.close()
>>> pathlib.Path(path).unlink()
Methods:
open_pgn_read_only
classmethod
open_pgn_read_only(
path: str | PathLike[str],
progress_report_callback: ProgressReportCallback
| None = None,
should_cancel: ShouldCancelFn | None = None,
) -> Database
Open a Portable Game Notation (.pgn) archive in read-only mode.
Scans the PGN text archive, indexing game offsets and header metadata in memory for fast random access and searching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Filesystem path to the PGN file. |
required |
progress_report_callback
|
ProgressReportCallback | None
|
Optional callback receiving |
None
|
should_cancel
|
ShouldCancelFn | None
|
Optional predicate function returning True to request cooperative early cancellation. |
None
|
Returns:
| Type | Description |
|---|---|
Database
|
An opened read-only |
Raises:
| Type | Description |
|---|---|
LibScidError
|
If the file cannot be opened, is malformed, or indexing is cancelled. |
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "Hastings"]\n[White "Capa"]\n[Black "Lasker"]'
... '\n\n1. e4 1-0\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.game_count
1
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_database.py
get_tag
Retrieve a header tag value for a game directly from the index.
Retrieves metadata (e.g. player names, event, date, ECO, result) without parsing the complete game movetext.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
0-based database game index. |
required |
name
|
str | bytes
|
PGN tag header name (e.g. "White", "Date", "ECO"). |
required |
Returns:
| Type | Description |
|---|---|
str
|
The tag value string, or an empty string if the tag is absent. |
Raises:
| Type | Description |
|---|---|
LibScidError
|
If |
Examples:
>>> import tempfile, pathlib, libscid
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(
... '[Event "Match"]\n[White "Tal"]\n[Black "Botvinnik"]'
... '\n\n1. e4 1-0\n'
... )
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.get_tag(0, "White")
'Tal'
>>> db.get_tag(0, "Black")
'Botvinnik'
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_database.py
get_game
get_game(index: int) -> Game
Load and deserialise the full chess game at the specified index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
0-based database game index. |
required |
Returns:
| Type | Description |
|---|---|
Game
|
A newly allocated |
Raises:
| Type | Description |
|---|---|
LibScidError
|
If |
Examples:
>>> import tempfile, pathlib, libscid
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(
... '[Event "Match"]\n[White "Tal"]\n[Black "Botvinnik"]'
... '\n\n1. e4 e5 1-0\n'
... )
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> game = db.get_game(0)
>>> game.get_tag("White")
'Tal'
>>> game.mainline_move_count
2
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_database.py
close
Close database storage files and release resources.
Examples:
>>> import tempfile, pathlib, libscid
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write('[Event "E"]\n[White "W"]\n\n1. e4 1-0\n')
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_database.py
DatabaseFilters
libscid.DatabaseFilters
Manager for chess database selection filters and subset views.
Provides access to universal and primary predefined filters as well as allocation of custom user-defined filters for query execution, sorting, and game subset inspection.
Direct instantiation of DatabaseFilters is disallowed; instances are
accessed via the Database.filters property.
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = '[Event "E1"]\n\n1. e4 1-0\n\n[Event "E2"]\n\n1. d4 1-0\n'
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> database = libscid.Database.open_pgn_read_only(path)
>>> database.filters.all_games.game_count
2
>>> database.filters.primary.game_count
2
>>> custom_filter = database.filters.create()
>>> custom_filter.game_count
2
>>> custom_filter.delete()
>>> database.close()
>>> pathlib.Path(path).unlink()
Disallow direct filter manager instantiation.
Raises:
| Type | Description |
|---|---|
TypeError
|
Always raised if instantiated directly. |
Source code in src/libscid/_database_filters.py
Attributes
all_games
property
all_games: Filter
Universal built-in filter matching all games in the database.
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = '[Event "E1"]\n\n1. e4 1-0\n\n[Event "E2"]\n\n1. d4 1-0\n'
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.filters.all_games.game_count
2
>>> db.close()
>>> pathlib.Path(path).unlink()
primary
property
primary: Filter
Primary working filter used for interactive search results.
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = '[Event "E1"]\n\n1. e4 1-0\n\n[Event "E2"]\n\n1. d4 1-0\n'
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.filters.primary.game_count
2
>>> db.close()
>>> pathlib.Path(path).unlink()
Methods:
create
create() -> Filter
Allocate and register a new, empty user filter in the database.
Returns:
| Type | Description |
|---|---|
Filter
|
A newly allocated user |
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = '[Event "E1"]\n\n1. e4 1-0\n'
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> custom = db.filters.create()
>>> custom.game_count
1
>>> custom.delete()
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_database_filters.py
DatabaseSearch
libscid.DatabaseSearch
Chess database query and search engine.
Executes fast header queries, exact position lookups via transposition hashing, and flexible board pattern searches against database filter subsets.
Direct instantiation of DatabaseSearch is disallowed; instances are
accessed via the Database.search property.
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "E"]\n[White "Fischer"]\n[Black "Spassky"]\n'
... '[Result "1-0"]\n\n1. e4 e5 1-0\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> database = libscid.Database.open_pgn_read_only(path)
>>> criteria = libscid.HeaderCriteria(white="Fischer", result="1-0")
>>> matched_filter = database.search.headers(criteria)
>>> matched_filter.game_count
1
>>> database.close()
>>> pathlib.Path(path).unlink()
Disallow direct search engine instantiation.
Raises:
| Type | Description |
|---|---|
TypeError
|
Always raised if instantiated directly. |
Source code in src/libscid/_database_search.py
Methods:
headers
headers(
criteria: HeaderCriteria,
*,
source: Filter | None = None,
destination: Filter | None = None,
progress_report_callback: ProgressReportCallback
| None = None,
should_cancel_fn: ShouldCancelFn | None = None,
) -> Filter
Execute a multi-criteria header query across a database filter subset.
Scans game headers matching the criteria from the source filter,
writing matching game indices into the destination filter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
criteria
|
HeaderCriteria
|
|
required |
source
|
Filter | None
|
Source |
None
|
destination
|
Filter | None
|
Destination |
None
|
progress_report_callback
|
ProgressReportCallback | None
|
Optional progress callback receiving
|
None
|
should_cancel_fn
|
ShouldCancelFn | None
|
Optional predicate returning True to request cooperative cancellation. |
None
|
Returns:
| Type | Description |
|---|---|
Filter
|
The |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
LibScidError
|
If search execution fails or is cancelled. |
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "E1"]\n[White "Fischer"]\n[Result "1-0"]\n\n'
... '1. e4 1-0\n\n'
... '[Event "E2"]\n[White "Spassky"]\n[Result "0-1"]\n\n'
... '1. d4 0-1\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> criteria = libscid.HeaderCriteria(white="Fischer")
>>> result_filter = db.search.headers(criteria)
>>> result_filter.game_count
1
>>> result_filter.get_game_indices()
(0,)
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_database_search.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
position
position(
position: Position,
*,
source: Filter | None = None,
destination: Filter | None = None,
progress_report_callback: ProgressReportCallback
| None = None,
should_cancel_fn: ShouldCancelFn | None = None,
) -> Filter
Search for games reaching an exact board position snapshot.
Performs fast transposition hash lookup across games in the source
filter, writing matches into destination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
position
|
Position
|
Target |
required |
source
|
Filter | None
|
Source |
None
|
destination
|
Filter | None
|
Destination |
None
|
progress_report_callback
|
ProgressReportCallback | None
|
Optional progress callback. |
None
|
should_cancel_fn
|
ShouldCancelFn | None
|
Optional cooperative cancellation predicate. |
None
|
Returns:
| Type | Description |
|---|---|
Filter
|
The |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
LibScidError
|
If search execution fails or is cancelled. |
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "E1"]\n\n1. e4 e5 1-0\n\n'
... '[Event "E2"]\n\n1. d4 d5 1-0\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> fen = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1"
>>> pos = libscid.Position.from_fen(fen)
>>> matched_filter = db.search.position(pos)
>>> matched_filter.game_count
1
>>> matched_filter.get_game_indices()
(0,)
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_database_search.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
board
board(
position: Position,
*,
match: BoardSearchMatch = BOARD_MATCH_EXACT,
source: Filter | None = None,
destination: Filter | None = None,
include_variations: bool = False,
include_flipped: bool = False,
progress_report_callback: ProgressReportCallback
| None = None,
should_cancel_fn: ShouldCancelFn | None = None,
) -> Filter
Search for games matching a board configuration or material pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
position
|
Position
|
Target |
required |
match
|
BoardSearchMatch
|
Matching mode algorithm: "exact" (identical piece squares), "pawns" (identical pawn structure and piece count balance), or "files" (identical piece counts per file). Defaults to "exact". |
BOARD_MATCH_EXACT
|
source
|
Filter | None
|
Source |
None
|
destination
|
Filter | None
|
Destination |
None
|
include_variations
|
bool
|
Whether to search alternative variation branches in addition to the mainline. Defaults to False. |
False
|
include_flipped
|
bool
|
Whether to also match colour-flipped board positions (White and Black swapped). Defaults to False. |
False
|
progress_report_callback
|
ProgressReportCallback | None
|
Optional progress callback. |
None
|
should_cancel_fn
|
ShouldCancelFn | None
|
Optional cooperative cancellation predicate. |
None
|
Returns:
| Type | Description |
|---|---|
Filter
|
The |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
LibScidError
|
If search execution fails or is cancelled. |
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "E1"]\n\n1. e4 e5 1-0\n\n'
... '[Event "E2"]\n\n1. d4 d5 1-0\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> fen = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1"
>>> pos = libscid.Position.from_fen(fen)
>>> matched_filter = db.search.board(pos, match="exact")
>>> matched_filter.game_count
1
>>> matched_filter.get_game_indices()
(0,)
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_database_search.py
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
HeaderCriteria
libscid.HeaderCriteria
dataclass
HeaderCriteria(
player: str | None = None,
white: str | None = None,
black: str | None = None,
event: str | None = None,
site: str | None = None,
site_country: str | None = None,
round: str | None = None,
date_min: str | None = None,
date_max: str | None = None,
event_date_min: str | None = None,
event_date_max: str | None = None,
eco_min: str | None = None,
eco_max: str | None = None,
result: HeaderResult = None,
game_number_min: int | None = None,
game_number_max: int | None = None,
halfmove_count_min: int | None = None,
halfmove_count_max: int | None = None,
white_elo_min: int | None = None,
white_elo_max: int | None = None,
black_elo_min: int | None = None,
black_elo_max: int | None = None,
elo_difference_min: int | None = None,
elo_difference_max: int | None = None,
has_variations: bool = False,
has_comments: bool = False,
has_nags: bool = False,
)
Multi-criteria search parameters for chess game headers.
Encapsulates text pattern filters, date/ECO ranges, rating limits, game lengths, tournament results, and structural movetext flags.
Attributes:
| Name | Type | Description |
|---|---|---|
player |
str | None
|
Substring match on either White or Black player name. |
white |
str | None
|
Substring match on White player name. |
black |
str | None
|
Substring match on Black player name. |
event |
str | None
|
Substring match on event/tournament name. |
site |
str | None
|
Substring match on venue/site name. |
site_country |
str | None
|
Substring match on site country name or code. |
round |
str | None
|
Substring match on round identifier. |
date_min |
str | None
|
Minimum game date string (e.g. "1921.01.01"). |
date_max |
str | None
|
Maximum game date string (e.g. "1927.12.31"). |
event_date_min |
str | None
|
Minimum event date string. |
event_date_max |
str | None
|
Maximum event date string. |
eco_min |
str | None
|
Minimum ECO code classification (e.g. "B20"). |
eco_max |
str | None
|
Maximum ECO code classification (e.g. "B99"). |
result |
HeaderResult
|
Desired game outcome (e.g. "1-0", "0-1", "1/2-1/2", "*") or an iterable of acceptable outcomes. |
game_number_min |
int | None
|
Minimum 1-based game number in the database. |
game_number_max |
int | None
|
Maximum 1-based game number in the database. |
halfmove_count_min |
int | None
|
Minimum game length in halfmoves (ply). |
halfmove_count_max |
int | None
|
Maximum game length in halfmoves (ply). |
white_elo_min |
int | None
|
Minimum Elo rating for White. |
white_elo_max |
int | None
|
Maximum Elo rating for White. |
black_elo_min |
int | None
|
Minimum Elo rating for Black. |
black_elo_max |
int | None
|
Maximum Elo rating for Black. |
elo_difference_min |
int | None
|
Minimum Elo difference ( |
elo_difference_max |
int | None
|
Maximum Elo difference ( |
has_variations |
bool
|
If True, matches only games with alternative variations. |
has_comments |
bool
|
If True, matches only games with text commentary. |
has_nags |
bool
|
If True, matches only games with Numeric Annotation Glyphs. |
Examples:
>>> import libscid
>>> criteria = libscid.HeaderCriteria(
... white="Kasparov",
... result="1-0",
... eco_min="B80",
... eco_max="B89",
... has_comments=True,
... )
>>> criteria.white
'Kasparov'
>>> criteria.result
'1-0'
Filter
libscid.Filter
Subset view of games within a chess database.
A Filter represents a filtered or selected subset of games within a
Database. Filters provide high-performance pagination,
multi-criteria sorting, and bidirectional mapping between 0-based database
game indices and sorted display row positions.
Filters are obtained via
DatabaseFilters.all_games,
DatabaseFilters.primary,
DatabaseFilters.create(), or as search result
destinations from DatabaseSearch.
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "E1"]\n[White "W1"]\n\n1. e4 1-0\n\n'
... '[Event "E2"]\n[White "W2"]\n\n1. d4 1-0\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> database = libscid.Database.open_pgn_read_only(path)
>>> all_games = database.filters.all_games
>>> all_games.game_count
2
>>> all_games.get_game_indices(start_row=0, row_count=2)
(0, 1)
>>> database.close()
>>> pathlib.Path(path).unlink()
Disallow direct filter instantiation.
Raises:
| Type | Description |
|---|---|
TypeError
|
Always raised if instantiated directly. |
Source code in src/libscid/_filter.py
Attributes
game_count
property
Total number of games currently matched by this filter.
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "E1"]\n\n1. e4 1-0\n\n'
... '[Event "E2"]\n\n1. d4 1-0\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.filters.all_games.game_count
2
>>> db.close()
>>> pathlib.Path(path).unlink()
Methods:
get_game_indices
get_game_indices(
sort_criteria: str | bytes = "N+",
start_row: int = 0,
row_count: int | None = None,
) -> tuple[int, ...]
Retrieve database game indices in sorted display row order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sort_criteria
|
str | bytes
|
Sorting specification string (e.g. "N+" for game number, "D-" for descending date, "W+" for White player, "B+" for Black player, "E+" for ECO code). Defaults to "N+". |
'N+'
|
start_row
|
int
|
0-based starting row offset in the sorted view. Defaults to 0. |
0
|
row_count
|
int | None
|
Maximum number of game indices to retrieve. If None,
retrieves all remaining games from |
None
|
Returns:
| Type | Description |
|---|---|
tuple[int, ...]
|
A tuple of 0-based database game indices in sorted order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "E1"]\n\n1. e4 1-0\n\n'
... '[Event "E2"]\n\n1. d4 1-0\n\n'
... '[Event "E3"]\n\n1. c4 1-0\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> filter_view = db.filters.all_games
>>> filter_view.get_game_indices("N+", start_row=1, row_count=2)
(1, 2)
>>> filter_view.get_game_indices()
(0, 1, 2)
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_filter.py
get_game_index_at_row
Retrieve the database game index for a specific sorted display row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
int
|
0-based display row index. |
required |
sort_criteria
|
str | bytes
|
Sorting specification string. Defaults to "N+". |
'N+'
|
Returns:
| Type | Description |
|---|---|
int
|
The 0-based database game index at the specified row. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "E1"]\n\n1. e4 1-0\n\n'
... '[Event "E2"]\n\n1. d4 1-0\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.filters.all_games.get_game_index_at_row(1, "N+")
1
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_filter.py
get_game_row_for_index
Find the sorted display row index for a specific database game index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
game_index
|
int
|
0-based database game index. |
required |
sort_criteria
|
str | bytes
|
Sorting specification string. Defaults to "N+". |
'N+'
|
Returns:
| Type | Description |
|---|---|
int
|
The 0-based display row position of the game in the sorted filter view. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = (
... '[Event "E1"]\n\n1. e4 1-0\n\n'
... '[Event "E2"]\n\n1. d4 1-0\n'
... )
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> db.filters.all_games.get_game_row_for_index(1, "N+")
1
>>> db.close()
>>> pathlib.Path(path).unlink()
Source code in src/libscid/_filter.py
delete
Delete this user-created filter and release its database resources.
Raises:
| Type | Description |
|---|---|
ValueError
|
If attempting to delete a built-in filter (such as
|
Examples:
>>> import tempfile, pathlib, libscid
>>> pgn = '[Event "E1"]\n\n1. e4 1-0\n'
>>> with tempfile.NamedTemporaryFile(
... "w+", suffix=".pgn", delete=False
... ) as f:
... _ = f.write(pgn)
... f.flush()
... path = f.name
>>> db = libscid.Database.open_pgn_read_only(path)
>>> custom = db.filters.create()
>>> custom.game_count
1
>>> custom.delete()
>>> db.close()
>>> pathlib.Path(path).unlink()