cmiN

rmovie

Dec 7th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3.  
  4. import os
  5. import sys
  6. import time
  7. import random
  8. import argparse
  9.  
  10.  
  11. DELAY = 1    # sleep time in seconds for each round (with verbose)
  12. CLEAR = "clear" if "linux" in sys.platform else "cls"
  13.  
  14.  
  15. clear = lambda: os.system(CLEAR)
  16.  
  17.  
  18. def process(fin, count, shuffle, verbose):
  19.     # read all lines
  20.     lines = list()
  21.     for line in fin:
  22.         lines.append(line.strip())
  23.     fin.close()
  24.     # shuffle them
  25.     for ind in range(shuffle):
  26.         clear()
  27.         print("Shuffle #{}".format(ind + 1))
  28.         random.shuffle(lines)
  29.         if verbose:    # show them for each round
  30.             for line in lines:
  31.                 print(line)
  32.             time.sleep(1)
  33.     # extract the first `count` lines
  34.     if count > 1:
  35.         frm = ("s", "are")
  36.     else:
  37.         frm = ("", "is")
  38.     clear()
  39.     print("And the winner{} {}:".format(*frm))
  40.     for ind in range(1, min(count, len(lines)) + 1):
  41.         print("#{} {}".format(ind, lines[ind - 1]))
  42.  
  43.  
  44. def main():
  45.     parser = argparse.ArgumentParser(description="Random movie selector.")
  46.     parser.add_argument("-c", "--count", type=int, default=1,
  47.                         help="how many movies to select")
  48.     parser.add_argument("-s", "--shuffle", type=int, default=1,
  49.                         help="how many shuffles to make")
  50.     parser.add_argument("-v", "--verbose", action="store_true",
  51.                         help="show each round")
  52.     parser.add_argument("infile", nargs="?", type=argparse.FileType("r"),
  53.                         default=sys.stdin, help="input file")
  54.  
  55.     args = parser.parse_args()
  56.     process(args.infile, args.count, args.shuffle, args.verbose)
  57.  
  58.  
  59. if __name__ == "__main__":
  60.     main()
Add Comment
Please, Sign In to add comment