Skip to content

Config

Kloppy has an API to configure and customize global behavior related to setting the default coordinate system for all load calls, setting the cache directory (or disabling the cache) and passing settings to adapters.

The API is composed of 3 functions, available from the kloppy.config namespace:

from kloppy.config import get_config, set_config, config_context
  • get_config() - get the value of a single option.
  • set_config() - set the value of a single option.
  • config_context() - execute a codeblock with a set of options that revert to prior settings after execution.

Get config

You can get all configuration variables or just a single one using get_config().

1
2
3
>>> cfg = get_config()
>>> print(cfg)
{'cache': '/opt/buildhome/kloppy_cache', 'coordinate_system': 'kloppy', 'event_factory': None, 'adapters.http.basic_authentication': None, 'adapters.s3.s3fs': None, 'adapters.zip.fo': None, 'dataframe.engine': 'pandas'}

To get a single configuration variable, you must pass the name of the variable as an argument.

1
2
3
>>> cfg_coordinate_system = get_config("coordinate_system")
>>> print(cfg_coordinate_system)
kloppy

Set config

Using set_config() you can set the value for a single config variable. This value will be used for all calls to kloppy.

1
2
3
4
5
>>> from kloppy import statsbomb
>>> set_config("coordinate_system", "opta")
>>> dataset = statsbomb.load_open_data()
>>> print(dataset.metadata.coordinate_system)
<kloppy.domain.models.common.OptaCoordinateSystem object at 0x7f02a3dad5e0>

Config context

Inspired by pandas, kloppy allows you to set config variables for a context (using a context manager). Config set using config_context() will be reverted to the original values when python exits the context.

>>> print(f"Before context: {get_config('coordinate_system')}")
>>> with config_context("coordinate_system", "statsbomb"):
>>>     print(f"Within context: {get_config('coordinate_system')}")
>>>     dataset = statsbomb.load_open_data()
>>> print(f"After context: {get_config('coordinate_system')}")
>>> print(dataset.metadata.coordinate_system)
Before context: opta
Within context: statsbomb
After context: opta
<kloppy.domain.models.common.StatsBombCoordinateSystem object at 0x7f02a389d5e0>