<commit_before>import json
import os

from betfairlightweight.resources import MarketCatalogue
from flumine.markets.middleware import Middleware

MARKET_CATALOGUE_PATH = ""  # update to correct path


# Will read and parse the market_catalogue file and add to the market object when backtesting
# Usage framework.add_middleware(MarketCatalogueMiddleware())
class MarketCatalogueMiddleware(Middleware):
    def __call__(self, market) -> None:
        catalogue_file_path = os.path.join(MARKET_CATALOGUE_PATH, market.market_id)
        if os.path.exists(catalogue_file_path):
            with open(catalogue_file_path, "r") as r:
                data = r.read()
                catalogue_json_data = json.loads(data)
                market.market_catalogue = MarketCatalogue(**catalogue_json_data)<commit_msg>Updates market catalogue middleware to only update when adding market<commit_after>import json
import os

from betfairlightweight.resources import MarketCatalogue
from flumine.markets.middleware import Middleware

MARKET_CATALOGUE_PATH = ""  # update to correct path


# Will read and parse the market_catalogue file and add to the market object when backtesting
# Usage framework.add_middleware(MarketCatalogueMiddleware())
class MarketCatalogueMiddleware(Middleware):
    def __call__(self, market) -> None:
        pass

    def add_market(self, market) -> None:
        catalogue_file_path = os.path.join(MARKET_CATALOGUE_PATH, market.market_id)
        if os.path.exists(catalogue_file_path):
            with open(catalogue_file_path, "r") as r:
                data = r.read()
                catalogue_json_data = json.loads(data)
                market.market_catalogue = MarketCatalogue(**catalogue_json_data)
{'fraction_non_alphanumeric': 0.07068366164542295, 'fraction_numerical': 0.0, 'mean_word_length': 3.851123595505618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2543128', 'n_tokens_mistral': 503, 'n_tokens_neox': 485, 'n_words': 125}