<commit_before>import sys
import types

from minecraft_data.v1_8 import find_item_or_block, windows_list
from minecraft_data.v1_8 import windows as windows_by_id

from spock.mcdata import constants
from spock.utils import camel_case, snake_case


def make_slot_check(wanted):
    """
    Creates and returns a function that takes a slot and checks
    if it matches the wanted item.
    :param wanted: function(Slot) or Slot or itemID or (itemID, metadata)
    """
    if isinstance(wanted, types.FunctionType):
        return wanted  # just forward the slot check function

    if isinstance(wanted, int):
        item, meta = wanted, None
    elif isinstance(wanted, Slot):
        item, meta = wanted.item_id, wanted.damage
        # TODO compare NBT
    else:  # wanted is list of (id, meta)
        item, meta = wanted

    return lambda slot: item == slot.item_id and meta in (None, slot.damage)


# TODO move to mcdata.items

def apply_variation(item_dict, metadata):
    if item_dict and metadata is not None and 'variations' in item_dict:
        for variation in item_dict['variations']:
            if variation['metadata'] == metadata:
                # variants provide replacements for some fields
                item_dict = item_dict.copy()
                item_dict.update(variation)
                return item_dict
    # TODO no matching metadata was found, make it 0? None? leave blank?
    return item_dict


def find_item_dict(item, metadata=None):
    if metadata is None:  # check for complex types
        if isinstance(item, Slot):
            item, metadata = item.item_id, item.damage
        elif not isinstance(item, (int, str)):
            # name_or_id is tuple of (item_id, metadata)
            item, metadata = item

    return apply_variation(find_item_or_block(item), metadata)


class Slot(object):
    def __init__(self, window, slot_nr, id=constants.INV_ITEMID_EMPTY,
                 damage=0, amount=0, enchants=None):
        self.window = window
        self.slot_nr = slot_nr
        self.item_id = id
        self.damage = damage
        self.amount = amount
        self.nbt = enchants

    def move_to_window(self, window, slot_nr):
        self.window, self.slot_nr = window, slot_nr

    def stacks_with(self, other):
        if self.item_id != other.item_id:
            return False
        if self.damage != other.damage:
            return False
        # raise NotImplementedError('Stacks might differ by NBT data: %s %s'
        #                           % (self, other))
        # if self.nbt != other.nbt: return False
        # TODO implement stacking correctly (NBT data comparison)<commit_msg>Add make_slot_check() for easy slot/item comparison<commit_after>import sys
import types

from minecraft_data.v1_8 import windows_list
from minecraft_data.v1_8 import windows as windows_by_id

from spock.mcdata import constants
from spock.utils import camel_case, snake_case


def make_slot_check(wanted):
    """
    Creates and returns a function that takes a slot and checks
    if it matches the wanted item.
    :param wanted: function(Slot) or Slot or itemID or (itemID, metadata)
    """
    if isinstance(wanted, types.FunctionType):
        return wanted  # just forward the slot check function

    if isinstance(wanted, int):
        item, meta = wanted, None
    elif isinstance(wanted, Slot):
        item, meta = wanted.item_id, wanted.damage
        # TODO compare NBT
    else:  # wanted is list of (id, meta)
        item, meta = wanted

    return lambda slot: item == slot.item_id and meta in (None, slot.damage)


class Slot(object):
    def __init__(self, window, slot_nr, id=constants.INV_ITEMID_EMPTY,
                 damage=0, amount=0, enchants=None):
        self.window = window
        self.slot_nr = slot_nr
        self.item_id = id
        self.damage = damage
        self.amount = amount
        self.nbt = enchants

    def move_to_window(self, window, slot_nr):
        self.window, self.slot_nr = window, slot_nr

    def stacks_with(self, other):
        if self.item_id != other.item_id:
            return False
        if self.damage != other.damage:
            return False
        # raise NotImplementedError('Stacks might differ by NBT data: %s %s'
        #                           % (self, other))
        # if self.nbt != other.nbt: return False
        # TODO implement stacking correctly (NBT data comparison)
{'fraction_non_alphanumeric': 0.07878650667874122, 'fraction_numerical': 0.0029431741000679195, 'mean_word_length': 2.5400641025641026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5251355', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1273, 'n_words': 422}