Contributing to a RPG with CSV

slaterslater profile image Anthony Slater ・2 min read

Recent events << looks over at covid19 >> have forced many of us to isolate indoors. My own DnD campaign has been slowly advancing through Zoom for the past six months. So this year, I figured I would start my Hacktoberfest by making a perception check (with advantage) and looking for game-related issues. I pretty quickly came across Mimic Tools:

GitHub logo Mimic-Tools / name-generation

Generation of character names

The issue they wanted addressed was how to display a report of tabular data from their github repo. The script region-checker.py could produce the desired report as HTML, but if you click on an HTML file in github... you see HTML. The solution was CSV. If you click on a CSV file in github, it's displayed as a nice, easy-to-read, zebra-striped table.

The region-checker.py already includes functions to iterate through all the important objects and builds strings of HTML where appropriate. I thought it would be easy enough to make a copy of said function and swap HTML syntax with CSV... But, as it turns out, CSV doesn't mean a string with commas in it; it means values separated by commas. After a little research, I found the python csv library and was almost ready to go.

If a table cell required a positive value, the ✓ character u'\u2713' was written. An X was written for negative values. One of the things requested was a cross symbol instead of a literal X.

I browsed through the Unicode Block “Dingbats” and it turns out the
u'\u2715' and ✓ u'\u2713' are effectively neighbours and should work well together.

I set these variables at the top of the document and I think if I were to do this over again I would move them down closer to where the reports are being built. There is also some code repetition during the loop of dictionary items.

html_output = """<html><table border="1"><tr><th>Region</th>"""
csv_output = [['Region'] +  report_headers]
for items in report_headers:
    html_output += "<th>{}</th>".format(items)
html_output += """</tr>"""
for report_item in report_dictionary:
    region = splitext(report_item)[0].capitalize()
    csv_row = [region]
    html_output += "<tr><td>{}</td>".format(region)
    for location in report_dictionary[report_item]: 
        if report_dictionary[report_item][location]:
            html_output += "<td>{}</td>".format(check)
            csv_row += [check]
            html_output += "<td>{}</td>".format(cross)
            csv_row += [cross]    
    html_output += "</tr>"
html_output += "</table></html>"

csv_file = open(csv_name, 'w', newline='', encoding='utf-8')
csv_writer = csv.writer(csv_file)
Enter fullscreen mode Exit fullscreen mode

I didn't want to disturb the existing code any more than was necessary, but I think I should have modified the loop to look like this:

for location in report_dictionary[report_item]: 
  symbol = check if report_dictionary[report_item][location] else cross
  html_output += "<td>{}</td>".format(symbol)
  csv_row += [symbol]
Enter fullscreen mode Exit fullscreen mode

Overall fixing this issue was pretty straightforward. I learned a little bit about building/writing CSV files in python and how they're displayed in github.

Fixes #52: Change report to produce CSV instead of HTML #55

  • region-checker.py creates a csv file in addition to the html report
  • added the ✕ to compliment the ✓


Editor guide