Exemplo n.º 1
0
 def to_matrix(self, el):
     mx, tp, bt, lf, rt = self.matrix, self.top, self.bottom, self.left, self.right
     dash_x, dash_y = bool(bt), bool(rt)
     upper = [marginal(row, lf, rt, dash_y and el) for row in mx[:tp]] if tp else []
     lower = [marginal(row, lf, rt, dash_y and el) for row in mx[-bt:]] if bt else []
     w = size(upper)[1] or size(lower)[1]
     blank_line = [[el] * w] if w and dash_x and el else []
     return upper + blank_line + lower
Exemplo n.º 2
0
def deco_matrix(matrix,
                discrete=False,
                delim=COSP,
                bracket=BRK,
                read=None,
                presets=(FRESH, PLANET),
                direct=ROWWISE,
                top=0,
                bottom=0,
                left=0,
                right=0,
                ansi=False,
                level=0,
                hr=ELLIP):
    if not matrix: return str(matrix)
    height, width = size(matrix)
    if not height or not width: return liner([], delim, level, bracket, discrete)
    vn = MatrixMargin.build(matrix, top, bottom, left, right, height, width)
    raw, text = vn.map(oneself).to_matrix(hr), vn.stringify(read).to_matrix(hr)
    dye = fluo_matrix(raw, direct, presets, colorant=True) if presets else None
    rows = pad_matrix(text, raw, dye, ansi)
    lines = mapper(rows, lambda line: bracket_fn(delim.join(line))) \
        if bracket \
        else mapper(rows, lambda line: delim.join(line))
    return liner(lines, delim=COLF, bracket=bracket, level=level, discrete=discrete)
Exemplo n.º 3
0
def solebound(
        mx: List[list],
        opt: Select = Select(is_numeric, float)
):
    h, w = size(mx)
    matrix, count, when, to = iso(h, w, None), 0, opt.when, opt.to
    ma = mi = None
    for i, row in enumerate(mx):
        for j, v in enumerate(row):
            if when(v) and (count := count + 1):
                matrix[i][j] = v = to(v) if to else v
                if mi is None: ma = mi = v
                if v > ma:
                    ma = v
                elif v < mi:
                    mi = v
Exemplo n.º 4
0
def duobound(mx: List[list],
             opt_x: Select = Select(is_numeric, float),
             opt_y: Select = Select(has_literal, str_value)):
    height, width = size(mx)
    mx_x, count_x, when_x, to_x = iso(height, width,
                                      None), 0, opt_x.when, opt_x.to
    mx_y, count_y, when_y, to_y = iso(height, width,
                                      None), 0, opt_y.when, opt_y.to
    max_x = max_y = min_x = min_y = None
    for i, row in enumerate(mx):
        for j, v in enumerate(row):
            if when_x(v) and (count_x := count_x + 1):
                mx_x[i][j] = v = to_x(v) if to_x else v
                if min_x is None: max_x = min_x = v
                if v > max_x:
                    max_x = v
                elif v < min_x:
                    min_x = v
            elif when_y(v) and (count_y := count_y + 1):
Exemplo n.º 5
0
def fluo_pointwise(mx, presets, effects=None, colorant=False, mutate=False):
    h, w = size(mx)
    if not h or not w: return [[]]
    if effects is None: effects = ()
    if isinstance(presets, tuple):
        preset_x, preset_y = presets
        info_x, info_y = duobound(mx)
        dye_x, dye_y = (ProjectorFactory(info_x, preset_x, *effects),
                        ProjectorFactory(info_y, preset_y, *effects))
        projector = (to_colorant if colorant else to_pigment)(
            info_x, dye_x, info_y, dye_y, preset_to_flat(preset_x))
    else:
        preset = presets
        info = solebound(mx)
        dye = ProjectorFactory(info, preset, *effects)
        projector = (to_colorant if colorant else to_pigment)(
            info, dye, None, None, preset_to_flat(preset))
    mapper = mutate_fn if mutate else mapper_fn
    return mapper(mx, projector)
Exemplo n.º 6
0
def mutazip(a, b, fn):
    h, w = size(a)
    while (h := h - 1) >= 0:
        ra, rb, y = a[h], b[h], w
        while (y := y - 1) >= 0:
            ra[y] = fn(ra[y], rb[y])
Exemplo n.º 7
0
def iterate(mx, fn_on_column):
    _, w = size(mx)
    for j in range(w):
        col = column(mx, j)
        fn_on_column(col, j)
    pass
Exemplo n.º 8
0
def mapper(mx, fn_on_column):
    _, w = size(mx)
    return [fn_on_column(column(mx, j), j) for j in range(w)]
Exemplo n.º 9
0
def sizing(mx, tp, bt, lf, rt, ht=None, wd=None):
    if not ht or not wd: (ht, wd) = size(mx)
    if not ht or not wd: tp, bt = 0, 0
    if (not tp and not bt) or (tp + bt >= ht): tp, bt = ht, 0
    if (not lf and not rt) or (lf + rt >= wd): lf, rt = wd, 0
    return tp, bt, lf, rt, ht, wd
Exemplo n.º 10
0
def max_by(mx, indicator=None, initial=None):
    h, w = size(mx)
    if not h or not w: return initial
    if initial is None: initial = indicator(mx[0][0]) if indicator else mx[0][0]
    compare = partial(max_indicate, indicator=partial(max_vec_by, indicator=indicator, initial=initial))
    return reduce(compare, mx, initial)
Exemplo n.º 11
0
 def size(self):
     return size(self.rows)