1
0

Duration.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. """Duration module."""
  2. import operator
  3. from matplotlib import cbook
  4. class Duration:
  5. """Class Duration in development.
  6. """
  7. allowed = ["ET", "UTC"]
  8. def __init__(self, frame, seconds):
  9. """Create a new Duration object.
  10. = ERROR CONDITIONS
  11. - If the input frame is not in the allowed list, an error is thrown.
  12. = INPUT VARIABLES
  13. - frame The frame of the duration. Must be 'ET' or 'UTC'
  14. - seconds The number of seconds in the Duration.
  15. """
  16. cbook._check_in_list(self.allowed, frame=frame)
  17. self._frame = frame
  18. self._seconds = seconds
  19. def frame(self):
  20. """Return the frame the duration is in."""
  21. return self._frame
  22. def __abs__(self):
  23. """Return the absolute value of the duration."""
  24. return Duration(self._frame, abs(self._seconds))
  25. def __neg__(self):
  26. """Return the negative value of this Duration."""
  27. return Duration(self._frame, -self._seconds)
  28. def seconds(self):
  29. """Return the number of seconds in the Duration."""
  30. return self._seconds
  31. def __bool__(self):
  32. return self._seconds != 0
  33. def __eq__(self, rhs):
  34. return self._cmp(rhs, operator.eq)
  35. def __ne__(self, rhs):
  36. return self._cmp(rhs, operator.ne)
  37. def __lt__(self, rhs):
  38. return self._cmp(rhs, operator.lt)
  39. def __le__(self, rhs):
  40. return self._cmp(rhs, operator.le)
  41. def __gt__(self, rhs):
  42. return self._cmp(rhs, operator.gt)
  43. def __ge__(self, rhs):
  44. return self._cmp(rhs, operator.ge)
  45. def _cmp(self, rhs, op):
  46. """Compare two Durations.
  47. = INPUT VARIABLES
  48. - rhs The Duration to compare against.
  49. - op The function to do the comparison
  50. = RETURN VALUE
  51. - Returns op(self, rhs)
  52. """
  53. self.checkSameFrame(rhs, "compare")
  54. return op(self._seconds, rhs._seconds)
  55. def __add__(self, rhs):
  56. """Add two Durations.
  57. = ERROR CONDITIONS
  58. - If the input rhs is not in the same frame, an error is thrown.
  59. = INPUT VARIABLES
  60. - rhs The Duration to add.
  61. = RETURN VALUE
  62. - Returns the sum of ourselves and the input Duration.
  63. """
  64. # Delay-load due to circular dependencies.
  65. import matplotlib.testing.jpl_units as U
  66. if isinstance(rhs, U.Epoch):
  67. return rhs + self
  68. self.checkSameFrame(rhs, "add")
  69. return Duration(self._frame, self._seconds + rhs._seconds)
  70. def __sub__(self, rhs):
  71. """Subtract two Durations.
  72. = ERROR CONDITIONS
  73. - If the input rhs is not in the same frame, an error is thrown.
  74. = INPUT VARIABLES
  75. - rhs The Duration to subtract.
  76. = RETURN VALUE
  77. - Returns the difference of ourselves and the input Duration.
  78. """
  79. self.checkSameFrame(rhs, "sub")
  80. return Duration(self._frame, self._seconds - rhs._seconds)
  81. def __mul__(self, rhs):
  82. """Scale a UnitDbl by a value.
  83. = INPUT VARIABLES
  84. - rhs The scalar to multiply by.
  85. = RETURN VALUE
  86. - Returns the scaled Duration.
  87. """
  88. return Duration(self._frame, self._seconds * float(rhs))
  89. def __rmul__(self, lhs):
  90. """Scale a Duration by a value.
  91. = INPUT VARIABLES
  92. - lhs The scalar to multiply by.
  93. = RETURN VALUE
  94. - Returns the scaled Duration.
  95. """
  96. return Duration(self._frame, self._seconds * float(lhs))
  97. def __div__(self, rhs):
  98. """Divide a Duration by a value.
  99. = INPUT VARIABLES
  100. - rhs The scalar to divide by.
  101. = RETURN VALUE
  102. - Returns the scaled Duration.
  103. """
  104. return Duration(self._frame, self._seconds / rhs)
  105. def __rdiv__(self, rhs):
  106. """Divide a Duration by a value.
  107. = INPUT VARIABLES
  108. - rhs The scalar to divide by.
  109. = RETURN VALUE
  110. - Returns the scaled Duration.
  111. """
  112. return Duration(self._frame, rhs / self._seconds)
  113. def __str__(self):
  114. """Print the Duration."""
  115. return "%g %s" % (self._seconds, self._frame)
  116. def __repr__(self):
  117. """Print the Duration."""
  118. return "Duration('%s', %g)" % (self._frame, self._seconds)
  119. def checkSameFrame(self, rhs, func):
  120. """Check to see if frames are the same.
  121. = ERROR CONDITIONS
  122. - If the frame of the rhs Duration is not the same as our frame,
  123. an error is thrown.
  124. = INPUT VARIABLES
  125. - rhs The Duration to check for the same frame
  126. - func The name of the function doing the check.
  127. """
  128. if self._frame != rhs._frame:
  129. raise ValueError(
  130. f"Cannot {func} Durations with different frames.\n"
  131. f"LHS: {self._frame}\n"
  132. f"RHS: {rhs._frame}")