pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin private pastebin - collaborative debugging tool What's a private pastebin?

Posted by frymaster on Wed 20th Jan 16:26
download | new post

  1. #!/usr/bin/python
  2. import sys,re
  3. lines = sys.stdin.readlines()
  4.  
  5. number = re.compile("^[0-9]+$")
  6. auto = False
  7.  
  8. def error():
  9.         print "Argument is an int or list of ints that specifies field(s) to humanize"
  10.         print "or, if you prefer, no argument for auto"
  11.         sys.exit(1)
  12.  
  13. # Parse input
  14. try:
  15.         sys.argv[1]
  16. except IndexError:
  17.         auto = True
  18. else:
  19.         uin = sys.argv[1:]
  20.         try:
  21.                 if len(uin) > 1:
  22.                         fields = [int(foo) for foo in uin]
  23.                 else:
  24.                         fields = [int(uin[0])]
  25.         except ValueError:
  26.                 error()
  27.  
  28. # Do the work
  29. def human(num):
  30.         for x in ['KB','MB','GB','TB']:
  31.                 if num < 1024.0:
  32.                         size = "%3.1f%s" % (num, x)
  33.                         break
  34.                 num /= 1024.0
  35.         return size
  36.  
  37. #Takes a string, returns a list of number positions
  38. def numbers(input):
  39.         out = []
  40.         i = 0
  41.         for word in input.split():
  42.                 if number.match(word):
  43.                         out.append(i)
  44.                 i = i + 1
  45.         return out
  46.  
  47.  
  48. for line in lines:
  49.         #print line.split()[field]
  50.         try:
  51.                 if auto:
  52.                         fields = numbers(line)
  53.  
  54.                 output = []
  55.                 listlines = line.split()
  56.                 i = 0
  57.                 for listline in listlines:
  58.                         if i in fields:
  59.                                 output.append(human(int(listline)))
  60.                         else:
  61.                                 output.append(listline)
  62.                         i = i + 1
  63.                 print " ".join(output)
  64.         except ValueError, e:
  65.                 # This line didn't meet our expectations
  66.                 # Just print it with no conversion
  67.                 #print e
  68.                 print line,

Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Use syntax highlighting

To highlight particular lines, prefix each line with @@


Remember my settings