Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Below is what I currently have in my init.sage to mimic GAP printing. It works great if there are few different values or they are all related, but not well at all if there are many different unrelated values.

def increment_string(s):
    """
    Magically increment a string a la perl

    Arguments:
    - `str`:
    """
    if s == "":
        return "A"
    elif s[-1] == 'Z':
        return increment_string(s[:-1]) + 'A'
    else:
        return s[:-1] + chr(ord(s[-1]) + 1)


def pretty_print(M,maxlen=5,suppress_legend=False):

    elements = [x for x in Set(M.list()) if len(repr(x)) > maxlen]
    specials={M.base_ring().zero():'.'}

    if len(elements) == 0:
        print M.str(specials)
        return

    cur_label=""
    for x in elements:
        if specials.has_key(-x):
            specials[x]='-'+specials[-x]
        elif specials.has_key(conjugate(x)):
            specials[x]='/'+specials[conjugate(x)]
        elif specials.has_key(-conjugate(x)):
            specials[x]='-/'+specials[-conjugate(x)]
        else:
            cur_label = increment_string(cur_label)
            specials[x] = cur_label

    s = M.str(specials)
    if not suppress_legend:
        # "Sort by value"
        from operator import itemgetter
        for key,val in sorted(specials.iteritems(), key=itemgetter(1)):
            k = val[0]
            if k != '-' and k != '/' and k != '.':
                s += "\n"+val+" = "+repr(key)
    print s

pp = pretty_print