[WIP] Either refactoring

This commit is contained in:
Matthias Bisping 2023-02-06 18:40:30 +01:00
parent f53f0fea29
commit 066cf17add
2 changed files with 28 additions and 13 deletions

View File

@ -8,7 +8,7 @@ from typing import List, Union
import fitz
import numpy as np
from PIL import Image
from funcy import merge, compose, rcompose, keep, lfilter, none
from funcy import merge, compose, rcompose, keep
from pymonad.either import Right, Left, Either
from pymonad.tools import curry, identity
@ -19,7 +19,7 @@ from image_prediction.info import Info
from image_prediction.stitching.stitching import stitch_pairs
from image_prediction.stitching.utils import validate_box
from image_prediction.utils import get_logger
from image_prediction.utils.generic import bottom
from image_prediction.utils.generic import bottom, left, right
logger = get_logger()
@ -50,7 +50,7 @@ class ParsablePDFImageExtractor(ImageExtractor):
metadata = extract_valid_metadata(self.doc, page)
maybe_image_metadata_pairs = map(partial(metadatum_to_image_metadata_pair, self.doc), metadata)
image_metadata_pairs = keep(right, maybe_image_metadata_pairs)
image_metadata_pairs = keep(take_right, maybe_image_metadata_pairs)
clear_caches()
image_metadata_pairs = stitch_pairs(list(image_metadata_pairs), tolerance=self.tolerance)
@ -58,10 +58,14 @@ class ParsablePDFImageExtractor(ImageExtractor):
yield from image_metadata_pairs
def right(pair: Either):
def take_right(pair: Either):
if pair.is_right():
return pair.either(bottom, identity)
logger.warning(f"Skipping bad image. {pair.either(identity, bottom)}")
logger.warning(f"Skipping bad image. {pair.either(format_context, bottom)}")
def format_context(context):
return f"Reason: {context['reason']}. Metadata: {EnumFormatter()(context['metadata'])}"
def extract_pages(doc, page_range):
@ -127,14 +131,16 @@ def xref_to_maybe_image(doc, xref) -> Either:
def make_maybe_image_metadata_pair(image: Either, metadata: Either):
# haskell.org/tutorial/monads.html
# (>>) :: m a -> m b -> m b
either = Right(make_image_metadata_pair).amap(image).amap(metadata)
return (
Left({"reason": either.either(identity, bottom), "metadata": EnumFormatter()(metadata.value)})
if either.is_left()
else either
)
"""
Reference:
haskell.org/tutorial/monads.html
(>>) :: m a -> m b -> m b
"""
def context(value):
return {"reason": value, "metadata": metadata.either(bottom, identity)}
return Right(make_image_metadata_pair).amap(image).amap(metadata).either(left(context), right(identity))
@curry(2)

View File

@ -1,6 +1,7 @@
from itertools import starmap
from funcy import iterate, first, curry, map
from pymonad.either import Left, Right
def until(cond, func, *args, **kwargs):
@ -17,3 +18,11 @@ def starlift(fn):
def bottom(*args, **kwargs):
return None
def left(fn):
return lambda x: Left(fn(x))
def right(fn):
return lambda x: Right(fn(x))