[docs]defpretty_use_unicode(flag=None):"""Set whether pretty-printer should use unicode by default"""global_use_unicodeglobalunicode_warningsifflagisNone:return_use_unicode# we know that some letters are not supported in Python 2.X so# ignore those warnings. Remove this when 2.X support is dropped.ifunicode_warnings:known=['LATIN SUBSCRIPT SMALL LETTER %s'%iforiin'HKLMNPST']unicode_warnings='\n'.join([lforlinunicode_warnings.splitlines()ifnotany(iinlforiinknown)])# ------------ end of 2.X warning filteringifflagandunicode_warnings:# print warnings (if any) on first unicode usagewarnings.warn(unicode_warnings)unicode_warnings=''use_unicode_prev=_use_unicode_use_unicode=flagreturnuse_unicode_prev

[docs]defpretty_try_use_unicode():"""See if unicode output is available and leverage it if possible"""try:symbols=[]# see, if we can represent greek alphabetsymbols.extend(greek_unicode.values())# and atomssymbols+=atoms_table.values()forsinsymbols:ifsisNone:return# common symbols not present!encoding=getattr(sys.stdout,'encoding',None)# this happens when e.g. stdout is redirected through a pipe, or is# e.g. a cStringIO.StringOifencodingisNone:return# sys.stdout has no encoding# try to encodes.encode(encoding)exceptUnicodeEncodeError:passelse:pretty_use_unicode(True)

[docs]defxstr(*args):"""call str or unicode depending on current mode"""if_use_unicode:returnunicode(*args)else:returnstr(*args)

[docs]defxobj(symb,length):"""Construct spatial object of given length. return: [] of equal-length strings """iflength<=0:raiseValueError("Length should be greater than 0")# TODO robustify when no unicodedat availableif_use_unicode:_xobj=_xobj_unicodeelse:_xobj=_xobj_asciivinfo=_xobj[symb]c1=top=bot=mid=Noneifnotisinstance(vinfo,tuple):# 1 entryext=vinfoelse:ifisinstance(vinfo[0],tuple):# (vlong), c1vlong=vinfo[0]c1=vinfo[1]else:# (vlong), c1vlong=vinfoext=vlong[0]try:top=vlong[1]bot=vlong[2]mid=vlong[3]exceptIndexError:passifc1isNone:c1=extiftopisNone:top=extifbotisNone:bot=extifmidisnotNone:if(length%2)==0:# even height, but we have to print it somehow anyway...# XXX is it ok?length+=1else:mid=extiflength==1:returnc1res=[]next=(length-2)//2nmid=(length-2)-next*2res+=[top]res+=[ext]*nextres+=[mid]*nmidres+=[ext]*nextres+=[bot]returnres

[docs]defvobj(symb,height):"""Construct vertical object of a given height see: xobj """return'\n'.join(xobj(symb,height))

[docs]defhobj(symb,width):"""Construct horizontal object of a given width see: xobj """return''.join(xobj(symb,width))

[docs]defpretty_symbol(symb_name):"""return pretty representation of a symbol"""# let's split symb_name into symbol + index# UC: beta1# UC: f_betaifnot_use_unicode:returnsymb_namename,sups,subs=split_super_sub(symb_name)deftranslate(s):gG=greek_unicode.get(s)ifgGisnotNone:returngGforkeyinsorted(modifier_dict.keys(),key=lambdak:len(k),reverse=True):ifs.lower().endswith(key)andlen(s)>len(key):returnmodifier_dict[key](translate(s[:-len(key)]))returnsname=translate(name)# Let's prettify sups/subs. If it fails at one of them, pretty sups/subs are# not used at all.defpretty_list(l,mapping):result=[]forsinl:pretty=mapping.get(s)ifprettyisNone:try:# match by separate characterspretty=''.join([mapping[c]forcins])except(TypeError,KeyError):returnNoneresult.append(pretty)returnresultpretty_sups=pretty_list(sups,sup)ifpretty_supsisnotNone:pretty_subs=pretty_list(subs,sub)else:pretty_subs=None# glue the results into one stringifpretty_subsisNone:# nice formatting of sups/subs did not workifsubs:name+='_'+'_'.join([translate(s)forsinsubs])ifsups:name+='__'+'__'.join([translate(s)forsinsups])returnnameelse:sups_result=' '.join(pretty_sups)subs_result=' '.join(pretty_subs)return''.join([name,sups_result,subs_result])

[docs]defannotated(letter):""" Return a stylised drawing of the letter ``letter``, together with information on how to put annotations (super- and subscripts to the left and to the right) on it. See pretty.py functions _print_meijerg, _print_hyper on how to use this information. """ucode_pics={'F':(2,0,2,0,u'\N{BOX DRAWINGS LIGHT DOWN AND RIGHT}\N{BOX DRAWINGS LIGHT HORIZONTAL}\n'u'\N{BOX DRAWINGS LIGHT VERTICAL AND RIGHT}\N{BOX DRAWINGS LIGHT HORIZONTAL}\n'u'\N{BOX DRAWINGS LIGHT UP}'),'G':(3,0,3,1,u'\N{BOX DRAWINGS LIGHT ARC DOWN AND RIGHT}\N{BOX DRAWINGS LIGHT HORIZONTAL}\N{BOX DRAWINGS LIGHT ARC DOWN AND LEFT}\n'u'\N{BOX DRAWINGS LIGHT VERTICAL}\N{BOX DRAWINGS LIGHT RIGHT}\N{BOX DRAWINGS LIGHT DOWN AND LEFT}\n'u'\N{BOX DRAWINGS LIGHT ARC UP AND RIGHT}\N{BOX DRAWINGS LIGHT HORIZONTAL}\N{BOX DRAWINGS LIGHT ARC UP AND LEFT}')}ascii_pics={'F':(3,0,3,0,' _\n|_\n|\n'),'G':(3,0,3,1,' __\n/__\n\\_|')}if_use_unicode:returnucode_pics[letter]else:returnascii_pics[letter]