Yet another Python script
The answer of user3058846answer of user3058846 isn't bad, but it displays every sentences, every time. Here, I propose a script that output a random sentence from the Zen of Python:
from random import choice import subprocess proc = subprocess.Popen(('python', '-c', 'import this'), stdout=subprocess.PIPE,) # Get output of proc, split by newline sentences = [x for x in proc.communicate()[0].splitlines() if x != ''] print(choice(sentences)) In one line, for code-golf fans:
from random import choice;import subprocess;print(choice([x for x in subprocess.Popen("python -c 'import this'",shell=True,stdout=subprocess.PIPE).communicate()[0].split('\n') if x])) (Boooh, dirty.)
Examples:
>>> a() # <--- a is just the oneline above Explicit is better than implicit. >>> a() Although never is often better than *right* now. >>> a() Errors should never pass silently. >>> a() Special cases aren't special enough to break the rules. ## Another fun way in Python Thanks to @TheDoctor pour the idea :-) Silent the import output and then play with the pseudo-encrypted dict in the module.
import sys, random _stdout, sys.stdout = sys.stdout, open('/tmp/trash', 'w') # silent the stdout just for the import import this sys.stdout = _stdout lst = [] for x in this.s: if x in this.d: lst.append(this.d[x]) else: lst.append(x) # Then, example from the interpreter >>> random.choice(''.join(lst).split('\n')) 'Beautiful is better than ugly.' >>> random.choice(''.join(lst).split('\n')) 'Although never is often better than *right* now.' >>>