Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
701bf76
fix #483 and #516: .proj() preserves original order. Also rename cla…
dimitri-yatsenko Nov 13, 2018
38f1dba
Merge branch 'dev'
dimitri-yatsenko Nov 13, 2018
08e61e6
complete renaming query -> expression
dimitri-yatsenko Nov 13, 2018
025dd93
rename class Expression -> QueryExpression
dimitri-yatsenko Nov 13, 2018
1d5b593
add test for create_virtual_module and for issue #516
dimitri-yatsenko Nov 13, 2018
f0c8b8f
Merge branch 'master' of https://github.com/datajoint/datajoint-python
dimitri-yatsenko Nov 13, 2018
a348758
fix doc strings to use QueryExpression
dimitri-yatsenko Nov 13, 2018
8edb4d7
add test for QueryExpression iterator
dimitri-yatsenko Nov 13, 2018
85dca73
finish renaming query -> expression in fetch.py
dimitri-yatsenko Nov 13, 2018
470682a
update version and CHANGELOG for release 0.11.1
dimitri-yatsenko Nov 13, 2018
65ea0e7
minor
dimitri-yatsenko Nov 13, 2018
8cca325
minor
dimitri-yatsenko Nov 14, 2018
77644ee
increase timeout for nosetests in travis
dimitri-yatsenko Nov 14, 2018
b46b64b
change terminology from `relations` to `query expressions` in doc str…
dimitri-yatsenko Nov 14, 2018
277398f
undo travis setting -- travis still not working on GitHub
dimitri-yatsenko Nov 16, 2018
06d5cc3
undo inconsequential change from previous commit to try to figure out…
dimitri-yatsenko Nov 16, 2018
10f7393
simplify context management, move test initialization into setup_clas…
dimitri-yatsenko Nov 17, 2018
29ca4c9
remove make_module_code for lack of potential use, tagging this commi…
dimitri-yatsenko Nov 17, 2018
bae0900
remove erd.topological_sort -- it is never called
dimitri-yatsenko Nov 17, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename class Expression -> QueryExpression
  • Loading branch information
