source code of /small-scripts/ppt.py

Last modified
Lines 52

Parent directory Download CGIread sitemap Main page

Quick links: count_holes main print_byte

  1. #!/usr/bin/python
  2. '''
  3. Reads from stdin, writes to stdout.
  4. - BEGIN
  5. - | 7 6 5 4 | 3 2 1 0 |
  6.  7|   -   - |   -     |0
  7. - END
  8. '''
  9. import sys
  10. def count_holes(s):
  11.     holes = 0
  12.     for c in s:
  13.         x = ord(c)
  14.         for i in range(8):
  15.             if x & (1<<i):
  16.                 holes += 1
  17.     return holes
  18. def print_byte(c):
  19.     x = ord(c)
  20.     sys.stdout.write(' 7|')
  21.     for i in range(7, -1, -1):
  22.         if i == 3:
  23.             sys.stdout.write(' | ')
  24.         else:
  25.             sys.stdout.write(' ')
  26.         if x & (1<<i):
  27.             sys.stdout.write('-')
  28.         else:
  29.             sys.stdout.write(' ')
  30.     sys.stdout.write(' |0\n')
  31. def main():
  32.     content = sys.stdin.read()
  33.     
  34.     sys.stdout.write('- BEGIN            BEGIN\n')
  35.     sys.stdout.write('- | 7 6 5 4 | 3 2 1 0 | \n')
  36.     
  37.     for c in content:
  38.         print_byte(c)
  39.     
  40.     sys.stdout.write('- END                END\n')
  41.     
  42.     sys.stderr.write('{} Holes\n'.format(count_holes(content)))
  43. if __name__ == '__main__':
  44.     main()