Also, how can I get the xsym("*") character to display correctly? When I do print xsym("*") it works fine, i.e., I get the small dot that looks nice. But in my printing methods where I build the string for the outputs, and I do something like: s += print_pydy(e.dict[k]) + xsym('*') + k.__str__()
It gives me an error because of the xsym('*') part: s += print_pydy(e.dict[k]) + xsym('*') + k.__str__() UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 11: ordinal not in range(128) Any suggestions on how to fix this? Thanks, ~Luke On May 28, 4:35 pm, Luke <hazelnu...@gmail.com> wrote: > Sebastian, > Thanks for the reply. I have read printer.py, str.py, and repr.py, > and am still confused as to how to properly customize Sympy's printing > system. > > Suppose I want to put the _print_myclass(self, e) code into my > subclass of StrPrinter. What methods should I keep in the classes > themselves? Anything? _sympystr_, _repr_, or _str_, or none of them? > > Finally, suppose I do put all the printing code for each of my classes > into my sublcass of StrPrinter. Will > inst = MyClass() > print inst > > automatically look into my subclass of StrPrinter for a method called > _print_MyClass()? Or do I need to define something like: > > def print_pydy(e): > pp = PyDyPrinter() > pp.doprint(e) > > And then call: > print_pydy(inst) > ? > > I don't feel the use of the printing system is clearly conveyed in the > documentation. More explicit examples would be helpful. I'm happy to > do this once I get to the point of understand how it works, but I'm > not there yet. > > Thanks, > ~Luke > > On May 28, 4:00 pm, Sebastian <basti...@gmail.com> wrote: > > > Hi Luke, > > > I think all your questions are answered in the docstring of > > printing/printer.py. There it tells you in which order it is tried to > > print an object. > > > 1) Let the object print itself if it has the method defined as printmethod. > > 2) Use the method defined in the Printer if available. > > 3) Use some fallback printer. > > > Luke wrote: > > > Sorry, I accidentally clicked send before I had finished. A few > > > questions: > > > > So here is part of my PyDyPrinter class: > > > class PyDyPrinter(StrPrinter): > > > printmethod = "_pydystr_" > > > ... > > > def _print_sin(self, e): > > > name = str(e.args[0]) > > > if name[0] == "q": > > > index = name[1] > > > return "s%s" % index > > > else: > > > return str(e) > > > ... > > > > And here is a convenience function. > > > > def print_pydy(e): > > > pp = PyDyPrinter() > > > return pp.doprint(e) > > > > Question 1) What does printmethod do / control? Does this control > > > what the name of my print methods in each of my classes needs to be? > > Is explained above. > > > Question 2) Do I need to have _sympystr_ defined in the classes I > > > wish to customize the printing for? Or should it be: _pydystr_, since > > > that is what is printmethod is defined to be. > > > If all your classes are handled in your own printer you don't need any > > additional methods.> Question 3) Do I put the printing code for my class > > into > > > myclassname._sympystr_ (or ._pydystr_), or does it go inside my > > > subclass of StrPrinter, PyDyPrinter? > > > That's your choice, use what you like more.> Question 4) If the printing > > code goes in my subclass of StrPrinter > > > (in my case PyDyPrinter), then do I just put in the _sympystr_ method > > > of my class something like: return print_pydy(self)? > > > Okay this influences question3. If you have one (or only a few) common > > base class, you can just overwrite the __str__ and __repr__ methods > > there. For an example look into core/basic.py into the Base class. > > Otherwise it may be better to just write the printing code into > > _sympystr_ methods in every class. > > > > I've read the printing documentation, and the wiki, and the mailing > > > list, and I'm still not clear how the Printing system works. In some > > > examples I have seen there is no printingmethod variable set, and I'm > > > not clear what it does exactly. Maybe somebody could post a complete > > > example, that would show their subclassing of StrPrinter, as well as > > > the code inside their cusstom class, and any other relevant code as a > > > complete example of how to properly customize the printing? > > > Just go through the printers implemented in the printing directory. > > Especially printer.py is useful and for some full implementation > > examples you can go through str.py and repr.py. > > > I hope this helps, > > Sebastian > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---