Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Fixed repr
Browse files Browse the repository at this point in the history
  • Loading branch information
mitryp committed Feb 7, 2022
1 parent 5ff7341 commit 8d09bba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 56 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/dist
dist/
*.egg-info
venv
.idea
venv/
.idea/
__pycache__
10 changes: 4 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import setuptools
from distutils.core import setup

with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

setuptools.setup(
name='vectors2d',
packages=setuptools.find_packages(),
version='1.0.1',
version='1.0.2',
license='MIT',
description='Small but useful module that allows to work with vectors in the 2-dimensional space.',
description='A small but useful module that allows to work with vectors in the 2-dimensional space.',
long_description=long_description,
long_description_content_type="text/markdown",
author='Dmitry Popov',
author='Dmytro Popov',
author_email='thedmitryp@ukr.net',
url='https://github.com/MitryP/vectors',
download_url='https://github.com/MitryP/vectors/archive/1.0.1.tar.gz',
download_url='https://github.com/MitryP/vectors/archive/1.0.2.tar.gz',
keywords=['vectors', '2-dimensional', 'flat', 'coordinates', 'open-source'],
install_requires=[],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Topic :: Software Development',
'Topic :: Software Development :: Build Tools',
'Topic :: Software Development :: Libraries',
'Topic :: Software Development :: Libraries :: Python Modules',
'License :: OSI Approved :: MIT License',
Expand Down
79 changes: 32 additions & 47 deletions vectors2d/vectors2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def __init__(self, *args: Union[int, float, tuple, list, 'Vector2D']):
Takes tuple or list of int/float with length == 2
:param args: list or tuple with len == 2 of int/float default = [0,0]
"""

if len(args) == 0:
Expand All @@ -53,32 +52,31 @@ def __init__(self, *args: Union[int, float, tuple, list, 'Vector2D']):
else:
raise TypeError

def __getitem__(self, key: int):
def __getitem__(self, key: int) -> Union[float, int]:
if key > len(self.vector) - 1 or key < -1:
raise IndexError

else:
return self.vector[key]

def __setitem__(self, key: int, value: Union[int, float]):
def __setitem__(self, key: int, value: Union[int, float]) -> None:
if key > len(self.vector)-1 or key < -1:
raise IndexError

else:
self.vector[key] = value

def __abs__(self):
def __abs__(self) -> float:
"""Absolute value of vector
Returns absolute value of vector
:return: float
"""

return absolute_vector(self)

def __eq__(self, other):
def __eq__(self, other) -> bool:
if type(other) in (type(self), tuple, list):
return self.vector == Vector2D(other).vector

Expand All @@ -88,26 +86,26 @@ def __eq__(self, other):
else:
return self == other

def __add__(self, other: Union[list, tuple, 'Vector2D']):
def __add__(self, other: Union[list, tuple, 'Vector2D']) -> 'Vector2D':
if type(other) in (type(self), tuple, list):
return sum_vectors(self, other)

else:
raise TypeError
raise TypeError(f"Unsupported type '{other.__class__.__name__}' for addition operation.")

def __sub__(self, other: Union[list, tuple, 'Vector2D']):
def __sub__(self, other: Union[list, tuple, 'Vector2D']) -> 'Vector2D':
if type(other) in (type(self), tuple, list):
return sub_vectors(self, other)

else:
raise TypeError
raise TypeError(f"Unsupported type '{other.__class__.__name__}' for subtraction operation.")

def __mul__(self, other: Union[int, float, list, tuple, 'Vector2D']):
def __mul__(self, other: Union[int, float, list, tuple, 'Vector2D']) -> Union['Vector2D', float]:
"""
Returns scalar multiplication of vectors or multiplies the vector by given number
:param other: If Vector2D: returns scalar multiplication(int); if number: returns * (Vector)
:param other: If Vector2D: returns scalar multiplication(float); if number: returns * (Vector)
:returns: If scalar_mult_vectors multiplication, returns float; If vector multiplication, returns Vector2D
"""

if type(other) in (int, float):
Expand All @@ -117,25 +115,23 @@ def __mul__(self, other: Union[int, float, list, tuple, 'Vector2D']):
return scalar_mult_vectors(self, other)

else:
raise TypeError
raise TypeError(f"Unsupported type '{other.__class__.__name__}' for multiplication operation.")

def __imul__(self, other: Union[int, float]):
def __imul__(self, other: Union[int, float]) -> 'Vector2D':
return mult_vector(self.vector, other)

def __call__(self):
def __call__(self) -> tuple:
"""
:return: tuple(x, y)
"""

return tuple(self.vector)

def __neg__(self):
def __neg__(self) -> 'Vector2D':
"""
Returns vector with negative coordinates of self.
:return: Vector2D
"""

result_vector = self.vector.copy()
Expand All @@ -144,23 +140,21 @@ def __neg__(self):

return Vector2D(result_vector)

def is_zero(self):
def is_zero(self) -> bool:
"""
Check if the vector is zero vector.
:return: bool
"""

return self.vector[0] == 0 and self.vector[1] == 0

def is_perpendicular_to(self, vector: Union[tuple, list, 'Vector2D']):
def is_perpendicular_to(self, vector: Union[tuple, list, 'Vector2D']) -> bool:
"""
Check if vector is perpendicular to the given vector.
:param vector: Vector2D or list or tuple
:return: bool
"""

if type(vector) is not type(self):
Expand All @@ -175,13 +169,12 @@ def is_perpendicular_to(self, vector: Union[tuple, list, 'Vector2D']):
else:
return False

def is_parallel_to(self, vector: Union[tuple, list, 'Vector2D']):
def is_parallel_to(self, vector: Union[tuple, list, 'Vector2D']) -> bool:
"""
Check if vector is co-directional to the given vector.
:param vector: Vector2D or list or tuple
:return: bool
"""

if type(vector) is not type(self):
Expand All @@ -196,13 +189,12 @@ def is_parallel_to(self, vector: Union[tuple, list, 'Vector2D']):
else:
return False

def is_codirectional_to(self, vector: Union[tuple, list, 'Vector2D']):
def is_codirectional_to(self, vector: Union[tuple, list, 'Vector2D']) -> bool:
"""
Check if vector is co-directed to given vector.
:param vector: Vector2D or list or tuple
:return: bool
"""

if type(vector) is not type(self):
Expand All @@ -218,16 +210,15 @@ def is_codirectional_to(self, vector: Union[tuple, list, 'Vector2D']):
return False

def __repr__(self) -> str:
pass
return f"<Vector2D {self.vector}>"


def absolute_vector(vector: Union[Vector2D, list, tuple]):
def absolute_vector(vector: Union[Vector2D, list, tuple]) -> float:
"""
Calculates absolute value of given vector.
Calculates an absolute value of given vector.
:param vector: Vector2D or list or tuple
:return: float
"""

if type(vector) is not Vector2D:
Expand All @@ -236,13 +227,12 @@ def absolute_vector(vector: Union[Vector2D, list, tuple]):
return sqrt(vector[0] ** 2 + vector[1] ** 2)


def sum_vectors(*vectors: Union[Vector2D, list, tuple]):
def sum_vectors(*vectors: Union[Vector2D, list, tuple]) -> Vector2D:
"""
Adds given vectors.
:param vectors: Vector2D or list or tuple
:return: Vector2D
"""

result = [0, 0]
Expand All @@ -256,13 +246,12 @@ def sum_vectors(*vectors: Union[Vector2D, list, tuple]):
return Vector2D(result)


def sub_vectors(*vectors: Union[Vector2D, list, tuple]):
def sub_vectors(*vectors: Union[Vector2D, list, tuple]) -> Vector2D:
"""
Subtracts given vectors.
:param vectors: Vector2D or list or tuple
:return: Vector2D
"""

result = None
Expand All @@ -280,14 +269,13 @@ def sub_vectors(*vectors: Union[Vector2D, list, tuple]):
return Vector2D(result)


def mult_vector(vector: Union[Vector2D, tuple, list], modifier: Union[int, float]):
def mult_vector(vector: Union[Vector2D, tuple, list], modifier: Union[int, float]) -> Vector2D:
"""
Multiplies given vector by given number.
Multiplies the given vector by the given number.
:param vector: Vector2D or tuple or list
:param modifier: must be number
:return: Vector2D
"""

if type(vector) is not Vector2D:
Expand All @@ -296,13 +284,12 @@ def mult_vector(vector: Union[Vector2D, tuple, list], modifier: Union[int, float
return Vector2D((vector[0] * modifier, vector[1] * modifier))


def scalar_mult_vectors(*vectors: Union[Vector2D, list, tuple]):
def scalar_mult_vectors(*vectors: Union[Vector2D, list, tuple]) -> float:
"""
Calculates scalar multiplication of given vectors.
Calculates a scalar multiplication of the given vectors.
:param vectors: Vector2D or list or tuple
:return: float
"""

temp = [1, 1]
Expand All @@ -320,14 +307,13 @@ def scalar_mult_vectors(*vectors: Union[Vector2D, list, tuple]):
return result


def get_angle(vector1: Union[Vector2D, list, tuple], vector2: Union[Vector2D, list, tuple]):
def get_angle(vector1: Union[Vector2D, list, tuple], vector2: Union[Vector2D, list, tuple]) -> float:
"""
Returns angle between two given vectors in radians.
Returns an angle between the two given vectors in radians.
:param vector1: Vector2D or list or tuple
:param vector2: Vector2D or list or tuple
:return: float
"""

if type(vector1) is not Vector2D:
Expand All @@ -347,14 +333,13 @@ def get_angle(vector1: Union[Vector2D, list, tuple], vector2: Union[Vector2D, li
return round(acos(result), 5)


def vector_from_dots(dot1: Union[tuple, list], dot2: Union[tuple, list]):
def vector_from_dots(dot1: Union[tuple, list], dot2: Union[tuple, list]) -> Vector2D:
"""
Creates vector from two given dots
Creates a vector from the two given dots
:param dot1: tuple or list
:param dot2: tuple or list
:return: Vector2D
"""

if len(dot1) != 2 or len(dot2) != 2:
Expand Down

0 comments on commit 8d09bba

Please sign in to comment.