Skip to content

Commit 7b28c64

Browse files
committed
Eliminate _jobs_to_do method
- Inline the logic directly in populate() and progress() - Move restriction check to populate() - Use (self.key_source & AndList(restrictions)).proj() directly - Remove unused QueryExpression import
1 parent 55d7f32 commit 7b28c64

File tree

1 file changed

+9
-31
lines changed

1 file changed

+9
-31
lines changed

src/datajoint/autopopulate.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from tqdm import tqdm
1414

1515
from .errors import DataJointError, LostConnectionError
16-
from .expression import AndList, QueryExpression
16+
from .expression import AndList
1717

1818
# noinspection PyExceptionInherit,PyCallingNonCallable
1919

@@ -167,34 +167,6 @@ def jobs(self):
167167
self._jobs_table = JobsTable(self)
168168
return self._jobs_table
169169

170-
def _jobs_to_do(self, restrictions):
171-
"""
172-
:return: the query yielding the keys to be computed (derived from self.key_source)
173-
"""
174-
if self.restriction:
175-
raise DataJointError(
176-
"Cannot call populate on a restricted table. Instead, pass conditions to populate() as arguments."
177-
)
178-
todo = self.key_source
179-
180-
# key_source is a QueryExpression subclass -- trigger instantiation
181-
if inspect.isclass(todo) and issubclass(todo, QueryExpression):
182-
todo = todo()
183-
184-
if not isinstance(todo, QueryExpression):
185-
raise DataJointError("Invalid key_source value")
186-
187-
try:
188-
# check if target lacks any attributes from the primary key of key_source
189-
raise DataJointError(
190-
"The populate target lacks attribute %s "
191-
"from the primary key of key_source"
192-
% next(name for name in todo.heading.primary_key if name not in self.heading)
193-
)
194-
except StopIteration:
195-
pass
196-
return (todo & AndList(restrictions)).proj()
197-
198170
def populate(
199171
self,
200172
*restrictions,
@@ -243,6 +215,11 @@ def populate(
243215
if self.connection.in_transaction:
244216
raise DataJointError("Populate cannot be called during a transaction.")
245217

218+
if self.restriction:
219+
raise DataJointError(
220+
"Cannot call populate on a restricted table. " "Instead, pass conditions to populate() as arguments."
221+
)
222+
246223
valid_order = ["original", "reverse", "random"]
247224
if order not in valid_order:
248225
raise DataJointError("The order argument must be one of %s" % str(valid_order))
@@ -272,7 +249,8 @@ def handler(signum, frame):
272249
else:
273250
# Legacy behavior: get keys from key_source
274251
if keys is None:
275-
keys = (self._jobs_to_do(restrictions) - self).fetch("KEY", limit=limit)
252+
todo = (self.key_source & AndList(restrictions)).proj()
253+
keys = (todo - self).fetch("KEY", limit=limit)
276254

277255
if order == "reverse":
278256
keys.reverse()
@@ -457,7 +435,7 @@ def progress(self, *restrictions, display=False):
457435
Report the progress of populating the table.
458436
:return: (remaining, total) -- numbers of tuples to be populated
459437
"""
460-
todo = self._jobs_to_do(restrictions)
438+
todo = (self.key_source & AndList(restrictions)).proj()
461439
total = len(todo)
462440
remaining = len(todo - self)
463441
if display:

0 commit comments

Comments
 (0)