<commit_before>import csv
import os.path

from mmx_legacy_info import BASE_DIR, DEFAULT_LOCALE
import mmx_legacy_info
from mmx_legacy_info.locale import MMXLocale
from mmx_legacy_info.npc import NPC


class MMX(object):
    """
    Defines the main class to load files and process them.

    Using locales:
        Simple example (with default locale 'de'):

        >>> m = MMX()
        >>> m.parse_locale()
        >>> m.locale['DIALOG_OPTION_AIOLOS_1']
        'Anwerbung: Kundschafter'

    Getting NPC data:
        >>> m.load_npcs()
        >>> m.npc_list[0].name_key
        'NPC_NAME_LORD_HAART'

    Using NPC data together with locale:
        >>> m.locale[m.npc_list[0].name_key]
        '<NAME>'
    """

    def __init__(self, base_dir=BASE_DIR):
        """
        :param base_dir: directory where to find the game files. Assumes an installation with Steam and defaults to
        "D:\\Games\\Steam\\SteamApps\\common\\Might & Magic X - Legacy\\Might and Magic X Legacy_Data\\StreamingAssets".
        """
        self.base_dir = base_dir
        self.locale = None
        self.npc_list = []

    def parse_locale(self, locale=DEFAULT_LOCALE):
        """
        Loads a local file to memory.

        use locales:
        >>> m = MMX()
        >>> m.parse_locale("en")
        >>> m.locale['DIALOG_OPTION_AIOLOS_1']
        'Hire: Scout'

        >>> m = MMX()
        >>> m.parse_locale("jp")
        >>> m.locale['DIALOG_OPTION_AIOLOS_1']
        '雇用：スカウト'

        >>> # separate install dir
        >>> import mmx_legacy_info, os.path
        >>> m = MMX(r"d:\test_mmx\StramingAssets")
        >>> m.parse_locale("kr")
        >>> m.locale['DIALOG_OPTION_AIOLOS_1']
        '고용: 정찰병'
        """
        self.locale = MMXLocale(locale)

    def load_npcs(self):
        """
        Loads all NPCs to memory.
        """

        npc_file = os.path.join(mmx_legacy_info.BASE_DIR, "StaticData", "NpcStaticData.csv")
        # filter comment lines and lines starting with 'StaticID', which are used as column headers.
        reader = csv.reader(
            filter(lambda row: row[0] != '#' and not row.startswith('StaticID'), open(npc_file, newline=''))
        )
        self.npc_list = [NPC(*line) for line in reader]  # note list expansion
<commit_msg>added NPC filter example<commit_after>import csv
import os.path

from mmx_legacy_info import BASE_DIR, DEFAULT_LOCALE
import mmx_legacy_info
from mmx_legacy_info.locale import MMXLocale
from mmx_legacy_info.npc import NPC


class MMX(object):
    """
    Defines the main class to load files and process them.

    Using locales:
        Simple example (with default locale 'de'):

        >>> m = MMX()
        >>> m.parse_locale()
        >>> m.locale['DIALOG_OPTION_AIOLOS_1']
        'Anwerbung: Kundschafter'

    Getting NPC data:
        >>> m.load_npcs()
        >>> m.npc_list[0].name_key
        'NPC_NAME_LORD_HAART'

    Using NPC data together with locale:
        >>> m.locale[m.npc_list[0].name_key]
        '<NAME>'
    """

    def __init__(self, base_dir=BASE_DIR):
        """
        :param base_dir: directory where to find the game files. Assumes an installation with Steam and defaults to
        "D:\\Games\\Steam\\SteamApps\\common\\Might & Magic X - Legacy\\Might and Magic X Legacy_Data\\StreamingAssets".
        """
        self.base_dir = base_dir
        self.locale = None
        self.npc_list = []

    def parse_locale(self, locale=DEFAULT_LOCALE):
        """
        Loads a local file to memory.

        use locales:
        >>> m = MMX()
        >>> m.parse_locale("en")
        >>> m.locale['DIALOG_OPTION_AIOLOS_1']
        'Hire: Scout'

        >>> m = MMX()
        >>> m.parse_locale("jp")
        >>> m.locale['DIALOG_OPTION_AIOLOS_1']
        '雇用：スカウト'

        >>> # separate install dir
        >>> import mmx_legacy_info, os.path
        >>> m = MMX(r"d:\test_mmx\StramingAssets")
        >>> m.parse_locale("kr")
        >>> m.locale['DIALOG_OPTION_AIOLOS_1']
        '고용: 정찰병'
        """
        self.locale = MMXLocale(locale)

    def load_npcs(self):
        """
        Loads all NPCs to memory.

        filter hirelings:
        >>> m = MMX()
        >>> m.load_npcs()
        >>> hirelings = [n for n in filter(lambda npc: npc.is_hireling, m.npc_list)]
        >>> hirelings[0].name_key
        'NPC_NAME_HAJI'

        """

        npc_file = os.path.join(mmx_legacy_info.BASE_DIR, "StaticData", "NpcStaticData.csv")
        # filter comment lines and lines starting with 'StaticID', which are used as column headers.
        reader = csv.reader(
            filter(lambda row: row[0] != '#' and not row.startswith('StaticID'), open(npc_file, newline=''))
        )
        self.npc_list = [NPC(*line) for line in reader]  # note list expansion

{'fraction_non_alphanumeric': 0.12688127090301005, 'fraction_numerical': 0.0031354515050167225, 'mean_word_length': 2.304558011049724, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 119, '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': '476538', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1609, 'n_words': 363}