Skip to content

Commit

Permalink
remove backup mechanism (#76)
Browse files Browse the repository at this point in the history
* remove backup mechanism

* remove no longer useful test
  • Loading branch information
alisterburt authored Nov 18, 2024
1 parent 820e6c3 commit 30877a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
38 changes: 14 additions & 24 deletions src/starfile/writer.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -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():
Expand All @@ -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 ''
Expand All @@ -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,
Expand Down
14 changes: 1 addition & 13 deletions tests/test_functional_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
Expand Down
21 changes: 12 additions & 9 deletions tests/test_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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",
Expand All @@ -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()
Expand Down

0 comments on commit 30877a0

Please sign in to comment.