You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

  1. import datetime
  2. from pydoc import pager
  3. def str_coerce(s, **kwargs):
  4. if isinstance(s, datetime.datetime):
  5. return s.strftime(kwargs['datetime_format'])
  6. else:
  7. return str(s)
  8. def print2d(list_param,
  9. datetime_format="%A, %B %e, %Y %H:%M:%S",
  10. seperator=' | ',
  11. spacer=' ',
  12. bottom='=',
  13. l_end='|', r_end='|',
  14. interactive=False
  15. ):
  16. list_param = [[str_coerce(s, datetime_format=datetime_format)
  17. for s in row] for row in list_param]
  18. max_col = []
  19. for row in list_param:
  20. for i, col in enumerate(row):
  21. try:
  22. max_col[i] = max(max_col[i], len(col))
  23. except IndexError:
  24. max_col.append(len(col))
  25. fmt_row = '{content}'
  26. if l_end:
  27. fmt_row = '{} {}'.format(l_end, fmt_row)
  28. if r_end:
  29. fmt_row = '{} {}'.format(fmt_row, r_end)
  30. done = []
  31. for row in list_param:
  32. content = seperator.join(col.ljust(max_col[i], spacer if i < len(
  33. row)-1 or r_end else ' ') for i, col in enumerate(row))
  34. done.append(fmt_row.format(content=content))
  35. if bottom:
  36. bottom = bottom*len(done[0])
  37. row_sep = ('\n'+bottom+'\n')
  38. else:
  39. row_sep = '\n'
  40. final = row_sep.join(done)
  41. if bottom:
  42. final = '\n'.join((bottom, final, bottom))
  43. if interactive:
  44. if not bottom:
  45. final += '\n'
  46. pager(final)
  47. else:
  48. return final