Advertisement
cmiN

subprocess-tester

Apr 12th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # Testing "tool" for popen, fork, exec, system, etc
  3. # 12.04.2012 cmiN
  4.  
  5.  
  6. import sys
  7. from time import ctime, sleep
  8. from subprocess import call
  9.  
  10.  
  11. # some miscellaneous
  12. clear = lambda: call(["clear" if "linux" in sys.platform else "cls"])
  13. flush = [lambda: sys.stdout.flush(), lambda: sys.stderr.flush()]
  14.  
  15.  
  16. def mode1():
  17.     """Used only for output."""
  18.     for i in xrange(10):
  19.         print i
  20.         #flush[0]()
  21.         #flush[1]()
  22.         # as you can see there is no flush
  23.         sleep(2)
  24.  
  25.  
  26. def mode2():
  27.     """For both input and output."""
  28.     menu = ["1) Quit",
  29.             "2) Show date and time"]
  30.     data = "\n".join(menu) # preserve beauty for large lists
  31.     while True:
  32.         print data
  33.         flush[0]()
  34.         flush[1]()
  35.         try:
  36.             op = int(raw_input("Option: "))
  37.         except ValueError:
  38.             op = 0
  39.         #print "after" # odd
  40.         flush[0]()
  41.         if op == 1:
  42.             break
  43.         elif op == 2:
  44.             print ctime()
  45.         else:
  46.             sys.stderr.write("You mothafucka -,-.\n")
  47.         # flush data right after it's printed again
  48.         flush[0]()
  49.         flush[1]()
  50.         raw_input() # pause
  51.         clear()
  52.  
  53.  
  54. def main(argv):
  55.     if len(argv) != 2:
  56.         print "Usage: %s mode" % argv[0]
  57.         print "\nModes:\n    1    stdout only\n    2    full i/o"
  58.         return
  59.     try:
  60.         mode = int(argv[1])
  61.     except ValueError:
  62.         mode = 0
  63.     if mode == 1:
  64.         mode1()
  65.     elif mode == 2:
  66.         mode2()
  67.     else:
  68.         print "Invalid mode."
  69.  
  70.  
  71. if __name__ == "__main__":
  72.     main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement