コード例 #1
0
def int_check(*args, stacklevel=2):
    """Check if arguments are integrals."""
    for var in args:
        if not isinstance(var, numbers.Integral):
            name = type(var).__name__
            func = inspect.stack()[stacklevel][3]
            raise IntError(f'Function {func} expected integral number, {name} got instead.')
コード例 #2
0
    def __index__(cls):  # pylint: disable=invalid-index-returned
        """Numeral registry index of the protocol.

        Raises:
            IntError: This protocol doesn't support :meth:`__index__`.

        """
        raise IntError(
            f'{cls.__name__!r} object cannot be interpreted as an integer')
コード例 #3
0
    def index(self, value, start=0, stop=None):  # pylint: disable=inconsistent-return-statements
        """First index of ``value``.

        Args:
            value (Union[str, pcapkit.protocols.protocol.Protocol, Type[pcapkit.protocols.protocol, Protocol]]):
                value to search
            start (int): start offset
            stop (int): stop offset

        Returns:
            int: First index of ``value``.

        Raises:
            IntError: If the value is not present.

        """
        if start is not None and start < 0:
            start = max(len(self) + start, 0)
        if stop is not None and stop < 0:
            stop += len(self)

        try:
            if not isinstance(start, numbers.Integral):
                start = self.index(start)
            if not isinstance(stop, numbers.Integral):
                stop = self.index(stop)
        except IndexNotFound:
            raise IntError(
                'slice indices must be integers or have an __index__ method'
            ) from None

        from pcapkit.protocols.protocol import Protocol  # pylint: disable=import-outside-toplevel

        try:
            flag = issubclass(value, Protocol)
        except TypeError:
            flag = issubclass(type(value), Protocol)

        if flag or isinstance(value, Protocol):
            value = value.id()
            if isinstance(value, tuple):
                value = r'|'.join(value)

        try:
            for index, data in enumerate(self.__data__[start:stop]):
                if re.fullmatch(value, data, re.IGNORECASE):
                    return index
        except Exception:
            raise IndexNotFound(
                f'{value!r} is not in {self.__class__.__name__!r}')
        return -1
コード例 #4
0
ファイル: validations.py プロジェクト: ljouans/PyPCAPKit
def int_check(*args, stacklevel=2):
    """Check if arguments are *integrals* (``int``).

    Args:
        *args: Arguments to check.
        stacklevel (int): Stack level to fetch originated function name.

    Raises:
        IntError: If any of the arguments is **NOT** *integral* (``int``).

    """
    for var in args:
        if not isinstance(var, numbers.Integral):
            name = type(var).__name__
            func = inspect.stack()[stacklevel][3]
            raise IntError(f'Function {func} expected integral number, {name} got instead.')
コード例 #5
0
    def index(self, value, start=0, stop=None):  # pylint: disable=inconsistent-return-statements
        """S.index(value, [start, [stop]]) -> integer -- return first index of value.
           Raises ValueError if the value is not present.

           Supporting start and stop arguments is optional, but
           recommended.
        """
        if start is not None and start < 0:
            start = max(len(self) + start, 0)
        if stop is not None and stop < 0:
            stop += len(self)

        try:
            if not isinstance(start, numbers.Integral):
                start = self.index(start)
            if not isinstance(stop, numbers.Integral):
                stop = self.index(stop)
        except IndexNotFound:
            raise IntError(
                'slice indices must be integers or have an __index__ method'
            ) from None

        from pcapkit.protocols.protocol import Protocol
        try:
            flag = issubclass(value, Protocol)
        except TypeError:
            flag = issubclass(type(value), Protocol)
        if flag or isinstance(value, Protocol):
            value = value.__index__()
            if isinstance(value, tuple):
                value = r'|'.join(value)

        try:
            for index, data in enumerate(self.__data__[start:stop]):
                if re.fullmatch(value, data, re.IGNORECASE):
                    return index
        except Exception:
            raise IndexNotFound(
                f'{value!r} is not in {self.__class__.__name__!r}')