def pretty(expr, **settings): """ Returns a string containing the prettified form of expr. Arguments --------- expr: the expression to print wrap_line: line wrapping enabled/disabled, should be a boolean value (default to True) num_columns: number of columns before line breaking (default to None which reads the terminal width), it is useful when using SymPy without terminal. use_unicode: use unicode characters, such as the Greek letter pi instead of the string pi. Values should be boolean or None full_prec: use full precision. Default to "auto" order: set to 'none' for long expressions if slow; default is None """ pp = PrettyPrinter(settings) # XXX: this is an ugly hack, but at least it works use_unicode = pp._settings['use_unicode'] uflag = pretty_use_unicode(use_unicode) try: return pp.doprint(expr) finally: pretty_use_unicode(uflag)
def pretty(expr, use_unicode=None): """ Returns a string containing the prettified form of expr. If use_unicode is set to True then certain expressions will use unicode characters, such as the greek letter pi for Pi instances. """ uflag = pretty_use_unicode(use_unicode) try: pp = PrettyPrinter() return pp.doprint(expr) finally: pretty_use_unicode(uflag)
def pretty(expr, **settings): """Returns a string containing the prettified form of expr. For information on keyword arguments see pretty_print function. """ pp = PrettyPrinter(settings) # XXX: this is an ugly hack, but at least it works use_unicode = pp._settings['use_unicode'] uflag = pretty_use_unicode(use_unicode) try: return pp.doprint(expr) finally: pretty_use_unicode(uflag)
def parens(self, left='(', right=')', ifascii_nougly=False): """Put parentheses around self. Returns string, baseline arguments for stringPict. left or right can be None or empty string which means 'no paren from that side' """ h = self.height() b = self.baseline # XXX this is a hack -- ascii parens are ugly! if ifascii_nougly and not pretty_use_unicode(): h = 1 b = 0 res = self if left: lparen = stringPict(vobj(left, h), baseline=b) res = stringPict(*lparen.right(self)) if right: rparen = stringPict(vobj(right, h), baseline=b) res = stringPict(*res.right(rparen)) return ('\n'.join(res.picture), res.baseline)
def _print_floor(self, e): if pretty_use_unicode(): pform = self._print(e.args[0]) pform = prettyForm(*pform.parens('lfloor', 'rfloor')) return pform else: return self._print_Function(e)
def _print_ceiling(self, e): if pretty_use_unicode(): pform = self._print(e.args[0]) pform = prettyForm(*pform.parens('lceil', 'rceil')) return pform else: return self._print_Function(e)
def pretty(expr, profile=None, **kargs): """ Returns a string containing the prettified form of expr. If use_unicode is set to True then certain expressions will use unicode characters, such as the greek letter pi for Pi instances. """ if profile is not None: profile.update(kargs) else: profile = kargs uflag = pretty_use_unicode(kargs.get("use_unicode", None)) try: pp = PrettyPrinter(profile) return pp.doprint(expr) finally: pretty_use_unicode(uflag)
def _print_gamma(self, e): if pretty_use_unicode(): pform = self._print(e.args[0]) pform = prettyForm(*pform.parens()) pform = prettyForm(*pform.left(greek['gamma'][1])) return pform else: return self._print_Function(e)
def pretty(expr, **settings): """ Returns a string containing the prettified form of expr. Arguments --------- expr: the expression to print wrap_line: line wrapping enabled/disabled, should be a boolean value (default to True) use_unicode: use unicode characters, such as the Greek letter pi instead of the string pi. Values should be boolean or None full_prec: use full precision. Default to "auto" """ uflag = pretty_use_unicode(settings.get("use_unicode", None)) try: pp = PrettyPrinter(settings) return pp.doprint(expr) finally: pretty_use_unicode(uflag)
def pretty(expr, **settings): """ Returns a string containing the prettified form of expr. Arguments --------- expr: the expression to print wrap_line: line wrapping enabled/disabled, should be a boolean value (default to True) use_unicode: use unicode characters, such as the Greek letter pi instead of the string pi. Values should be boolean or None full_prec: use full precision. Default to "auto" """ pp = PrettyPrinter(settings) # XXX: this is an ugly hack, but at least it works use_unicode = pp._settings['use_unicode'] uflag = pretty_use_unicode(use_unicode) try: return pp.doprint(expr) finally: pretty_use_unicode(uflag)
def parens(self, left='(', right=')', ifascii_nougly=False): """Put parentheses around self. Returns string, baseline arguments for stringPict. """ h = self.height() b = self.baseline # XXX this is a hack -- ascii parens are ugly! if ifascii_nougly and not pretty_use_unicode(): h = 1 b = 0 lparen = stringPict(vobj(left, h), baseline=b) rparen = stringPict(vobj(right, h), baseline=b) return lparen.right(self, rparen)
def _use_unicode(self): if self._settings['use_unicode']: return True else: return pretty_use_unicode()
def _print_Integral(self, integral): f = integral.function # Add parentheses if a sum and create pretty form for argument prettyF = self._print(f) # XXX generalize parents if f.is_Add: prettyF = prettyForm(*prettyF.parens()) # dx dy dz ... arg = prettyF for x, ab in integral.limits: prettyArg = self._print(x) # XXX qparens (parens if needs-parens) if prettyArg.width() > 1: prettyArg = prettyForm(*prettyArg.parens()) arg = prettyForm(*arg.right(' d', prettyArg)) # \int \int \int ... firstterm = True S = None for x, ab in integral.limits: # Create bar based on the height of the argument h = arg.height() H = h + 2 # XXX hack! ascii_mode = not pretty_use_unicode() if ascii_mode: H += 2 vint = vobj('int', H) # Construct the pretty form with the integral sign and the argument pform = prettyForm(vint) #pform.baseline = pform.height()//2 # vcenter pform.baseline = arg.baseline + ( H - h) // 2 # covering the whole argument if ab is not None: # Create pretty forms for endpoints, if definite integral prettyA = self._print(ab[0]) prettyB = self._print(ab[1]) if ascii_mode: # XXX hack # Add spacing so that endpoint can more easily be # identified with the correct integral sign spc = max(1, 3 - prettyB.width()) prettyB = prettyForm(*prettyB.left(' ' * spc)) spc = max(1, 4 - prettyA.width()) prettyA = prettyForm(*prettyA.right(' ' * spc)) pform = prettyForm(*pform.above(prettyB)) pform = prettyForm(*pform.below(prettyA)) #if ascii_mode: # XXX hack # # too much vspace beetween \int and argument # # but I left it as is # pform = prettyForm(*pform.right(' ')) if not ascii_mode: # XXX hack pform = prettyForm(*pform.right(' ')) if firstterm: S = pform # first term firstterm = False else: S = prettyForm(*S.left(pform)) pform = prettyForm(*arg.left(S)) return pform
def _print_Integral(self, integral): f = integral.function # Add parentheses if a sum and create pretty form for argument prettyF = self._print(f) # XXX generalize parents if f.is_Add: prettyF = prettyForm(*prettyF.parens()) # dx dy dz ... arg = prettyF for x,ab in integral.limits: prettyArg = self._print(x) # XXX qparens (parens if needs-parens) if prettyArg.width() > 1: prettyArg = prettyForm(*prettyArg.parens()) arg = prettyForm(*arg.right(' d', prettyArg)) # \int \int \int ... firstterm = True s = None for x,ab in integral.limits: # Create bar based on the height of the argument h = arg.height() H = h+2 # XXX hack! ascii_mode = not pretty_use_unicode() if ascii_mode: H += 2 vint= vobj('int', H) # Construct the pretty form with the integral sign and the argument pform = prettyForm(vint) #pform.baseline = pform.height()//2 # vcenter pform.baseline = arg.baseline + (H-h)//2 # covering the whole argument if ab is not None: # Create pretty forms for endpoints, if definite integral. # Do not print empty endpoints. if ab[0] is None: prettyA = prettyForm("") else: prettyA = self._print(ab[0]) if ab[1] is None: prettyB = prettyForm("") else: prettyB = self._print(ab[1]) if ascii_mode: # XXX hack # Add spacing so that endpoint can more easily be # identified with the correct integral sign spc = max(1, 3 - prettyB.width()) prettyB = prettyForm(*prettyB.left(' ' * spc)) spc = max(1, 4 - prettyA.width()) prettyA = prettyForm(*prettyA.right(' ' * spc)) pform = prettyForm(*pform.above(prettyB)) pform = prettyForm(*pform.below(prettyA)) #if ascii_mode: # XXX hack # # too much vspace beetween \int and argument # # but I left it as is # pform = prettyForm(*pform.right(' ')) if not ascii_mode: # XXX hack pform = prettyForm(*pform.right(' ')) if firstterm: s = pform # first term firstterm = False else: s = prettyForm(*s.left(pform)) pform = prettyForm(*arg.left(s)) return pform