Contributing to a RPG with CSV
Tags: hacktoberfest, opensource, python
Published: 2020-10-22 23:56:34

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 Mimic-Tools/name-generation no-readme %}  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](https://github.com/Mimic-Tools/name-generation/blob/main/availability_report.csv), 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](https://docs.python.org/3/library/csv.html) 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”](https://www.compart.com/en/unicode/block/U+2700) 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. 
```python
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]
        else:
            html_output += "<td>{}</td>".format(cross)
            csv_row += [cross]    
    csv_output.append(csv_row)        
    html_output += "</tr>"
html_output += "</table></html>"

csv_file = open(csv_name, 'w', newline='', encoding='utf-8')
csv_writer = csv.writer(csv_file)
csv_writer.writerows(csv_output)
csv_file.close() 
```
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:
```python
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]
``` 
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.
{% github https://github.com/Mimic-Tools/name-generation/pull/55 %}