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

Use comprehension in it_IT address provider to populate cities #2043

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sevdog
Copy link

@sevdog sevdog commented May 7, 2024

What does this change

This PR uses a single set-comprehension to populate the cities list for it_IT locale.

What was wrong

The previous implementation used a series of nested for-loops to build up a unique list of cities in the it_IT locale using a strange inline at line 10 to avoid using a normal IF statement.

def getcities(fulldict):
cities = []
for cap in fulldict:
for c in fulldict[cap]:
cities.append(c[0]) if c[0] not in cities else cities
return cities

While profiling the code in which I use this provider I found out it was taking a long time to load.

How this fixes it

Using a set-comprehension and then turning that into a list takes advantages of built-in features of set and the usage of .items() for iterating over a dict provide better performances.

Local benchmark tests with timeit shows the latter is at least 100x faster.

from timeit import timeit
from faker.providers.address.it_IT import Provider

fragment = 'getcities(cap_city_province)'

def original_getcities(fulldict):
    cities = []
    for cap in fulldict:
        for c in fulldict[cap]:
            cities.append(c[0]) if c[0] not in cities else cities
    return cities

def new_getcities(fulldict):
    return list({c[0] for _cap, cities in fulldict.items() for c in cities})

print(timeit(fragment, globals={'cap_city_province': Provider.cap_city_province, 'getcities': original_getcities}))
print(timeit(fragment, globals={'cap_city_province': Provider.cap_city_province, 'getcities': new_getcities}))

Copy link
Collaborator

@fcurella fcurella left a comment

Choose a reason for hiding this comment

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

Thank you!

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.

None yet

2 participants