Skip to content

Commit a53f367

Browse files
Teach .describe to do populations and variables.
1 parent 0a1ef51 commit a53f367

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

shell/src/core.py

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,9 @@ def dot_describe(self, line):
509509
tokens = line.split()
510510
if len(tokens) == 0:
511511
self.stdout.write('Usage: .describe table(s) [<table>...]\n')
512+
self.stdout.write(' .describe population(s) [<pop>...]\n')
513+
self.stdout.write(' .describe variables <pop>\n')
512514
self.stdout.write(' .describe generator(s) [<gen>...]\n')
513-
self.stdout.write(' .describe columns <gen>\n')
514515
self.stdout.write(' .describe model(s) <gen> [<model>...]\n')
515516
return
516517
if casefold(tokens[0]) == 'table' or \
@@ -542,6 +543,31 @@ def dot_describe(self, line):
542543
''' % (qualifier,)
543544
with self._bdb.savepoint():
544545
pretty.pp_cursor(self.stdout, self._bdb.execute(sql, params))
546+
elif casefold(tokens[0]) in ('population', 'populations'):
547+
params = None
548+
qualifier = None
549+
if len(tokens) == 1:
550+
params = ()
551+
qualifier = '1'
552+
else:
553+
params = tokens[1:]
554+
names = ','.join('?%d' % (i + 1,) for i in xrange(len(params)))
555+
qualifier = '(name IN (%s))' % (names,)
556+
ok = True
557+
for population in params:
558+
if not core.bayesdb_has_population(self._bdb, population):
559+
self.stdout.write('No such population: %s\n' %
560+
(repr(population),))
561+
ok = False
562+
if not ok:
563+
return
564+
with self._bdb.savepoint():
565+
cursor = self._bdb.sql_execute('''
566+
SELECT id, name, tabname
567+
FROM bayesdb_population
568+
WHERE %s
569+
''' % (qualifier,), params)
570+
pretty.pp_cursor(self.stdout, cursor)
545571
elif casefold(tokens[0]) == 'generator' or \
546572
casefold(tokens[0]) == 'generators':
547573
params = None
@@ -571,29 +597,30 @@ def dot_describe(self, line):
571597
with self._bdb.savepoint():
572598
pretty.pp_cursor(self.stdout,
573599
self._bdb.sql_execute(sql, params))
574-
elif casefold(tokens[0]) == 'columns':
600+
elif casefold(tokens[0]) == 'variables':
575601
if len(tokens) != 2:
576-
self.stdout.write('Describe columns of what generator?\n')
602+
self.stdout.write('Usage: .describe variables <population>\n')
577603
return
578-
generator = tokens[1]
604+
population = tokens[1]
579605
with self._bdb.savepoint():
580-
if not core.bayesdb_has_generator(self._bdb, generator):
581-
self.stdout.write('No such generator: %s\n' %
582-
(repr(generator),))
606+
if not core.bayesdb_has_population(self._bdb, population):
607+
self.stdout.write('No such population: %r\n' %
608+
(population,))
583609
return
584-
generator_id = core.bayesdb_get_generator(self._bdb, generator)
610+
population_id = core.bayesdb_get_population(
611+
self._bdb, population)
585612
sql = '''
586613
SELECT c.colno AS colno, c.name AS name,
587-
gc.stattype AS stattype, c.shortname AS shortname
588-
FROM bayesdb_generator AS g,
614+
v.stattype AS stattype, c.shortname AS shortname
615+
FROM bayesdb_population AS p,
589616
(bayesdb_column AS c LEFT OUTER JOIN
590-
bayesdb_generator_column AS gc
617+
bayesdb_variable AS v
591618
USING (colno))
592-
WHERE g.id = ? AND g.id = gc.generator_id
593-
AND g.tabname = c.tabname
619+
WHERE p.id = ? AND p.id = v.population_id
620+
AND p.tabname = c.tabname
594621
ORDER BY colno ASC;
595622
'''
596-
cursor = self._bdb.sql_execute(sql, (generator_id,))
623+
cursor = self._bdb.sql_execute(sql, (population_id,))
597624
pretty.pp_cursor(self.stdout, cursor)
598625
elif casefold(tokens[0]) == 'model' or \
599626
casefold(tokens[0]) == 'models':
@@ -637,5 +664,5 @@ def dot_describe(self, line):
637664
else:
638665
self.stdout.write('Usage: .describe table(s) [<table>...]\n')
639666
self.stdout.write(' .describe generator(s) [<gen>...]\n')
640-
self.stdout.write(' .describe columns <gen>\n')
667+
self.stdout.write(' .describe variables <pop>\n')
641668
self.stdout.write(' .describe model(s) <gen> [<model>...]\n')

0 commit comments

Comments
 (0)