Coverage for common/file_io.py: 30%
10 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
1"""
2crate_anon/common/file_io.py
4===============================================================================
6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CRATE.
11 CRATE is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 CRATE is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with CRATE. If not, see <https://www.gnu.org/licenses/>.
24===============================================================================
26crate_anon/anonymise/anonymise.py
27**Simple functions for file I/O.**
29"""
31from typing import Generator
34def gen_words_from_file(filename: str) -> Generator[str, None, None]:
35 """
36 Generate words from a file.
38 Args:
39 filename:
41 Yields:
42 each word
43 """
44 for line in open(filename):
45 for word in line.split():
46 yield word
49def gen_integers_from_file(filename: str) -> Generator[int, None, None]:
50 """
51 Generates integers from a file.
53 Args:
54 filename: filename to parse
56 Yields:
57 all valid integers from words in the file
58 """
59 for word in gen_words_from_file(filename):
60 if word.isdigit():
61 pid = int(word)
62 yield pid