Skip to content

Instantly share code, notes, and snippets.

@theronic
Created February 24, 2017 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theronic/6127fd2c870307d4604d4d5028c51f4d to your computer and use it in GitHub Desktop.
Save theronic/6127fd2c870307d4604d4d5028c51f4d to your computer and use it in GitHub Desktop.
Mock Implementation of Missed Revenue Calculations
import unittest
from datetime import datetime
THE_PAST = datetime(2000, 1, 1, 9)
THE_FUTURE = datetime(2000, 1, 1, 17)
if __name__ == '__main__':
a(10)
def add(acc, v): return acc + v
def calc_stock_level(buys, sales):
""" expect sales to be pos, but currently neg. """
# hoekom is die joernaal SO????
buy_qtys = map(lambda entry: entry['Increment'], buys)
total_buy_qtys = reduce(add, buy_qtys, 0)
sale_qtys = map(lambda entry: -1 * entry['Increment'], sales) # would be better to negate at caller
total_sale_qtys = reduce(ad-d, sale_qtys, 0)
return total_buy_qtys - total_sale_qtys # purchases are +
def test_calc_level_is_correct(self):
journal = [{'Tag': 'sale', 'Increment': -1},
{'Tag': 'purchase', 'Increment': 5},
{'Tag': 'sale', 'Increment': -2}]
sales = filter(lambda x: x['Tag'] == 'sale', journal) # sales are negative
buys = filter(lambda x: x['Tag'] == 'purchase', journal)
level = calc_stock_level(buys, sales)
print('level:', level)
assert level == 2
#self.assertEqual(2, level)
NORMAL_HOURS = [['9am', '10am'], ['12am', '5pm']]
business_hours_interval = {'monday': NORMAL_HOURS,
'sunday': WEEKEND_HOURS}
def calc_interval_qty_losses(interval, run_rate_fn, TIME_AGGR):
# Q: does the interval clip at COB/OOB?
start_time, duration = interval
# what happens between and on the edegs?
# spec states assume constants ros, so use the ROS at start
ros = run_rate_fn(start_time, TIME_AGGRE)
return ros * duration # returns a scalar of ros * TIME_AGGR unit
# todo think about the shape of this
losses_over_interval = map(lambda interval: calc_interval_qty_losses(interval, calc_run_rate_fn, HOURS))
def test_low_stock_time_slot_calculation():
#time_slots = calc_low_stock_periods
# what shpae?
# missed_qty_by_period = time_slots * run_rate
opened_on = THE_PAST
sold_on = THE_PAST + datetime.timedelta(hours=1)
buy_on = THE_PAST + datetime.timedelta(hours=2)
sold_on2 = THE_PAST + datetime.timedelta(hours=8)
closed_on = THE_FUTURE
open_time = closed_on - opened_on
assert open_time == datetime.timedelta(hours=8)
# What happens below zero? Don't know.
journal = [
{'Date': THE_PAST, 'Tag': 'purchase', 'Increment': 5},
# need something inbetween?
{'Date': sold_on, 'Tag': 'sale', 'Increment': -5},
{'Date': buy_on, 'Tag': 'purchase', 'Increment': 3},
{'Date': sold_on2, 'Tag': 'sale', 'Increment': -3},
# another sale?
# ideally two periods
]
p1 = (sold_on, datetime.timedelta(hours=1))
p2 = (sold_on2, datetime.timedelta(hours=3))
periods = calc_low_stock_periods(journal, opened_on, closed_on)
# can these periods fit into the business hours?
assert periods == [p1, p2]
class MyTestFramework:
def assertEqual(a, b): assert a == b
test_calc_level_is_correct({})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment