Skip to content

Commit

Permalink
Use cached version of faker rather than instance every time (#1406)
Browse files Browse the repository at this point in the history
* Use cached version of faker rather than instance every time

* instance lru_cache
  • Loading branch information
pvk-developer authored May 2, 2023
1 parent 898850a commit 72144c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
9 changes: 8 additions & 1 deletion sdv/metadata/anonymization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import inspect
import warnings
from functools import lru_cache

from faker import Faker
from faker.config import AVAILABLE_LOCALES
Expand Down Expand Up @@ -47,6 +48,12 @@
}


@lru_cache()
def get_faker_instance():
"""Return a ``faker.Faker`` instance with all the locales."""
return Faker(AVAILABLE_LOCALES)


def is_faker_function(function_name):
"""Return whether or not the function name is a valid Faker function.
Expand All @@ -60,7 +67,7 @@ def is_faker_function(function_name):
try:
with warnings.catch_warnings():
warnings.filterwarnings('ignore', module='faker')
getattr(Faker(AVAILABLE_LOCALES), function_name)
getattr(get_faker_instance(), function_name)
except AttributeError:
return False

Expand Down
21 changes: 17 additions & 4 deletions tests/unit/metadata/test_anonymization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import Mock, patch

from sdv.metadata.anonymization import (
_detect_provider_name, get_anonymized_transformer, is_faker_function)
_detect_provider_name, get_anonymized_transformer, get_faker_instance, is_faker_function)


class TestAnonimization:
Expand Down Expand Up @@ -110,18 +110,31 @@ def test_is_faker_function(self, faker_mock):
# Assert
assert result is True

@patch('sdv.metadata.anonymization.Faker')
def test_is_faker_function_error(self, faker_mock):
@patch('sdv.metadata.anonymization.get_faker_instance')
def test_is_faker_function_error(self, mock_get_faker_instance):
"""Test that the method returns False if ``function_name`` is not a valid faker function.
If the ``function_name`` is not an attribute of ``Faker()`` then we should return false.
This test mocks ``Faker`` to not have the attribute that is passed as ``function_name``.
"""
# Setup
faker_mock.return_value = Mock(spec=[])
mock_get_faker_instance.return_value = Mock(spec=[])

# Run
result = is_faker_function('blah')

# Assert
assert result is False
mock_get_faker_instance.assert_called_once()

@patch('sdv.metadata.anonymization.Faker')
def test_get_faker_instance(self, mock_faker):
"""Test that ``get_faker_instance`` returns the same object."""
# Setup
first_instance = get_faker_instance()

# Run
second_instance = get_faker_instance()

# Assert
assert id(first_instance) == id(second_instance)

0 comments on commit 72144c0

Please sign in to comment.