jgtrosh

boxes.py with correctish use of oop

Aug 5th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. import os
  2. class box:
  3.     def __init__(self, text):
  4.         t = []
  5.         maxlen = []
  6.         text = text.split("[p]")
  7.         for i in range(len(text)):
  8.             text[i] = text[i].split('[l]')
  9.             maxlen.append(max(len(line) for line in text[i]))
  10.             if i == 0:
  11.                 t.append(' '+'_'*(maxlen[i]+2))
  12.             elif maxlen[i-1] < maxlen[i]:
  13.                 t.append('|'+'_'*(maxlen[i-1]+2)+'|'+'_'*(maxlen[i]-maxlen[i-1]-1))
  14.             else:
  15.                 t.append('|'+'_'*(maxlen[i-1]+2)+'|')
  16.             t.append('|'+' '*(maxlen[i]+2)+'|')
  17.             for line in text[i]:
  18.                 t.append('| '+line+' '*(maxlen[i]+1-len(line))+'|')
  19.         t.append('|'+'_'*(maxlen[len(text)-1]+2)+'|')
  20.         self.t = t
  21.         del t, maxlen, i
  22.     def output(self):
  23.         for line in self.t:
  24.             print(line)
  25.  
  26. if __name__ == '__main__':
  27.     a = box('this is the first box\
  28. [l]as you can see\
  29. [p]now you\'re reading the content from the second box\
  30. [p]we just switched to the third box\
  31. [l]not as large a box\
  32. [l]though slightly higher\
  33. [l]you can see that even though\
  34. [l]it goes on and on\
  35. [l]nothing broke yet\
  36. [l](well i hope so at least)\
  37. [p]anyway we reached the last box\
  38. [l]so it\'s time for good byes')
  39.     a.output()
  40.     if 'win' in os.sys.platform:
  41.         os.system('PAUSE')
  42.  
  43. ===========================================================
  44.  
  45. $ python boxes.py
  46.  _______________________
  47. |                       |
  48. | this is the first box |
  49. | as you can see        |
  50. |_______________________|____________________________
  51. |                                                    |
  52. | now you're reading the content from the second box |
  53. |____________________________________________________|
  54. |                                   |
  55. | we just switched to the third box |
  56. | not as large a box                |
  57. | though slightly higher            |
  58. | you can see that even though      |
  59. | it goes on and on                 |
  60. | nothing broke yet                 |
  61. | (well i hope so at least)         |
  62. |___________________________________|
  63. |                                |
  64. | anyway we reached the last box |
  65. | so it's time for good byes     |
  66. |________________________________|
Advertisement
Add Comment
Please, Sign In to add comment