dimitri-yatsenko committed Nov 13, 2018
commit 025dd93908c8124154a7f23c65b426c01aee5e9f
4 changes: 2 additions & 2 deletions datajoint/autopopulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import random
from tqdm import tqdm
from pymysql import OperationalError
from .expression import Expression, AndList, U
from .expression import QueryExpression, AndList, U
from .errors import DataJointError
from .table import FreeTable
import signal
Expand Down Expand Up @@ -84,7 +84,7 @@ def _jobs_to_do(self, restrictions):
raise DataJointError('Cannot call populate on a restricted table. '
'Instead, pass conditions to populate() as arguments.')
todo = self.key_source
if not isinstance(todo, Expression):
if not isinstance(todo, QueryExpression):
raise DataJointError('Invalid key_source value')
# check if target lacks any attributes from the primary key of key_source
try:
Expand Down
52 changes: 26 additions & 26 deletions datajoint/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def assert_join_compatibility(rel1, rel2):
Determine if relations rel1 and rel2 are join-compatible. To be join-compatible, the matching attributes
in the two relations must be in the primary key of one or the other relation.
Raises an exception if not compatible.
:param rel1: A Expression object
:param rel2: A Expression object
:param rel1: A QueryExpression object
:param rel2: A QueryExpression object
"""
for rel in (rel1, rel2):
if not isinstance(rel, (U, Expression)):
if not isinstance(rel, (U, QueryExpression)):
raise DataJointError('Object %r is not a relation and cannot be joined.' % rel)
if not isinstance(rel1, U) and not isinstance(rel2, U): # dj.U is always compatible
try:
Expand Down Expand Up @@ -56,13 +56,13 @@ def is_true(restriction):
return restriction is True or isinstance(restriction, AndList) and not len(restriction)


class Expression:
class QueryExpression:
"""
Expression implements the relational algebra.
Expression objects link other relational operands with relational operators.
QueryExpression implements the relational algebra.
QueryExpression objects link other relational operands with relational operators.
The leaves of this tree of objects are base relations.
When fetching data from the database, this tree of objects is compiled into an SQL expression.
Expression operators are restrict, join, proj, and aggr.
QueryExpression operators are restrict, join, proj, and aggr.
"""

def __init__(self, arg=None):
Expand All @@ -72,7 +72,7 @@ def __init__(self, arg=None):
self._distinct = False
self._heading = None
else: # copy
assert isinstance(arg, Expression), 'Cannot make Expression from %s' % arg.__class__.__name__
assert isinstance(arg, QueryExpression), 'Cannot make QueryExpression from %s' % arg.__class__.__name__
self._restriction = AndList(arg._restriction)
self._distinct = arg.distinct
self._heading = arg._heading
Expand Down Expand Up @@ -163,11 +163,11 @@ def prep_value(v):
AndList(('`%s`=%r' % (k, prep_value(arg[k])) for k in arg.dtype.fields if k in self.heading)))

# restrict by a Relation class -- triggers instantiation
if inspect.isclass(arg) and issubclass(arg, Expression):
if inspect.isclass(arg) and issubclass(arg, QueryExpression):
arg = arg()

# restrict by another relation (aka semijoin and antijoin)
if isinstance(arg, Expression):
if isinstance(arg, QueryExpression):
assert_join_compatibility(self, arg)
common_attributes = [q for q in arg.heading.names if q in self.heading.names]
return (
Expand Down Expand Up @@ -508,7 +508,7 @@ def __next__(self):
key = self._iter_keys.pop(0)
except AttributeError:
# self._iter_keys is missing because __iter__ has not been called.
raise TypeError("'Expression' object is not an iterator. Use iter(obj) to create an iterator.")
raise TypeError("'QueryExpression' object is not an iterator. Use iter(obj) to create an iterator.")
except IndexError:
raise StopIteration
else:
Expand Down Expand Up @@ -545,7 +545,7 @@ def __init__(self, restriction):
self.restriction = restriction


class Join(Expression):
class Join(QueryExpression):
"""
Relational join.
Join is a private DataJoint class not exposed to users.
Expand All @@ -564,7 +564,7 @@ def __init__(self, arg=None):
@classmethod
def create(cls, arg1, arg2, keep_all_rows=False):
obj = cls()
if inspect.isclass(arg2) and issubclass(arg2, Expression):
if inspect.isclass(arg2) and issubclass(arg2, QueryExpression):
arg2 = arg2() # instantiate if joining with a class
assert_join_compatibility(arg1, arg2)
if arg1.connection != arg2.connection:
Expand Down Expand Up @@ -594,7 +594,7 @@ def from_clause(self):
from2=self._arg2.from_clause)


class Union(Expression):
class Union(QueryExpression):
"""
Union is a private DataJoint class that implements relational union.
"""
Expand All @@ -613,9 +613,9 @@ def __init__(self, arg=None):
@classmethod
def create(cls, arg1, arg2):
obj = cls()
if inspect.isclass(arg2) and issubclass(arg2, Expression):
if inspect.isclass(arg2) and issubclass(arg2, QueryExpression):
arg2 = arg2() # instantiate if a class
if not isinstance(arg1, Expression) or not isinstance(arg2, Expression):
if not isinstance(arg1, QueryExpression) or not isinstance(arg2, QueryExpression):
raise DataJointError('a relation can only be unioned with another relation')
if arg1.connection != arg2.connection:
raise DataJointError("Cannot operate on relations from different connections.")
Expand Down Expand Up @@ -645,10 +645,10 @@ def from_clause(self):
where2=self._arg2.where_clause)) % next(self.__count)


class Projection(Expression):
class Projection(QueryExpression):
"""
Projection is a private DataJoint class that implements relational projection.
See Expression.proj() for user interface.
See QueryExpression.proj() for user interface.
"""

