Syllabus Lesson 62 of 141 · Data models that bend
Data models that bend

Put time in the key

Why it matters "A product has a price" is true for exactly one moment. Refunds, invoices, audits and planned sales all need the price that applied on a different day.

An online shop is moving its price list out of a spreadsheet. Today's request: set a product's price, and look it up. The merchandising team already plans sales weeks ahead, and the refunds desk refunds at the price the customer actually paid.

Validity period: the stretch of time a value is true for. Here a price is true from its from_day until the next price for the same product starts.

Your turn

Today's request. Write new_catalog(), set_price(catalog, sku, price) and price(catalog, sku) (the price, or None if the product has none).

What is coming. Prices will be planned ahead and looked up for past days. Hidden change requests will call these, so write them now as well:

  • set_price(catalog, sku, price, from_day=day): the price applies from that day (with no from_day, use day 0). Prices are entered in any order of day, and setting a price again for the same product and day corrects it;
  • price(catalog, sku, on=day): the price in force on that day, None before the first one. With no day, the price with the latest from_day;
  • changes(catalog, sku): a list of (from_day, price) in day order.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.