🔍 Code Extractor

function is_ibis_expr

Maturity: 20

Checks whether a given data object is an Ibis column expression by verifying if the ibis module is loaded and if the data is an instance of ibis.expr.types.ColumnExpr.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1513 - 1517
Complexity:
simple

Purpose

This function provides a safe way to determine if data is an Ibis expression without requiring ibis as a hard dependency. It first checks if ibis is already imported in sys.modules before attempting to use it, making it suitable for libraries that optionally support Ibis but don't require it. This pattern allows for graceful degradation when ibis is not available.

Source Code

def is_ibis_expr(data):
    if 'ibis' in sys.modules:
        import ibis
        return isinstance(data, ibis.expr.types.ColumnExpr)
    return False

Parameters

Name Type Default Kind
data - - positional_or_keyword

Parameter Details

data: Any Python object to be checked. Typically expected to be a data structure that might be an Ibis column expression, but can be any type. No constraints on input type.

Return Value

Returns a boolean value. True if the ibis module is loaded in sys.modules AND the data parameter is an instance of ibis.expr.types.ColumnExpr. Returns False if ibis is not loaded or if data is not an Ibis column expression.

Dependencies

  • sys
  • ibis

Required Imports

import sys

Conditional/Optional Imports

These imports are only needed under specific conditions:

import ibis

Condition: only imported if 'ibis' is already present in sys.modules (lazy import within function)

Optional

Usage Example

import sys
import ibis

# Example 1: With ibis loaded and valid expression
conn = ibis.connect('duckdb://')
table = conn.table('my_table')
column_expr = table.column_name
result = is_ibis_expr(column_expr)
print(result)  # True

# Example 2: With non-ibis data
result = is_ibis_expr([1, 2, 3])
print(result)  # False

# Example 3: When ibis is not imported
# (assuming ibis was never imported)
result = is_ibis_expr(some_data)
print(result)  # False

Best Practices

  • This function uses lazy importing to avoid forcing ibis as a hard dependency, which is a good pattern for optional features
  • The function checks sys.modules before importing to avoid ImportError exceptions
  • Use this function when you need to handle both ibis and non-ibis data types in a unified interface
  • This pattern is thread-safe as sys.modules is a shared dictionary managed by Python's import system
  • Consider using this function in type dispatch or polymorphic functions that need to handle multiple data backend types

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function is_dataframe 45.6% similar

    Checks whether the supplied data object is a pandas DataFrame or a Dask DataFrame, with support for lazy imports of both libraries.

    From: /tf/active/vicechatdev/patches/util.py
  • function is_cupy_array 44.1% similar

    Checks whether the provided data object is a CuPy ndarray by conditionally importing CuPy and performing an isinstance check.

    From: /tf/active/vicechatdev/patches/util.py
  • function is_series 42.3% similar

    Checks whether the supplied data object is a pandas Series or dask Series type, with lazy loading support for dask.

    From: /tf/active/vicechatdev/patches/util.py
  • function is_dask_array 41.7% similar

    Checks whether the provided data object is a Dask array by conditionally importing dask.array and performing an isinstance check.

    From: /tf/active/vicechatdev/patches/util.py
  • function identify_variables 38.9% similar

    Categorizes DataFrame columns into Eimeria infection variables, performance measure variables, and grouping variables based on keyword matching in column names.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/5a059cb7-3903-4020-8519-14198d1f39c9/analysis_1.py
← Back to Browse