<commit_before>#-------------------------------------------------------------------------------
# Name:        error
# Purpose:     Definitions of all custom Errors for the OpenAnharmonic
#                package.
#
# Author:      <NAME>
#                <EMAIL>
#
# Created:     1 Oct 2014
# Copyright:   (c) <NAME> 2015
# License:     The MIT License; see "license.txt" for full license terms
#                   and contributor agreement.
#
#       This file is part of opan (OpenAnharmonic), a system for automated
#       computation of anharmonic properties of molecular systems via wrapper
#       calls to computational/quantum chemical software packages.
#
#       http://www.github.com/bskinn/opan
#
#-------------------------------------------------------------------------------

# Module-level imports


class OPANError(Exception):
    """Base class for custom Errors defined for OpenAnharmonic

    OPANError is an abstract superclass of any custom errors built under the
    OpenAnharmonic umbrella. It defines all common methods shared among the
    various subtype error classes, such that the only contents that must be
    declared by a subclass (as of this comment) is the 'typecodes' class-level
    dictionary, which maps string typecode keys to *unique* typecode int values.

    Instantiation
    -------------
    tc  : str
        String representation of typecode to be associated with the OPANError
        subclass instance. *Must* be a valid key of the 'typecodes' class
        variable defined for the relevant subtype.
    msg : str
        Explanation of the nature of the error being reported.
    src : str
        Further detail of the code source of the error behavior.

    Class Variables
    ---------------
    typecodes: dict
        (Defined separately by each subclass of OPANError.) Dictionary of the
        allowed typecodes for the relevant OPANError subclass.

    Instance Variables
    ------------------
    msg : str
        Explanation of the nature of the error being reported.
    src : str
        Further detail of the code source of the error behavior.
    subclass_name   : str
        String representation of the name of the OPANError subclass of the
        current OPANError instance.
    tc  : int
        Numeric value of the typecode associated with the instance, as defined
        in the 'typecodes' dict of the subclass

    Class Methods
    -------
    _typecode_str -- Returns string representation of a numerical typecode
        value. Meant primarily for introspection.

    """


    def __init__(self, tc, msg, src):
        """ Uniform constructor for subclasses of abstract OPANError.

        Parameters
        ----------
        tc   :  typecode for error
                    (see subclass docstrings for allowed typecodes)
        msg  :  explanation of the error
        src  :  source of the problematic data

        Returns
        -------
        (none)

        Raises
        ------
        KeyError : Invalid typecode provided in 'tc'
        AttributeError : If ORCAError subclass has not defined the class
                            variable 'typecodes'
        NotImplementedError : Upon attempt to instantiate abstract ORCAError
            base class.

        """

        # Import(s)
        import re

        # Check for and complain at instantiation of base class. Will need
        #  to update this every time a new subclass of error is created.
        if not (
                    isinstance(self, XYZError) or \
                    isinstance(self, GRADError) or \
                    isinstance(self, HESSError) or \
                    isinstance(self, OUTPUTError) or \
                    isinstance(self, SYMMError) or \
                    isinstance(self, REPOError) or \
                    isinstance(self, ANHARMError)
                ):
            raise(NotImplementedError("OPANError base class is abstract."))
        ## end if

        # Quick RegEx to extract the name of the subclass.
        self.subclass_name = re.search(self.__module__ + "\\.(?P<cls>\w+)'", \
                    str(self.__class__), re.I).group("cls")

        # Check for valid typecode and throw a more descriptive error if
        #  invalid.
        if not tc in self.typecodes:
            raise(KeyError("Invalid " + self.subclass_name + \
                                                " typecode: " + str(tc)))
        ## end if

        # Store error content
        self.tc = tc
        self.msg = msg
        self.src = src


    def __str__(self):
        """ String representation of an instance of an ORCAError subclass.

        Implemented primarily so that the error stack handling of the Python
        interpreter will provide useful information to the user.

        Return value is constructed as follows:
            (typecode string) Error message: Error source

        Parameters
        ----------
        (none)

        Returns
        -------
        retstr : str
            String representation of the instance.

        Raises
        ------
        (none)

        """

        # Store and return the descriptive string
        retstr = "(" + str(self.tc) + ") " + str(self.msg) + \
                (": " + str(self.src) if str(self.src) != "" else "")
        return retstr

    ## end def __str__


    @classmethod
    def _typecode_str(self, tc):
        """Return string representation of provided typecode number.

        Helper function for introspection lookup of the typecode string of the
        typecode number input as 'tc'.  Raises ValueError if the typecode value
        is not found in the .typecodes dict of the appropriate subclass.

        POTENTIALLY OBSOLETE!

        Parameters
        ----------
        tc  : int
            Typecode enumeration constant to be converted to its string
            representation

        Returns
        -------
        tc_str  : string
            Representation of the indicated typecode for the class of which
            'self' is a member.

        Raises
        ------
        ValueError  : If an invalid typecode value is passed.

        """<commit_msg>Added iterability to OPANError typecodes<commit_after>#-------------------------------------------------------------------------------
