Home > Back-end >  How to print when a dict value changes?
How to print when a dict value changes?

Time:10-27

I have currently worked on a comparison function where I want to be able to print out whenever there has been a change in a dict. What I currently do is that I send request to my local-api where it returns different values and here is the example:

First request

{
  '00194953243062': {
    'value': '00194953243062',
    'stock': 'OOS',
    'modificationDate': '2022-10-22T12:02:06.000Z'
  },
  '00194953243086': {
    'value': '00194953243086',
    'stock': 'OOS',
    'modificationDate': '2022-09-30T10:55:45.000Z'
  },
  '00194953243093': {
    'value': '00194953243093',
    'stock': 'OOS',
    'modificationDate': '2022-10-22T11:05:54.000Z'
  },
  '00194953243130': {
    'value': '00194953243130',
    'stock': 'OOS',
    'modificationDate': '2022-10-22T08:55:48.000Z'
  }
}

print("All values are OOS!")
****************************************************************************************************
Second request

{
  '00194953243062': {
    'value': '00194953243062',
    'stock': 'OOS',
    'modificationDate': '2022-10-22T12:02:06.000Z'
  },
  '00194953243086': {
    'value': '00194953243086',
    'stock': 'OOS',
    'modificationDate': '2022-09-30T10:55:45.000Z'
  },
  '00194953243093': {
    'value': '00194953243093',
    'stock': 'OOS',
    'modificationDate': '2022-10-22T11:05:54.000Z'
  },
  '00194953243130': {
    'value': '00194953243130',
    'stock': 'LOW',
    'modificationDate': '2022-10-22T08:55:48.000Z'
  }
}

print("New value has been found!")
****************************************************************************************************
Third request

{
  '00194953243062': {
    'value': '00194953243062',
    'stock': 'LOW',
    'modificationDate': '2022-10-22T12:02:06.000Z'
  },
  '00194953243086': {
    'value': '00194953243086',
    'stock': 'OOS',
    'modificationDate': '2022-09-30T10:55:45.000Z'
  },
  '00194953243093': {
    'value': '00194953243093',
    'stock': 'OOS',
    'modificationDate': '2022-10-22T11:05:54.000Z'
  },
  '00194953243130': {
    'value': '00194953243130',
    'stock': 'OOS',
    'modificationDate': '2022-10-22T08:55:48.000Z'
  }
}

print("New value has been found!")   
****************************************************************************************************
Forth request

{
  '00194953243062': {
    'value': '00194953243062',
    'stock': 'OOS',
    'modificationDate': '2022-10-22T12:02:06.000Z'
  },
  '00194953243086': {
    'value': '00194953243086',
    'stock': 'OOS',
    'modificationDate': '2022-09-30T10:55:45.000Z'
  },
  '00194953243093': {
    'value': '00194953243093',
    'stock': 'OOS',
    'modificationDate': '2022-10-22T11:05:54.000Z'
  },
  '00194953243130': {
    'value': '00194953243130',
    'stock': 'OOS',
    'modificationDate': '2022-10-22T08:55:48.000Z'
  }
}
print("All values are OOS!")

and the problem that I currently have is that I do not know how to print out whenever a stock value goes from OOS -> LOW -> then I should print out that there has been a restock and whenever the sizes goes from LOW -> OOS then I want to check if ALL stock values has the OOS then we should print out that "All values are OOS".

I have done something like this:

previous_data = {}
gtin = # Is the example I have given above

if previous_data.keys() != gtin.keys():
    if all(value['stock'].casefold() == 'oos' for att, value in gtin.items()):
        print("All values are OOS!")

    else:
        print("New value has been found!")

previous_data = gtin

but I noticed that I am missing where I check whenever a value goes from OOS -> LOW, I seem to only check the key of each key-value and therefore I seem to miss the "stock" value changes, I wonder how can I notify myself whenever a stock goes from OOS -> LOW/Whatever else besides OOS and NOT printing if it goes back to LOW -> OOS besides if all stock values are OOS?

CodePudding user response:

Your first part will already notify you any time a change causes all items to be out of stock.

I think you just need an elif statement to follow it up. Something like this should work:

previous_data = {}
gtin = # Is the example I have given above

if not previous_data:
    print("collecting initial inventory")
elif previous_data.keys() == gtin.keys():
    # if our keys are the same we can check which values have changed based on your logic 
    if all(value['stock'].casefold() == 'oos' for att, value in gtin.items()):
        print("All values are OOS!")
    
    #only if they have changed to low, medium or high
    elif any(
            (value['stock'].casefold() in ['low', 'medium', 'high'] and 
            previous_data[att]['stock'].casefold() == 'oos') 
            for att, value in gtin.items()):
        print("New value has been found!")
else:
    # the dictionary keys dont match
    # so the size of your inventory has changed do some other logic 

previous_data = gtin

CodePudding user response:

I can see a few things on your code that might not be what you intend to happen.

1. The stock check is only executed if your inventory keys change: The if previous_data.keys() != gtin.keys(): only checks for changes in the inventory IDs. Therefore, following your example, the stock check is only executed for the first API request (because of previous_data = {}).

2. You're not checking for value changes, but for the existence of non oos values: Depending on what you intend to print, this might be okay. If you don't care about changes, but only about reporting when something is not oos then your logic from line 5 works fine.

Suggested logic given your explanation

You want to keep track of stock status for any inventory item you get from the API, and only print when something went from oos to another value, or when everything is oos. The following code might do the trick.

from collections import defaultdict
inv_stock = defaultdict(lambda: 'OOS') # Assign stock to 'OOS' by default

gtin = get_inventory() # Is the example given above
new_value = False
all_oos = True

for inv_id, status_dict in gtin.items():
  stock_status = status_dict['stock']
  if stock_status != 'OOS':
    all_oos = False
    if inv_stock[inv_id] != stock_status:
      new_value = True
  inv_stock[inv_id] = stock_status

if all_oos:
  print("All values are OOS!")
elif new_value:
  print("New value has been found!")

Hope this helps

  • Related