Skip to content

Commit 1a22487

Browse files
committed
feat: add cached factory function (#65)
1 parent 6de784e commit 1a22487

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/agct/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""Provide fast liftover in Python via the ``chainfile`` crate."""
22

33
from agct.assembly_registry import Assembly, get_assembly_from_refget_id
4-
from agct.converter import Converter, LiftoverResult, Strand
4+
from agct.converter import Converter, LiftoverResult, Strand, get_converter
55

66
__all__ = [
77
"Assembly",
88
"Converter",
99
"LiftoverResult",
1010
"Strand",
1111
"get_assembly_from_refget_id",
12+
"get_converter",
1213
]

src/agct/converter.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
from collections.abc import Callable
55
from enum import Enum
6+
from functools import cache
67
from pathlib import Path
78
from typing import NamedTuple
89

@@ -53,8 +54,8 @@ def __init__(
5354
the `wags-tails documentation <https://wags-tails.readthedocs.io/>`_ for more info.
5455
* If ``chainfile`` arg is provided, all other args are ignored.
5556
56-
:param from_assembly: Assembly name, e.g. ``<Assembly.HG19>``
57-
:param to_assembly: database name, e.g. ``"hg38"``.
57+
:param from_assembly: Name of assembly being lifted over from
58+
:param to_assembly: Name of assembly to lift over to
5859
:param chainfile: Path to chainfile
5960
:raise ValueError: if required arguments are not passed or are invalid
6061
:raise FileNotFoundError: if unable to open corresponding chainfile
@@ -178,3 +179,17 @@ def convert_coordinate(
178179
LiftoverResult(result[0], lifted_over_start, lifted_over_end, strand)
179180
)
180181
return formatted_results
182+
183+
184+
@cache
185+
def get_converter(from_assembly: Assembly, to_assembly: Assembly) -> Converter:
186+
"""Get a converter to lift from one assembly to another.
187+
188+
This function wraps converter initialization with ``functools.cache``, so successive
189+
calls should return the same converter instance.
190+
191+
:param from_assembly: Name of assembly being lifted over from
192+
:param to_assembly: Name of assembly to lift over to
193+
:return: Converter instance
194+
"""
195+
return Converter(from_assembly=from_assembly, to_assembly=to_assembly)

tests/test_converter.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from agct import Assembly, Converter
8+
from agct import Assembly, Converter, get_converter
99

1010

1111
def test_valid(data_dir: Path):
@@ -34,3 +34,13 @@ def test_invalid():
3434
),
3535
):
3636
Converter(Assembly.HG19, "hg18")
37+
38+
39+
def test_factory_function():
40+
assert id(get_converter(Assembly.HG19, Assembly.HG38)) == id(
41+
get_converter(Assembly.HG19, Assembly.HG38)
42+
), "Factory function isn't returning the same instance on successive calls"
43+
44+
assert id(get_converter(Assembly.HG19, Assembly.HG38)) != id(
45+
get_converter(Assembly.HG38, Assembly.HG19)
46+
)

0 commit comments

Comments
 (0)