# Name:        error
# Purpose:     Definitions of all custom Errors for the OpenAnharmonic
#                package.
#
# Author:      <NAME>
#                <EMAIL>
#
# Created:     1 Oct 2014
# Copyright:   (c) <NAME> 2015
# License:     The MIT License; see "license.txt" for full license terms
#                   and contributor agreement.
#
#       This file is part of opan (OpenAnharmonic), a system for automated
#       computation of anharmonic properties of molecular systems via wrapper
#       calls to computational/quantum chemical software packages.
#
#       http://www.github.com/bskinn/opan
#
#-------------------------------------------------------------------------------

# Module-level imports


class OPANError(Exception):
    """Base class for custom Errors defined for OpenAnharmonic

    OPANError is an abstract superclass of any custom errors built under the
    OpenAnharmonic umbrella. It defines all common methods shared among the
    various subtype error classes, such that the only contents that must be
    declared by a subclass (as of this comment) is the 'typecodes' class-level
    dictionary, which maps string typecode keys to *unique* typecode int values.

    Instantiation
    -------------
    tc  : str
        String representation of typecode to be associated with the OPANError
        subclass instance. *Must* be a valid key of the 'typecodes' class
        variable defined for the relevant subtype.
    msg : str
        Explanation of the nature of the error being reported.
    src : str
        Further detail of the code source of the error behavior.

    Class Variables
    ---------------
    typecodes: dict
        (Defined separately by each subclass of OPANError.) Dictionary of the
        allowed typecodes for the relevant OPANError subclass.

    Instance Variables
    ------------------
    msg : str
        Explanation of the nature of the error being reported.
    src : str
        Further detail of the code source of the error behavior.
    subclass_name   : str
        String representation of the name of the OPANError subclass of the
        current OPANError instance.
    tc  : int
        Numeric value of the typecode associated with the instance, as defined
        in the 'typecodes' dict of the subclass

    Class Methods
    -------
    _typecode_str -- Returns string representation of a numerical typecode
        value. Meant primarily for introspection.

    """


    def __init__(self, tc, msg, src):
        """ Uniform constructor for subclasses of abstract OPANError.

        Parameters
        ----------
        tc   :  typecode for error
                    (see subclass docstrings for allowed typecodes)
        msg  :  explanation of the error
        src  :  source of the problematic data

        Returns
        -------
        (none)

        Raises
        ------
        KeyError : Invalid typecode provided in 'tc'
        AttributeError : If ORCAError subclass has not defined the class
                            variable 'typecodes'
        NotImplementedError : Upon attempt to instantiate abstract ORCAError
            base class.

        """

        # Import(s)
        import re

        # Check for and complain at instantiation of base class. Will need
        #  to update this every time a new subclass of error is created.
        if not (
                    isinstance(self, XYZError) or \
                    isinstance(self, GRADError) or \
                    isinstance(self, HESSError) or \
                    isinstance(self, OUTPUTError) or \
                    isinstance(self, SYMMError) or \
                    isinstance(self, REPOError) or \
                    isinstance(self, ANHARMError)
                ):
            raise(NotImplementedError("OPANError base class is abstract."))
        ## end if

        # Quick RegEx to extract the name of the subclass.
        self.subclass_name = re.search(self.__module__ + "\\.(?P<cls>\w+)'", \
                    str(self.__class__), re.I).group("cls")

        # Check for valid typecode and throw a more descriptive error if
        #  invalid.
        if not tc in self.typecodes:
            raise(KeyError("Invalid " + self.subclass_name + \
                                                " typecode: " + str(tc)))
        ## end if

        # Store error content
        self.tc = tc
        self.msg = msg
        self.src = src


    def __str__(self):
        """ String representation of an instance of an ORCAError subclass.

        Implemented primarily so that the error stack handling of the Python
        interpreter will provide useful information to the user.

        Return value is constructed as follows:
            (typecode string) Error message: Error source

        Parameters
        ----------
        (none)

        Returns
        -------
        retstr : str
            String representation of the instance.

        Raises
        ------
        (none)

        """

        # Store and return the descriptive string
        retstr = "(" + str(self.tc) + ") " + str(self.msg) + \
                (": " + str(self.src) if str(self.src) != "" else "")
        return retstr

    ## end def __str__


    class __metaclass__(type):
        # Enable iteration over the typecodes
        def __iter__(self):
            for item in self.__dict__:
                if item == self.__dict__[item]:
                    yield item
                ## end if
            ## next item
        ##end def __iter__
    ## end class __metaclass__


    @classmethod
    def _typecode_str(self, tc):
        """Return string representation of provided typecode number.

        Helper function for introspection lookup of the typecode string of the
        typecode number input as 'tc'.  Raises ValueError if the typecode value
        is not found in the .typecodes dict of the appropriate subclass.

        POTENTIALLY OBSOLETE!

        Parameters
        ----------
        tc  : int
            Typecode enumeration constant to be converted to its string
            representation

        Returns
        -------
        tc_str  : string
            Representation of the indicated typecode for the class of which
            'self' is a member.

        Raises
        ------
        ValueError  : If an invalid typecode value is passed.

        """
{'fraction_non_alphanumeric': 0.09808253767852916, 'fraction_numerical': 0.001420342460348773, 'mean_word_length': 1.6240165631469978, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6447883', 'n_tokens_mistral': 3115, 'n_tokens_neox': 2945, 'n_words': 1240}