def test_clone(s, v, e): column1 = Column(s) column1.add_value(v, e) schema1 = column1.get_schema() column2 = column1.clone() schema2 = column2.get_schema() assert schema1 == schema2
def test_get_shema(s, v): column = Column(s) column.add_value(v) schema = column.get_schema() assert "id" in schema and schema[ "id"] == s and "count" in schema and schema[ "count"] == 1 and "min_value" in schema and schema[ "min_value"] == v and "max_value" in schema and schema[ "max_value"] == v
def __init__(self,data): self.table = data['table'] self.type = data['type'] if('count' in data.keys()): self.count = data['count'] self.columns = [] for col in data['columns']: colOjb = Column(col) colOjb.setParent(self) self.columns.append(colOjb)
def test_get_shema(s, v, e): column = Column(s) column.add_value(v, e) schema = column.get_schema() assert "id" in schema assert schema["id"] == s assert "count" in schema assert schema["count"] == 1 assert "min_value" in schema assert schema["min_value"] == v assert "max_value" in schema assert schema["max_value"] == v
def apply_filter(self, filter): column_name = filter["column"] if column_name not in self.columns: logging.error("table.apply_filter wrong column: %s" % column_name) return self if filter["from"] > filter["to"]: logging.error("table.apply_filter wrong from-to: %s" % column_name) return self filtered_table = Table(self.id) for tmp_column_name in self.columns: filtered_table.columns[tmp_column_name] = Column(tmp_column_name) column = self.columns[column_name] values = np.array(column.values) filtered_indexes = np.where((values >= filter["from"]) & (values <= filter["to"]))[0] for column_name in self.columns: col_values, col_error_values = self.columns[ column_name].get_values(filtered_indexes) filtered_table.columns[column_name].add_values( col_values, col_error_values) return filtered_table
def apply_filter(self, filter): column_name = filter["column"] if column_name not in self.columns: logging.error("table.apply_filter wrong column: %s" % column_name) return self if filter["from"] > filter["to"]: logging.error("table.apply_filter wrong from-to: %s" % column_name) return self filtered_table = Table(self.id) for tmp_column_name in self.columns: filtered_table.columns[tmp_column_name] = Column(tmp_column_name) column = self.columns[column_name] filtered_indexes = np.array([ i for i in range(len(column.values)) if ((column.values[i] >= filter["from"]) and ( column.values[i] <= filter["to"])) ]) for i in filtered_indexes: filtered_table.add_row(self.get_row(i)) return filtered_table
def __init__(self,data): self.name = data['name'] if('count' in data.keys()): self.count = data['count'] self.tableName = data['table'] self.columns = [] self.relations = [] for col in data['columns']: colObj = Column(col); colObj.setParent(self) self.columns.append(colObj) if 'relations' in data.keys(): for rel in data['relations']: relObj = Relation(rel); relObj.setParent(self) self.relations.append(relObj)
def attributes(this): d = {} if isinstance(this, object.__class__): d = dict([(k, v) for k, v in this.__dict__.items() if v.__class__ == Column]) else: d = dict([(k, v) for k, v in this.__class__.__dict__.items() if v.__class__ == Column]) if "id" not in d.keys(): d["id"] = Column(type=Type.int) return d
def post(self, workspace, action): #logging.info("hello world") ##used for CRUD for tasks and dnd operations #if action not in self.__actions__: # raise WrongActionException("Action %s is not supported" % action) #if action != "create" and not id: # raise WrongActionException("Id should not be null for %s action" % action) logging.info("============= Doing %s ===============" % action) if action == "create": name = self.request.get("name") body = self.request.get("body") column = Column.gql("WHERE allow_create=TRUE").get() logging.info("column: %s, name: %s, body: %s" % (column, name, body)) task = Task(column=column, name=unicode(name), body=unicode(body)) task.put() if action == "dnd": task_id = self.request.get("task") dest_column_key = self.request.get("to") logging.info("task: %s" % task_id) logging.info("to: %s" % dest_column_key) task = Task.get(task_id) dest_column = Column.get(dest_column_key) task.column = dest_column task.put() if action == "delete": # #delete task id = self.request.get("taskId") task = Task.get(id) db.delete(task) if action == "edit": id = self.request.get("taskId") name = self.request.get("name") body = self.request.get("body") task = Task.get(id) task.name = name task.body = body task.put()
def post(self): user = users.get_current_user() name = self.request.get("name") ws = Workspace(user = user, name = unicode(name)) ws.put() backlog = Column(workspace = ws, name = u"Backlog", allow_create = True) backlog.put() for i in range(1, 5): c = Column(workspace = ws, name = u"Column %s" % i) c.put()
def post(self): user = users.get_current_user() name = self.request.get("name") ws = Workspace(user = user, name = unicode(name)) ws.put() backlog = Column(workspace = ws, name = u"Backlog", allow_create = True) backlog.put() for i in range(1, 5): c = Column(workspace = ws, name = u"Column %s" % i) c.put() data = {ws.id: ws.json} res = json.dumps(data, indent=4) self.error(200) self.response.out.write(res)
def test_get_error_value(s, v, e): column = Column(s) column.add_value(v, e) assert column.get_error_value(0) == e
def test_add_value(s, v, e): column = Column(s) column.add_value(v, e) assert len(column.values) == 1
def attributes(this): d = dict([(k, v) for k, v in this.__dict__.items() if v.__class__ is Column]) if "id" not in d.keys(): d["id"] = Column(type=Type.int) return d
def add_columns(self, column_names): for i in range(len(column_names)): self.columns[column_names[i]] = Column(column_names[i])
def test_init(s): column = Column(s) assert column and column.id == s
class Model: id = Column(type=Type.int) def __new__(cls, *args, **kwargs): if not cls.is_exist_table(): cls.create() else: attributes = Model.attributes(cls) diff = cls.difference(attributes) if diff is not None: for k, v in diff.items(): if v == "new": column = getattr(cls, k) cls.add_column(k, column) return super().__new__(cls) def __init__(self): pass @classmethod def query(cls, where=[], order=[], limit=None): criteria = Locator.query(cls) return criteria.query(where, order, limit) @classmethod def all(cls): criteria = Locator.query(cls) return criteria.all() @classmethod def size(cls): criteria = Locator.query(cls) return criteria.size() @classmethod def create(cls): criteria = Locator.query(cls) return criteria.create() @classmethod def remove(cls): pass @classmethod def is_exist_table(cls): criteria = Locator.query(cls) return criteria.is_exist_table() @classmethod def difference(cls, attributes): criteria = Locator.query(cls) return criteria.difference(attributes) @classmethod def add_column(cls, name, column): criteria = Locator.query(cls) return criteria.add_column(name, column) def save(self): connector = None try: connector = Database.connector() cursor = connector.cursor() try: sql = "insert into " + Model.table_name(self) + " (" for k, v in Model.attributes(self).items(): value = getattr(self, k) if k == "id" and value is Column: continue sql += k + "," sql = sql[0:-1] + ") values(" for k, v in Model.attributes(self).items(): value = getattr(self, k) if k == "id" and value is Column: continue if isinstance(getattr(self, k), int): sql += str(value) + "," else: if value is not None and value.__class__ is not Column: sql += "\"" + str(value) + "\"," else: sql += "null," sql = sql[0:-1] + ")" cursor.execute(sql) connector.commit() pass except Exception as e: print('type:' + str(type(e))) print('args:' + str(e.args)) finally: cursor.close() finally: connector.close() def delete(self): connector = None try: connector = Database.connector() cursor = connector.cursor() try: sql = "delete from " + Model.table_name( self) + " where id = " + str(getattr(self, "id")) + ";" cursor.execute(sql) connector.commit() pass except Exception as e: print('type:' + str(type(e))) print('args:' + str(e.args)) finally: cursor.close() finally: connector.close() @staticmethod def attributes(this): d = {} if isinstance(this, object.__class__): d = dict([(k, v) for k, v in this.__dict__.items() if v.__class__ == Column]) else: d = dict([(k, v) for k, v in this.__class__.__dict__.items() if v.__class__ == Column]) if "id" not in d.keys(): d["id"] = Column(type=Type.int) return d @staticmethod def table_name(this): return re.sub("([A-Z])", lambda x: "_" + x.group(1).lower(), this.__class__.__name__)[1:]
def test_get_value(s, v): column = Column(s) column.add_value(v) assert column.get_value(0) == v