Luke wrote: > Sebastian, > Thanks again for the responses, I really appreciate it. I was a > little confused in your second example, see below. > > On May 28, 5:17 pm, Sebastian <basti...@gmail.com> wrote: >> Luke 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) >>> ? >> This is hard to tell without knowing how MyClass is defined. The point >> is if you want that "print MyClass " uses your printer MyClass.__str__ >> has to look like the following: >> >> MyClass: >> __str__(self): >> return MyPrinter.doprint(self) >> > > You mean > def __str__(self): > ... > > I am assuming.
it seems it's too late for me ;). I forgot all the "def" statements. > > >> This ensures that your class is printed using your own printer. >> >> But this might not be terribly useful, since all built-in sympy classes >> will still use the standard StrPrinter. So if you have something like: >> >>>>> print Sum(MyClass1, MyClass2) >> where Sum is the default sympy sum, MyClass would also be printed by the >> StrPrinter and your own printer would not be used. To make sure your >> printer is used for your classes by the standard StrPrinter you can >> define the _sympystr_ method. >> >> MyClass: >> _sympystr_(self): >> return MyPrinter.doprint(self) >> __str__(self): >> return StrPrinter.doprint(self) >> > > I don't understand why they wouldn't both be MyPrinter.doprint(self). > Is doing it the above way causing > print an_instance_of_MyClass > to call StrPrinter.doprint(self)? Which, in turn, would call > _sympystr_, right? Or am I mistaken and that isn't at all the > behavior. Yes, it doesn't matter if you use the StrPrinter or the MyPrinter in the "__str__" method. I just thought that you probably are inheriting from Basic and therefor the __str__ method will look like this. Then, as you said, StrPrinter will call MyPrinter and everything works without redefining __str__. > > >> If MyClass somehow inherits from sympy.core.Basic this specific __str__ >> method will already be defined. >> >> This should work perfectly as long as you don't overwrite methods of the >> default strPrinter in your own Printer. Because then sometimes your own >> printer and sometimes the default StrPrinter might be used for printing >> the same class. >> The simple solution would be to define a print_pydy function as you >> mentioned above and then you can simply forget about __str__ and >> _sympystr_. (Then it would make sense to define your own printmethod >> like "_pydystr_" that can be used by users of pydy who extend pydy by >> own classes). >> Obviously this has the disadvantage that "print PydyClass" will not work >> for printing but everytime "print_pydy(PydyClass)" has to be used. The >> only solution I see for this problem is to refactor the printing system >> in sympy a bit more and let the user define an own printer that will be >> used as default strPrinter. >> >> Hopefully this helps you, >> Sebastian >> > > It does help, thank you for your time. > > ~Luke > > >> >>> 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 -~----------~----~----~----~------~----~------~--~---