<commit_before>from __future__ import absolute_import, print_function
import sys
import re
import os
import argh
import pager
import s
import json
import pprint
import blessed


def _header(data, highlight=False):
    try:
        val = '-' * (1 + len(data['stack']))
    except:
        print('data:', data)
        raise
    if data['direction'] == 'out':
        val = val[1:] + '<'
    else:
        val += '>'
    if data['fntype'] in ['gen.send', 'gen.yield']:
        val = val + val[-1]
    if data['fntype'] == 'gen.send':
        val = val[1:]
    name = data['name']
    if highlight:
        val = s.colors.green(val)
    return val + '\n ' + name


def _body(data, hide_keys, pretty, max_lines):
    val = []
    for k, v in s.dicts.drop(data, hide_keys).items():
        if not v:
            continue
        elif isinstance(v, (tuple, list, dict)) and pretty:
            v = pprint.pformat(v, width=1).splitlines()
            size = len(k) + 3
            if len(v) > max_lines:
                v = v[:max_lines]
                v[-1] += ' ...'
            v = v[:1] + [s.strings.indent(x, size) for x in v[1:]]
            v = '\n'.join(v)
        elif k != 'traceback':
            v = repr(v)
            max_chars = 160
            if len(v) > max_chars:
                v = v[:max_chars] + ' ...'
        val.append(' {}: {}'.format(k, v))
    return '\n'.join(val)


@s.schema.check(int, int, int, [dict])
def _visualize_flat(width, height, index, datas):
    datas = datas[index:index + height - 2]
    datas = [[_header(data).split('\n'), data] for data in datas]
    size = max(len(data) for (data, _), _ in datas)
    datas = [[x.ljust(size) + y, data] for (x, y), data in datas]
    size = max(len(x) for x, _ in datas) + 1
    datas = [[x.ljust(size) + ('args=' + str(data['args']) if data.get('args') else
                               'value=' + str(data['value']) if data.get('value') else
                               ''),
              data]
             for x, data in datas]
    datas = [[x + (' kwargs=' + str(data['kwargs'].items()) if data.get('kwargs') else ''), data]
             for x, data in datas]
    datas = [[x[:min(width - 1, 200)], data] for x, data in datas]
    datas[0] = [s.colors.green(datas[0][0]), datas[0][1]]
    return '\n'.join([x for x, _ in datas])


def _visualize_single(index, path, datas, hidden_keys, max_lines, pair, pretty):
    if not pair or datas[index]['fntype'] in ['gen.send', 'gen.yield']:
        vals = [(datas[index], True)]
    else:
        vals = _pair(index, datas)
    output = ['path: {}'.format(path),
              'index: {}'.format(index)]
    for data, highlight in vals:
        output += ['',
                   _header(data, highlight),
                   _body(data, hidden_keys, pretty, max_lines)]
    return '\n'.join(output)


def _visualize(width, height, index, path, datas, hidden_keys, max_lines, pair, pretty, flat):
    if flat:
        return _visualize_flat(width, height, index, datas)
    else:
        return _visualize_single(index, path, datas, hidden_keys, max_lines, pair, pretty)


@s.trace.logic
def _pair(index, datas):
    data = datas[index]
    inward = datas[index]['direction'] == 'in'

    if inward:
        datas = datas[index + 1:]
    else:
        datas = reversed(datas[:index])

    for pair in datas:
        match_names = pair['name'] == data['name']
        opposite_directions = pair['direction'], data['direction'] == 'in', 'out'
        send_or_yield = pair['fntype'] in ['gen.send', 'gen.yield']
        if match_names and opposite_directions and not send_or_yield:
            break
    else:
        raise Exception('no pair found')
    vals = [(pair, False), (data, True)]
    if inward:
        vals = list(reversed(vals))
    return vals


