Skip to content

CLN: Orderdict -> defaultdict(list) #30468

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

Merged
merged 2 commits into from
Dec 25, 2019

Conversation

ShaharNaveh
Copy link
Member

@ShaharNaveh ShaharNaveh commented Dec 25, 2019

# TODO: aggspec type: typing.OrderedDict[str, List[AggScalar]]
# May be hitting https://github.com/python/mypy/issues/5958
# saying it doesn't have an attribute __name__
aggspec = OrderedDict()
aggspec = defaultdict(list)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is more complex than this. The code below can be simplified if this is a defaultdict.

Also, is OrderedDict still being used after this, or can it be removed from the imports?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite a few other uses of OrderedDict -> i'm aiming to eliminate most in this issue #30469

Copy link
Member Author

@ShaharNaveh ShaharNaveh Dec 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is OrderedDict still being used after this, or can it be removed from the imports?

Yes OrderedDIct is still being used.

I am really new to developing (I just greped for the words 'droped' '35' 'support' etc..), can you please explain what is the problem with OrderedDict ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a regular dict:

groupping_values = {}
for value in [1, 3, 1, 1, 2, 3]:
    if value not in groupping_values:
        groupping_values[value] = [value]
    else:
        groupping_values[value].append(value)

The if is needed, because when we create the key in the dictionary, we need to create the list with its value. The other times we can append to it.

With an example, if we have an empty dictionary {} and we want to add the Python programming language, we can do stuff['languages'] = ['python']. If now that we already have languages with a list, if we want to add javascript we use a different instruction stuff['languages'].append('javascript'). This is what the if in the example does.

Python defaultdict exists to make things simpler:

import collections
groupping_values = collections.defaultdict(list)
for value in [1, 3, 1, 1, 2, 3]:
    groupping_values[value].append(value)

A defaultdict is "smart", since in the creation we tell its elements will be list. So, when we call the append, it'll create an empty list automatically before. We don't need the if anymore, and the code is simpler.

This is what it's expected here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for the detailed explanation.

@datapythonista datapythonista added Clean Code Style Code style, linting, code_checks labels Dec 25, 2019
@datapythonista datapythonista changed the title TYP: Orderdict -> defaultdict(list) CLN: Orderdict -> defaultdict(list) Dec 25, 2019
Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks for cleaning this @MomIsBestFriend

@ShaharNaveh
Copy link
Member Author

lgtm, thanks for cleaning this @MomIsBestFriend

Thanks for the detailed explanation, and the patience ;)

@jreback jreback added this to the 1.0 milestone Dec 25, 2019
@jreback jreback merged commit 8818407 into pandas-dev:master Dec 25, 2019
@jreback
Copy link
Contributor

jreback commented Dec 25, 2019

thanks @MomIsBestFriend

@ShaharNaveh ShaharNaveh deleted the TYP-drop-py35 branch December 28, 2019 17:28
AlexKirko pushed a commit to AlexKirko/pandas that referenced this pull request Dec 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clean Code Style Code style, linting, code_checks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants