SHARE
TWEET

Untitled

a guest Jun 25th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def api_wallet_translate_gold(value):
  2.     """Translate a value into string of money"""
  3.     if value >= 10000:  # Gold
  4.         return ("{0} gold, {1} silver and {2} copper."
  5.                 .format(str(value)[:-4], str(value)[-4:-2], str(value)[-2:]))
  6.     elif value >= 100:  # Silver
  7.         return "{0} silver and {1} copper.".format(str(value)[-4:-2], str(value)[-2:])
  8.     else:  # Copper
  9.         return "{0} copper.".format(str(value)[-2:])
  10.      
  11. coins = [
  12.     ("gold",  100 * 100),
  13.     ("silver", 100),
  14.     ("copper", 1)
  15. ]
  16.  
  17. def translate_coins(value, coins):
  18.     res = {}
  19.     for coin, v in coins:
  20.         res[coin], value = divmod(value, v)
  21.     return res
  22.  
  23. translate_coins(1013323, coins)
  24.      
  25. {'gold': 101, 'silver': 33, 'copper': 23}
  26.      
  27. class Currency(object):
  28.  
  29.     COPPER_CONVERSION_MAP = {
  30.         'copper': 1,
  31.         'gold': 100 * 100,
  32.         'silver': 100
  33.     }
  34.     gold = 0
  35.     silver = 0
  36.     copper = 0
  37.  
  38.     def __init__(self, copper=0, silver=0, gold=0):
  39.         # convert all inputs into copper
  40.         self.copper = (
  41.             copper +
  42.             silver * self.COPPER_CONVERSION_MAP['silver'] +
  43.             gold * self.COPPER_CONVERSION_MAP['gold']
  44.           )
  45.         self.break_currency()
  46.  
  47.     def break_currency(self):
  48.         for coin_type in ['gold', 'silver']:
  49.             coins, coppers = divmod(self.copper, self.COPPER_CONVERSION_MAP[coin_type])
  50.             setattr(self, coin_type, coins)
  51.             self.copper = coppers
  52.  
  53.     def __str__(self):
  54.         return '{:,} gold, {:,} silver and {:,} copper'.format(self.gold, self.silver, self.copper)
  55.      
  56. >>> c = Currency(copper=653751735176)
  57. >>> str(c)
  58. '65,375,173 gold, 51 silver and 76 copper'
  59. >>> c.copper
  60. 76
  61. >>> c.silver
  62. 51
  63. >>> c.gold
  64. 65375173
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top