def query(program, indexspec, *args): """Usage: <indexspec> [query] Query the specified index using the given clauses. """ from discodb import Q query = Q.scan(' '.join(args), and_op=' ', or_op=',') for result in program.client.query(indexspec, query): print result
def input_stream(fd, size, url, params): scheme, netloc, rest = util.urlsplit(url) if netloc[0] == Task.netloc[0]: path, rest = rest.split('!', 1) if '!' in rest else (rest, '') Task.discodb = DiscoDB.load(open(os.path.join(Task.root, path))) if rest: method, arg = rest.split('/', 1) if method == 'query': if hasattr(params, 'discodb_query'): return Task.discodb.query(params.discodb_query), size, url return Task.discodb.query(Q.urlscan(arg)), size, url return getattr(Task.discodb, method)(), size, url return Task.discodb, size, url raise core.DiscoError("Scheme 'discodb' can only be used with force_local=True")
def input_stream(fd, size, url, params): import os from disco import util from disco.comm import download from discodb import DiscoDB, Q scheme, netloc, rest = util.urlsplit(url) path, rest = rest.split('!', 1) if '!' in rest else (rest, '') if netloc[0] == Task.netloc[0]: discodb = DiscoDB.load(open(os.path.join(Task.root, path))) else: discodb = DiscoDB.loads(download('disco://%s/%s' % (netloc, path))) if rest: method_name, arg = rest.split('/', 1) if '/' in rest else (rest, None) method = getattr(discodb, method_name) if method_name in ('metaquery', 'query'): return method(Q.urlscan(arg)), size, url return method(*filter(None, arg)), size, url return discodb, size, url
def Open(url, task=None): if task: disco_data = task.disco_data ddfs_data = task.ddfs_data else: from disco.settings import DiscoSettings settings = DiscoSettings() disco_data = settings['DISCO_DATA'] ddfs_data = settings['DDFS_DATA'] scheme, netloc, rest = util.urlsplit(url) path, rest = rest.split('!', 1) if '!' in rest else (rest, '') discodb = DiscoDB.load(open(util.localize(path, disco_data=disco_data, ddfs_data=ddfs_data))) if rest: method_name, arg = rest.split('/', 1) if '/' in rest else (rest, None) method = getattr(discodb, method_name) if method_name in ('metaquery', 'query'): return method(Q.urlscan(arg)) return method(*filter(None, arg)) return discodb
def Open(url, task=None): if task: disco_data = task.disco_data ddfs_data = task.ddfs_data else: from disco.settings import DiscoSettings settings = DiscoSettings() disco_data = settings['DISCO_DATA'] ddfs_data = settings['DDFS_DATA'] scheme, netloc, rest = util.urlsplit(url) path, rest = rest.split('!', 1) if '!' in rest else (rest, '') discodb = DiscoDB.load( open(util.localize(path, disco_data=disco_data, ddfs_data=ddfs_data))) if rest: method_name, arg = rest.split('/', 1) if '/' in rest else (rest, None) method = getattr(discodb, method_name) if method_name in ('metaquery', 'query'): return method(Q.urlscan(arg)) return method(*filter(None, arg)) return discodb
def test_query(self): q = Q.parse('5 & 10 & (15 | 30)') list(self.discodb.query(q))
def q(self, s): return self.discodb.query(Q.parse(s))
def test_query_results_nonkey(self): q = Q.parse('nonkey') self.assertEquals(list(self.discodb.query(q)), [])
def test_query_results(self): q = Q.parse('5') self.assertEquals(list(self.discodb.query(q)), list(self.discodb.get('5')))
def __init__(self, index, query_path): self.index = index self.query = Q.urlscan(query_path)
# could do multiple criteria of sub-relation: # - tag co-occurence # - label inheritance # - subset of musical feature space # * testing of discodb data = {'mammals': ['cow', 'dog', 'cat', 'whale'], 'pets': ['dog', 'cat', 'goldfish'], 'aquatic': ['goldfish', 'whale']} dbx = DiscoDB(data) # create an immutable discodb object x = dbx.query(Q.parse('mammals & aquatic')) [print(i) for i in x] qry = Q.parse('mammals') qry2 = Q.metaquery(dbx, 'mammals') x = dbx.metaquery('pets', qry2) dbx. d = DiscoDB({'A': ['B', 'C'], 'B': 'D', 'C': 'E', 'D': 'F', 'E': 'G'}) sorted(d.query(Q.parse('A'))) sorted(d.query(Q.parse('B'))) sorted(d.query(Q.parse('*A'))) sorted(d.query(Q.parse('A | B'))) sorted(d.query(Q.parse('*A | B')))