utils.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import unicode_literals
  2. import six
  3. def csv_format(data):
  4. """
  5. Encapsulate any data which contains a comma within double quotes.
  6. """
  7. csv = []
  8. for value in data:
  9. # Represent None or False with empty string
  10. if value in [None, False]:
  11. csv.append('')
  12. continue
  13. # Force conversion to string first so we can check for any commas
  14. if not isinstance(value, six.string_types):
  15. value = '{}'.format(value)
  16. # Double-quote the value if it contains a comma
  17. if ',' in value:
  18. csv.append('"{}"'.format(value))
  19. else:
  20. csv.append('{}'.format(value))
  21. return ','.join(csv)
  22. def foreground_color(bg_color):
  23. """
  24. Return the ideal foreground color (black or white) for a given background color in hexadecimal RGB format.
  25. """
  26. bg_color = bg_color.strip('#')
  27. r, g, b = [int(bg_color[c:c + 2], 16) for c in (0, 2, 4)]
  28. if r * 0.299 + g * 0.587 + b * 0.114 > 186:
  29. return '000000'
  30. else:
  31. return 'ffffff'