tools.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import time
  2. def count(testFn, items):
  3. s = 0
  4. for x in items:
  5. if testFn(x):
  6. s += 1
  7. return s
  8. def indent(text, i=" "):
  9. result = ""
  10. for x in text.split("\n"):
  11. result += i + x + "\n"
  12. return result
  13. def indentPair(a, b, e="", i=" "):
  14. m = a + " "
  15. if len(m) < 32:
  16. m += "_" * (32 - len(m))
  17. if len(b) < 16:
  18. m += "_" * (10 - len(b))
  19. m += " "
  20. m += b
  21. if e == False:
  22. pass
  23. elif e == True:
  24. m += " *"
  25. else:
  26. m += e
  27. print(" " + m)
  28. class Timing:
  29. def __init__(self, name="Duration"):
  30. self.name = name
  31. self.tStart = time.process_time()
  32. self.tStepStart = self.tStart
  33. def step(self, message=""):
  34. now = time.process_time()
  35. duration = now - self.tStart
  36. durationStep = now - self.tStepStart
  37. self.tStepStart = now
  38. if message == "":
  39. print(f"{self.name}: {durationStep:0.5f} / {duration:0.3f}s")
  40. else:
  41. print(f"{self.name} ({message}): {durationStep:0.5f} / {duration:0.3f}s")
  42. return duration