Skip to content

file_io.py

Helper methods for performing disk I/O operation on analysis results.

To generate file name for storing results

from pymwp.file_io import default_file_out

To save or restore results as JSON:

from pymwp.file_io import save_result, load_result

default_file_out(input_file)

Generates default output file.

Parameters:

Name Type Description Default
input_file str

input filename (with or without path)

required

Returns:

Type Description
str

Generated output filename with path.

Source code in pymwp/file_io.py
16
17
18
19
20
21
22
23
24
25
26
27
def default_file_out(input_file: str) -> str:
    """Generates default output file.

    Arguments:
        input_file: input filename (with or without path)

    Returns:
        Generated output filename with path.
    """
    file_only = os.path.splitext(input_file)[0]
    file_name = os.path.basename(file_only)
    return os.path.join("output", f"{file_name}.json")

load_result(file_name)

Load previous analysis result from file.

This method is the reverse of save_relation and assumes the input matches the output of that method.

Parameters:

Name Type Description Default
file_name str

file to read

required

Raises:

Type Description
Exception

if file_name does not exist or cannot be read.

Returns:

Type Description
Result

Parsed result from file

Source code in pymwp/file_io.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def load_result(file_name: str) -> Result:
    """Load previous analysis result from file.

    This method is the reverse of
    [`save_relation`](file_io.md#pymwp.file_io.save_relation)
    and assumes the input matches the output of that method.

    Arguments:
        file_name: file to read

    Raises:
          Exception: if `file_name` does not exist or cannot be read.

    Returns:
        Parsed result from file
    """
    # read the file
    with open(file_name) as file_object:
        data = json.load(file_object)

    return Result.deserialize(**data)

loc(input_file)

Get number of lines is a file

Source code in pymwp/file_io.py
10
11
12
13
def loc(input_file: str) -> int:
    """Get number of lines is a file"""
    with open(input_file, 'r') as fp:
        return len(fp.readlines())

save_result(file_name, analysis_result)

Save analysis result to file as JSON.

Expected behavior:

  • if path to output file does not exist it will be created
  • if output file does not exist it will be created
  • if output file exists it will be overwritten

Parameters:

Name Type Description Default
file_name str

filename where to write

required
analysis_result Result

A Result object.

required
Source code in pymwp/file_io.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def save_result(file_name: str, analysis_result: Result) -> None:
    """Save analysis result to file as JSON.

    Expected behavior:

    - if path to output file does not exist it will be created
    - if output file does not exist it will be created
    - if output file exists it will be overwritten

    Arguments:
        file_name: filename where to write
        analysis_result: A [`Result`](result.md) object.
    """
    # JSON serializable object
    file_content = analysis_result.serialize()

    # ensure directory path exists
    dir_path, _ = os.path.split(file_name)
    if len(dir_path) > 0 and not os.path.exists(dir_path):
        os.makedirs(dir_path)

    # write to file
    with open(file_name, "w") as outfile:
        json.dump(file_content, outfile, indent=4)

    logger.info(f'saved result in {file_name}')