From 08e5131479dd0f015b4326fd3d68444cb5440a86 Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Thu, 13 Aug 2020 14:37:55 +0200 Subject: [PATCH] Removed update/insert/delete I don't use them and AFAIK this module has no other users --- relational/relation.py | 66 ------------------------------------- tests_dir/rename_insert1.py | 4 --- tests_dir/rename_insert2.py | 4 --- tests_dir/update.py | 16 --------- 4 files changed, 90 deletions(-) delete mode 100644 tests_dir/rename_insert1.py delete mode 100644 tests_dir/rename_insert2.py delete mode 100644 tests_dir/update.py diff --git a/relational/relation.py b/relational/relation.py index c8a5f1e..9527aef 100644 --- a/relational/relation.py +++ b/relational/relation.py @@ -371,72 +371,6 @@ class Relation(NamedTuple): return res - def update(self, expr: str, dic: dict) -> int: - ''' - Updates certain values of a relation. - - expr must be a valid Python expression that can contain field names. - - This operation will change the relation itself instead of generating a new one, - updating all the tuples where expr evaluates as True. - - Dic must be a dictionary that has the form "field name":"new value". Every kind of value - will be converted into a string. - - Returns the number of affected rows. - ''' - affected = self.selection(expr) - not_affected = self.difference(affected) - - new_values = tuple( - zip(self.header.getAttributesId(dic.keys()), dic.values()) - ) - - for i in set(affected.content): - li = list(i) - - for column, value in new_values: - li[column] = value - not_affected.insert(li) - - self.content = not_affected.content - return len(affected) - - def insert(self, values: Union[list,tuple]) -> int: - ''' - Inserts a tuple in the relation. - This function will not insert duplicate tuples. - All the values will be converted in string. - Will return the number of inserted rows. - - Will fail if the tuple has the wrong amount of items. - ''' - - if len(self.header) != len(values): - raise Exception( - 'Tuple has the wrong size. Expected %d, got %d' % ( - len(self.header), - len(values) - ) - ) - - prevlen = len(self.content) - self.content.add(tuple(map(Rstring, values))) - return len(self.content) - prevlen - - def delete(self, expr: str) -> int: - ''' - Delete, expr must be a valid Python expression; can contain field names. - - This operation will change the relation itself instead of generating a new one, - deleting all the tuples where expr evaluates as True. - - Returns the number of affected rows.''' - - l = len(self.content) - self.content = self.difference(self.selection(expr)).content - return len(self.content) - l - class Header(tuple): diff --git a/tests_dir/rename_insert1.py b/tests_dir/rename_insert1.py deleted file mode 100644 index b2ccad6..0000000 --- a/tests_dir/rename_insert1.py +++ /dev/null @@ -1,4 +0,0 @@ -p1=people.rename({"id":"ido"}) -people.insert((123,"lala",0,31)) -assert people!=p1 -people.delete("id==123") diff --git a/tests_dir/rename_insert2.py b/tests_dir/rename_insert2.py deleted file mode 100644 index 8e271af..0000000 --- a/tests_dir/rename_insert2.py +++ /dev/null @@ -1,4 +0,0 @@ -p1=people.rename({"id":"ido"}) -p1.insert((123,"lala",0,31)) -assert people!=p1 -people.delete("id==123") diff --git a/tests_dir/update.py b/tests_dir/update.py deleted file mode 100644 index b0d0b6a..0000000 --- a/tests_dir/update.py +++ /dev/null @@ -1,16 +0,0 @@ -p1=people -p2=p1.rename({'id':'i'}) -p2=p2.rename({'i':'id'}) -assert p1==p2 -assert p1._readonly -assert p2._readonly -# It is VERY important to not change the original relations -# or other tests might fail randomly, since the relations are -# only loaded once - -p2.update('age==20', {'age':50}) -assert p2._readonly == False -assert p1!=p2 -p3 = p2.selection('age!=50') -p4 = p1.selection('age!=20') -assert p3==p4