Skip to content

Commit 477ea98

Browse files
authored
Merge pull request #87 from tlambert03/delay-import
refactor: delay import of pandas until needed
2 parents afe97d3 + 6f89935 commit 477ea98

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/scyjava/_convert.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import math
99
from bisect import insort
10+
from importlib.util import find_spec
1011
from pathlib import Path
1112
from typing import Any, Callable, Dict, List, NamedTuple
1213

@@ -677,7 +678,7 @@ def _stock_py_converters() -> List:
677678
priority=Priority.VERY_LOW,
678679
),
679680
]
680-
if _import_pandas(required=False):
681+
if find_spec("pandas"):
681682
converters.append(
682683
Converter(
683684
name="org.scijava.table.Table -> pandas.DataFrame",
@@ -716,7 +717,7 @@ def _stock_py_converters() -> List:
716717
),
717718
]
718719
)
719-
if _import_numpy(required=False):
720+
if find_spec("numpy"):
720721
converters.append(
721722
Converter(
722723
name="primitive array -> numpy.ndarray",
@@ -803,16 +804,15 @@ def _jarray_shape(jarr):
803804
return shape
804805

805806

806-
def _import_numpy(required=True):
807+
def _import_numpy():
807808
try:
808809
import numpy as np
809810

810811
return np
811812
except ImportError as e:
812-
if required:
813-
msg = "The NumPy library is missing (https://numpy.org/). "
814-
msg += "Please install it before using this function."
815-
raise RuntimeError(msg) from e
813+
msg = "The NumPy library is missing (https://numpy.org/). "
814+
msg += "Please install it before using this function."
815+
raise RuntimeError(msg) from e
816816

817817

818818
######################################
@@ -838,16 +838,15 @@ def _convert_table(obj: Any):
838838
return None
839839

840840

841-
def _import_pandas(required=True):
841+
def _import_pandas():
842842
try:
843843
import pandas as pd
844844

845845
return pd
846846
except ImportError as e:
847-
if required:
848-
msg = "The Pandas library is missing (http://pandas.pydata.org/). "
849-
msg += "Please install it before using this function."
850-
raise RuntimeError(msg) from e
847+
msg = "The Pandas library is missing (http://pandas.pydata.org/). "
848+
msg += "Please install it before using this function."
849+
raise RuntimeError(msg) from e
851850

852851

853852
def _table_to_pandas(table):

0 commit comments

Comments
 (0)