""" # -*- coding: utf-8 -*- # # Copyright 2021 Michael Büsch # # Licensed under the Apache License version 2.0 # or the MIT license, at your option. # SPDX-License-Identifier: Apache-2.0 OR MIT # """ __all__ = [ "GenericIter", "pprint", "printlist", ] from dataclasses import dataclass from pprint import pprint from typing import Any def printlist(lst, prefix=""): if prefix: print(f"{prefix}:") for i, one in enumerate(lst): for j, line in enumerate(str(one).splitlines()): if j == 0: print(f"{i:02d}: {line}") else: print(f" {line}") @dataclass class GenericIter(object): obj: Any size: int rev: bool = False pos: int = 0 def __iter__(self): return self def _next(self): pos = self.pos if self.rev: if pos < 0: raise StopIteration self.pos -= 1 else: if pos >= self.size: raise StopIteration self.pos += 1 return self.obj, pos # vim: ts=4 sw=4 expandtab