From f787630edbe194c74ad778a9008fb6a99193e6a4 Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Tue, 14 Jul 2015 11:09:42 +0200 Subject: [PATCH] Use isinstance rather than comparing class attribute This allows to create subclasses --- relational/relation.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/relational/relation.py b/relational/relation.py index f0a3a4a..fea905f 100644 --- a/relational/relation.py +++ b/relational/relation.py @@ -94,7 +94,7 @@ class relation (object): It is not exactely related to relational algebra. Just a method used internally. Will raise an exception if they don't share the same attributes''' - if (self.__class__ != other.__class__): + if not isinstance(other, relation): raise Exception('Expected an instance of the same class') elif self.header == other.header: return other @@ -113,7 +113,7 @@ class relation (object): # Fills the attributes dictionary with the values of the tuple attributes = {attr: i[j].autocast() for j, attr in enumerate(self.header) - } + } try: if eval(expr, attributes): @@ -129,7 +129,9 @@ class relation (object): cause an exception. It is possible to use rename on attributes and then use the product''' - if (self.__class__ != other.__class__)or(self.header.sharedAttributes(other.header) != 0): + if (not isinstance(other, relation)): + raise Exception('Operand must be a relation') + if self.header.sharedAttributes(other.header) != 0: raise Exception( 'Unable to perform product on relations with colliding attributes' ) @@ -347,7 +349,7 @@ class relation (object): def __eq__(self, other): '''Returns true if the relations are the same, ignoring order of items. This operation is rather heavy, since it requires sorting and comparing.''' - if self.__class__ != other.__class__: + if not isinstance(other, relation): return False if set(self.header) != set(other.header):