Typing
import this
will return the Zen of Python, but nowhere do I seem to be able find a solution about how to set it equal to a string variable which I can use further on in my code...
Typing
import this
will return the Zen of Python, but nowhere do I seem to be able find a solution about how to set it equal to a string variable which I can use further on in my code...
You can temporarily redirect stdout
to a StringIO
instance, import this
, and then get its value.
>>> import sys, cStringIO
>>> zen = cStringIO.StringIO()
>>> old_stdout = sys.stdout
>>> sys.stdout = zen
>>> import this
>>> sys.stdout = old_stdout
>>> print zen.getvalue()
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
This code works on python2.7 -- for python 3, use io.StringIO
instead of cStringIO.StringIO
, and also have a look at contextlib.redirect_stdout
which was added in 3.4. That would look like this:
>>> import contextlib, io
>>> zen = io.StringIO()
>>> with contextlib.redirect_stdout(zen):
... import this
...
>>> print(zen.getvalue())
In Python3.8+ you can also do:
>>> import contextlib, io
>>> with contextlib.redirect_stdout(zen := io.StringIO()):
... import this
...
>>> print(zen.getvalue())
Let's look at what this.py
does:
s = "some encrypted string"
d = a map to decrypt the string
print "".join([d.get(c, c) for c in s])
Let's note that the encryption is just ROT13.
So if we really wanted to grab the string, we could do:
import this
s = this.s.decode('rot13')
Or, to explicitly follow the style of the this.py
module...
import this
s = "".join([this.d.get(c, c) for c in this.s])
I think the accepted answer is overcomplicated for this case (while very interesting for capturing the standard output in general).
In Python 3, you can get the Zen of Python as a string by simply doing:
import this
import codecs
zen_of_python = codecs.encode(this.s, 'rot13')
the string is stored in this.s
, but
that's funny, because it's encrypted:
>>> help(this)
NAME
this
FILE
/usr/lib/python2.7/this.py
MODULE DOCS
http://docs.python.org/library/this
DATA
c = '!'
d = {'A': 'N', 'B': 'O', 'C': 'P', 'D': 'Q', 'E': 'R', 'F': 'S', 'G': ...
i = 25
s = "Gur Mra bs Clguba, ol Gvz Crgref\n\nOrnhgvshy vf o...bar ubaxvat ...
$ head /usr/lib/python2.7/this.py
s = """Gur Mra bs Clguba, ol Gvz Crgref
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.