Guest User

Untitled

a guest
Feb 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. class Table:
  2. columns = []
  3. data = []
  4. shorthand = {'ml': 'Melee(Light)', 'mh': 'Melee(Heavy)', 'rl': 'Ranged(Light)', 'rh': 'Ranged(Heavy)',
  5. 'g': 'Gunnery', 'e': 'engaged', 's': 'short', 'm': 'medium', 'l': 'long', 'ex': 'extreme',
  6. 'st': 'strategic'}
  7.  
  8. def __init__(self, name, *columns):
  9. self.name = name
  10. self.columns = columns
  11.  
  12. def addRow(self, *values):
  13. self.data.append(values)
  14.  
  15. def translateField(self, field):
  16. if isinstance(field, int):
  17. return '{:,}'.format(field)
  18. else:
  19. f = str(field)
  20. if f in self.shorthand:
  21. return self.shorthand[f]
  22. else:
  23. return f
  24.  
  25. def toHTML(self):
  26. html = '<span class="wide" style="font-family: \'Bebas Neue\'; color: #2D4864; font-weight: 4; font-variant: uppercase; font-size: 18pt; line-height:20px;">' + self.name + '</span>'
  27. html += '\n<table class="wide">'
  28. html += '\n\t<thead>\n\t\t<tr>'
  29. colspan = str(len(self.columns))
  30. for c in self.columns:
  31. html += '<th>' + c + '</th>'
  32. html += '</tr>\n\t</thead>\n\t<tbody>'
  33. i = 0
  34. for row in self.data:
  35. if row[0] == 'subheader':
  36. if i % 2 == 0:
  37. html += '\n\t\t<tr></tr>'
  38. html += '\n\t\t<tr class="subheader"><td colspan="' + colspan + '">' + row[1] + '</td></tr>'
  39. else:
  40. html += '\n\t\t<tr>'
  41. for field in row:
  42. html += '<td>'+self.translateField(field)+'</td>'
  43. html += '</tr>'
  44. html += '\n\t<tbody>\n<table>'
  45. return html
  46.  
  47. # Initlializing the table
  48. table = Table('Table 1: Weapons', 'name', 'skill', 'dam', 'crit', 'range', 'encum', 'price', 'rarity', 'special');
  49. # Adding a subheader
  50. table.addRow('subheader', 'Melee Weapons')
  51. # Adding a normal row
  52. table.addRow('Lance', 'mh', 6, 4, 'e', 4, 600, 3, 'Accurate 1, Breach 1, Defensive 1')
  53.  
  54. # Printing the final table to console
  55. print(table.toHTML())
Add Comment
Please, Sign In to add comment