Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding dictionary value under wrong key. #85656

Closed
admir mannequin opened this issue Aug 5, 2020 · 2 comments
Closed

Overriding dictionary value under wrong key. #85656

admir mannequin opened this issue Aug 5, 2020 · 2 comments
Labels
3.7 (EOL) end of life OS-mac type-bug An unexpected behavior, bug, or error

Comments

@admir
Copy link
Mannequin

admir mannequin commented Aug 5, 2020

BPO 41484
Nosy @ronaldoussoren, @ned-deily

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-08-05.08:34:13.062>
created_at = <Date 2020-08-05.07:33:25.337>
labels = ['OS-mac', 'invalid', 'type-bug', '3.7']
title = 'Overriding dictionary value under wrong key.'
updated_at = <Date 2020-08-05.08:34:13.061>
user = 'https://bugs.python.org/admir'

bugs.python.org fields:

activity = <Date 2020-08-05.08:34:13.061>
actor = 'ronaldoussoren'
assignee = 'none'
closed = True
closed_date = <Date 2020-08-05.08:34:13.062>
closer = 'ronaldoussoren'
components = ['macOS']
creation = <Date 2020-08-05.07:33:25.337>
creator = 'admir'
dependencies = []
files = []
hgrepos = []
issue_num = 41484
keywords = []
message_count = 2.0
messages = ['374860', '374862']
nosy_count = 3.0
nosy_names = ['ronaldoussoren', 'ned.deily', 'admir']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue41484'
versions = ['Python 3.7']

@admir
Copy link
Mannequin Author

admir mannequin commented Aug 5, 2020

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
}
}
}
}

@admir admir mannequin added 3.7 (EOL) end of life OS-mac type-bug An unexpected behavior, bug, or error labels Aug 5, 2020
@ronaldoussoren
Copy link
Contributor

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')

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life OS-mac type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant