1
0
forked from tdwojak/Python2017
Python2017/labs04/task03.py

89 lines
2.5 KiB
Python
Raw Normal View History

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
2017-12-14 20:31:14 +01:00
2017-12-14 18:44:10 +01:00
def __repr__(self):
return '<Point %s, is numeric: %s>' %(self.points,self.isnumeric)
2017-12-14 20:31:14 +01:00
2017-12-14 18:44:10 +01:00
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
2017-12-14 20:31:14 +01:00
def to_string(self):
point_tuple = tuple(self.points)
return str(point_tuple)
2017-12-14 18:44:10 +01:00
2017-12-14 20:31:14 +01:00
def __str__(self):
return (self.to_string())
2017-12-14 18:44:10 +01:00
def TestFunction():
2017-12-14 20:31:14 +01:00
punkt1=Point([1,2,2])
2017-12-14 20:02:17 +01:00
punkt2=Point([1,7,25])
2017-12-14 20:31:14 +01:00
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__()
2017-12-14 18:44:10 +01:00
TestFunction()