Q:

How to pass messages between emacs and a Python program? ( goal: trying to use emacs as a UI)

The situation. 
Say I have opened up a file in emacs, and I type in something like:
x \and \top
Then, a Python program reads that file, and:

replaces all \and to ∧
replaces all \top to T
it does some thinking on the expression x \and \top and decides that that is the same as x, so will append an = x

The final result of all the changes made by the program to the file will leave it like so:
x ∧ T = x
Question: how should I pass messages between emacs and a Python program? 
I don't want the Python program to be constantly polling the file for changes (using a while loop), and I probably don't want emacs to be constantly polling the file for updates (which I know how to do using the auto-revert command).  
Maybe I press some key combination, and then that sends a message to a Python script that its time to read the file and make updates and then the  Python script would message emacs and ask it to update what it is displaying in its buffer.  
How could this be done? 
My goal: I want to take advantage of the fact that emacs knows how to intelligently display pixels on a screen in an interactive way (it can manage dividing up the screen space, display text and allow editing, syntax highlighting, etc.).  I want to take advantage of my knowledge of Python to do heavy programming (rather than using emacs Lisp), and emacs would be the UI for this program.  I view communication between emacs, and the Python program as crucial to achieving this goal.  If there is a better way to think about this though, I'd love to learn (for instance, maybe I really should be involving emacs Lisp a lot more, and eliminating Python entirely?). 

A:

you could check out https://github.com/tkf/python-epc
or if you like org-mode:
https://github.com/gregsexton/ob-ipython
(actually the regular python support in org-mode might be sufficient.  You can run sessions so you can have separate "blocks" of code).