_help = """
<commit_msg>dont hide falsy values in debug<commit_after>from __future__ import absolute_import, print_function
import sys
import re
import os
import argh
import pager
import s
import json
import pprint
import blessed


def _header(data, highlight=False):
    try:
        val = '-' * (1 + len(data['stack']))
    except:
        print('data:', data)
        raise
    if data['direction'] == 'out':
        val = val[1:] + '<'
    else:
        val += '>'
    if data['fntype'] in ['gen.send', 'gen.yield']:
        val = val + val[-1]
    if data['fntype'] == 'gen.send':
        val = val[1:]
    name = data['name']
    if highlight:
        val = s.colors.green(val)
    return val + '\n ' + name


def _body(data, hide_keys, pretty, max_lines):
    val = []
    for k, v in s.dicts.drop(data, hide_keys).items():
        if isinstance(v, (tuple, list, dict)) and pretty:
            v = pprint.pformat(v, width=1).splitlines()
            size = len(k) + 3
            if len(v) > max_lines:
                v = v[:max_lines]
                v[-1] += ' ...'
            v = v[:1] + [s.strings.indent(x, size) for x in v[1:]]
            v = '\n'.join(v)
        elif k != 'traceback':
            v = repr(v)
            max_chars = 160
            if len(v) > max_chars:
                v = v[:max_chars] + ' ...'
        val.append(' {}: {}'.format(k, v))
    return '\n'.join(val)


@s.schema.check(int, int, int, [dict])
def _visualize_flat(width, height, index, datas):
    datas = datas[index:index + height - 2]
    datas = [[_header(data).split('\n'), data] for data in datas]
    size = max(len(data) for (data, _), _ in datas)
    datas = [[x.ljust(size) + y, data] for (x, y), data in datas]
    size = max(len(x) for x, _ in datas) + 1
    datas = [[x.ljust(size) + ('args=' + str(data['args']) if data.get('args') else
                               'value=' + str(data['value']) if data.get('value') else
                               ''),
              data]
             for x, data in datas]
    datas = [[x + (' kwargs=' + str(data['kwargs'].items()) if data.get('kwargs') else ''), data]
             for x, data in datas]
    datas = [[x[:min(width - 1, 200)], data] for x, data in datas]
    datas[0] = [s.colors.green(datas[0][0]), datas[0][1]]
    return '\n'.join([x for x, _ in datas])


def _visualize_single(index, path, datas, hidden_keys, max_lines, pair, pretty):
    if not pair or datas[index]['fntype'] in ['gen.send', 'gen.yield']:
        vals = [(datas[index], True)]
    else:
        vals = _pair(index, datas)
    output = ['path: {}'.format(path),
              'index: {}'.format(index)]
    for data, highlight in vals:
        output += ['',
                   _header(data, highlight),
                   _body(data, hidden_keys, pretty, max_lines)]
    return '\n'.join(output)


def _visualize(width, height, index, path, datas, hidden_keys, max_lines, pair, pretty, flat):
    if flat:
        return _visualize_flat(width, height, index, datas)
    else:
        return _visualize_single(index, path, datas, hidden_keys, max_lines, pair, pretty)


def _pair(index, datas):
    data = datas[index]
    inward = datas[index]['direction'] == 'in'

    if inward:
        datas = datas[index + 1:]
    else:
        datas = reversed(datas[:index])

    for pair in datas:
        match_names = pair['name'] == data['name']
        opposite_directions = pair['direction'], data['direction'] == 'in', 'out'
        send_or_yield = pair['fntype'] in ['gen.send', 'gen.yield']
        if match_names and opposite_directions and not send_or_yield:
            break
    else:
        raise Exception('no pair found')
    vals = [(pair, False), (data, True)]
    if inward:
        vals = list(reversed(vals))
    return vals


_help = """

{'fraction_non_alphanumeric': 0.16323839662447256, 'fraction_numerical': 0.006329113924050633, 'mean_word_length': 2.2483940042826553, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2918297', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2451, 'n_words': 694}