2017-05-24 11:33:11 -04:00
|
|
|
from __future__ import unicode_literals
|
2017-02-01 11:49:54 -05:00
|
|
|
import six
|
|
|
|
|
|
|
|
|
2017-01-04 10:47:00 -05:00
|
|
|
def csv_format(data):
|
|
|
|
"""
|
|
|
|
Encapsulate any data which contains a comma within double quotes.
|
|
|
|
"""
|
|
|
|
csv = []
|
2017-02-01 11:49:54 -05:00
|
|
|
for value in data:
|
|
|
|
|
|
|
|
# Represent None or False with empty string
|
|
|
|
if value in [None, False]:
|
2017-05-24 11:33:11 -04:00
|
|
|
csv.append('')
|
2017-02-01 11:49:54 -05:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Force conversion to string first so we can check for any commas
|
|
|
|
if not isinstance(value, six.string_types):
|
2017-05-24 11:33:11 -04:00
|
|
|
value = '{}'.format(value)
|
2017-02-01 11:49:54 -05:00
|
|
|
|
|
|
|
# Double-quote the value if it contains a comma
|
2017-05-24 11:33:11 -04:00
|
|
|
if ',' in value:
|
|
|
|
csv.append('"{}"'.format(value))
|
2017-01-04 10:47:00 -05:00
|
|
|
else:
|
2017-05-24 11:33:11 -04:00
|
|
|
csv.append('{}'.format(value))
|
2017-02-01 11:49:54 -05:00
|
|
|
|
2017-05-24 11:33:11 -04:00
|
|
|
return ','.join(csv)
|
2017-05-15 12:56:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
def foreground_color(bg_color):
|
|
|
|
"""
|
|
|
|
Return the ideal foreground color (black or white) for a given background color in hexadecimal RGB format.
|
|
|
|
"""
|
|
|
|
bg_color = bg_color.strip('#')
|
2017-05-15 13:18:49 -04:00
|
|
|
r, g, b = [int(bg_color[c:c + 2], 16) for c in (0, 2, 4)]
|
2017-05-15 12:56:16 -04:00
|
|
|
if r * 0.299 + g * 0.587 + b * 0.114 > 186:
|
|
|
|
return '000000'
|
|
|
|
else:
|
|
|
|
return 'ffffff'
|