ElectionDataAnalysis / electiondata

Tools for consolidation and analysis of raw election results from the most reliable sources -- the election agencies themselves.
Other
20 stars 5 forks source link

Convert or slugify file names #740

Closed sfsinger19103 closed 2 years ago

sfsinger19103 commented 2 years ago

From JOSS review:

Second, Windows/NTSF filenames have somewhat more restrictive rules than Linux/UNIX-based systems, and so (for example) this attempt to provide warnings based on information with multiple colons was not possible:

Traceback (most recent call last): File "[...]\electiontest\testing.py", line 3, in dl.load_all() File "[...]\electiondata.venv\lib\site-packages\electiondata-2.0-py3.9.egg\electiondata__init__.py", line 874, in load_all err = ui.report( File "[...]\electiondata.venv\lib\site-packages\electiondata-2.0-py3.9.egg\electiondata\userinterface__init__.py", line 728, in report with open(out_path, "a") as f: OSError: [Errno 22] Invalid argument: './output\load_all_1123_1355\Maricopa-County_test_Election: 2020 Primary; Jurisdiction: Maricopa County.warnings' There are a bunch of ways to handle the problem of turning arbitrary strings into valid filenames, see e.g., https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename. I'd suggest slugify() from https://github.com/un33k/python-slugify (via pip as python-slugify) for simplicity and ease of adjustment as desired:

from slugify import slugify slugify("Maricopa-County_test_Election: 2020 Primary; Jurisdiction: Maricopa County") 'maricopa-county-test-election-2020-primary-jurisdiction-maricopa-county' slugify("Maricopa-County_test_Election: 2020 Primary; Jurisdiction: Maricopa County", regexpattern=r'[^ A-z0-9-]+', lowercase=False) 'Maricopa-County_test_Election- 2020 Primary- Jurisdiction- Maricopa County'

sfsinger19103 commented 2 years ago

Closed in #744