Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_concrete_types():
- def _abuse(*args):
- concrete_instances = [arg for arg in args if not isinstance(arg, str)]
- return map(type, args)
- return _abuse"""{interpolation} decoded"""
- InterpolationConcrete, DecodedConcrete = get_concrete_types()
- from collections.abc import Iterable
- from textwrap import dedent
- local_funcs = {}
- def _lambda(definition, *non_local_vars, local_funcs=local_funcs):
- if non_local_vars:
- for item in non_local_vars:
- if isinstance(item, InterpolationConcrete):
- item.getvalue()
- fname = "x" + str(len(local_funcs)-1)
- definition = definition.strip() + f'local_funcs[{fname!r}]'
- else:
- definition = definition.strip() + item.strip()
- fname = f"x{len(local_funcs)}"
- definition = dedent(f"def {fname}" + definition)
- exec(definition, globals=globals(), locals=local_funcs)
- fname = f"x{len(local_funcs)-1}"
- return local_funcs[fname]
- def flat_map(iterable, func, *, merge_str: bool = False):
- res = []
- for item in iterable:
- val = func(item)
- if isinstance(val, str) and merge_str:
- res.append(val)
- continue
- if isinstance(val, Iterable):
- res.extend(val)
- return res
- # tesseract-ocr like result
- ocr_results = [
- {"lines": [
- {"words": [
- {"symbols": [
- "m",
- ]},
- {"symbols": [
- "u", "l", "t",
- ]},
- {"symbols": [
- "i", "-"
- ]},
- ]},
- {"words": [
- {"symbols": [
- "l", "i",
- ]},
- {"symbols": [
- "n", "e", " ",
- ]},
- ]},
- {"words": [
- {"symbols": [
- "l", "a", "m",
- ]},
- {"symbols": [
- "b", "d", "a",
- ]},
- ]},
- ]}
- ]
- symbols = flat_map(
- ocr_results,
- _lambda"""(paragraph: dict[str, list]) -> Iterable[str]:
- lines = paragraph["lines"]
- print("from lambda para", paragraph)
- return flat_map(
- lines,
- {_lambda"""(line: dict[str, list]) -> Iterable[str]:
- print("from lambda line", line)
- words = line["words"]
- return flat_map(
- words,
- {_lambda"""(word: dict[str, list]) -> Iterable[str]:
- print("from lambda word", word)
- return "".join(word["symbols"])"""},
- merge_str=True,
- )
- """}
- )
- """
- )
- print(symbols)
- print("".join(symbols))
Add Comment
Please, Sign In to add comment