<commit_before>from collections import OrderedDict

from malcolm.core.scalarmeta import ScalarMeta
from malcolm.core.serializable import Serializable


@Serializable.register_subclass("malcolm:core/ChoiceMeta:1.0")
class ChoiceMeta(ScalarMeta):
    """Meta object containing information for a enum"""

    endpoints = ["choices", "description", "tags", "writeable", "label"]

    def __init__(self, name, description, choices):
        super(ChoiceMeta, self).__init__(name=name, description=description)

        self.choices = choices

    def set_choices(self, choices):
        """
        Set allowed values

        Args:
            choices (list): List of allowed values
        """

        self.choices = choices

    def validate(self, value):
        """
        Check if the value is valid returns it

        Args:
            value: Value to validate

        Returns:
            Value if it is valid
        Raises:
            ValueError: If value not valid
        """

        if value is None or value in self.choices:
            return value
        else:
            raise ValueError("%s is not a valid value" % value)

    @classmethod
    def from_dict(cls, name, d):
        """Create a ChoiceMeta subclass instance from the serialized version
        of itself

        Args:
            name (str): ChoiceMeta instance name
            d (dict): Serialised version of ChoiceMeta
        """

        description = d['description']
        choices = d['choices']
        choice_meta = cls(name, description, choices)
        choice_meta.tags = d['tags']
        choice_meta.writeable = d['writeable']
        choice_meta.label = d['label']

        return choice_meta<commit_msg>Make choicemeta error message better<commit_after>from collections import OrderedDict

from malcolm.core.scalarmeta import ScalarMeta
from malcolm.core.serializable import Serializable


@Serializable.register_subclass("malcolm:core/ChoiceMeta:1.0")
class ChoiceMeta(ScalarMeta):
    """Meta object containing information for a enum"""

    endpoints = ["choices", "description", "tags", "writeable", "label"]

    def __init__(self, name, description, choices):
        super(ChoiceMeta, self).__init__(name=name, description=description)

        self.choices = choices

    def set_choices(self, choices):
        """
        Set allowed values

        Args:
            choices (list): List of allowed values
        """

        self.choices = choices

    def validate(self, value):
        """
        Check if the value is valid returns it

        Args:
            value: Value to validate

        Returns:
            Value if it is valid
        Raises:
            ValueError: If value not valid
        """

        if value is None or value in self.choices:
            return value
        else:
            raise ValueError(
                "%s is not a valid value in %s" % (value, self.choices))

    @classmethod
    def from_dict(cls, name, d):
        """Create a ChoiceMeta subclass instance from the serialized version
        of itself

        Args:
            name (str): ChoiceMeta instance name
            d (dict): Serialised version of ChoiceMeta
        """

        description = d['description']
        choices = d['choices']
        choice_meta = cls(name, description, choices)
        choice_meta.tags = d['tags']
        choice_meta.writeable = d['writeable']
        choice_meta.label = d['label']

        return choice_meta
{'fraction_non_alphanumeric': 0.08889528193325662, 'fraction_numerical': 0.0011507479861910242, 'mean_word_length': 2.25561797752809, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4481489', 'n_tokens_mistral': 909, 'n_tokens_neox': 886, 'n_words': 295}