Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion of Printing to Python 3 #38

Closed
ghost opened this issue Sep 30, 2019 · 8 comments
Closed

Conversion of Printing to Python 3 #38

ghost opened this issue Sep 30, 2019 · 8 comments
Labels
component: printer string, latex, and ansi escape printing

Comments

@ghost
Copy link

ghost commented Sep 30, 2019

Thank you for picking up the torch with regard to galgebra. I just did not have enough time to work on it as required. I have a question about your conversion to python 3. I am trying to use your version and it looks like the python 3 print function is not overloaded. That is if A is a multivector or other object in galgebra the statement "print(A)" does not work. Is this correct?

@utensil
Copy link
Member

utensil commented Oct 1, 2019

Thank you for picking up the torch with regard to galgebra. I just did not have enough time to work on it as required.

So glad to see you again! How is everything going?

GAlgebra is a great piece of work and we are honored to do something about keeping it on.

And due to your time constraint and our enthusiasm in GA, we are happy to implement any features you want as long as you specify them and we're up to the task in the sense of Mathematical sophistication.

it looks like the python 3 print function is not overloaded. That is if A is a multivector or other object in galgebra the statement "print(A)" does not work. Is this correct?

It's overloaded for enhanced printing (to my knowledge) and I verified the 4 ways of printing that you designed.

First, printing plain texts. (I don't have an example at hand, will provide later)

Secondly, if called Eprint() upfront, print() will do Enhanced Console Printing(colored printing with ANSI escape sequences):

Thirdly, if called Format() in Jupyter Notebook upfront, print() will do LaTeX printing with MathJax. In a Jupyter Notebook upfront, without calling print(), it will do LaTeX printing with MathJax for Geometric Algebra expressions.

Finally, if called Format() upfront and xpdf() eventually, print() will do LaTeX printing (internally, the standard output will be redirected to a buffer for later consumption):

@ghost
Copy link
Author

ghost commented Oct 1, 2019

I took dop.ipynb from your ipython example and changed the line in cell 3 from "f" to "print(f)" and saved the executed and checkpoint ipynb file in dop.zip. There were no errors generated and "print(f)" produced no output. I used jupyterlab and was running python 3 as indicated by jupyterlab.

dop.zip

Do you have any ideas as to what is happening?

@ghost
Copy link
Author

ghost commented Oct 1, 2019

In looking at your code in printer for redirect and restore in GAlatexprinter I did not see the builtin print function redefined to overload it. I also have worked on a python 3 version of galgebra. Shown below is the critical code in the GALatexPrinter class for overloading the print function. Any comments would be appreciated.

@staticmethod
def redirect():
    GaLatexPrinter.latex_str = ''
    GaLatexPrinter.text_printer = print   #Save original print function
    builtins.print = printer.latex_print  #Redefine original print function
    GaLatexPrinter.latex_flg = True
    GaLatexPrinter.Basic__str__ = Basic.__str__
    GaLatexPrinter.Matrix__str__ = Matrix.__str__
    Basic.__str__ = lambda self: GaLatexPrinter().doprint(self)
    Matrix.__str__ = lambda self: GaLatexPrinter().doprint(self)

    return

@staticmethod
def restore():
    if GaLatexPrinter.latex_flg:
        builtins.print = GaLatexPrinter.text_printer  #Redefine orginal print function
        GaLatexPrinter.latex_flg = False
        Basic.__str__ = GaLatexPrinter.Basic__str__
        Matrix.__str__ = GaLatexPrinter.Matrix__str__
    return

@utensil
Copy link
Member

utensil commented Oct 2, 2019

I took dop.ipynb from your ipython example and changed the line in cell 3 from "f" to "print(f)" and saved the executed and checkpoint ipynb file in dop.zip. There were no errors generated and "print(f)" produced no output. I used jupyterlab and was running python 3 as indicated by jupyterlab.

dop.zip

Do you have any ideas as to what is happening?

I consider this to be the expected behavior of your design. Calling Format() has the effect of redirecting standard output to a buffer object, so printing to standard output using print() will have no output as you had called Format() upfront. And if you do not call Format(), print() will work normally and output in the form of plain texts.

I've just made a complete example for 4 ways to print in GAlgebra: https://nbviewer.jupyter.org/github/utensil/julia-playground/blob/master/py/printing-galgebra.ipynb and hopes it clarify the confusions.

@ghost
Copy link
Author

ghost commented Oct 2, 2019

I modified both printer.py and dop.ipynb in your python 3 distribution both are in
new_printer.zip
I ran dop.ipynb using the new printer and saved at checkpoint after executing. This is the behavior I would want the print function to have in jupyter notebook/lab if "Format()" is called. The reason is that it makes if easy to annotate an output cell as can be seen in dop.ipynb or to print multiple objects in one cell. Also the code for outputting LaTeX is then the same in an pure python program or in a notebook using python. What do you think?

@utensil
Copy link
Member

utensil commented Oct 2, 2019

Oh, that's what you mean by overloading the print function.

I'm currently out for vocation, but I'll evaluate and merge this ASAP.

Sent with GitHawk

utensil added a commit that referenced this issue Oct 7, 2019
utensil added a commit that referenced this issue Oct 7, 2019
Co-authored-by: Alan Bromborsky <[email protected]>
@ghost
Copy link
Author

ghost commented Oct 12, 2019

No rush. My objective was to make printing seem as much as possible like python 3 printing. Also, overloading the print function allows notebook printing to be the same as in normal (.py) python files. In addition you can use multiple print function calls in a single notebook cell and print multiple objects in a notebook output cell.

@eric-wieser eric-wieser added the component: printer string, latex, and ansi escape printing label Apr 30, 2020
@utensil
Copy link
Member

utensil commented Jun 2, 2020

This would be tracked in #39 .

@utensil utensil closed this as completed Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: printer string, latex, and ansi escape printing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants