Hello beautiful people!
Im currently working on a script where I am trying to print out whenever there is a increse in a dict value and print it out once it happens by using this data 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': 'MEDIUM',
'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!")
Those are examples of each requests that could possible happen and with some help from another stackoverflow threads I managed to end up doing something like this:
previous_data = {}
gtin = # Is the example I have given above
if previous_data == gtin:
# 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!")
previous_data = gtin
What it currently solves it that whenever a stock value goes from OOS to any of low/medium/high, it will print out that there has been a change which is correct:
if OOS -> LOW/MEDIUM/HIGH -> Print
if LOW -> MEDIUM/HIGH -> Print
if MEDIUM -> HIGH/LOW -> Print
if HIGH -> LOW/MEDIUM -> Print
But there problem is that I do not want to print whenever HIGH -> OOS/LOW/MEDIUM, MEDIUM -> LOW/OOS and LOW -> OOS.
My goal is to have this expected result:
Expected result :
if OOS -> LOW/MEDIUM/HIGH -> Print
if LOW -> MEDIUM/HIGH -> Print
if MEDIUM -> HIGH -> Print
if LOW -> OOS - **dont** print (print if all stock are oos)
if MEDIUM -> OOS/LOW- **dont** print (print if all stock are oos)
if HIGH -> OOS/LOW/MEDIUM - **dont** print (print if all stock are oos)
if ALL values are OOS -> print All OOS
It is enough for me that if any of these value gets increased then we do not have to check for the other values as long as one of these scenarios are true.
My question is, how can I with my code get the expected result where it doesn't print out when this happens:
if LOW -> OOS - **dont** print (print if all stock are oos)
if MEDIUM -> OOS/LOW- **dont** print (print if all stock are oos)
if HIGH -> OOS/LOW/MEDIUM - **dont** print (print if all stock are oos)
?
CodePudding user response:
Use a dictionary that maps the stock strings to a number. Then you can check if the numeric value has increased.
stock_map = {
'oos': 0,
'low': 1,
'medium': 2,
'high': 3
}
...
elif (any(stock_map[value['stock'].casefold()] > stock_map[previous_data[att]['stock'].casefold()]
for att, value in gtin.items()):
print("New value has been found")