Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Table:
- columns = []
- data = []
- shorthand = {'ml': 'Melee(Light)', 'mh': 'Melee(Heavy)', 'rl': 'Ranged(Light)', 'rh': 'Ranged(Heavy)',
- 'g': 'Gunnery', 'e': 'engaged', 's': 'short', 'm': 'medium', 'l': 'long', 'ex': 'extreme',
- 'st': 'strategic'}
- def __init__(self, name, *columns):
- self.name = name
- self.columns = columns
- def addRow(self, *values):
- self.data.append(values)
- def translateField(self, field):
- if isinstance(field, int):
- return '{:,}'.format(field)
- else:
- f = str(field)
- if f in self.shorthand:
- return self.shorthand[f]
- else:
- return f
- def toHTML(self):
- 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>'
- html += '\n<table class="wide">'
- html += '\n\t<thead>\n\t\t<tr>'
- colspan = str(len(self.columns))
- for c in self.columns:
- html += '<th>' + c + '</th>'
- html += '</tr>\n\t</thead>\n\t<tbody>'
- i = 0
- for row in self.data:
- if row[0] == 'subheader':
- if i % 2 == 0:
- html += '\n\t\t<tr></tr>'
- html += '\n\t\t<tr class="subheader"><td colspan="' + colspan + '">' + row[1] + '</td></tr>'
- else:
- html += '\n\t\t<tr>'
- for field in row:
- html += '<td>'+self.translateField(field)+'</td>'
- html += '</tr>'
- html += '\n\t<tbody>\n<table>'
- return html
- # Initlializing the table
- table = Table('Table 1: Weapons', 'name', 'skill', 'dam', 'crit', 'range', 'encum', 'price', 'rarity', 'special');
- # Adding a subheader
- table.addRow('subheader', 'Melee Weapons')
- # Adding a normal row
- table.addRow('Lance', 'mh', 6, 4, 'e', 4, 600, 3, 'Accurate 1, Breach 1, Defensive 1')
- # Printing the final table to console
- print(table.toHTML())
Add Comment
Please, Sign In to add comment