This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Overriding dictionary value under wrong key.
Type: behavior Stage: resolved
Components: macOS Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: admir, ned.deily, ronaldoussoren
Priority: normal Keywords:

Created on 2020-08-05 07:33 by admir, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg374860 - (view) Author: Admir Ljubijankić (admir) Date: 2020-08-05 07:33
I'm writing a function that combines two dictionaries called "baseProducts" and "variantProducts".
The logic behind is simple, I loop trough variantProducts, create a string that will be used as a key for that combined product. Add all values of baseProduct under that key, and add all fitting variants to this new nested dictionary under key "variants". And return the new dict once all variants are grouped correctly.
The code looks something like this:

def combineProductsByColor(baseProducts, variantProducts):
    retDict = {}

    for key, variant in variantProducts.items():
        productKey = variant['parent'] + '_' + variant['color']
        if productKey not in retDict:
            retDict[productKey] = baseProducts[variant['parent']]
            retDict[productKey]['variants'] = {}
        retDict[productKey]['variants'][key] = variant
    
    return retDict

Now with my test data, baseProducts only contains one item in it. While variantProducts contain 4 items. When the first two loops happen it creates a new key, and adds the first and second variant under that key correctly (this was tested using ms code debugger) but when it looks at the 3rd item, it creates a new key, and adds the variant to that key, but it also overrides all the variants in the previous key, even if the variable productKey is the new key. And when the 4th item gets processed it again gets added to both.
So to make sure that I'm not getting some wrong key somehow. I also tried to implement it like this:

def combineProductsByColor(baseProducts, variantProducts):
    retDict = {}
    keys = list(variantProducts.keys())

    for k in keys:
        productKey = variantProducts[k]['parent'] + '_' + variantProducts[k]['color']
        if productKey not in retDict:
            retDict[productKey] = baseProducts[variantProducts[k]['parent']]
            retDict[productKey]['variants'] = {}
        retDict[productKey]['variants'][k] = variantProducts[k]

    return retDict

But the result was identical. Again following the whole procedure with ms code debugger.
I've also tried making the productKey simpler, but the problem still remains.

Now I do have an idea how to implement this in a different way, but I don't see and understand why the code I provided doesn't work. Because when the new key gets generated, its not the same as the old, but it still can change values under the old key.

Here is what the data looks like before and after processing it.

baseProducts:

{
    '2321963000004': {
        'code': 'P20-0002',
        'name': 'Ženske hlače',
        'regular_price': '22,99',
        'vat': '22,00',
        'attrib': {
            'material': '97% bombaž, 3% elastan, 20% viskoza, 5% elastan'
        },
        'categories': ['01', '0101', '010104']
    }
}

variantProducts:

{
    '2321963991029': {
        'parent': '2321963000004',
        'color': '99',
        'size': '102',
        'title': None,
        'name': None,
        'regular_price': None,
        'vat': None,
        'attrib': None,
        'categories': None
    },
    '2321963991036': {
        'parent': '2321963000004',
        'color': '99',
        'size': '103',
        'title': None,
        'name': None,
        'regular_price': '25,99',
        'vat': '22,00',
        'attrib': None,
        'categories': None
    },
    '2321963981029': {
        'parent': '2321963000004',
        'color': '98',
        'size': '102',
        'title': None,
        'name': None,
        'regular_price': None,
        'vat': None,
        'attrib': None,
        'categories': None
    },
    '2321963981036': {
        'parent': '2321963000004',
        'color': '98',
        'size': '103',
        'title': None,
        'name': None,
        'regular_price': None,
        'vat': None,
        'attrib': None,
        'categories': None
    }
}

And last combined data:

{
    '2321963000004_99': {
        'code': 'P20-0002',
        'name': 'Ženske hlače',
        'regular_price': '22,99',
        'vat': '22,00',
        'attrib': {
            'material': '97% bombaž, 3% elastan, 20% viskoza, 5% elastan'
        },
        'categories': ['01', '0101', '010104'],
        'variants': {
            '2321963981029': {
                'parent': '2321963000004',
                'color': '98',
                'size': '102',
                'title': None,
                'name': None,
                'regular_price': None,
                'vat': None,
                'attrib': None,
                'categories': None
            },
            '2321963981036': {
                'parent': '2321963000004',
                'color': '98',
                'size': '103',
                'title': None,
                'name': None,
                'regular_price': None,
                'vat': None,
                'attrib': None,
                'categories': None
            }
        }
    },
    '2321963000004_98': {
        'code': 'P20-0002',
        'name': 'Ženske hlače',
        'regular_price': '22,99',
        'vat': '22,00',
        'attrib': {
            'material': '97% bombaž, 3% elastan, 20% viskoza, 5% elastan'
        },
        'categories': ['01', '0101', '010104'],
        'variants': {
            '2321963981029': {
                'parent': '2321963000004',
                'color': '98',
                'size': '102',
                'title': None,
                'name': None,
                'regular_price': None,
                'vat': None,
                'attrib': None,
                'categories': None
            },
            '2321963981036': {
                'parent': '2321963000004',
                'color': '98',
                'size': '103',
                'title': None,
                'name': None,
                'regular_price': None,
                'vat': None,
                'attrib': None,
                'categories': None
            }
        }
    }
}
msg374862 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2020-08-05 08:34
This is expected behaviour. the assessment to retProduct[productKey] is not a copy. If those variants have the same parent and a different color you'll end up with a baseProduct where 'variants' refers to the same variant dict (and the second color seen will overwrite the value of 'variants')
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85656
2020-08-05 08:34:13ronaldoussorensetstatus: open -> closed
resolution: not a bug
messages: + msg374862

stage: resolved
2020-08-05 07:33:25admircreate