Skip to content

Instantly share code, notes, and snippets.

@todun
Forked from AgustinLado/cowzen.py
Created August 3, 2019 08:24
Show Gist options
  • Save todun/a586c05415b2bf5bc81c8c3e4dac4564 to your computer and use it in GitHub Desktop.
Save todun/a586c05415b2bf5bc81c8c3e4dac4564 to your computer and use it in GitHub Desktop.

Revisions

  1. @AgustinLado AgustinLado renamed this gist Jun 29, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @AgustinLado AgustinLado created this gist Jun 29, 2015.
    31 changes: 31 additions & 0 deletions cowzen
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    # Get a random line from the Zen of Python without a print (preferably for a colorful cow to chant!)

    # Replace stdout by an anonymous class that returns nothing on write()
    import sys
    stdout = sys.stdout
    sys.stdout = type('BlackHole', (), {'write': (lambda self, string: '')})()

    # This import's output has now been supressed
    import this

    # Restate stdout
    sys.stdout = stdout

    # Get a list of this' zen-lines and print a random one.
    import random
    print random.choice(''.join([this.d.get(i, i) for i in this.s]).splitlines()[2:])


    # Condensed into a line:
    import sys, random; stdout = sys.stdout; sys.stdout = type('BlackHole', (), {'write': (lambda self, string: '')})(); import this; sys.stdout = stdout; print random.choice(''.join([this.d.get(i, i) for i in this.s]).splitlines()[2:]);




    # For the colorful cow run in a shell and pipe to cowsay and lolcat :P
    # python -c "import sys, random; stdout = sys.stdout; sys.stdout = type('BlackHole', (), {'write': (lambda self, string: '')})(); import this; sys.stdout = stdout; print random.choice(''.join([this.d.get(i, i) for i in this.s]).splitlines()[2:]);" | cowsay | lolcat

    # cowsay: Likely to be in your distro's repository (e.g. sudo apt-get cowsay)
    # lolcat: It's a Ruby gem. Make sure you have Ruby installed and run gem install lolcat

    # Add to .bashrc for ultimate zen-ness