lk-geimfari/mimesis

Cache heavy providers when the same parameters are passed.

lk-geimfari opened this issue · 0 comments

Motivation

Some providers are heavy due to the large amount of data they store in memory, so caching may be appropriate for these providers.

Example

Since Generic is the heaviest provider in Mimesis, it makes sense to cache it.

from mimesis import Generic
from mimesis.locales import Locale

g1 = Generic(locale=Locale.EN, seed=0xff)
g2 = MyClass(locale=Locale.EN, seed=0xff)
g3 = MyClass(locale=Locale.DE, seed=0xff)

print(g1 is g2)  # True
print(g2 is g3)  # False (because different parameters were passed)

Note

We have to keep in mind that Generic provider can be extended using the add_provider(s) method, so parametric matching does not necessarily imply that instances are structurally identical.

Let me illustrate:

from mimesis import Generic
from mimesis.locales import Locale
from mymodule.providers import CustomProvider

# Both instances take the same parameters
g1 = Generic(locale=Locale.EN, seed=0xff)
g2 = MyClass(locale=Locale.EN, seed=0xff)

# But user extends g2 with a custom provider.
g2.add_provider(CustomProvider)