Source code for jgdv.structs.strang.debug

  1#!/usr/bin/env python3
  2"""
  3
  4"""
  5# ruff: noqa: PLW0211, ARG004
  6
  7# Imports:
  8from __future__ import annotations
  9
 10# ##-- stdlib imports
 11import datetime
 12import enum
 13import functools as ftz
 14import itertools as itz
 15import logging as logmod
 16import pathlib as pl
 17import re
 18import time
 19import collections
 20import contextlib
 21import hashlib
 22from copy import deepcopy
 23from uuid import uuid1
 24from weakref import ref
 25import atexit # for @atexit.register
 26import faulthandler
 27# ##-- end stdlib imports
 28
 29from .strang import Strang
 30from . import errors
 31
 32# ##-- types
 33# isort: off
 34# General
 35import abc
 36import collections.abc
 37import typing
 38import types
 39from typing import cast, assert_type, assert_never
 40from typing import Generic, NewType, Never
 41from typing import no_type_check, final, override, overload
 42# Protocols and Interfaces:
 43from typing import Protocol
 44
 45if typing.TYPE_CHECKING:
 46    from uuid import UUID
 47    from . import _interface as API # noqa: N812
 48
 49    from typing import Final, ClassVar, Any, Self
 50    from typing import Literal, LiteralString
 51    from typing import TypeGuard
 52    from collections.abc import Iterable, Iterator, Callable, Generator
 53    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
 54
 55    from jgdv import Maybe
 56
 57# isort: on
 58# ##-- end types
 59
 60##-- logging
 61logging = logmod.getLogger(__name__)
 62##-- end logging
 63
 64# Vars:
 65
 66# Body:
 67
[docs] 68class StrangDebug: 69
[docs] 70 @staticmethod 71 def debug(cls:type[API.Strang_p], text:str) -> str: 72 """ Return a str of details about a str -> strang """ 73 hooks = [x for x in dir(cls) if x.endswith("_h")] 74 abs_sections = StrangDebug._describe_section_specs(cls._sections) 75 ang = cls(text) 76 sections = StrangDebug._describe_text(cls, ang) 77 raw_data = StrangDebug._describe_raw_data(cls, ang) 78 result = [ 79 "", 80 f"** Strang Debug of: {text} **", 81 f"Type: {cls.__module__} : {cls.__qualname__}", 82 f"Annotation: {cls.cls_annotation()}", 83 f"Hooks: {hooks}", 84 *abs_sections, 85 *StrangDebug._describe_pre_process_results(cls, text), 86 *StrangDebug._describe_process_results(cls, ang), 87 *StrangDebug._describe_post_process_results(cls, ang), 88 *sections, 89 *raw_data, 90 "----", 91 "Result:", 92 f"type(ang) = {type(ang)}", 93 f"str(ang) = {ang:s}", 94 f"ang[:] = {ang[:]}", 95 f"ang[:,:] = {ang[:,:]}", 96 "Formatting: ", 97 f"ang:u = {ang:u}", 98 f"ang:a- = {ang:a-}", 99 f"ang:a = {ang:a}", 100 f"ang:a+ = {ang:a+}", 101 f"{text} -> {ang}", 102 ] 103 return "\n".join(result)
104
[docs] 105 @staticmethod 106 def _describe_pre_process_results(cls:type[API.Strang_p], text:str) -> list[str]: 107 preproc = cls._processor.pre_process(cls, text) 108 result = [ 109 "---- Pre-Process:", 110 f"{text} -> {preproc}", 111 "", 112 ] 113 return result
114
[docs] 115 @staticmethod 116 def _describe_process_results(cls:type[API.Strang_p], actual:API.Strang_p) -> list[str]: 117 section_slices = [(x.start, x.stop) for x in actual.data.sections] 118 word_slices = [(x.start, x.stop) for x in actual.data.words] 119 result = [ 120 "---- Process: ", 121 f"Sections: {section_slices}", 122 f"SecWords: {actual.data.sec_words}", 123 f"FlatIdx: {actual.data.flat_idx}", 124 f"Words: {word_slices}", 125 "", 126 ] 127 return result
128
[docs] 129 @staticmethod 130 def _describe_post_process_results(cls:type[API.Strang_p], actual:API.Strang_p) -> list[str]: 131 result = [ 132 "---- Post-Process:", 133 134 "", 135 ] 136 return result
137
[docs] 138 @staticmethod 139 def _describe_section_specs(secs:API.Sections_d) -> list[str]: 140 result = [ 141 "---- Section Specs:", 142 *(f"- {x.idx} : {x.name}. Case: ({x.case} {x.end}). " for x in secs), 143 "", 144 ] 145 return result
146
[docs] 147 @staticmethod 148 def _describe_text(cls:type[API.Strang_p], actual:API.Strang_p) -> list[str]: 149 result = [ 150 "---- Section Text:", 151 "Format: ..-(index[sec], index[word]) : FlatIndex : word : (meta, type[meta])", 152 ] 153 154 for i,sec in enumerate(actual.data.sec_words): 155 result.append(f"- {actual.section(i).name}:") 156 for j,idx in enumerate(sec): 157 sl = actual.data.words[idx] 158 word = actual[sl] 159 match actual.data.meta[idx]: 160 case None: 161 meta = "" 162 case x: 163 meta = f"({x}, {type(x)})" 164 165 result.append(f"..- ({i},{j}) : {idx} : {word} : {meta}") 166 else: 167 args_text = actual[actual.data.args_start:] 168 arg_len : int = len(actual.data.args) if actual.data.args else 0 169 result += [ 170 "", 171 f"- args ({arg_len}). Position: {actual.data.args_start}:", 172 f"..- {args_text}", 173 "", 174 ] 175 return result
176 177
[docs] 178 @staticmethod 179 def _describe_raw_data(cls:type[API.Strang_p], actual:API.Strang_p) -> list[str]: 180 result = [ 181 "---- Raw Data:", 182 f"Meta: {actual.data.meta}", 183 f"UUID: {actual.data.uuid}", 184 "", 185 ] 186 return result
187
[docs] 188 @staticmethod 189 def diff_uuids(this:UUID, that:UUID) -> str: 190 this_ing : str = str(this) 191 that_ing : str = str(that) 192 result = [y if x==y else "_" for x,y in zip(this_ing, that_ing, strict=True)] 193 return "".join(result)