Utils.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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):
  8. for var in source:
  9. Utils.copyValueToStateTrace(target, var, step, iteration, source)
  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, source):
  22. assert target.has_key(var)
  23. if step == len(target[var]):
  24. target[var].append([source[var]])
  25. elif iteration == len(target[var][step]):
  26. target[var][step].append(source[var])
  27. else:
  28. target[var][step][iteration] = source[var]
  29. @staticmethod
  30. def trimList(listToTrim, target_size):
  31. assert target_size <= len(listToTrim)
  32. assert target_size >= 0
  33. while target_size < len(listToTrim):
  34. del listToTrim[-1]
  35. assert target_size == len(listToTrim)