main_34.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. def pseudonymize(
  2. self, key_file: KeyFile, identifiers: List["Identifier"]
  3. ) -> List["Key"]:
  4. """Get a pseudonym for each identifier. If identifier is known in PIMS,
  5. return this. Otherwise, have PIMS generate a new pseudonym and return that.
  6. Parameters
  7. ----------
  8. identifiers: List[Identifier]
  9. The identifiers to get pseudonyms for
  10. key_file: KeyFile
  11. The key_file to use
  12. Notes
  13. -----
  14. Each call this function calls PIMS API twice for each unique source in
  15. identifiers. This is result of the way the API can be called
  16. Returns
  17. -------
  18. List[Key]
  19. The PIMS pseudonym for each identifier
  20. """
  21. keys = []
  22. # Each call to process a list of identifiers only allows a single source.
  23. # Split identifiers by source
  24. per_source = defaultdict(list)
  25. for x in identifiers:
  26. per_source[x.source].append(x)
  27. for source, items in per_source.items():
  28. keys = keys + self.deidentify(key_file, [x.value for x in items], source)
  29. return keys