envs.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from jinja2 import Environment, FileSystemLoader
  2. from pathlib import Path
  3. def get_span(l:list, loc:int, width=None) -> int:
  4. "Get span of each value in index/column"
  5. def get_value(item):
  6. val = item[:width+1] if width is not None else item
  7. return val
  8. prev = get_value(l[loc-1])
  9. curr = get_value(l[loc])
  10. if len(l) == 1:
  11. # Index/column of size 1
  12. span = 1
  13. elif prev == curr:
  14. # Previous value is the same as current
  15. # --> hide (span=0)
  16. # The previous should have span>=2
  17. span = 0
  18. else:
  19. span = 1
  20. for nxt in l[loc+1:]:
  21. if get_value(nxt) != curr:
  22. break
  23. span += 1
  24. return span
  25. def is_last_group_row(n, index:list, level=None):
  26. "Check if iteration is the last of the group"
  27. curr = index[n]
  28. if not isinstance(curr, tuple):
  29. return False
  30. elif n == (index.shape[0] - 1):
  31. # Last of the whole frame
  32. return True
  33. n += 1
  34. next = index[n]
  35. if level == 0:
  36. return True
  37. elif level is None:
  38. return curr[0] != next[0]
  39. while curr[:level+1] == next[:level+1]:
  40. # fast forward to the span of the level
  41. try:
  42. next = index[n+1]
  43. n += 1
  44. except IndexError:
  45. # End of the dataframe
  46. return True
  47. # ie. ("blue", "car"), ("green", "car") --> True
  48. # ("blue", "car"), ("blue", "red") --> False
  49. return curr[0] != next[0]