FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor: delay import of pandas until needed by tlambert03 · Pull Request #87 · scijava/scyjava · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 11 additions & 12 deletions src/scyjava/_convert.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import math
from bisect import insort
from importlib.util import find_spec
from pathlib import Path
from typing import Any, Callable, Dict, List, NamedTuple

Expand Down Expand Up @@ -677,7 +678,7 @@ def _stock_py_converters() -> List:
priority=Priority.VERY_LOW,
),
]
if _import_pandas(required=False):
if find_spec("pandas"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is a great trick, thanks!

converters.append(
Converter(
name="org.scijava.table.Table -> pandas.DataFrame",
Expand Down Expand Up @@ -716,7 +717,7 @@ def _stock_py_converters() -> List:
),
]
)
if _import_numpy(required=False):
if find_spec("numpy"):
converters.append(
Converter(
name="primitive array -> numpy.ndarray",
Expand Down Expand Up @@ -803,16 +804,15 @@ def _jarray_shape(jarr):
return shape


def _import_numpy(required=True):
def _import_numpy():
try:
import numpy as np

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


######################################
Expand All @@ -838,16 +838,15 @@ def _convert_table(obj: Any):
return None


def _import_pandas(required=True):
def _import_pandas():
try:
import pandas as pd

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


def _table_to_pandas(table):
Expand Down
Loading

Back | FazBrowse Home | New Git URL