2017-12-03 13:05:05 +01:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-12-14 20:02:17 +01:00
|
|
|
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'
|
|
|
|
|
|
|
|
|
2017-12-14 18:44:10 +01:00
|
|
|
class Point():
|
|
|
|
def __init__(self,list):
|
2017-12-14 20:02:17 +01:00
|
|
|
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)
|
2017-12-14 18:44:10 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Point %s, is numeric: %s>' %(self.points,self.isnumeric)
|
|
|
|
|
|
|
|
def __add__(self, other):
|
2017-12-14 20:02:17 +01:00
|
|
|
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)
|
2017-12-14 18:44:10 +01:00
|
|
|
return new
|
|
|
|
|
2017-12-14 20:02:17 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-12-14 18:44:10 +01:00
|
|
|
@staticmethod
|
|
|
|
def is_numeric(point):
|
|
|
|
value=0
|
|
|
|
for element in point:
|
|
|
|
if isinstance(element, int) or isinstance(element, float):
|
|
|
|
value+=1
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def TestFunction():
|
2017-12-14 20:02:17 +01:00
|
|
|
punkt1=Point(['a',2,2])
|
|
|
|
punkt2=Point([1,7,25])
|
|
|
|
print(len(punkt1))
|
|
|
|
print(len(punkt2))
|
2017-12-14 18:44:10 +01:00
|
|
|
print(punkt1,punkt2,punkt1+punkt2)
|
|
|
|
TestFunction()
|