14.4.13. crate_anon.linkage.person_io

crate_anon/linkage/person_io.py


Copyright (C) 2015, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

This file is part of CRATE.

CRATE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CRATE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CRATE. If not, see <https://www.gnu.org/licenses/>.


Read/write people from/to disk.

class crate_anon.linkage.person_io.PersonWriter(file: Optional[io.TextIOBase] = None, filename: Optional[str] = None, plaintext: bool = False, plaintext_jsonl: bool = False, include_frequencies: bool = True, include_other_info: bool = False)[source]

A context manager for writing Person objects to CSV (plaintext) or JSONL (hashed).

__init__(file: Optional[io.TextIOBase] = None, filename: Optional[str] = None, plaintext: bool = False, plaintext_jsonl: bool = False, include_frequencies: bool = True, include_other_info: bool = False) None[source]
Parameters
  • file – File-like object to which to write. Use either this or filename, not both.

  • filename – Filename to which to write. Use either this or file, not both.

  • plaintext – Plaintext (in CSV or JSONL)? If False, will be written hashed (in JSONL).

  • plaintext_jsonl – (For plaintext.) Use JSONL rather than CSV?

  • include_frequencies – (For hashed writing only.) Include frequency information. Without this, the resulting file is suitable for use as a sample, but not as a proband file.

  • include_other_info – (For hashed writing only.) Include the (potentially identifying) other_info data? Usually False; may be True for validation.

write(person: crate_anon.linkage.person.Person) None[source]

Write a person to the file.

crate_anon.linkage.person_io.gen_person_from_file(cfg: crate_anon.linkage.matchconfig.MatchConfig, filename: str, plaintext: bool = True, jsonl: Optional[bool] = None) Generator[crate_anon.linkage.person.Person, None, None][source]

Read a list of people from a CSV/JSONLines file. See Person.PersonKey for the column details.

Parameters
  • cfg – Configuration object.

  • filename – Filename to read.

  • plaintext – Read in plaintext (from CSV or JSONL), rather than hashed (from JSONL), format?

  • jsonl – True = read from JSONL; False = read from CSV; None = autodetect from filename.

Yields

Person objects

crate_anon.linkage.person_io.write_people(people: Union[crate_anon.linkage.people.People, Iterable[crate_anon.linkage.person.Person]], file: Optional[io.TextIOBase] = None, filename: Optional[str] = None, plaintext: bool = False, plaintext_jsonl: bool = False, include_frequencies: bool = True, include_other_info: bool = False) None[source]

Writes from a People object, or an iterable of Person objects, to a file (specified by name or as a file-like object). See PeopleWriter.