From aed06ae658088998fd6221603f5874e767d5c383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Kaczmarek?= Date: Thu, 14 Dec 2017 20:02:17 +0100 Subject: [PATCH] in progress --- labs04/task03.py | 63 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/labs04/task03.py b/labs04/task03.py index 7180837..31a0696 100644 --- a/labs04/task03.py +++ b/labs04/task03.py @@ -1,23 +1,64 @@ #!/usr/bin/env python2 # -*- coding: utf-8 -*- +class DimensionError(Exception): + """Discreapncies in dimensions. We cannot add points from different dimension""" + def __init__(self): + super().__init__(self) + self.msg='Discreapncies in dimensions. We cannot add points from different dimension' + +class IsNumericError(Exception): + """Point contains other chars than numbers""" + def __init__(self): + super().__init__(self) + self.msg='Point contains other chars than numbers' + + class Point(): def __init__(self,list): - self.points=list - if len(list)==Point.is_numeric(self.points): - self.isnumeric=True - else: - self.isnumeric=False + try: + if Point.is_numeric(list)!=len(list): + self.points=[] + self.isnumeric = False + raise IsNumericError() + else: + self.points=list + self.isnumeric=True + except IsNumericError as e: + print(e.msg) def __repr__(self): return '' %(self.points,self.isnumeric) def __add__(self, other): - new=[] - for index in range(len(self.points)): - new.append(self.points[index]+other.points[index]) + try: + new = [] + if self.isnumeric==False or other.isnumeric==False: + raise IsNumericError() + elif len(self.points) != len(other.points): + raise DimensionError() + else: + for index in range(len(self.points)): + new.append(self.points[index]+other.points[index]) + except IsNumericError as e: + print(e.msg) + except DimensionError as e: + print(e.msg) return new + def __len__(self): + len=0 + try: + if self.isnumeric==False: + raise IsNumericError() + else: + for element in self.points: + len+=1 + except IsNumericError as e: + print(e.msg) + return len + + @staticmethod def is_numeric(point): value=0 @@ -30,7 +71,9 @@ class Point(): def TestFunction(): - punkt1=Point([12,12,2]) - punkt2=Point([1,3,7]) + punkt1=Point(['a',2,2]) + punkt2=Point([1,7,25]) + print(len(punkt1)) + print(len(punkt2)) print(punkt1,punkt2,punkt1+punkt2) TestFunction() \ No newline at end of file