Skip to content

Instantly share code, notes, and snippets.

@huanzhang
Created January 22, 2017 06:44
Show Gist options
  • Save huanzhang/507939402aecc7fa960714d4229d0dcc to your computer and use it in GitHub Desktop.
Save huanzhang/507939402aecc7fa960714d4229d0dcc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import collections
SG_DATA_OUT_PRICING = [
(1, 0),
(10*1024-1, 0.12),
(40*1024, 0.085),
(100*1024, 0.082),
(350*1024, 0.080)
]
def cost_sg_data_out(usage_amount, pricing):
pricing = collections.OrderedDict(pricing)
if usage_amount > sum(pricing.keys()):
raise Exception('Such large usage amount was not expected')
total_costs = []
remaining_amount = usage_amount
for level_amount in pricing.keys():
if remaining_amount > level_amount:
remaining_amount = remaining_amount - level_amount
total_costs.append(level_amount * pricing[level_amount])
else:
total_costs.append(remaining_amount * pricing[level_amount])
break
return sum(total_costs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment