Beispiel #1
0
    def reset(self) -> None:
        task = self._task() if self._task else None
        if task is None:
            raise ReferenceError("Task reference is not available anymore")

        self.__exit__(None, None, None)
        self._task = ReferenceType(task)
        self.__enter__()
Beispiel #2
0
    def append(self, item: T) -> "WeakImmutableList[T]":
        """Append an item and return a new list

        :param item: the item to append
        :return: a new list
        """

        new_instance = WeakImmutableList()  # type: WeakImmutableList[T]
        new_instance.__list = self.__list.copy()
        new_instance.__list.append(ReferenceType(item))
        return new_instance
Beispiel #3
0
    def __enter__(self) -> "Expires":
        if self._task is None:
            task = current_task(self.loop)
            if task is None:
                raise RuntimeError(
                    "Timeout context manager should be used inside a task")
            self._task = ReferenceType(task)
        else:
            if self._cancel_handler is not None:
                raise RuntimeError("This context is already in use")

            if self._task() != current_task(self.loop):
                raise ValueError("Can't change bound task after first use")

        self.reset()

        return self
Beispiel #4
0
    def __enter__(self) -> "expires":
        self._expired = False

        if self._timeout is not None:
            # Get current task
            if self._task is None:
                task = current_task(self.loop)
                if task is None:
                    raise RuntimeError(
                        "Timeout context manager should be used inside a task")

                self._task = ReferenceType(task)

            self._expire_at = self.loop.time()
            if self._timeout <= 0:
                self._cancel_handler = self.loop.call_soon(self._expire_task)
            else:
                self._expire_at += self._timeout
                self._cancel_handler = self.loop.call_at(
                    self._expire_at, self._expire_task)

        return self
Beispiel #5
0
 def append(self, item: T) -> "WeakImmutableList[T]":
     new_instance = WeakImmutableList()  # type: WeakImmutableList[T]
     new_instance.__list = self.__cleanList()
     new_instance.__list.append(ReferenceType(item))
     return new_instance