Guest User

tagstringmll

a guest
Aug 11th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. def get_concrete_types():
  2.     def _abuse(*args):
  3.         concrete_instances = [arg for arg in args if not isinstance(arg, str)]
  4.         return map(type, args)
  5.     return _abuse"""{interpolation} decoded"""
  6.  
  7. InterpolationConcrete, DecodedConcrete = get_concrete_types()
  8.  
  9. from collections.abc import Iterable
  10. from textwrap import dedent
  11.  
  12. local_funcs = {}
  13.  
  14. def _lambda(definition, *non_local_vars, local_funcs=local_funcs):
  15.     if non_local_vars:
  16.         for item in non_local_vars:
  17.             if isinstance(item, InterpolationConcrete):
  18.                 item.getvalue()
  19.                 fname = "x" + str(len(local_funcs)-1)
  20.                 definition = definition.strip() + f'local_funcs[{fname!r}]'
  21.             else:
  22.                 definition = definition.strip() + item.strip()
  23.     fname = f"x{len(local_funcs)}"
  24.     definition = dedent(f"def {fname}" + definition)
  25.     exec(definition, globals=globals(), locals=local_funcs)
  26.     fname = f"x{len(local_funcs)-1}"
  27.     return local_funcs[fname]
  28.  
  29.  
  30. def flat_map(iterable, func, *, merge_str: bool = False):
  31.     res = []
  32.     for item in iterable:
  33.         val = func(item)
  34.         if isinstance(val, str) and merge_str:
  35.             res.append(val)
  36.             continue
  37.         if isinstance(val, Iterable):
  38.             res.extend(val)
  39.     return res
  40.  
  41. # tesseract-ocr like result
  42. ocr_results = [
  43. {"lines": [
  44.     {"words": [
  45.         {"symbols": [
  46.              "m",
  47.          ]},
  48.         {"symbols": [
  49.              "u", "l", "t",
  50.          ]},
  51.         {"symbols": [
  52.              "i", "-"
  53.          ]},
  54.     ]},
  55.     {"words": [
  56.         {"symbols": [
  57.              "l", "i",
  58.          ]},
  59.         {"symbols": [
  60.              "n", "e", " ",
  61.          ]},
  62.     ]},
  63.     {"words": [
  64.         {"symbols": [
  65.              "l", "a", "m",
  66.          ]},
  67.         {"symbols": [
  68.              "b", "d", "a",
  69.          ]},
  70.     ]},
  71. ]}
  72. ]
  73.  
  74. symbols = flat_map(
  75.         ocr_results,
  76.         _lambda"""(paragraph: dict[str, list]) -> Iterable[str]:
  77.        lines = paragraph["lines"]
  78.        print("from lambda para", paragraph)
  79.        return flat_map(
  80.                lines,
  81.                {_lambda"""(line: dict[str, list]) -> Iterable[str]:
  82.                     print("from lambda line", line)
  83.                     words = line["words"]
  84.                     return flat_map(
  85.                             words,
  86.                             {_lambda"""(word: dict[str, list]) -> Iterable[str]:
  87.                                print("from lambda word", word)
  88.                                return "".join(word["symbols"])"""},
  89.                             merge_str=True,
  90.                         )
  91.                     """}
  92.            )
  93.        """
  94.     )
  95.  
  96. print(symbols)
  97. print("".join(symbols))
Add Comment
Please, Sign In to add comment