Guest User

Untitled

a guest
Jun 18th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. def get_current_btc(self, time: datetime = None) -> Decimal:
  2. """
  3. Get how much btc this investment is currently worth.
  4. :param time: the time you want data regarding. Cant ever be after closed_date
  5. :return: current worth in btc
  6. """
  7. time, cache_time_name = _default_time_and_cache_name(self._default_time(time))
  8.  
  9. def inner_method():
  10. fraction = self.get_fraction(time=time)
  11. if fraction != 0:
  12. return fraction * \
  13. GlobalBalance.objects.get_btc_by_time(time=time)
  14. return Decimal(0)
  15.  
  16. return get_and_or_set_cache(f"investment_{self.pk}_{cache_time_name}_current_btc",
  17. inner_method,
  18. 60 * 5)
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. def get_and_or_set_cache(key: str, setterMethod: FunctionType, timeout: int = None) -> Any:
  30. """
  31. Method for getting the content of the cache and if it's empty setting it, so it's use-able for the next request.
  32. :param key: the key of value in the cache
  33. :param setterMethod: method used to set the cache value if the cache is invalid
  34. :param timeout: how long the cache is valid - none if forever
  35. :return: the value of the cache or the setterMethod if the cache is invalid
  36. """
  37. output = cache.get(key)
  38. if not output:
  39. output = setterMethod()
  40. cache.set(key, output, timeout)
  41.  
  42. return output
Add Comment
Please, Sign In to add comment