Config¶
Kloppy 3.3 adds support for setting global configuration. This makes it possible to set the coordinate system for all load
calls, setting the cache directory (or disabling the cache) and passing settings to adapters.
In [5]:
Copied!
from kloppy.config import get_config, set_config, config_context
from kloppy.config import get_config, set_config, config_context
Get config¶
You can get all configuration variable or just a single one using get_config
.
In [6]:
Copied!
get_config()
get_config()
Out[6]:
{'cache': '/Users/koen/kloppy_cache', 'coordinate_system': 'kloppy', 'event_factory': None, 'adapters.http.basic_authentication': None, 'adapters.s3.s3fs': None, 'dataframe.engine': 'pandas'}
In [7]:
Copied!
get_config("coordinate_system")
get_config("coordinate_system")
Out[7]:
'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.
In [8]:
Copied!
from kloppy import statsbomb
set_config("coordinate_system", "opta")
dataset = statsbomb.load_open_data()
dataset.metadata.coordinate_system
from kloppy import statsbomb
set_config("coordinate_system", "opta")
dataset = statsbomb.load_open_data()
dataset.metadata.coordinate_system
/Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages/kloppy-3.7.1-py3.10.egg/kloppy/_providers/statsbomb.py:67: UserWarning: You are about to use StatsBomb public data. By using this data, you are agreeing to the user agreement. The user agreement can be found here: https://github.com/statsbomb/open-data/blob/master/LICENSE.pdf warnings.warn(
Out[8]:
OptaCoordinateSystem(normalized=False, length=120, width=80)
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 to original values when python exists the context
In [9]:
Copied!
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')}")
dataset.metadata.coordinate_system
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')}")
dataset.metadata.coordinate_system
Before context: opta Within context: statsbomb After context: opta
/Users/koen/Developer/Projects/PySport/kloppy/.venv/lib/python3.10/site-packages/kloppy-3.7.1-py3.10.egg/kloppy/_providers/statsbomb.py:67: UserWarning: You are about to use StatsBomb public data. By using this data, you are agreeing to the user agreement. The user agreement can be found here: https://github.com/statsbomb/open-data/blob/master/LICENSE.pdf warnings.warn(
Out[9]:
StatsBombCoordinateSystem(normalized=False, length=120, width=80)