2626import sys
2727from typing import Iterable , List
2828
29- from . import broad_splits
29+ from . import broad_splits , smart_sort
3030from . import data_repository as dr
3131from . import db
3232from .exceptions import InvalidAlleleError , InvalidMACError , InvalidTypingError
4141 expression_chars ,
4242 DEFAULT_CACHE_SIZE ,
4343)
44- from .smart_sort import smart_sort_comparator
4544
4645default_config = {
4746 "reduce_serology" : True ,
@@ -128,6 +127,12 @@ def __init__(
128127 self ._redux_allele
129128 )
130129 self .redux = functools .lru_cache (maxsize = max_cache_size )(self .redux )
130+ self .is_mac = functools .lru_cache (maxsize = max_cache_size )(self .is_mac )
131+ self .smart_sort_comparator = functools .lru_cache (maxsize = max_cache_size )(
132+ smart_sort .smart_sort_comparator
133+ )
134+ else :
135+ self .smart_sort_comparator = smart_sort .smart_sort_comparator
131136
132137 # reference data is read-only and can be frozen
133138 # Works only for Python >= 3.9
@@ -256,8 +261,7 @@ def _redux_allele(
256261 else :
257262 raise InvalidAlleleError (f"{ allele } is an invalid allele." )
258263
259- @staticmethod
260- def _sorted_unique_gl (gls : List [str ], delim : str ) -> str :
264+ def _sorted_unique_gl (self , gls : List [str ], delim : str ) -> str :
261265 """
262266 Make a list of sorted unique GL Strings separated by delim.
263267 As the list may itself contains elements that are separated by the
@@ -274,7 +278,7 @@ def _sorted_unique_gl(gls: List[str], delim: str) -> str:
274278 if delim == "+" :
275279 # No need to make unique. eg. homozygous cases are valid for SLUGs
276280 return delim .join (
277- sorted (gls , key = functools .cmp_to_key (smart_sort_comparator ))
281+ sorted (gls , key = functools .cmp_to_key (self . smart_sort_comparator ))
278282 )
279283
280284 # generate a unique list over a delimiter
@@ -284,7 +288,7 @@ def _sorted_unique_gl(gls: List[str], delim: str) -> str:
284288 all_gls += gl .split (delim )
285289 unique_gls = set (all_gls )
286290 return delim .join (
287- sorted (unique_gls , key = functools .cmp_to_key (smart_sort_comparator ))
291+ sorted (unique_gls , key = functools .cmp_to_key (self . smart_sort_comparator ))
288292 )
289293
290294 @functools .lru_cache (maxsize = DEFAULT_CACHE_SIZE )
@@ -427,6 +431,7 @@ def is_serology(self, allele: str) -> bool:
427431
428432 return db .is_valid_serology (self .db_connection , allele )
429433
434+ @functools .lru_cache (maxsize = DEFAULT_CACHE_SIZE )
430435 def is_mac (self , allele : str ) -> bool :
431436 """
432437 MAC has non-digit characters after the : character.
0 commit comments