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

default_file_out(input_file: str) -> str

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.

del_comments

del_comments(text)

Remove C-style comments. Thanks to https://stackoverflow.com/a/241506

Parameters:

Name Type Description Default
text

C code fragment

required

Returns:

Type Description

Text without comments.

load_result

load_result(file_name: str) -> Result

Load previous analysis result from file.

This method is the reverse of save_result 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.

loc

loc(input_file: str) -> int

Get number of lines is a file.

Parameters:

Name Type Description Default
input_file str

Path to input file.

required

Returns:

Type Description
int

Number of non-empty lines in input file.

save_result

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

Parameters:

Name Type Description Default
file_name str

Filename where to write.

required
analysis_result Result

Result object.

required