python: get the print output in an exec statement

Since Python 3.4 there is a solution is the stdlib: https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout from io import StringIO from contextlib import redirect_stdout f = StringIO() with redirect_stdout(f): help(pow) s = f.getvalue() In older versions you can write a context manager to handle replacing stdout: import sys from io import StringIO import contextlib @contextlib.contextmanager def stdoutIO(stdout=None): old = sys.stdout … Read more

Java out.println() how is this possible?

static imports do the trick: import static java.lang.System.out; or alternatively import every static method and field using import static java.lang.System.*; Addendum by @Steve C: note that @sfussenegger said this in a comment on my Answer. “Using such a static import of System.out isn’t suited for more than simple run-once code.” So please don’t imagine that … Read more

Printing variables in Python 3.4

The syntax has changed in that print is now a function. This means that the % formatting needs to be done inside the parenthesis:1 print(“%d. %s appears %d times.” % (i, key, wordBank[key])) However, since you are using Python 3.x., you should actually be using the newer str.format method: print(“{}. {} appears {} times.”.format(i, key, … Read more

How do I print an IFrame from javascript in Safari/Chrome

Here is my complete, cross browser solution: In the iframe page: function printPage() { print(); } In the main page function printIframe(id) { var iframe = document.frames ? document.frames[id] : document.getElementById(id); var ifWin = iframe.contentWindow || iframe; iframe.focus(); ifWin.printPage(); return false; } Update: Many people seem to be having problems with this in versions of … Read more

Rewrite multiple lines in the console

On Unix, use the curses module. On Windows, there are several options: PDCurses: http://www.lfd.uci.edu/~gohlke/pythonlibs/ The HOWTO linked above recommends the Console module http://newcenturycomputers.net/projects/wconio.html http://docs.activestate.com/activepython/2.6/pywin32/win32console.html Simple example using curses (I am a total curses n00b): import curses import time def report_progress(filename, progress): “””progress: 0-10″”” stdscr.addstr(0, 0, “Moving file: {0}”.format(filename)) stdscr.addstr(1, 0, “Total progress: [{1:10}] {0}%”.format(progress * … Read more

Print not showing in ipython notebook

I had a similar printing problem when my first code cell was: import sys reload(sys) sys.setdefaultencoding(“utf-8”) Then I’ve commented the second and third lines like this: import sys #reload(sys) #sys.setdefaultencoding(“utf-8”) Reset the kernel and re-ran the program and now my print statements are working properly. Later on, I’ve found that when I was first having … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)