def __init__(self, arg=None):
Expand Down Expand Up @@ -706,11 +706,11 @@ def from_clause(self):
return self._arg.from_clause


class GroupBy(Expression):
class GroupBy(QueryExpression):
"""
GroupBy(rel, comp1='expr1', ..., compn='exprn') produces a relation with the primary key specified by rel.heading.
The computed arguments comp1, ..., compn use aggregation operators on the attributes of rel.
GroupBy is used Expression.aggr and U.aggr.
GroupBy is used QueryExpression.aggr and U.aggr.
GroupBy is a private class in DataJoint, not exposed to users.
"""

Expand All @@ -726,7 +726,7 @@ def __init__(self, arg=None):

@classmethod
def create(cls, arg, group, attributes=None, named_attributes=None, keep_all_rows=False):
if inspect.isclass(group) and issubclass(group, Expression):
if inspect.isclass(group) and issubclass(group, QueryExpression):
group = group() # instantiate if a class
assert_join_compatibility(arg, group)
obj = cls()
Expand Down Expand Up @@ -756,7 +756,7 @@ def __len__(self):
return len(Subquery.create(self))


class Subquery(Expression):
class Subquery(QueryExpression):
"""
A Subquery encapsulates its argument in a SELECT statement, enabling its use as a subquery.
The attribute list and the WHERE clause are resolved. Thus, a subquery no longer has any renamed attributes.
Expand Down Expand Up @@ -858,9 +858,9 @@ def primary_key(self):
return self._primary_key

def __and__(self, relation):
if inspect.isclass(relation) and issubclass(relation, Expression):
if inspect.isclass(relation) and issubclass(relation, QueryExpression):
relation = relation() # instantiate if a class
if not isinstance(relation, Expression):
if not isinstance(relation, QueryExpression):
raise DataJointError('Relation U can only be restricted with another relation.')
return Projection.create(relation, attributes=self.primary_key,
named_attributes=dict(), include_primary_key=False)
Expand All @@ -871,9 +871,9 @@ def __mul__(self, relation):
:param relation: other relation
:return: a copy of the other relation with the primary key extended.
"""
if inspect.isclass(relation) and issubclass(relation, Expression):
if inspect.isclass(relation) and issubclass(relation, QueryExpression):
relation = relation() # instantiate if a class
if not isinstance(relation, Expression):
if not isinstance(relation, QueryExpression):
raise DataJointError('Relation U can only be joined with another relation.')
copy = relation.__class__(relation)
copy._heading = copy.heading.extend_primary_key(self.primary_key)
Expand Down
10 changes: 5 additions & 5 deletions datajoint/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pymysql import OperationalError, InternalError, IntegrityError
from . import config
from .declare import declare
from .expression import Expression
from .expression import QueryExpression
from .blob import pack
from .utils import user_choice
from .heading import Heading
Expand All @@ -24,7 +24,7 @@ class _rename_map(tuple):
pass


class Table(Expression):
class Table(QueryExpression):
"""
Table is an abstract class that represents a base relation, i.e. a table in the schema.
To make it a concrete class, override the abstract properties specifying the connection,
Expand All @@ -37,7 +37,7 @@ class Table(Expression):
_log_ = None
_external_table = None

# -------------- required by Expression ----------------- #
# -------------- required by QueryExpression ----------------- #
@property
def heading(self):
"""
Expand Down Expand Up @@ -180,9 +180,9 @@ def insert(self, rows, replace=False, skip_duplicates=False, ignore_extra_fields
'Auto-populate tables can only be inserted into from their make methods during populate calls.')

heading = self.heading
if inspect.isclass(rows) and issubclass(rows, Expression): # instantiate if a class
if inspect.isclass(rows) and issubclass(rows, QueryExpression): # instantiate if a class
rows = rows()
if isinstance(rows, Expression):
if isinstance(rows, QueryExpression):
# insert from select
if not ignore_extra_fields:
try:
Expand Down