1. import json
  2. def limit_user_records(config_file="config.json"):
  3. """
  4. Limits the output of user records for hypothesis validation based on a configuration file.
  5. """
  6. try:
  7. with open(config_file, "r") as f:
  8. config = json.load(f)
  9. except FileNotFoundError:
  10. print(f"Error: Configuration file '{config_file}' not found.")
  11. return
  12. except json.JSONDecodeError:
  13. print(f"Error: Invalid JSON format in '{config_file}'.")
  14. return
  15. limit = config.get("record_limit", 100) # Default limit to 100 records
  16. filters = config.get("filters", {}) # Dictionary of filters
  17. # Simulate user records (replace with your actual data source)
  18. user_records = [
  19. {"id": i, "name": f"User {i}", "age": i % 50, "city": f"City {i % 10}"}
  20. for i in range(250)
  21. ]
  22. filtered_records = []
  23. for record in user_records:
  24. # Apply filters
  25. keep_record = True
  26. for key, value in filters.items():
  27. if key in record and record[key] != value:
  28. keep_record = False
  29. break
  30. if keep_record:
  31. filtered_records.append(record)
  32. # Limit the output
  33. output_records = filtered_records[:limit]
  34. # Print the output
  35. for record in output_records:
  36. print(record)
  37. if __name__ == "__main__":
  38. limit_user_records()

Add your comment