From 60f54bdf618ebbf46c9a276c5a1cf4e18aa6ab7f Mon Sep 17 00:00:00 2001 From: Fabian Sinz Date: Mon, 13 Jul 2015 09:36:44 -0500 Subject: [PATCH 1/2] added getitem with commit functionality --- datajoint/relational_operand.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/datajoint/relational_operand.py b/datajoint/relational_operand.py index 38c1d8ea8..c408f6189 100644 --- a/datajoint/relational_operand.py +++ b/datajoint/relational_operand.py @@ -304,6 +304,24 @@ def make_condition(arg): return ' WHERE ' + ' AND '.join(condition_string) + def __getitem__(self, item): + if isinstance(item, str): + return self.project(item) + elif isinstance(item, dict): + return self.project(**item) + elif isinstance(item, list) or isinstance(item, tuple): + return self.project(*item) + elif isinstance(item, slice): + tmp = list(self.heading.attributes.keys()) + start = tmp.index(item.start) if isinstance(item.start, str) else item.start + stop = tmp.index(item.stop) if isinstance(item.stop, str) else item.stop + item = slice(start, stop, item.step) + return self.project(*tmp[item]) + elif isinstance(item, int): + return self.project(list(self.heading.attributes.keys())[item]) + + + class Not: """ inverse restriction @@ -363,6 +381,7 @@ def __init__(self, arg, group=None, *attributes, **renamed_attributes): if group: if arg.connection != group.connection: raise DataJointError('Cannot join relations with different database connections') + # TODO: don't Subquery if not necessary (if does not have some types of restrictions) self._group = Subquery(group) self._arg = Subquery(arg) else: From 498f40b8be6dc27d2942bee02e532e0c381dbb3b Mon Sep 17 00:00:00 2001 From: Fabian Sinz Date: Tue, 14 Jul 2015 18:43:19 -0500 Subject: [PATCH 2/2] declare table is not exists --- datajoint/declare.py | 2 +- datajoint/relational_operand.py | 24 +++++++++++++----------- setup.py | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/datajoint/declare.py b/datajoint/declare.py index eceb70b48..3810d64e7 100644 --- a/datajoint/declare.py +++ b/datajoint/declare.py @@ -66,7 +66,7 @@ def declare(full_table_name, definition, context): # compile SQL if not primary_key: raise DataJointError('Table must have a primary key') - sql = 'CREATE TABLE %s (\n ' % full_table_name + sql = 'CREATE TABLE IF NOT EXISTS %s (\n ' % full_table_name sql += ',\n '.join(attribute_sql) sql += ',\n PRIMARY KEY (`' + '`,`'.join(primary_key) + '`)' if foreign_key_sql: diff --git a/datajoint/relational_operand.py b/datajoint/relational_operand.py index c408f6189..9b2ec1a4e 100644 --- a/datajoint/relational_operand.py +++ b/datajoint/relational_operand.py @@ -303,23 +303,25 @@ def make_condition(arg): return ' WHERE ' + ' AND '.join(condition_string) + def __getitem__(self, item): # TODO: implement dj.key and primary key return + + attr_keys = list(self.heading.attributes.keys()) - def __getitem__(self, item): if isinstance(item, str): - return self.project(item) - elif isinstance(item, dict): - return self.project(**item) + args = (item,) elif isinstance(item, list) or isinstance(item, tuple): - return self.project(*item) + args = item elif isinstance(item, slice): - tmp = list(self.heading.attributes.keys()) - start = tmp.index(item.start) if isinstance(item.start, str) else item.start - stop = tmp.index(item.stop) if isinstance(item.stop, str) else item.stop + start = attr_keys.index(item.start) if isinstance(item.start, str) else item.start + stop = attr_keys.index(item.stop) if isinstance(item.stop, str) else item.stop item = slice(start, stop, item.step) - return self.project(*tmp[item]) + args = attr_keys[item] elif isinstance(item, int): - return self.project(list(self.heading.attributes.keys())[item]) - + args = attr_keys[item] + else: + raise DataJointError("Index must be a slice, a tuple, a list, or a string.") + tmp = self.project(*args).fetch() + return tuple(tmp[e] for e in args) class Not: diff --git a/setup.py b/setup.py index 5c56b483c..0866fe90e 100644 --- a/setup.py +++ b/setup.py @@ -11,5 +11,5 @@ url='https://github.com/datajoint/datajoint-python', packages=['datajoint'], requires=['numpy', 'pymysql', 'networkx', 'matplotlib', 'sphinx_rtd_theme', 'mock', 'json'], - license = "MIT", + license = "GNU LGPL", )