1
0
forked from tdwojak/Python2017
Python2017/labs04/task03.py
Przemysław Kaczmarek e0beb9d336 resolved
2017-12-14 20:31:14 +01:00

89 lines
2.5 KiB
Python

#!/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):
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 '<Point %s, is numeric: %s>' %(self.points,self.isnumeric)
def __add__(self, other):
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
for element in point:
if isinstance(element, int) or isinstance(element, float):
value+=1
return value
def to_string(self):
point_tuple = tuple(self.points)
return str(point_tuple)
def __str__(self):
return (self.to_string())
def TestFunction():
punkt1=Point([1,2,2])
punkt2=Point([1,7,25])
print('Ilosc wymiarow: ', len(punkt1))
print('Ilość wymiarów: ', len(punkt2))
punkt3=punkt1+punkt2
print('punkt1 + punkt2 = ',punkt3)
print(punkt1.to_string(),punkt1)
print(punkt2.to_string(),punkt2)
#punkt1.__str__()
TestFunction()