diff --git a/kloppy/_providers/secondspectrum.py b/kloppy/_providers/secondspectrum.py index bc6434ed..e1218ddf 100644 --- a/kloppy/_providers/secondspectrum.py +++ b/kloppy/_providers/secondspectrum.py @@ -1,5 +1,4 @@ from typing import Optional -import contextlib from kloppy.domain import TrackingDataset from kloppy.infra.serializers.tracking.secondspectrum import ( diff --git a/kloppy/config.py b/kloppy/config.py index 3d9fe879..cbc0c299 100644 --- a/kloppy/config.py +++ b/kloppy/config.py @@ -1,15 +1,10 @@ import os from contextlib import contextmanager from copy import copy -from typing import Any, Optional, Union +from typing import Any, Optional, TypedDict, Union from kloppy.domain import EventFactory -try: - from typing import TypedDict -except ImportError: - from mypy_extensions import TypedDict - try: from typing import Literal except ImportError: diff --git a/kloppy/datafactory.py b/kloppy/datafactory.py index f57b1450..89784b7a 100644 --- a/kloppy/datafactory.py +++ b/kloppy/datafactory.py @@ -1 +1,3 @@ from ._providers.datafactory import load + +__all__ = ["load"] diff --git a/kloppy/domain/models/common.py b/kloppy/domain/models/common.py index 62cbb3a0..8e90df96 100644 --- a/kloppy/domain/models/common.py +++ b/kloppy/domain/models/common.py @@ -11,6 +11,7 @@ Generic, Iterable, List, + Literal, NewType, Optional, TypeVar, @@ -21,11 +22,6 @@ from ...utils import deprecated, snake_case from .position import PositionType -if sys.version_info >= (3, 8): - from typing import Literal -else: - from typing_extensions import Literal - if sys.version_info >= (3, 11): from typing import Self else: diff --git a/kloppy/domain/models/time.py b/kloppy/domain/models/time.py index b9c601e0..065b678a 100644 --- a/kloppy/domain/models/time.py +++ b/kloppy/domain/models/time.py @@ -4,7 +4,6 @@ Generic, List, Literal, - NamedTuple, Optional, Tuple, TypeVar, diff --git a/kloppy/domain/services/__init__.py b/kloppy/domain/services/__init__.py index d72375cc..bc9b9678 100644 --- a/kloppy/domain/services/__init__.py +++ b/kloppy/domain/services/__init__.py @@ -2,8 +2,8 @@ from kloppy.domain import AttackingDirection, Frame, Ground -from .transformers import DatasetTransformer, DatasetTransformerBuilder from .event_factory import EventFactory, create_event +from .transformers import DatasetTransformer, DatasetTransformerBuilder # NOT YET: from .enrichers import TrackingPossessionEnricher @@ -35,3 +35,12 @@ def attacking_direction_from_frame(frame: Frame) -> AttackingDirection: return AttackingDirection.LTR else: return AttackingDirection.RTL + + +__all__ = [ + "DatasetTransformer", + "DatasetTransformerBuilder", + "EventFactory", + "create_event", + "attacking_direction_from_frame", +] diff --git a/kloppy/domain/services/matchers/pattern/event.py b/kloppy/domain/services/matchers/pattern/event.py index 47a8f4eb..ab081854 100644 --- a/kloppy/domain/services/matchers/pattern/event.py +++ b/kloppy/domain/services/matchers/pattern/event.py @@ -1,18 +1,27 @@ from collections import defaultdict from dataclasses import dataclass from functools import partial -from typing import Callable, Tuple, Dict, List, Iterator +from typing import Callable, Dict, Iterator, List, Tuple from kloppy.domain import ( + CarryEvent, + Event, EventDataset, PassEvent, ShotEvent, - CarryEvent, TakeOnEvent, - Event, ) -from .regexp import * -from .regexp import _make_match, _TrailItem + +from .regexp import ( + Final, + Matcher, + Node, + Out, + RegExp, + Tok, + _make_match, + _TrailItem, +) from .regexp.regexp import _Match diff --git a/kloppy/domain/services/matchers/pattern/regexp/ast.py b/kloppy/domain/services/matchers/pattern/regexp/ast.py index 79d93042..55211bf4 100644 --- a/kloppy/domain/services/matchers/pattern/regexp/ast.py +++ b/kloppy/domain/services/matchers/pattern/regexp/ast.py @@ -1,6 +1,6 @@ from dataclasses import dataclass, field, replace from functools import reduce -from typing import Generic, Text, Union +from typing import Generic, Union from .matchers import Matcher, Out, Tok @@ -37,12 +37,12 @@ def __or__(self, other: "Node"): return Alternation(self, other) - def __getitem__(self, item: Text): + def __getitem__(self, item: str): """ Generates a capture group """ - if not isinstance(item, Text): + if not isinstance(item, str): raise KeyError("Cannot capture with a key other than a string") return Capture(name=item, statement=self) @@ -192,7 +192,7 @@ class Capture(DumbHash, Node): Represents a capture group around the statement """ - name: Text + name: str statement: Node = field(repr=False) def copy(self): diff --git a/kloppy/domain/services/matchers/pattern/regexp/matchers.py b/kloppy/domain/services/matchers/pattern/regexp/matchers.py index 144f5ea2..aba8779b 100644 --- a/kloppy/domain/services/matchers/pattern/regexp/matchers.py +++ b/kloppy/domain/services/matchers/pattern/regexp/matchers.py @@ -8,7 +8,6 @@ Iterator, Mapping, Sequence, - Text, Tuple, TypeVar, Union, @@ -36,7 +35,7 @@ class _TrailItem(Generic[Out]): """ item: Out - data: Dict[Text, Sequence[Text]] + data: Dict[str, Sequence[str]] @property def _comparable(self): @@ -108,7 +107,7 @@ def match( class AttributeHasValue(Matcher): - def __init__(self, attribute: Text, value: Any): + def __init__(self, attribute: str, value: Any): self.attribute = attribute self.value = value @@ -146,7 +145,7 @@ def __repr__(self): class Anything(Matcher): def __repr__(self): - return f"Anything()" + return "Anything()" def match( self, token: Tok, trail: Tuple[_TrailItem[Out], ...] diff --git a/kloppy/domain/services/matchers/pattern/regexp/regexp.py b/kloppy/domain/services/matchers/pattern/regexp/regexp.py index db73daa7..8660e586 100644 --- a/kloppy/domain/services/matchers/pattern/regexp/regexp.py +++ b/kloppy/domain/services/matchers/pattern/regexp/regexp.py @@ -8,7 +8,6 @@ List, Mapping, Sequence, - Text, Tuple, ) @@ -355,7 +354,7 @@ class _Match(Generic[Out]): def __init__(self, start_pos: int): self.start_pos = start_pos - self.children: Dict[Text, List[_Match]] = {} + self.children: Dict[str, List[_Match]] = {} self.trail: List[Out] = [] self._stack: List[Capture] = [] @@ -472,7 +471,7 @@ class Match(Generic[Out]): # Sub-groups that matched. As several groups could be matching, a list # is returned so you can access each one of them. - children: Mapping[Text, List["Match"]] + children: Mapping[str, List["Match"]] # Trail of matched items. It's the output of the matcher, not the input # tokens. @@ -637,7 +636,7 @@ def match( ) def _de_duplicate( - self, stack: Iterator[Explorer[Tok, Out]], key: Text = "signature" + self, stack: Iterator[Explorer[Tok, Out]], key: str = "signature" ) -> Iterator[Explorer[Tok, Out]]: """ As there is potentially several paths that lead to the same result, we diff --git a/kloppy/domain/services/state_builder/__init__.py b/kloppy/domain/services/state_builder/__init__.py index 3fcca8bb..d8073525 100644 --- a/kloppy/domain/services/state_builder/__init__.py +++ b/kloppy/domain/services/state_builder/__init__.py @@ -1,10 +1,10 @@ from dataclasses import replace +from typing import List -from kloppy.domain import List, EventDataset +from kloppy.domain import EventDataset # register all of them -from . import builders as _builders - +from . import builders as _builders # noqa: F401 from .registered import create_state_builder diff --git a/kloppy/domain/services/state_builder/builders/__init__.py b/kloppy/domain/services/state_builder/builders/__init__.py index 48bbbcbc..1e966922 100644 --- a/kloppy/domain/services/state_builder/builders/__init__.py +++ b/kloppy/domain/services/state_builder/builders/__init__.py @@ -1,4 +1,11 @@ +from .formation import FormationStateBuilder from .lineup import LineupStateBuilder from .score import ScoreStateBuilder from .sequence import SequenceStateBuilder -from .formation import FormationStateBuilder + +__all__ = [ + "LineupStateBuilder", + "ScoreStateBuilder", + "SequenceStateBuilder", + "FormationStateBuilder", +] diff --git a/kloppy/domain/services/state_builder/builders/lineup.py b/kloppy/domain/services/state_builder/builders/lineup.py index 785be781..09de207b 100644 --- a/kloppy/domain/services/state_builder/builders/lineup.py +++ b/kloppy/domain/services/state_builder/builders/lineup.py @@ -10,7 +10,6 @@ PlayerOnEvent, CardEvent, CardType, - Provider, ) from ..builder import StateBuilder @@ -24,16 +23,16 @@ class LineupStateBuilder(StateBuilder): def initial_state(self, dataset: EventDataset) -> Lineup: return Lineup( players=( - set( + { player for player in dataset.metadata.teams[0].players if player.starting - ) - | set( + } + | { player for player in dataset.metadata.teams[1].players if player.starting - ) + } ) ) diff --git a/kloppy/domain/services/state_builder/registered.py b/kloppy/domain/services/state_builder/registered.py index c360cae2..78bed3a2 100644 --- a/kloppy/domain/services/state_builder/registered.py +++ b/kloppy/domain/services/state_builder/registered.py @@ -11,9 +11,7 @@ class RegisteredStateBuilder(abc.ABCMeta): def __new__(mcs, cls_name, bases, class_dict): name = camelcase_to_snakecase(cls_name) class_dict["name"] = name - builder_cls = super(RegisteredStateBuilder, mcs).__new__( - mcs, cls_name, bases, class_dict - ) + builder_cls = super().__new__(mcs, cls_name, bases, class_dict) if not inspect.isabstract(builder_cls): _STATE_BUILDER_REGISTRY[ name.replace("_state_builder", "") diff --git a/kloppy/domain/services/transformers/__init__.py b/kloppy/domain/services/transformers/__init__.py index 92dac6b3..18ea0c75 100644 --- a/kloppy/domain/services/transformers/__init__.py +++ b/kloppy/domain/services/transformers/__init__.py @@ -1 +1,3 @@ from .dataset import DatasetTransformer, DatasetTransformerBuilder + +__all__ = ["DatasetTransformer", "DatasetTransformerBuilder"] diff --git a/kloppy/domain/services/transformers/attribute.py b/kloppy/domain/services/transformers/attribute.py index 8bdff050..05353ba7 100644 --- a/kloppy/domain/services/transformers/attribute.py +++ b/kloppy/domain/services/transformers/attribute.py @@ -155,7 +155,7 @@ def __init__( ): if include and exclude: raise KloppyParameterError( - f"Cannot specify both include as exclude" + "Cannot specify both include as exclude" ) self.exclude = exclude or [] @@ -250,7 +250,7 @@ def __init__( ): if include and exclude: raise KloppyParameterError( - f"Cannot specify both include as exclude" + "Cannot specify both include as exclude" ) self.exclude = exclude or [] @@ -322,7 +322,7 @@ def __init__( ): if include and exclude: raise KloppyParameterError( - f"Cannot specify both include as exclude" + "Cannot specify both include as exclude" ) self.exclude = exclude or [] diff --git a/kloppy/domain/services/transformers/data_record.py b/kloppy/domain/services/transformers/data_record.py index 8eb8f503..bb2ee5f4 100644 --- a/kloppy/domain/services/transformers/data_record.py +++ b/kloppy/domain/services/transformers/data_record.py @@ -44,7 +44,7 @@ def converter(data_record: T) -> Dict[str, Any]: res = column(data_record) if not isinstance(res, dict): raise KloppyError( - f"A function column should return a dictionary" + "A function column should return a dictionary" ) row.update(res) else: diff --git a/kloppy/infra/io/adapters/http.py b/kloppy/infra/io/adapters/http.py index 89096cea..21ff0a37 100644 --- a/kloppy/infra/io/adapters/http.py +++ b/kloppy/infra/io/adapters/http.py @@ -2,10 +2,11 @@ from kloppy.config import get_config from kloppy.exceptions import AdapterError, InputNotFoundError + from .adapter import Adapter try: - from js import XMLHttpRequest + from js import XMLHttpRequest # noqa: F401 RUNS_IN_BROWSER = True except ImportError: diff --git a/kloppy/infra/serializers/code/base.py b/kloppy/infra/serializers/code/base.py index 869837e1..7c38d951 100644 --- a/kloppy/infra/serializers/code/base.py +++ b/kloppy/infra/serializers/code/base.py @@ -1,7 +1,6 @@ from abc import ABC, abstractmethod -from typing import Tuple, Dict, Generic, TypeVar +from typing import Generic, TypeVar -from kloppy.utils import Readable from kloppy.domain import CodeDataset diff --git a/kloppy/infra/serializers/event/datafactory/__init__.py b/kloppy/infra/serializers/event/datafactory/__init__.py index eb37f812..18b2684f 100644 --- a/kloppy/infra/serializers/event/datafactory/__init__.py +++ b/kloppy/infra/serializers/event/datafactory/__init__.py @@ -1 +1,6 @@ from .deserializer import DatafactoryDeserializer, DatafactoryInputs + +__all__ = [ + "DatafactoryDeserializer", + "DatafactoryInputs", +] diff --git a/kloppy/infra/serializers/event/datafactory/deserializer.py b/kloppy/infra/serializers/event/datafactory/deserializer.py index 44f5df20..1a871106 100644 --- a/kloppy/infra/serializers/event/datafactory/deserializer.py +++ b/kloppy/infra/serializers/event/datafactory/deserializer.py @@ -2,43 +2,34 @@ import logging from dataclasses import replace from datetime import datetime, timedelta, timezone -from typing import IO, Dict, List, NamedTuple, Tuple, Union +from typing import IO, Dict, List, NamedTuple, Tuple from kloppy.domain import ( - AttackingDirection, - BallOutEvent, BallState, BodyPart, BodyPartQualifier, - CardEvent, CardType, DatasetFlag, Event, EventDataset, - FoulCommittedEvent, - GenericEvent, Ground, Metadata, Orientation, - PassEvent, PassResult, Period, Player, Point, Provider, Qualifier, - RecoveryEvent, Score, SetPieceQualifier, SetPieceType, - ShotEvent, ShotResult, - SubstitutionEvent, Team, ) from kloppy.exceptions import DeserializationError from kloppy.infra.serializers.event.deserializer import EventDataDeserializer -from kloppy.utils import Readable, performance_logging +from kloppy.utils import performance_logging logger = logging.getLogger(__name__) diff --git a/kloppy/infra/serializers/event/metrica/__init__.py b/kloppy/infra/serializers/event/metrica/__init__.py index 9951a309..caede0e8 100644 --- a/kloppy/infra/serializers/event/metrica/__init__.py +++ b/kloppy/infra/serializers/event/metrica/__init__.py @@ -2,3 +2,8 @@ MetricaJsonEventDataDeserializer, MetricaJsonEventDataInputs, ) + +__all__ = [ + "MetricaJsonEventDataDeserializer", + "MetricaJsonEventDataInputs", +] diff --git a/kloppy/infra/serializers/event/metrica/json_deserializer.py b/kloppy/infra/serializers/event/metrica/json_deserializer.py index 04a54050..b006d43e 100644 --- a/kloppy/infra/serializers/event/metrica/json_deserializer.py +++ b/kloppy/infra/serializers/event/metrica/json_deserializer.py @@ -1,8 +1,8 @@ -import logging import json +import logging from dataclasses import replace from datetime import timedelta -from typing import Dict, List, NamedTuple, IO, Optional +from typing import IO, Dict, List, NamedTuple, Optional from kloppy.domain import ( BallState, @@ -21,11 +21,10 @@ TakeOnResult, Team, ) +from kloppy.exceptions import DeserializationError from kloppy.infra.serializers.event.deserializer import EventDataDeserializer - from kloppy.infra.serializers.tracking.metrica_epts.metadata import ( load_metadata, - DeserializationError, ) from kloppy.utils import performance_logging diff --git a/kloppy/infra/serializers/event/sportec/__init__.py b/kloppy/infra/serializers/event/sportec/__init__.py index 1d3665c1..ef42d6eb 100644 --- a/kloppy/infra/serializers/event/sportec/__init__.py +++ b/kloppy/infra/serializers/event/sportec/__init__.py @@ -1 +1,6 @@ from .deserializer import SportecEventDataDeserializer, SportecEventDataInputs + +__all__ = [ + "SportecEventDataDeserializer", + "SportecEventDataInputs", +] diff --git a/kloppy/infra/serializers/event/sportec/deserializer.py b/kloppy/infra/serializers/event/sportec/deserializer.py index b240db49..b250d91e 100644 --- a/kloppy/infra/serializers/event/sportec/deserializer.py +++ b/kloppy/infra/serializers/event/sportec/deserializer.py @@ -1,6 +1,6 @@ from collections import OrderedDict from typing import Dict, List, NamedTuple, IO -from datetime import timedelta, datetime, timezone +from datetime import timedelta, datetime import logging from lxml import objectify @@ -26,7 +26,6 @@ BodyPart, Qualifier, CardType, - AttackingDirection, PositionType, Official, OfficialType, diff --git a/kloppy/infra/serializers/event/statsbomb/__init__.py b/kloppy/infra/serializers/event/statsbomb/__init__.py index 45e2f404..73737f7a 100644 --- a/kloppy/infra/serializers/event/statsbomb/__init__.py +++ b/kloppy/infra/serializers/event/statsbomb/__init__.py @@ -1,2 +1,8 @@ """Convert StatsBomb event stream data to a kloppy EventDataset.""" + from .deserializer import StatsBombDeserializer, StatsBombInputs + +__all__ = [ + "StatsBombDeserializer", + "StatsBombInputs", +] diff --git a/kloppy/infra/serializers/event/statsbomb/helpers.py b/kloppy/infra/serializers/event/statsbomb/helpers.py index 6671aa1a..521ce2ec 100644 --- a/kloppy/infra/serializers/event/statsbomb/helpers.py +++ b/kloppy/infra/serializers/event/statsbomb/helpers.py @@ -11,7 +11,6 @@ Point, Point3D, PositionType, - Provider, Team, ) from kloppy.domain.services.frame_factory import create_frame diff --git a/kloppy/infra/serializers/event/statsperform/parsers/base.py b/kloppy/infra/serializers/event/statsperform/parsers/base.py index 2a7ca7cc..b9c5aaa4 100644 --- a/kloppy/infra/serializers/event/statsperform/parsers/base.py +++ b/kloppy/infra/serializers/event/statsperform/parsers/base.py @@ -3,6 +3,7 @@ A parser reads a single data file and should extend the 'OptaParser' class to extract data about players, teams and events that is encoded in the file. """ + import json from typing import Tuple, List, Optional, IO, Dict diff --git a/kloppy/infra/serializers/event/statsperform/parsers/f24_xml.py b/kloppy/infra/serializers/event/statsperform/parsers/f24_xml.py index 282fcac2..5f67f909 100644 --- a/kloppy/infra/serializers/event/statsperform/parsers/f24_xml.py +++ b/kloppy/infra/serializers/event/statsperform/parsers/f24_xml.py @@ -13,7 +13,7 @@ def zero_pad_milliseconds(timestamp): parts = timestamp.split(".") if len(parts) == 1: return timestamp + ".000" - return ".".join(parts[:-1] + ["{:03d}".format(int(parts[-1]))]) + return ".".join(parts[:-1] + [f"{int(parts[-1]):03d}"]) dt_str = zero_pad_milliseconds(dt_str) naive_datetime = datetime.strptime(dt_str, "%Y-%m-%dT%H:%M:%S.%f") diff --git a/kloppy/infra/serializers/event/statsperform/parsers/f7_xml.py b/kloppy/infra/serializers/event/statsperform/parsers/f7_xml.py index 20d66399..10f166c8 100644 --- a/kloppy/infra/serializers/event/statsperform/parsers/f7_xml.py +++ b/kloppy/infra/serializers/event/statsperform/parsers/f7_xml.py @@ -13,10 +13,9 @@ Score, Team, ) -from kloppy.domain.models import PositionType from kloppy.exceptions import DeserializationError -from .base import OptaXMLParser, position_types_mapping +from .base import OptaXMLParser from ..formation_mapping import ( formation_position_mapping, formation_name_mapping, diff --git a/kloppy/infra/serializers/event/statsperform/parsers/ma1_xml.py b/kloppy/infra/serializers/event/statsperform/parsers/ma1_xml.py index 92058877..3710ed67 100644 --- a/kloppy/infra/serializers/event/statsperform/parsers/ma1_xml.py +++ b/kloppy/infra/serializers/event/statsperform/parsers/ma1_xml.py @@ -1,4 +1,5 @@ """XML parser for Stats Perform MA1 feeds.""" + from datetime import datetime, timezone from typing import Any, Optional, List, Dict, Tuple diff --git a/kloppy/infra/serializers/event/statsperform/parsers/ma3_json.py b/kloppy/infra/serializers/event/statsperform/parsers/ma3_json.py index a91cc148..b2ecb3cd 100644 --- a/kloppy/infra/serializers/event/statsperform/parsers/ma3_json.py +++ b/kloppy/infra/serializers/event/statsperform/parsers/ma3_json.py @@ -1,4 +1,5 @@ """JSON parser for Stats Perform MA3 feeds.""" + from datetime import datetime, timezone from typing import List diff --git a/kloppy/infra/serializers/event/statsperform/parsers/ma3_xml.py b/kloppy/infra/serializers/event/statsperform/parsers/ma3_xml.py index 823f8313..dc5d04fa 100644 --- a/kloppy/infra/serializers/event/statsperform/parsers/ma3_xml.py +++ b/kloppy/infra/serializers/event/statsperform/parsers/ma3_xml.py @@ -1,4 +1,5 @@ """XML parser for Stats Perform MA3 feeds.""" + from datetime import datetime, timezone from typing import List diff --git a/kloppy/infra/serializers/event/wyscout/__init__.py b/kloppy/infra/serializers/event/wyscout/__init__.py index 24041d28..25692ebe 100644 --- a/kloppy/infra/serializers/event/wyscout/__init__.py +++ b/kloppy/infra/serializers/event/wyscout/__init__.py @@ -1,2 +1,8 @@ -from .deserializer_v3 import WyscoutDeserializerV3, WyscoutInputs from .deserializer_v2 import WyscoutDeserializerV2 +from .deserializer_v3 import WyscoutDeserializerV3, WyscoutInputs + +__all__ = [ + "WyscoutDeserializerV2", + "WyscoutDeserializerV3", + "WyscoutInputs", +] diff --git a/kloppy/infra/serializers/event/wyscout/deserializer_v2.py b/kloppy/infra/serializers/event/wyscout/deserializer_v2.py index 896b663d..245fc2cc 100644 --- a/kloppy/infra/serializers/event/wyscout/deserializer_v2.py +++ b/kloppy/infra/serializers/event/wyscout/deserializer_v2.py @@ -2,7 +2,7 @@ import logging from dataclasses import replace from datetime import timedelta -from typing import Dict, List, Tuple, NamedTuple, IO, Optional +from typing import Dict, List, NamedTuple, IO, Optional from kloppy.domain import ( BodyPart, @@ -177,13 +177,11 @@ def _parse_shot(raw_event: Dict, next_event: Dict) -> Dict: result = ShotResult.GOAL elif _has_tag(raw_event, 2101): result = ShotResult.BLOCKED - elif any((_has_tag(raw_event, tag) for tag in wyscout_tags.SHOT_POST)): + elif any(_has_tag(raw_event, tag) for tag in wyscout_tags.SHOT_POST): result = ShotResult.POST - elif any( - (_has_tag(raw_event, tag) for tag in wyscout_tags.SHOT_OFF_TARGET) - ): + elif any(_has_tag(raw_event, tag) for tag in wyscout_tags.SHOT_OFF_TARGET): result = ShotResult.OFF_TARGET - elif any((_has_tag(raw_event, tag) for tag in wyscout_tags.SHOT_ON_GOAL)): + elif any(_has_tag(raw_event, tag) for tag in wyscout_tags.SHOT_ON_GOAL): result = ShotResult.SAVED if next_event["eventId"] == wyscout_events.SAVE.EVENT: @@ -493,12 +491,10 @@ def deserialize(self, inputs: WyscoutInputs) -> EventDataset: home_team = _parse_team(raw_events, home_team_id, Ground.HOME) away_team = _parse_team(raw_events, away_team_id, Ground.AWAY) teams = {home_team_id: home_team, away_team_id: away_team} - players = dict( - [ - (wyId, _players_to_dict(team.players)) - for wyId, team in teams.items() - ] - ) + players = { + wyId: _players_to_dict(team.players) + for wyId, team in teams.items() + } game_id = raw_events["events"][0].get("matchId", None) if game_id: game_id = str(game_id) @@ -572,7 +568,7 @@ def deserialize(self, inputs: WyscoutInputs) -> EventDataset: ) new_events.append(foul_event) if any( - (_has_tag(raw_event, tag) for tag in wyscout_tags.CARD) + _has_tag(raw_event, tag) for tag in wyscout_tags.CARD ): card_event_args = _parse_card(raw_event) card_event_id = ( diff --git a/kloppy/infra/serializers/event/wyscout/deserializer_v3.py b/kloppy/infra/serializers/event/wyscout/deserializer_v3.py index 637c94c9..cf7ccd0c 100644 --- a/kloppy/infra/serializers/event/wyscout/deserializer_v3.py +++ b/kloppy/infra/serializers/event/wyscout/deserializer_v3.py @@ -10,13 +10,11 @@ BodyPart, BodyPartQualifier, CardType, - CarryResult, CounterAttackQualifier, DuelQualifier, DuelResult, DuelType, EventDataset, - FormationType, GoalkeeperActionType, GoalkeeperQualifier, Ground, @@ -714,12 +712,10 @@ def deserialize(self, inputs: WyscoutInputs) -> EventDataset: home_team = _parse_team(raw_events, home_team_id, Ground.HOME) away_team = _parse_team(raw_events, away_team_id, Ground.AWAY) teams = {home_team_id: home_team, away_team_id: away_team} - players = dict( - [ - (wyId, _players_to_dict(team.players)) - for wyId, team in teams.items() - ] - ) + players = { + wyId: _players_to_dict(team.players) + for wyId, team in teams.items() + } date = raw_events["match"].get("dateutc") if date: date = datetime.strptime(date, "%Y-%m-%d %H:%M:%S").replace( diff --git a/kloppy/infra/serializers/event/wyscout/wyscout_events.py b/kloppy/infra/serializers/event/wyscout/wyscout_events.py index 02154718..d97aac1b 100644 --- a/kloppy/infra/serializers/event/wyscout/wyscout_events.py +++ b/kloppy/infra/serializers/event/wyscout/wyscout_events.py @@ -1,6 +1,3 @@ -from typing import List - - class DUEL: EVENT = 1 diff --git a/kloppy/infra/serializers/tracking/metrica_csv.py b/kloppy/infra/serializers/tracking/metrica_csv.py index 629f4962..2ec5acd9 100644 --- a/kloppy/infra/serializers/tracking/metrica_csv.py +++ b/kloppy/infra/serializers/tracking/metrica_csv.py @@ -2,13 +2,12 @@ import warnings from collections import namedtuple from datetime import timedelta -from typing import Tuple, Dict, Iterator, IO, NamedTuple +from typing import Iterator, IO, NamedTuple from kloppy.domain import ( attacking_direction_from_frame, TrackingDataset, AttackingDirection, - Frame, Point, Period, Orientation, @@ -24,7 +23,7 @@ from kloppy.infra.serializers.tracking.deserializer import ( TrackingDataDeserializer, ) -from kloppy.utils import Readable, performance_logging +from kloppy.utils import performance_logging logger = logging.getLogger(__name__) diff --git a/kloppy/infra/serializers/tracking/metrica_epts/__init__.py b/kloppy/infra/serializers/tracking/metrica_epts/__init__.py index 9064c36f..d66d36aa 100644 --- a/kloppy/infra/serializers/tracking/metrica_epts/__init__.py +++ b/kloppy/infra/serializers/tracking/metrica_epts/__init__.py @@ -2,3 +2,8 @@ MetricaEPTSTrackingDataDeserializer, MetricaEPTSTrackingDataInputs, ) + +__all__ = [ + "MetricaEPTSTrackingDataDeserializer", + "MetricaEPTSTrackingDataInputs", +] diff --git a/kloppy/infra/serializers/tracking/metrica_epts/metadata.py b/kloppy/infra/serializers/tracking/metrica_epts/metadata.py index c28414a9..e90391ea 100644 --- a/kloppy/infra/serializers/tracking/metrica_epts/metadata.py +++ b/kloppy/infra/serializers/tracking/metrica_epts/metadata.py @@ -1,26 +1,32 @@ -from typing import IO +import warnings from datetime import timedelta +from typing import IO, Dict, List, Optional, Tuple, Union from lxml import objectify -import warnings from kloppy.domain import ( - Period, - PitchDimensions, - NormalizedPitchDimensions, + AttackingDirection, + DatasetFlag, Dimension, - Score, Ground, - DatasetFlag, - AttackingDirection, + NormalizedPitchDimensions, Orientation, - Point, + Period, + PitchDimensions, + Player, PositionType, Provider, + Score, + Team, build_coordinate_system, ) -from .models import * +from .models import ( + DataFormatSpecification, + EPTSMetadata, + PlayerChannel, + Sensor, +) position_types_mapping: Dict[int, PositionType] = { -1: PositionType.Unknown, @@ -48,7 +54,7 @@ def _load_provider_parameters(parent_elm, value_mapper=None) -> Dict: def _load_periods( metadata_elm, team_map: dict, frame_rate: int -) -> List[Period]: +) -> Tuple[List[Period], AttackingDirection]: global_config_elm = metadata_elm.find("GlobalConfig") provider_params = _load_provider_parameters( global_config_elm.find("ProviderGlobalParameters") @@ -189,7 +195,7 @@ def _load_pitch_dimensions( return None -def _parse_provider(provider_name: Union[str, None]) -> Provider: +def _parse_provider(provider_name: Union[str, None]) -> Optional[Provider]: if provider_name: if provider_name == "Metrica Sports": return Provider.METRICA @@ -202,14 +208,16 @@ def _parse_provider(provider_name: Union[str, None]) -> Provider: return None -def _load_provider(metadata_elm, provider: Provider = None) -> Provider: +def _load_provider( + metadata_elm, provider: Optional[Provider] = None +) -> Optional[Provider]: provider_path = objectify.ObjectPath("Metadata.GlobalConfig.ProviderName") provider_name = provider_path.find(metadata_elm) provider_from_file = _parse_provider(provider_name) if provider: if provider_from_file and provider_from_file != provider: warnings.warn( - f"Given provider name is different to the name of the Provider read from the XML-file", + "Given provider name is different to the name of the Provider read from the XML-file", Warning, ) else: @@ -218,7 +226,7 @@ def _load_provider(metadata_elm, provider: Provider = None) -> Provider: def load_metadata( - metadata_file: IO[bytes], provider: Provider = None + metadata_file: IO[bytes], provider: Optional[Provider] = None ) -> EPTSMetadata: root = objectify.fromstring(metadata_file.read()) metadata = root.find("Metadata") @@ -274,9 +282,7 @@ def load_metadata( } _all_players = [ - player - for key, value in teams_metadata.items() - for player in value.players + player for value in teams_metadata.values() for player in value.players ] _player_map = {player.player_id: player for player in _all_players} diff --git a/kloppy/infra/serializers/tracking/metrica_epts/models.py b/kloppy/infra/serializers/tracking/metrica_epts/models.py index a35f670b..25de4481 100644 --- a/kloppy/infra/serializers/tracking/metrica_epts/models.py +++ b/kloppy/infra/serializers/tracking/metrica_epts/models.py @@ -1,7 +1,7 @@ from dataclasses import dataclass, field from typing import List, Dict, Union -from kloppy.domain import Team, Player, Metadata +from kloppy.domain import Player, Metadata # TODO: fill this with from SplitRegisters diff --git a/kloppy/infra/serializers/tracking/metrica_epts/reader.py b/kloppy/infra/serializers/tracking/metrica_epts/reader.py index 628a5b52..6f26e3c8 100644 --- a/kloppy/infra/serializers/tracking/metrica_epts/reader.py +++ b/kloppy/infra/serializers/tracking/metrica_epts/reader.py @@ -1,14 +1,12 @@ import re -from typing import List, Tuple, Set, Iterator, IO +from typing import List, Iterator, IO from datetime import timedelta -from kloppy.utils import Readable from .models import ( PlayerChannel, DataFormatSpecification, EPTSMetadata, - Channel, Sensor, ) diff --git a/kloppy/infra/serializers/tracking/secondspectrum.py b/kloppy/infra/serializers/tracking/secondspectrum.py index 94b0f4dc..3cd2b439 100644 --- a/kloppy/infra/serializers/tracking/secondspectrum.py +++ b/kloppy/infra/serializers/tracking/secondspectrum.py @@ -10,7 +10,6 @@ TrackingDataset, DatasetFlag, AttackingDirection, - Frame, Point, Point3D, Team, diff --git a/kloppy/infra/serializers/tracking/skillcorner.py b/kloppy/infra/serializers/tracking/skillcorner.py index acce0966..cbcf4e6f 100644 --- a/kloppy/infra/serializers/tracking/skillcorner.py +++ b/kloppy/infra/serializers/tracking/skillcorner.py @@ -9,7 +9,6 @@ from kloppy.domain import ( AttackingDirection, DatasetFlag, - Frame, Ground, Metadata, Orientation, diff --git a/kloppy/infra/serializers/tracking/sportec/__init__.py b/kloppy/infra/serializers/tracking/sportec/__init__.py index 245f9471..5ef31a90 100644 --- a/kloppy/infra/serializers/tracking/sportec/__init__.py +++ b/kloppy/infra/serializers/tracking/sportec/__init__.py @@ -2,3 +2,8 @@ SportecTrackingDataDeserializer, SportecTrackingDataInputs, ) + +__all__ = [ + "SportecTrackingDataDeserializer", + "SportecTrackingDataInputs", +] diff --git a/kloppy/infra/serializers/tracking/sportec/deserializer.py b/kloppy/infra/serializers/tracking/sportec/deserializer.py index b4e50906..bb726705 100644 --- a/kloppy/infra/serializers/tracking/sportec/deserializer.py +++ b/kloppy/infra/serializers/tracking/sportec/deserializer.py @@ -10,7 +10,6 @@ TrackingDataset, DatasetFlag, AttackingDirection, - Frame, Point, Point3D, BallState, @@ -163,7 +162,6 @@ def _iter(): for i, (frame_id, frame_data) in enumerate( sorted(raw_frames.items()) ): - if "ball" not in frame_data: # Frames without ball data are corrupt. continue diff --git a/kloppy/infra/serializers/tracking/statsperform.py b/kloppy/infra/serializers/tracking/statsperform.py index 21a5a4c3..08665a24 100644 --- a/kloppy/infra/serializers/tracking/statsperform.py +++ b/kloppy/infra/serializers/tracking/statsperform.py @@ -8,7 +8,6 @@ AttackingDirection, BallState, DatasetFlag, - Frame, Metadata, Orientation, Player, diff --git a/kloppy/infra/serializers/tracking/tracab/__init__.py b/kloppy/infra/serializers/tracking/tracab/__init__.py index ea341638..2d339353 100644 --- a/kloppy/infra/serializers/tracking/tracab/__init__.py +++ b/kloppy/infra/serializers/tracking/tracab/__init__.py @@ -1,3 +1,9 @@ from .common import TRACABInputs from .tracab_dat import TRACABDatDeserializer from .tracab_json import TRACABJSONDeserializer + +__all__ = [ + "TRACABDatDeserializer", + "TRACABJSONDeserializer", + "TRACABInputs", +] diff --git a/kloppy/infra/serializers/tracking/tracab/tracab_dat.py b/kloppy/infra/serializers/tracking/tracab/tracab_dat.py index 72ddbdf6..6ef20676 100644 --- a/kloppy/infra/serializers/tracking/tracab/tracab_dat.py +++ b/kloppy/infra/serializers/tracking/tracab/tracab_dat.py @@ -10,7 +10,6 @@ TrackingDataset, DatasetFlag, AttackingDirection, - Frame, Point, Point3D, Team, diff --git a/kloppy/infra/serializers/tracking/tracab/tracab_json.py b/kloppy/infra/serializers/tracking/tracab/tracab_json.py index 921e4df5..cd4ea085 100644 --- a/kloppy/infra/serializers/tracking/tracab/tracab_json.py +++ b/kloppy/infra/serializers/tracking/tracab/tracab_json.py @@ -9,7 +9,6 @@ TrackingDataset, DatasetFlag, AttackingDirection, - Frame, Point, Point3D, Team, diff --git a/kloppy/io.py b/kloppy/io.py index 0726f99c..d222126e 100644 --- a/kloppy/io.py +++ b/kloppy/io.py @@ -202,7 +202,7 @@ def _open( "raw", no compression is used. """ if mode not in ("rb", "wb", "ab"): - raise ValueError("Mode '{}' not supported".format(mode)) + raise ValueError(f"Mode '{mode}' not supported") filepath = _filepath_from_path_or_filelike(filename) if format not in (None, "gz", "xz", "bz2", "raw"): diff --git a/kloppy/metrica.py b/kloppy/metrica.py index 7e1b6de7..4114fdb3 100644 --- a/kloppy/metrica.py +++ b/kloppy/metrica.py @@ -1,6 +1,13 @@ from ._providers.metrica import ( load_event, + load_open_data, load_tracking_csv, load_tracking_epts, - load_open_data, ) + +__all__ = [ + "load_event", + "load_tracking_csv", + "load_tracking_epts", + "load_open_data", +] diff --git a/kloppy/opta.py b/kloppy/opta.py index f7fb1a05..f361e253 100644 --- a/kloppy/opta.py +++ b/kloppy/opta.py @@ -1 +1,3 @@ from ._providers.opta import load + +__all__ = ["load"] diff --git a/kloppy/secondspectrum.py b/kloppy/secondspectrum.py index 12f94e62..915203bd 100644 --- a/kloppy/secondspectrum.py +++ b/kloppy/secondspectrum.py @@ -1 +1,3 @@ from ._providers.secondspectrum import load + +__all__ = ["load"] diff --git a/kloppy/skillcorner.py b/kloppy/skillcorner.py index c1268d82..ee096f00 100644 --- a/kloppy/skillcorner.py +++ b/kloppy/skillcorner.py @@ -1 +1,3 @@ from ._providers.skillcorner import load, load_open_data + +__all__ = ["load", "load_open_data"] diff --git a/kloppy/sportec.py b/kloppy/sportec.py index 848416dd..012d3889 100644 --- a/kloppy/sportec.py +++ b/kloppy/sportec.py @@ -1,7 +1,15 @@ from ._providers.sportec import ( load, load_event, - load_tracking, load_open_event_data, load_open_tracking_data, + load_tracking, ) + +__all__ = [ + "load", + "load_event", + "load_tracking", + "load_open_event_data", + "load_open_tracking_data", +] diff --git a/kloppy/sportscode.py b/kloppy/sportscode.py index ac73fdb9..ff464d76 100644 --- a/kloppy/sportscode.py +++ b/kloppy/sportscode.py @@ -1 +1,3 @@ from ._providers.sportscode import load, save + +__all__ = ["load", "save"] diff --git a/kloppy/statsbomb.py b/kloppy/statsbomb.py index 704011db..10fe2250 100644 --- a/kloppy/statsbomb.py +++ b/kloppy/statsbomb.py @@ -1 +1,3 @@ from ._providers.statsbomb import load, load_open_data + +__all__ = ["load", "load_open_data"] diff --git a/kloppy/tests/conftest.py b/kloppy/tests/conftest.py index d79ee688..a0b6dffe 100644 --- a/kloppy/tests/conftest.py +++ b/kloppy/tests/conftest.py @@ -1,4 +1,4 @@ -"""Module to store common fixtures. """ +"""Module to store common fixtures.""" from pathlib import Path diff --git a/kloppy/tests/test_datafactory.py b/kloppy/tests/test_datafactory.py index 7354356f..7f209404 100644 --- a/kloppy/tests/test_datafactory.py +++ b/kloppy/tests/test_datafactory.py @@ -1,21 +1,17 @@ -from datetime import timedelta, datetime, timezone +from datetime import datetime, timedelta, timezone import pytest +from kloppy import datafactory from kloppy.domain import ( - AttackingDirection, + DatasetType, Ground, Orientation, - Period, Point, Provider, SetPieceType, - DatasetType, - PositionType, ) -from kloppy import datafactory - class TestDatafactory: @pytest.fixture @@ -43,7 +39,7 @@ def test_correct_deserialization(self, event_data: str): assert player.player_id == "38804" assert player.jersey_no == 1 assert str(player) == "Daniel Bold" - assert player.starting_position == None + assert player.starting_position is None assert player.starting assert dataset.metadata.periods[0].id == 1 diff --git a/kloppy/tests/test_helpers.py b/kloppy/tests/test_helpers.py index 83d6031f..97697227 100644 --- a/kloppy/tests/test_helpers.py +++ b/kloppy/tests/test_helpers.py @@ -10,7 +10,6 @@ AttackingDirection, DatasetFlag, Dimension, - Frame, Ground, Metadata, MetricaCoordinateSystem, diff --git a/kloppy/tests/test_metrica_csv.py b/kloppy/tests/test_metrica_csv.py index 48f66e04..95a9d0ca 100644 --- a/kloppy/tests/test_metrica_csv.py +++ b/kloppy/tests/test_metrica_csv.py @@ -3,9 +3,7 @@ import pytest from kloppy.domain import ( - Period, Provider, - AttackingDirection, Orientation, Point, DatasetType, diff --git a/kloppy/tests/test_metrica_epts.py b/kloppy/tests/test_metrica_epts.py index 028a8af8..e2f47417 100644 --- a/kloppy/tests/test_metrica_epts.py +++ b/kloppy/tests/test_metrica_epts.py @@ -1,21 +1,15 @@ import re -from datetime import datetime, timedelta +from datetime import timedelta import pytest -from pandas import DataFrame from lxml import objectify +from pandas import DataFrame -from kloppy.domain import ( - Orientation, - Point, - Point3D, - Provider, -) -from kloppy.infra.serializers.tracking.metrica_epts.metadata import ( - load_metadata, -) +from kloppy import metrica +from kloppy.domain import Orientation, Point, Provider, Score from kloppy.infra.serializers.tracking.metrica_epts.metadata import ( _load_provider, + load_metadata, ) from kloppy.infra.serializers.tracking.metrica_epts.reader import ( build_regex, @@ -24,9 +18,6 @@ from kloppy.utils import performance_logging -from kloppy import metrica - - class TestMetricaEPTSTracking: def test_regex(self, base_dir): with open( @@ -164,7 +155,8 @@ def test_read_with_sensor_unused_in_players_and_frame_count_name_modified( self, base_dir ): with open( - base_dir / "files/epts_metrica_metadata_unused_sensor.xml", "rb" + base_dir / "files/epts_metrica_metadata_unused_sensor.xml", + "rb", ) as metadata_fp, open( base_dir / "files/epts_metrica_tracking.txt", "rb" ) as raw_data: @@ -211,3 +203,4 @@ def test_read_metadata_withou_score_field(self, base_dir): base_dir / "files/epts_metrica_metadata_without_score.xml", "rb" ) as metadata_fp: metadata = load_metadata(metadata_fp) + assert metadata.score == Score(home=0, away=0) diff --git a/kloppy/tests/test_metrica_events.py b/kloppy/tests/test_metrica_events.py index 64046f16..4a0b729f 100644 --- a/kloppy/tests/test_metrica_events.py +++ b/kloppy/tests/test_metrica_events.py @@ -5,9 +5,7 @@ from kloppy import metrica from kloppy.domain import ( Orientation, - Period, Provider, - AttackingDirection, SetPieceType, BodyPart, EventDataset, diff --git a/kloppy/tests/test_opta.py b/kloppy/tests/test_opta.py index 7cb974f7..f12113af 100644 --- a/kloppy/tests/test_opta.py +++ b/kloppy/tests/test_opta.py @@ -7,7 +7,6 @@ BallState, BodyPart, BodyPartQualifier, - BodyPartQualifier, CardQualifier, CardType, CounterAttackQualifier, @@ -25,7 +24,6 @@ PassType, OptaPitchDimensions, Point, - Point, Point3D, PositionType, Provider, diff --git a/kloppy/tests/test_sportec.py b/kloppy/tests/test_sportec.py index ac8ad2de..cd57f458 100644 --- a/kloppy/tests/test_sportec.py +++ b/kloppy/tests/test_sportec.py @@ -4,9 +4,7 @@ import pytest from kloppy.domain import ( - Period, Provider, - AttackingDirection, Orientation, Point, SetPieceType, diff --git a/kloppy/tests/test_state_builder.py b/kloppy/tests/test_state_builder.py index 5976cb18..d27f6190 100644 --- a/kloppy/tests/test_state_builder.py +++ b/kloppy/tests/test_state_builder.py @@ -1,7 +1,7 @@ from itertools import groupby from kloppy.domain import EventType, Event, EventDataset, FormationType -from kloppy.domain.services.state_builder.builder import StateBuilder, T +from kloppy.domain.services.state_builder.builder import StateBuilder from kloppy.utils import performance_logging from kloppy import statsbomb diff --git a/kloppy/tests/test_statsperform.py b/kloppy/tests/test_statsperform.py index 9e8e9070..3079701c 100644 --- a/kloppy/tests/test_statsperform.py +++ b/kloppy/tests/test_statsperform.py @@ -13,10 +13,9 @@ Point3D, Provider, SportVUCoordinateSystem, - TrackingDataset, Time, + TrackingDataset, ) -from kloppy.exceptions import KloppyError @pytest.fixture(scope="module") @@ -228,9 +227,9 @@ def test_coordinate_system_without_pitch_dimensions( pitch_width=None, ) assert pitch_dimensions.x_dim.min == 0 - assert pitch_dimensions.x_dim.max == None + assert pitch_dimensions.x_dim.max is None assert pitch_dimensions.y_dim.min == 0 - assert pitch_dimensions.y_dim.max == None + assert pitch_dimensions.y_dim.max is None def test_coordinate_system_with_pitch_dimensions( self, tracking_data: Path, tracking_metadata_xml: Path diff --git a/kloppy/tests/test_tracab.py b/kloppy/tests/test_tracab.py index 87853a3e..1fd1f122 100644 --- a/kloppy/tests/test_tracab.py +++ b/kloppy/tests/test_tracab.py @@ -9,15 +9,11 @@ TRACABDatDeserializer, ) from kloppy.domain import ( - Period, - AttackingDirection, Orientation, Provider, Point, Point3D, BallState, - Team, - Ground, DatasetType, ) diff --git a/kloppy/tests/test_xml.py b/kloppy/tests/test_xml.py index 5e8e0182..bd663285 100644 --- a/kloppy/tests/test_xml.py +++ b/kloppy/tests/test_xml.py @@ -1,4 +1,3 @@ -import os from datetime import timedelta from pandas import DataFrame diff --git a/kloppy/tracab.py b/kloppy/tracab.py index 660a88db..1c9a3c8b 100644 --- a/kloppy/tracab.py +++ b/kloppy/tracab.py @@ -1 +1,3 @@ from ._providers.tracab import load + +__all__ = ["load"] diff --git a/kloppy/utils.py b/kloppy/utils.py index 68d36af2..f19c46f6 100644 --- a/kloppy/utils.py +++ b/kloppy/utils.py @@ -85,7 +85,7 @@ def inherit(obj): return inherit -string_types = (type(b""), type("")) +string_types = (bytes, str) def deprecated(reason): diff --git a/kloppy/wyscout.py b/kloppy/wyscout.py index c6b169d6..0d0cdf8b 100644 --- a/kloppy/wyscout.py +++ b/kloppy/wyscout.py @@ -1 +1,3 @@ from ._providers.wyscout import load, load_open_data + +__all__ = ["load", "load_open_data"]