This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Scrython is a Python wrapper for the Scryfall API (Magic: The Gathering card database). It provides a clean, Pythonic interface for querying cards, sets, and bulk data from Scryfall's REST API.
python test.py # Run the manual test script
pytest # Run pytest tests (if test suite exists)pip install -e . # Install in development mode
python setup.py install # Install packageThe library uses a base request handler (ScrythonRequestHandler) combined with mixins to compose API endpoint classes. This pattern allows different endpoints to share common functionality while maintaining specific behaviors.
Key components:
-
scrython/base.py: ContainsScrythonRequestHandler- the base class that handles all HTTP requests to Scryfall API_build_path(): Resolves endpoint path parameters (e.g.,:id,:code)_build_params(): Constructs query parameters_fetch(): Executes HTTP request and handles errors viaScryfallError- Path parameters use
:param_namesyntax; optional params end with?(e.g.,:lang?)
-
Mixins (in
base_mixins.pyand module-specific*_mixins.pyfiles):ScryfallListMixin: For endpoints returning lists (search results, collections)ScryfallCatalogMixin: For endpoints returning catalogsCoreFieldsMixin,GameplayFieldsMixin,PrintFieldsMixin: Card-specific data accessors- Mixins provide
@propertyaccessors toscryfall_datadictionary
-
Smart Factory Classes:
Cards,Sets,BulkData- Use
__new__()to dynamically instantiate the correct endpoint class based on kwargs - Example:
Cards(fuzzy="Lightning")returnsCardsNamed, whileCards(search="bolt")returnsCardsSearch - This provides a single entry point with intelligent routing based on parameters
- Use
scrython/
├── base.py # ScrythonRequestHandler, ScryfallError
├── base_mixins.py # ScryfallListMixin, ScryfallCatalogMixin
├── rate_limiter.py # RateLimiter, SlowRateLimiter (per-endpoint tiering)
├── cache.py # Request caching with TTL
├── utils.py # Utility functions (e.g., to_object_array)
├── cards/
│ ├── cards.py # Card endpoint classes + Cards factory
│ └── cards_mixins.py # Card data accessors (CoreFieldsMixin, etc.)
├── sets/
│ ├── sets.py # Set endpoint classes + Sets factory
│ └── sets_mixins.py # Set data accessors
└── bulk_data/
├── bulk_data.py # Bulk data endpoint classes + BulkData factory
└── bulk_data_mixins.py # Bulk data accessors
- User calls factory:
card = scrython.Cards(fuzzy="Black Lotus") - Factory's
__new__()selects appropriate class:CardsNamed - Class inherits from
ScrythonRequestHandler+ relevant mixins ScrythonRequestHandler.__init__()runs:_build_path()resolves endpoint template (e.g.,/cards/named)_build_params()adds query params (e.g.,?fuzzy=Black+Lotus)_fetch()makes HTTP request, parses JSON intoscryfall_data
- Mixin properties provide data access:
card.name→scryfall_data['name']
- All Scryfall API errors are wrapped in
ScryfallError(fromscrython/base.py) ScryfallErrorexposes:status,code,details,type,warnings- HTTP errors that aren't from Scryfall raise generic
Exception
From Contributing.md:
- No single character variables (except
ffor files,ifor iterations) - Complex code (regex, etc.) needs explanatory comments
- Weird/unexpected code needs comments explaining why (not just what)
- 4 spaces for indentation (no tabs)
- Avoid useless comments
- Built-in rate limiting: Automatic per-endpoint rate limiting (10/s default, 2/s for search/named/random/collection). See
scrython/rate_limiter.py. Users can override withrate_limit_per_secondkwarg or disable withrate_limit=False. - No backwards compatibility: Breaking changes expected as Scryfall API evolves
- Python 3.10+ required: Uses
X | Yunion syntax andtype[X]annotations throughout - Dependencies: urllib (standard library), no external HTTP dependencies
- Branches:
mainis stable/PyPI,developis staging
- Add endpoint class in appropriate module (e.g.,
scrython/cards/cards.py) - Set
_endpointclass variable with path template (use:paramfor path params) - Inherit from
ScrythonRequestHandler+ appropriate mixins - Update factory class's
__new__()method to route to your endpoint - Add new properties to mixin files if Scryfall returns new fields