From c4a2234acae8f29ebb3db381aaafb3d664b0597b Mon Sep 17 00:00:00 2001 From: Joshua Shorenstein Date: Thu, 12 Jun 2014 09:29:44 -0600 Subject: [PATCH] Taking care of @adamrp suggestions --- qiita_db/study.py | 55 ++++++++++++++++++------------------- qiita_db/test/test_study.py | 6 ++-- qiita_db/user.py | 6 ++-- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/qiita_db/study.py b/qiita_db/study.py index 4893f99b9..1d331097f 100644 --- a/qiita_db/study.py +++ b/qiita_db/study.py @@ -97,6 +97,7 @@ from __future__ import division from future.builtins import zip +from future.utils import viewitems from datetime import date from copy import deepcopy @@ -157,15 +158,15 @@ def create(cls, owner, title, efo, info, investigation=None): ---------- owner : User object the study' owner - title: str + title : str Title of the study - efo: int or list + efo : int or list Experimental Factor Ontology or -ies for the study - info: dict + info : dict the information attached to the study. All "*_id" keys must pass the objects associated with them. - investigation: Investigation object - if the study is part of an investigation, the id to associate with + investigation : Investigation object, optional + If passed, the investigation to associate with. Defaults to None. Raises ------ @@ -182,13 +183,13 @@ def create(cls, owner, title, efo, info, investigation=None): """ # make sure not passing non-info columns in the info dict if cls._non_info.intersection(info): - raise QiitaDBColumnError("non info keys passed: %s" % - cls._non_info.intersection(info)) + raise QiitaDBColumnError("non info keys passed: %s" % + cls._non_info.intersection(info)) # add default values to info insertdict = deepcopy(info) if "first_contact" not in insertdict: - insertdict['first_contact'] = date.today().strftime("%B %d, %Y") + insertdict['first_contact'] = date.today().isoformat() insertdict['email'] = owner.id insertdict['study_title'] = title if "reprocess" not in insertdict: @@ -203,13 +204,12 @@ def create(cls, owner, title, efo, info, investigation=None): check_required_columns(conn_handler, insertdict, cls._table) # Insert study into database - keys = insertdict.keys() sql = ("INSERT INTO qiita.{0} ({1}) VALUES ({2}) RETURNING " - "study_id".format(cls._table, ','.join(keys), + "study_id".format(cls._table, ','.join(insertdict), ','.join(['%s'] * len(insertdict)))) # make sure data in same order as sql column names, and ids are used data = [] - for col in keys: + for col in insertdict: if isinstance(insertdict[col], QiitaObject): data.append(insertdict[col].id) else: @@ -218,7 +218,7 @@ def create(cls, owner, title, efo, info, investigation=None): # insert efo information into database if isinstance(efo, int): - efo = [efo] + efo = [efo] sql = ("INSERT INTO qiita.{0}_experimental_factor (study_id, " "efo_id) VALUES (%s, %s)".format(cls._table)) conn_handler.executemany(sql, list(zip([study_id] * len(efo), efo))) @@ -309,9 +309,8 @@ def info(self, info): sql_vals = [] data = [] - # items() used for py3 compatability # build query with data values in correct order for SQL statement - for key, val in info.items(): + for key, val in viewitems(info): sql_vals.append("{0} = %s".format(key)) if isinstance(val, QiitaObject): data.append(val.id) @@ -336,13 +335,13 @@ def efo(self, efo_vals): Parameters ---------- - status_id: int + status_id : int ID for the new status """ conn_handler = SQLConnectionHandler() self._lock_public(conn_handler) if isinstance(efo_vals, int): - efo_vals = [efo_vals] + efo_vals = [efo_vals] # wipe out any EFOs currently attached to study sql = ("DELETE FROM qiita.{0}_experimental_factor WHERE " "study_id = %s".format(self._table)) @@ -351,7 +350,7 @@ def efo(self, efo_vals): sql = ("INSERT INTO qiita.{0}_experimental_factor (study_id, " "efo_id) VALUES (%s, %s)".format(self._table)) conn_handler.executemany(sql, list(zip([self._id] * len(efo_vals), - efo_vals))) + efo_vals))) @property def shared_with(self): @@ -451,7 +450,7 @@ def add_pmid(self, pmid): Parameters ---------- - pmid: str + pmid : str pmid to associate with study """ conn_handler = SQLConnectionHandler() @@ -465,13 +464,13 @@ class StudyPerson(QiitaObject): Attributes ---------- - name: str + name : str name of the person - email: str + email : str email of the person - address: str or None + address : str or None address of the person - phone: str or None + phone : str or None phone number of the person """ _table = "study_person" @@ -503,13 +502,13 @@ def create(cls, name, email, address=None, phone=None): Parameters ---------- - name: str + name : str name of person - email: str + email : str email of person - address: str, optional + address : str, optional address of person - phone: str, optional + phone : str, optional phone number of person Returns @@ -582,7 +581,7 @@ def address(self, value): Parameters ---------- - value: str + value : str New address for person """ conn_handler = SQLConnectionHandler() @@ -610,7 +609,7 @@ def phone(self, value): Parameters ---------- - value: str + value : str New phone number for person """ conn_handler = SQLConnectionHandler() diff --git a/qiita_db/test/test_study.py b/qiita_db/test/test_study.py index c4ba1e6bf..354ef17fb 100644 --- a/qiita_db/test/test_study.py +++ b/qiita_db/test/test_study.py @@ -1,6 +1,8 @@ from unittest import TestCase, main from datetime import date +from future.utils import viewitems + from qiita_core.exceptions import IncompetentQiitaDeveloperError from qiita_core.util import qiita_test_checker from qiita_db.base import QiitaObject @@ -154,7 +156,7 @@ def test_create_study_min_data(self): 'reprocess': False, 'study_status_id': 1, 'number_samples_promised': 28, 'emp_person_id': 2, 'funding': None, 'vamps_id': None, - 'first_contact': date.today().strftime("%B %d, %Y"), + 'first_contact': date.today().isoformat(), 'principal_investigator_id': 3, 'timeseries_type_id': 1, 'study_abstract': 'Exploring how a high fat diet changes the ' @@ -291,7 +293,7 @@ def test_set_efo_public(self): self.study.efo = 6 def test_retrieve_info(self): - for key, val in self.existingexp.items(): + for key, val in viewitems(self.existingexp): if isinstance(val, QiitaObject): self.existingexp[key] = val.id self.assertEqual(self.study.info, self.existingexp) diff --git a/qiita_db/user.py b/qiita_db/user.py index 975e0afa7..1383168b3 100644 --- a/qiita_db/user.py +++ b/qiita_db/user.py @@ -73,7 +73,7 @@ class User(QiitaObject): "pass_reset_code", "pass_reset_timestamp"} def _check_id(self, id_, conn_handler=None): - r"""Check that the provided ID actually exists on the database + r"""Check that the provided ID actually exists in the database Parameters ---------- @@ -84,8 +84,8 @@ def _check_id(self, id_, conn_handler=None): Notes ----- - This functionoverwrites the base function, as sql layout doesn't follow - the same conventions done in the other classes. + This function overwrites the base function, as sql layout doesn't + follow the same conventions done in the other classes. """ self._check_subclass()