Utils.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. '''
  2. Created on Mar 5, 2016
  3. @author: claudio gomes
  4. '''
  5. class Utils(object):
  6. @staticmethod
  7. def copyMapToStateTrace(target, step, iteration, source, ensureExists):
  8. for var in source:
  9. Utils.copyValueToStateTrace(target, var, step, iteration, source[var], ensureExists)
  10. @staticmethod
  11. def getValuesUpToDate(source, vars_to_select, step, iteration):
  12. result = {}
  13. for var in vars_to_select:
  14. # Only up-to-date values go in the calculations.
  15. # If a calcfunction crash, it's because this FMU is not being used correctly
  16. if step < len(source[var]):
  17. if iteration < source[var][step]:
  18. result[var] = source[var][step][iteration]
  19. return result
  20. @staticmethod
  21. def copyValueToStateTrace(target, var, step, iteration, value, ensureExists):
  22. if ensureExists:
  23. assert target.has_key(var)
  24. elif not target.has_key(var):
  25. return
  26. assert step <= len(target[var])
  27. if step == len(target[var]):
  28. target[var].append([value])
  29. elif step < len(target[var]):
  30. assert step == len(target[var]) - 1, "Makes no sense to rewrite past steps, without rolling back first. len(target[var])={}".format(len(target[var]))
  31. if iteration == len(target[var][step]):
  32. target[var][step].append(value)
  33. elif iteration < len(target[var][step]):
  34. assert iteration == len(target[var][step]) - 1, "Weird use of the iteration records. Either rewrite the last iteration, or never rewrite them (in case you want to keep track of them."
  35. target[var][step][iteration] = value
  36. """
  37. elif iteration == len(target[var][step]):
  38. target[var][step].append(value)
  39. else:
  40. target[var][step][iteration] = value
  41. """
  42. @staticmethod
  43. def trimList(listToTrim, target_size):
  44. assert target_size >= 0
  45. if target_size > len(listToTrim):
  46. return
  47. while target_size < len(listToTrim):
  48. del listToTrim[-1]
  49. assert target_size == len(listToTrim)