Skip to content

Commit 6ae1fc9

Browse files
committed
refactor: replace VfbConnect with SolrTermInfoFetcher and enhance DataFrame support
1 parent 313df3f commit 6ae1fc9

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

src/vfbquery/solr_fetcher.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import requests
22
import json
33
import logging
4-
from typing import List, Dict, Any, Optional
4+
import pandas as pd
5+
from typing import List, Dict, Any, Optional, Union
6+
from vfb_connect import vfb
57

68
class SolrTermInfoFetcher:
79
"""Fetches term information directly from the Solr server instead of using VfbConnect"""
@@ -10,20 +12,21 @@ def __init__(self, solr_url: str = "https://solr.virtualflybrain.org/solr/vfb_js
1012
"""Initialize with the Solr server URL"""
1113
self.solr_url = solr_url
1214
self.logger = logging.getLogger(__name__)
15+
self.vfb = vfb
1316

1417
def get_TermInfo(self, short_forms: List[str],
1518
return_dataframe: bool = False,
16-
summary: bool = False) -> List[Dict[str, Any]]:
19+
summary: bool = False) -> Union[List[Dict[str, Any]], pd.DataFrame]:
1720
"""
1821
Fetch term info from Solr directly, mimicking VFBconnect's interface
1922
2023
Args:
2124
short_forms: List of term IDs to fetch
22-
return_dataframe: If True, return as pandas DataFrame (not fully implemented)
25+
return_dataframe: If True, return as pandas DataFrame
2326
summary: If True, return summarized version
2427
2528
Returns:
26-
List of term info dictionaries
29+
List of term info dictionaries or DataFrame
2730
"""
2831
results = []
2932

@@ -54,7 +57,6 @@ def get_TermInfo(self, short_forms: List[str],
5457

5558
# Extract and parse the term_info string which is itself JSON
5659
term_info_str = docs[0]["term_info"][0]
57-
# No need to handle escapes - json.loads does that automatically
5860
term_info_obj = json.loads(term_info_str)
5961
results.append(term_info_obj)
6062

@@ -65,9 +67,23 @@ def get_TermInfo(self, short_forms: List[str],
6567
except Exception as e:
6668
self.logger.error(f"Unexpected error for {short_form}: {e}")
6769

68-
# Handle dataframe conversion if needed (this would need to be implemented)
69-
if return_dataframe:
70-
self.logger.warning("return_dataframe=True not fully implemented")
71-
# You would need to implement pandas DataFrame conversion logic here
70+
# Convert to DataFrame if requested
71+
if return_dataframe and results:
72+
try:
73+
return pd.json_normalize(results)
74+
except Exception as e:
75+
self.logger.error(f"Error converting to DataFrame: {e}")
76+
return results
7277

73-
return results
78+
return results
79+
80+
# Pass through any non-implemented methods to VFBconnect
81+
def __getattr__(self, name):
82+
"""
83+
Automatically pass through any non-implemented methods to VFBconnect
84+
85+
This allows us to use this class as a drop-in replacement for VfbConnect
86+
while only implementing the methods we want to customize.
87+
"""
88+
self.logger.debug(f"Passing through method call: {name}")
89+
return getattr(self.vfb, name)

src/vfbquery/vfb_queries.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import pysolr
22
from .term_info_queries import deserialize_term_info
3-
from vfb_connect.cross_server_tools import VfbConnect, dict_cursor
3+
# Replace VfbConnect import with our new SolrTermInfoFetcher
4+
from .solr_fetcher import SolrTermInfoFetcher
5+
# Keep dict_cursor if it's used elsewhere
6+
from vfb_connect.cross_server_tools import dict_cursor
47
from marshmallow import Schema, fields, post_load
58
from typing import List, Tuple, Dict, Any, Union
69
import pandas as pd
@@ -10,8 +13,8 @@
1013
# Connect to the VFB SOLR server
1114
vfb_solr = pysolr.Solr('http://solr.virtualflybrain.org/solr/vfb_json/', always_commit=False, timeout=990)
1215

13-
# Create a VFB connection object for retrieving instances
14-
vc = VfbConnect()
16+
# Replace VfbConnect with SolrTermInfoFetcher
17+
vc = SolrTermInfoFetcher()
1518

1619
class Query:
1720
def __init__(self, query, label, function, takes, preview=0, preview_columns=[], preview_results=[], output_format="table", count=-1):

0 commit comments

Comments
 (0)