On Thu, Jun 25, 2009 at 1:13 PM, Luke<[email protected]> wrote:
>
> This code will replicate the problem when run using the latest sympy
> pull:
>
> from sympy import Basic
> from sympy.printing.str import StrPrinter
> from sympy.printing.pretty.pretty import PrettyPrinter, xsym, pprint
>
> class GreenEggsAndHam(Basic):
> def __init__(self, string):
> self.s = string
>
> def _pretty_(self):
> return print_GreenEggsAndHam(self)
>
> class HamPrinter(PrettyPrinter):
> printmethod = '_pretty_'
> def _print_GreenEggsAndHam(self, e):
> return e.s.lower() + xsym('*') + "\xC2\xB7"
>
> def print_GreenEggsAndHam(e):
> pp = HamPrinter()
> return pp.doprint(e)
>
>
> MyBreakfast = GreenEggsAndHam('I LOVE SYMPY')
> print_GreenEggsAndHam(MyBreakfast)
> pprint(MyBreakfast)
it's because you have overridden *both* _pretty_ and
_print_GreenEggsAndHam(). You only should override one of those, then
it will work.
It's true we should improve the Printer code to handle such case too,
patches welcome.
Another problem is that if you use unicode, you have to change
"\xC2\xB7" to u"\xC2\xB7"
Yet another problem with the above code is that PrettyPrinter expects
StringPict() instance, so you can fake it by your own class that
implements render.
Another problem is that print_GreenEggsAndHam returns the result, but
you want to print it. Anyways, with all the above things fixed, it now
prints:
i love sympy⋅·
GreenEggsAndHam(I LOVE SYMPY)
Is this what you want?
The fixed script is below.
Ondrej
--------------------
from sympy import Basic
from sympy.printing.str import StrPrinter
from sympy.printing.pretty.pretty import PrettyPrinter, xsym, pprint
class GreenEggsAndHam(Basic):
def __init__(self, string):
self.s = string
#def _pretty_(self):
# return print_GreenEggsAndHam(self)
class HamPrinter(PrettyPrinter):
printmethod = '_pretty_'
def _print_GreenEggsAndHam(self, e):
class Fake(object):
def render(self, *args, **kwargs):
return e.s.lower() + xsym('*') + u"\xC2\xB7"
return Fake()
def print_GreenEggsAndHam(e):
pp = HamPrinter()
return pp.doprint(e)
MyBreakfast = GreenEggsAndHam('I LOVE SYMPY')
print print_GreenEggsAndHam(MyBreakfast)
pprint(MyBreakfast)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---