Search code examples
pythonpython-3.xlist-comprehensionsimulationfaker

How to reset unique method in Python Faker


I am using the Faker library to simulate weight-training data for an open-source app I am building (https://github.com/TheNewThinkTank/Fitness-Tracker), where part of this simulation (https://github.com/TheNewThinkTank/Fitness-Tracker/blob/main/src/simulate_data.py) is to select exercises from a list. Each chosen exercise may only appear once per workout, which I am assuring by using the unique method from Faker() in the faker library. This works fine when I simulate a single workout. However, when attempting to simulate more than one workout, I am met with faker.exceptions.UniquenessException: Got duplicated values after 1,000 iterations. Have attempted to reset the unique call after each iteration but struggling to find the correct syntax for this (fake.unique(reset=True) gives me TypeError: 'UniqueProxy' object is not callable). Here is what I have tried (minimal example to reproduce the error):

from faker import Faker
from faker.providers import DynamicProvider

fake = Faker()

chest_exercises_provider = DynamicProvider(
    provider_name="chest_exercises",
    elements=["benchpress", "flys", "pullovers", "dips"],
)
fake.add_provider(chest_exercises_provider)

for workout in range(2):
    workout_chest_exercises = [fake.unique.chest_exercises() for _ in range(3)]  # <----- UniquenessException HERE !!

Had a look at I need to generate 1000 unique first name In Python which is similar but addresses ensuring uniqueness and not resetting unique method-call. Is there some way to reset the unique after each iteration through the for-loop ? Or any other idea to solve it (e.g. using random.sample instead of Faker for exercise selection)?

The simulated data structure for a single workout is as follows:

{
  "date": "1994-11-28",
  "split": "back",
  "exercises": {
    "dead row": [{ "set no.": 1, "reps": 17, "weight": "5 kg" }],
    "chinups": [
      { "set no.": 1, "reps": 9, "weight": "92 kg" },
      { "set no.": 2, "reps": 11, "weight": "27 kg" },
      { "set no.": 3, "reps": 16, "weight": "85 kg" },
      { "set no.": 4, "reps": 19, "weight": "22 kg" },
      { "set no.": 5, "reps": 4, "weight": "65 kg" }
    ],
    "seated row": [
      { "set no.": 1, "reps": 19, "weight": "11 kg" },
      { "set no.": 2, "reps": 7, "weight": "24 kg" },
      { "set no.": 3, "reps": 10, "weight": "38 kg" },
      { "set no.": 4, "reps": 9, "weight": "70 kg" },
    ]
  }
}

Solution

  • You can use the .clear() method.
    Have a look at the docs.