Skip to content

Commit

Permalink
Alphebetized and added comments to lookup() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
hover2pi committed Apr 19, 2016
1 parent fae1237 commit 2865dc6
Showing 1 changed file with 51 additions and 44 deletions.
95 changes: 51 additions & 44 deletions astrodbkit/astrodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,57 @@ def inventory(self, source_id, fetch=False, fmt='table'):

if fetch: return data_tables


def lookup(self, criteria, table, columns=''):
"""
Returns a table of records from *table* the same length as *criteria*
with the best match for each element.
Parameters
----------
criteria: sequence
The search criteria
table: str
The table to search
columns: sequence
The column name in the sources table to search
Returns
-------
results: sequence
A sequence the same length as objlist with source_ids that correspond to successful matches
"""
results, colmasks = [], []

# Iterate through the list, trying to match objects
for n,criterion in enumerate(criteria):
records = self.search(criterion, table, columns=columns, fetch=True)

# If multiple matches, take the first but notify the user of the other matches
if len(records)>1:
print("'{}' matched to {} other record{}.".format(criterion, len(records)-1, \
's' if len(records)-1>1 else ''))

# If no matches, make an empty row
if len(records)==0:
records.add_row(np.asarray(np.zeros(len(records.colnames))).T)
colmasks.append([True]*len(records.colnames))
else:
colmasks.append([False]*len(records.colnames))

# Grab the first row
results.append(records[0])

# Add all the rows to the results table
table = at.Table(rows=results, names=results[0].colnames, masked=True)

# Mask the rows with no matches
for col,msk in zip(records.colnames,np.asarray(colmasks).T):
table[col].mask = msk

return table


def _lowest_rowids(self, table, limit):
"""
Gets the lowest available row ids for table insertion. Keeps things tidy!
Expand Down Expand Up @@ -977,50 +1028,6 @@ def search(self, criterion, table, columns='', fetch=False):
pprint(results, title=table.upper())
else:
print("No results found for {} in {} the table.".format(criterion, table.upper()))

def lookup(self, criteria, table, columns=''):
"""
Returns a table of records from *table* the same length as *criteria*
with the best match for each element.
Parameters
----------
criteria: sequence
The search criteria
columns: sequence
The column name in the sources table to search
Returns
-------
results: sequence
A sequence the same length as objlist with source_ids that correspond to successful matches
"""
results, colmasks = [], []

# Iterate through the list, trying to match objects
for n,criterion in enumerate(criteria):
records = self.search(criterion, table, columns=columns, fetch=True)

# If multiple matches, take the first but notify the user of the other matches
if len(records)>1:
print("'{}' matched to {} other record{}.".format(criterion, len(records)-1, \
's' if len(records)-1>1 else ''))

# If no matches, make an empty row
if len(records)==0:
records.add_row(np.asarray(np.zeros(len(records.colnames))).T)
colmasks.append([True]*len(records.colnames))
else:
colmasks.append([False]*len(records.colnames))

results.append(records[0])

table = at.Table(rows=results, names=results[0].colnames, masked=True)

for col,msk in zip(records.colnames,np.asarray(colmasks).T):
table[col].mask = msk

return table


def table(self, table, columns, types, constraints='', new_table=False):
Expand Down

0 comments on commit 2865dc6

Please sign in to comment.