Created
January 31, 2013 22:21
-
-
Save darkone23/4687174 to your computer and use it in GitHub Desktop.
get a random cow for use in `cowsay -f cow $msg`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import subprocess | |
| from random import randint | |
| COWSAY = "/usr/games/cowsay" | |
| def get_randomth(arr): | |
| return arr[randint(0, len(arr) - 1)] | |
| def get_critters(cowsay): | |
| (critters, err) = subprocess.Popen([cowsay, '-l'], stdout=subprocess.PIPE).communicate() | |
| if critters: | |
| animal_lines = critters.split('\n')[1:-1] # edges of stdout are garbage | |
| return [animal for animals in animal_lines for animal in animals.split(' ')] | |
| if __name__ == "__main__": | |
| available_critters = get_critters(COWSAY) | |
| print get_randomth(available_critters) | |
| # corutils version: | |
| # echo `/usr/games/cowsay -l | awk 'NR>1{ print $0 }' | xargs shuf -e | head -n 1` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment