Skip to content

Conversation

KommuSoft
Copy link
Contributor

Problem

Some dictionary lookups or membership checks are not done efficiently. Like if key in set(some_dict.keys()): this runs in linear time, since it first makes a set of all the keys in the dictionary.

Solution

Dropping .keys() where necessary, and if we check a dictionary to then get the corresponding value, we can replace:

if key in some_dict:
  val = some_dict[key]

by:

missing = object()
temp_val = some_dict.get(key, missing)
if temp_val is not missing:
    val = temp_val

and:

if some_key in my_dict.keys()

or:

if some_key in set(my_dict.keys())

is equivalent to just:

if some_key in my_dict

Acceptance Criteria

The tests run, and a bit faster (although that does not say much). The code just uses the fact that membership checks can be done on the dict directly, saving a few indirections.

@coveralls
Copy link

Coverage Status

coverage: 99.913%. remained the same
when pulling 803bcbf on hapytex:feature/membership-check-improvements
into 518aa88 on django-import-export:main.

Copy link
Contributor

@matthewhegarty matthewhegarty left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this.

@matthewhegarty matthewhegarty merged commit 13f1ae3 into django-import-export:main Jul 20, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants