diff --git a/src/starfile/writer.py b/src/starfile/writer.py index be8d784..5c5043d 100644 --- a/src/starfile/writer.py +++ b/src/starfile/writer.py @@ -1,15 +1,14 @@ from __future__ import annotations -from datetime import datetime -from pathlib import Path -from typing import TYPE_CHECKING, Union, Dict, List, Generator, Optional -from importlib.metadata import version import csv - import pandas as pd +from datetime import datetime +from importlib.metadata import version +from pathlib import Path -from .utils import TextBuffer +from typing import TYPE_CHECKING, Union, Dict, List, Generator, Optional from .typing import DataBlock +from .utils import TextBuffer if TYPE_CHECKING: from os import PathLike @@ -41,7 +40,6 @@ def __init__( self.quote_character = quote_character self.quote_all_strings = quote_all_strings self.buffer = TextBuffer() - self.backup_if_file_exists() def coerce_data_blocks( self, @@ -97,14 +95,6 @@ def data_block_generator(self) -> Generator[str, None, None]: ): yield line - def backup_if_file_exists(self): - if self.filename and self.filename.exists(): - new_name = self.filename.name + '~' - backup_path = self.filename.resolve().parent / new_name - if backup_path.exists(): - backup_path.unlink() - self.filename.rename(backup_path) - def coerce_dataframe(df: pd.DataFrame) -> Dict[str, DataBlock]: return {'': df} @@ -133,9 +123,11 @@ def package_info(): return f'# Created by the starfile Python package (version {__version__}) at {time} on {date}' -def quote(x, *, - quote_character: str = '"', - quote_all_strings: bool = False) -> str: +def quote( + x, *, + quote_character: str = '"', + quote_all_strings: bool = False +) -> str: if isinstance(x, str) and (quote_all_strings or ' ' in x or not x): return f'{quote_character}{x}{quote_character}' return x @@ -147,7 +139,6 @@ def simple_block( quote_character: str = '"', quote_all_strings: bool = False ) -> Generator[str, None, None]: - yield f'data_{block_name}' yield '' for k, v in data.items(): @@ -165,7 +156,6 @@ def loop_block( quote_character: str = '"', quote_all_strings: bool = False ) -> Generator[str, None, None]: - # Header yield f'data_{block_name}' yield '' @@ -175,10 +165,10 @@ def loop_block( # Data for line in df.map(lambda x: - quote(x, - quote_character=quote_character, - quote_all_strings=quote_all_strings) - ).to_csv( + quote(x, + quote_character=quote_character, + quote_all_strings=quote_all_strings) + ).to_csv( mode='a', sep=separator, header=False, diff --git a/tests/test_functional_interface.py b/tests/test_functional_interface.py index 2f24c40..5b149f3 100644 --- a/tests/test_functional_interface.py +++ b/tests/test_functional_interface.py @@ -2,8 +2,8 @@ import pandas as pd import pytest -import starfile +import starfile from .constants import loop_simple, postprocess, test_df, test_data_directory @@ -29,18 +29,6 @@ def test_write(): assert output_file.exists() -def test_write_overwrites_with_backup(): - output_file = test_data_directory / 'test_overwrite_backup.star' - starfile.write(test_df, output_file) - assert output_file.exists() - - starfile.write(test_df, output_file) - backup = test_data_directory / 'test_overwrite_backup.star~' - assert backup.exists() - starfile.write(test_df, output_file) - assert backup.exists() - - def test_read_non_existent_file(): f = Path('non-existent-file.star') assert f.exists() is False diff --git a/tests/test_writing.py b/tests/test_writing.py index 8e0ee00..471ccae 100644 --- a/tests/test_writing.py +++ b/tests/test_writing.py @@ -11,6 +11,7 @@ from .constants import loop_simple, postprocess, test_data_directory, test_df from .utils import generate_large_star_file, remove_large_star_file + def test_write_simple_block(): s = StarParser(postprocess) output_file = test_data_directory / 'basic_block.star' @@ -72,19 +73,19 @@ def test_can_write_non_zero_indexed_one_row_dataframe(): assert (expected in output) -@pytest.mark.parametrize("quote_character, quote_all_strings, num_quotes", +@pytest.mark.parametrize("quote_character, quote_all_strings, num_quotes", [('"', False, 6), ('"', True, 8), ("'", False, 6), ("'", True, 8) - ]) + ]) def test_string_quoting_loop_datablock(quote_character, quote_all_strings, num_quotes, tmp_path): - df = pd.DataFrame([[1,"nospace", "String with space", " ", ""]], - columns=["a_number","string_without_space", "string_space", "just_space", "empty_string"]) + df = pd.DataFrame([[1, "nospace", "String with space", " ", ""]], + columns=["a_number", "string_without_space", "string_space", "just_space", "empty_string"]) filename = tmp_path / "test.star" StarWriter(df, filename, quote_character=quote_character, quote_all_strings=quote_all_strings).write() - + # Test for the appropriate number of quotes with open(filename) as f: star_content = f.read() @@ -93,6 +94,7 @@ def test_string_quoting_loop_datablock(quote_character, quote_all_strings, num_q s = StarParser(filename) assert df.equals(s.data_blocks[""]) + def test_writing_speed(): start = time.time() generate_large_star_file() @@ -102,13 +104,14 @@ def test_writing_speed(): # Check that execution takes less than a second assert end - start < 1 -@pytest.mark.parametrize("quote_character, quote_all_strings, num_quotes", + +@pytest.mark.parametrize("quote_character, quote_all_strings, num_quotes", [('"', False, 6), ('"', True, 8), ("'", False, 6), ("'", True, 8) - ]) -def test_string_quoting_simple_datablock(quote_character, quote_all_strings,num_quotes, tmp_path): + ]) +def test_string_quoting_simple_datablock(quote_character, quote_all_strings, num_quotes, tmp_path): o = { "a_number": 1, "string_without_space": "nospace", @@ -119,7 +122,7 @@ def test_string_quoting_simple_datablock(quote_character, quote_all_strings,num_ filename = tmp_path / "test.star" StarWriter(o, filename, quote_character=quote_character, quote_all_strings=quote_all_strings).write() - + # Test for the appropriate number of quotes with open(filename) as f: star_content = f.read()