Skip to content

Commit 24ffe9f

Browse files
implement describe for projected dependencies
1 parent 658beea commit 24ffe9f

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

datajoint/declare.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,13 @@ def build_foreign_key_parser_old():
3434

3535

3636
def build_foreign_key_parser():
37-
left = pp.Literal('(').suppress()
38-
right = pp.Literal(')').suppress()
39-
attribute_name = pp.Word(pp.srange('[a-z]'), pp.srange('[a-z0-9_]'))
40-
new_attrs = pp.Optional(left + pp.delimitedList(attribute_name) + right).setResultsName('new_attrs')
4137
arrow = pp.Literal('->').suppress()
4238
lbracket = pp.Literal('[').suppress()
4339
rbracket = pp.Literal(']').suppress()
4440
option = pp.Word(pp.srange('[a-zA-Z]'))
4541
options = pp.Optional(lbracket + pp.delimitedList(option) + rbracket).setResultsName('options')
4642
ref_table = pp.restOfLine.setResultsName('ref_table')
47-
return new_attrs + arrow + options + ref_table
43+
return arrow + options + ref_table
4844

4945

5046
def build_attribute_parser():
@@ -96,16 +92,16 @@ def compile_foreign_key(line, context, attributes, primary_key, attr_sql, foreig
9692
from .table import Table
9793
from .query import Projection
9894

99-
new_style = False # See issue #436. Old style to be deprecated in a future release
95+
new_style = True # See issue #436. Old style to be deprecated in a future release
10096
try:
101-
result = foreign_key_parser_old.parseString(line)
102-
except pp.ParseException as err:
97+
result = foreign_key_parser.parseString(line)
98+
except pp.ParseException:
10399
try:
104-
result = foreign_key_parser.parseString(line)
100+
result = foreign_key_parser_old.parseString(line)
105101
except pp.ParseBaseException as err:
106-
raise DataJointError('Parsing error in line "%s". %s.' % (line, err))
102+
raise DataJointError('Parsing error in line "%s". %s.' % (line, err)) from None
107103
else:
108-
new_style = True
104+
new_style = False
109105
try:
110106
ref = eval(result.ref_table, context)
111107
except Exception if new_style else NameError:
@@ -129,7 +125,7 @@ def compile_foreign_key(line, context, attributes, primary_key, attr_sql, foreig
129125

130126
# check that dependency is of supported type
131127
if (not isinstance(ref, (Table, Projection)) or len(ref.restriction) or
132-
(isinstance(ref, Projection) and (not isinstance(ref._arg, Table) or not len(ref._arg.restriction)))):
128+
(isinstance(ref, Projection) and (not isinstance(ref._arg, Table) or len(ref._arg.restriction)))):
133129
raise DataJointError('Dependency "%s" is not supported (yet). Use a base table or its projection.' %
134130
result.ref_table)
135131

datajoint/table.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,15 +488,14 @@ def describe(self, context=None, printout=True):
488488
props=index_props,
489489
class_name=lookup_class_name(parent_name, context) or parent_name)
490490
else:
491-
# expression foreign key
491+
# projected foreign key
492492
parent_name = list(self.connection.dependencies.in_edges(parent_name))[0][0]
493493
lst = [(attr, ref) for attr, ref in fk_props['attr_map'].items() if ref != attr]
494-
definition += '({attr_list}) ->{props} {class_name}{ref_list}\n'.format(
494+
definition += '->{props} {class_name}.proj({proj_list})\n'.format(
495495
attr_list=', '.join(r[0] for r in lst),
496496
props=index_props,
497497
class_name=lookup_class_name(parent_name, context) or parent_name,
498-
ref_list=('' if len(attributes_thus_far) - len(attributes_declared) == 1
499-
else '(%s)' % ','.join(r[1] for r in lst)))
498+
proj_list=','.join('{}="{}"'.format(a,b) for a, b in lst))
500499
attributes_declared.update(fk_props['attr_map'])
501500
if do_include:
502501
attributes_declared.add(attr.name)

tests/schema.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class SigTermTable(dj.Computed):
260260
-> SimpleSource
261261
"""
262262

263-
def _make_tuples(self, key):
263+
def make(self, key):
264264
os.kill(os.getpid(), signal.SIGTERM)
265265

266266

@@ -271,12 +271,13 @@ class DecimalPrimaryKey(dj.Lookup):
271271
"""
272272
contents = zip((0.1, 0.25, 3.99))
273273

274+
274275
@schema
275276
class IndexRich(dj.Manual):
276277
definition = """
277278
-> Experiment
278279
---
279-
(first) ->[unique, nullable] User
280+
-> [unique, nullable] User.proj(first="username")
280281
first_date : date
281282
value : int
282283
index (first_date, value)

tests/schema_advanced.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ class LocalSynapse(dj.Manual):
108108

109109
@schema
110110
class GlobalSynapse(dj.Manual):
111+
# Mix old-style and new-style projected foreign keys
111112
definition = """
112113
# a synapse within the slice
113-
(pre_slice, pre_cell) -> Cell(slice, cell)
114+
-> Cell.proj(pre_slice="slice", pre_cell="cell")
114115
(post_slice, post_cell)-> Cell(slice, cell)
115116
"""

0 commit comments

Comments
 (0)