Skip to content

Commit

Permalink
Merge pull request #43 from JuBiotech/issue-42
Browse files Browse the repository at this point in the history
Make `Worklist` accept `Path` objects
  • Loading branch information
Y0dler authored Jul 18, 2023
2 parents 556b827 + 74b7e85 commit 8c1e4d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
12 changes: 12 additions & 0 deletions robotools/evotools/test_worklist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import tempfile
from pathlib import Path

import numpy as np
import pytest
Expand Down Expand Up @@ -268,6 +269,17 @@ def test_aspirate_systemliquid(self) -> None:
assert wl[-1] == "A;Systemliquid;;;1;;200.00;;;;"
return

def test_accepts_path(self):
fp = Path(tempfile.gettempdir(), os.urandom(24).hex() + ".gwl")
try:
with Worklist(fp) as wl:
wl.comment("Test")
assert isinstance(wl._filepath, Path)
assert fp.exists()
finally:
fp.unlink(missing_ok=True)
return

def test_save(self) -> None:
tf = tempfile.mktemp() + ".gwl"
error = None
Expand Down
20 changes: 11 additions & 9 deletions robotools/evotools/worklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import collections
import logging
import math
import os
import typing
import warnings
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union

import numpy
Expand Down Expand Up @@ -277,23 +277,25 @@ class Worklist(list):

def __init__(
self,
filepath: Optional[str] = None,
filepath: Optional[Union[str, Path]] = None,
max_volume: typing.Union[int, float] = 950,
auto_split: bool = True,
) -> None:
"""Creates a worklist writer.
Parameters
----------
filepath : str
filepath
Optional filename/filepath to write when the context is exited (must include a .gwl extension)
max_volume : int
Maximum aspiration volume in µL
auto_split : bool
If `True`, large volumes in transfer operations are automatically splitted.
If set to `False`, `InvalidOperationError` is raised when a pipetting volume exceeds `max_volume`.
"""
self._filepath = filepath
self._filepath: Optional[Path] = None
if filepath is not None:
self._filepath = Path(filepath)
if max_volume is None:
raise ValueError("The `max_volume` parameter is required.")
self.max_volume = max_volume
Expand All @@ -309,17 +311,17 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
self.save(self._filepath)
return

def save(self, filepath: str) -> None:
def save(self, filepath: Union[str, Path]) -> None:
"""Writes the worklist to the filepath.
Parameters
----------
filepath : str
filepath
File name or path to write (must include a .gwl extension)
"""
assert ".gwl" in filepath.lower(), "The filename did not contain the .gwl extension."
if os.path.exists(filepath):
os.remove(filepath)
filepath = Path(filepath)
assert ".gwl" in filepath.name.lower(), "The filename did not contain the .gwl extension."
filepath.unlink(missing_ok=True)
with open(filepath, "w", newline="\r\n", encoding="latin_1") as file:
file.write("\n".join(self))
return
Expand Down

0 comments on commit 8c1e4d3

Please sign in to comment.