Skip to content

Commit 99fd8d8

Browse files
committed
up
1 parent 36cbc29 commit 99fd8d8

12 files changed

+125
-249
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ html/
1111
.benchmarks/
1212
reports/
1313
lectures/
14+
.mypy_cache/
1415

1516
################################
1617
########### PYTHON #############

README.md

+5-21
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
![Python](https://img.shields.io/badge/python-3.7+-blue)
44
![License](https://camo.githubusercontent.com/890acbdcb87868b382af9a4b1fac507b9659d9bf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)
5-
[![Build](https://github.com/franneck94/Python-Project-Template/workflows/ci-test/badge.svg)](https://github.com/franneck94/Python-Project-Template/actions?query=workflow%3Aci-test)
6-
[![codecov](https://codecov.io/gh/franneck94/python-project-template/branch/master/graph/badge.svg)](https://codecov.io/gh/franneck94/python-project-template)
7-
[![Documentation](https://img.shields.io/badge/ref-Documentation-blue)](https://franneck94.github.io/Python-Project-Template/)
5+
[![Build](https://github.com/franneck94/Python-Project-Template-Eng/workflows/ci-test/badge.svg)](https://github.com/franneck94/Python-Project-Template-Eng/actions?query=workflow%3Aci-test)
6+
[![codecov](https://codecov.io/gh/franneck94/Python-Project-Template-Eng/branch/master/graph/badge.svg)](https://codecov.io/gh/franneck94/Python-Project-Template-Eng)
7+
[![Documentation](https://img.shields.io/badge/ref-Documentation-blue)](https://franneck94.github.io/Python-Project-Template-Eng/)
88

99
## Template For Python Projects
1010

@@ -13,11 +13,9 @@ This is a template for Python projects. What you get:
1313
- Source code and test code is seperated in different directories.
1414
- External libraries installed and managed by [Pip](https://pypi.org/project/pip/).
1515
- Setup for tests using [Pytest](https://docs.pytest.org/en/stable/).
16-
- Bechmark tests using [Pytest-Benchmark](https://github.com/ionelmc/pytest-benchmark)
1716
- Continuous testing with [Github-Actions](https://github.com/features/actions/).
1817
- Code coverage reports, including automatic upload to [Codecov](https://codecov.io).
1918
- Code documentation with [Mkdocs](https://www.mkdocs.org/).
20-
- Example of own Python package with the use of [Cython](https://cython.org/)
2119
- Optional: Use of [VSCode](https://code.visualstudio.com/) with the Python and UnitTest extension.
2220

2321
## Structure
@@ -29,25 +27,16 @@ This is a template for Python projects. What you get:
2927
├── ... other config files ...
3028
├── tests
3129
│ ├── __init__.py
32-
│ ├── test_vector.py
33-
│ ├── test_computations.py
34-
│ └── conftest.py
30+
│ └── test_vector.py
3531
├── docs
3632
│ ├── api.md
3733
│ └── index.md
3834
├── fastvector
3935
│ ├── __init__.py
4036
│ ├── vector.py
41-
│ ├── dtypes.py
42-
│ ├── version.py
43-
│ └── cython_computations.pyx
44-
│ └── computations.py
45-
└── benchmarks
46-
│ ├── __init__.py
47-
│ └── test_computations.py
37+
│ └── version.py
4838
└── tests
4939
├── __init__.py
50-
├── test_computations.py
5140
└── test_vector.py
5241
```
5342

@@ -67,8 +56,3 @@ pytest tests
6756
# Code Coverage
6857
pytest --cov=fastvector tests --cov-report=html
6958
```
70-
71-
```bash
72-
# Benchmarks
73-
pytest --benchmark-columns=min,max,mean,stddev --benchmark-sort=mean benchmarks
74-
```

codecov.yml

-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ coverage:
55

66
ignore:
77
- "tests/*"
8-
- "benchmarks/*"
98
- "docs/*"

fastvector/__init__.py

+4-28
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,8 @@
1-
from .computations import cython_clip_vector
2-
from .computations import naive_cython_clip_vector
3-
from .computations import python_clip_vector
4-
from .dtypes import float32
5-
from .dtypes import float64
6-
from .dtypes import int8
7-
from .dtypes import int16
8-
from .dtypes import int32
9-
from .dtypes import int64
10-
from .dtypes import uint8
11-
from .dtypes import uint16
12-
from .dtypes import uint32
13-
from .dtypes import uint64
14-
from .vector import VectorND
1+
from .vector import Vector2D
2+
from .version import __version__
153

164

175
__all__ = [
18-
'cython_clip_vector',
19-
'naive_cython_clip_vector',
20-
'python_clip_vector',
21-
'float32',
22-
'float64',
23-
'uint8',
24-
'int8',
25-
'uint16',
26-
'int16',
27-
'uint32',
28-
'int32',
29-
'uint64',
30-
'int64',
31-
'VectorND'
6+
'Vector2D'
7+
'__version__'
328
]

fastvector/vector.py

+41-74
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
from __future__ import annotations
22

3-
import array
43
import numbers
54
from functools import total_ordering
65
from math import sqrt
7-
from typing import Any
6+
from typing import SupportsFloat
87
from typing import Union
98

10-
from .dtypes import Dtype
11-
from .dtypes import Number
12-
from .dtypes import float64
13-
149

1510
@total_ordering
16-
class VectorND:
17-
def __init__(self, *args: Any, dtype: Dtype = float64) -> None:
18-
"""Create a vector with the given values.
11+
class Vector2D:
12+
def __init__(self, x: SupportsFloat = 0.0, y: SupportsFloat = 0.0) -> None:
13+
"""Create a vector with the given x and y values.
1914
2015
Args:
21-
args (Any): The vector values.
22-
dtype (Dtype): The data type of the array.array.
16+
x: x-Value.
17+
y: y-Value.
2318
2419
Raises:
2520
TypeError: If x or y are not a number.
2621
"""
27-
if len(args) == 1 and isinstance(args[0], list):
28-
self.values = array.array(dtype, args[0])
29-
elif len(args) > 1:
30-
inputs = [val for val in args]
31-
self.values = array.array(dtype, inputs)
22+
if isinstance(x, numbers.Real) and isinstance(y, numbers.Real):
23+
self.x = x
24+
self.y = y
3225
else:
3326
raise TypeError('You must pass in int/float value for x and y!')
3427

@@ -38,23 +31,23 @@ def __repr__(self) -> str:
3831
Returns:
3932
The representation of the vector.
4033
"""
41-
return f'vector.VectorND({self.values})'
34+
return f'vector.Vector2D({self.x}, {self.y})'
4235

4336
def __str__(self) -> str:
4437
"""The vector as a string.
4538
4639
Returns:
4740
The vector as a string.
4841
"""
49-
return f'({self.values})'
42+
return f'({self.x}, {self.y})'
5043

5144
def __abs__(self) -> float:
5245
"""Return the length (magnitude) of the vector.
5346
5447
Returns:
5548
Length of the vector.
5649
"""
57-
return sqrt(sum(pow(val, 2) for val in self.values))
50+
return sqrt(pow(self.x, 2) + pow(self.y, 2))
5851

5952
def __eq__(self, other_vector: object) -> bool:
6053
"""Check if the vectors have the same values.
@@ -66,11 +59,11 @@ def __eq__(self, other_vector: object) -> bool:
6659
True, if the both vectors have the same values.
6760
False, else.
6861
"""
69-
if not isinstance(other_vector, VectorND):
62+
if not isinstance(other_vector, Vector2D):
7063
return False
71-
return self.values == other_vector.values
64+
return self.x == other_vector.x and self.y == other_vector.y
7265

73-
def __lt__(self, other_vector: VectorND) -> bool:
66+
def __lt__(self, other_vector: Vector2D) -> bool:
7467
"""Check if the self is less than the other vector.
7568
7669
Args:
@@ -80,25 +73,26 @@ def __lt__(self, other_vector: VectorND) -> bool:
8073
True, if the self is less than the other vector.
8174
False, else.
8275
"""
83-
if not isinstance(other_vector, VectorND):
84-
raise TypeError('You must pass in a VectorND instance!')
76+
if not isinstance(other_vector, Vector2D):
77+
raise TypeError('You must pass in a Vector2D instance!')
8578
return abs(self) < abs(other_vector)
8679

87-
def __add__(self, other_vector: VectorND) -> VectorND:
88-
"""Returns the additon vector of the self and the other vector.
80+
def __add__(self, other_vector: Vector2D) -> Vector2D:
81+
"""Returns the addition vector of the self and the other vector.
8982
9083
Args:
9184
other_vector: Other vector (rhs).
9285
9386
Returns:
94-
The additon vector of the self and the other vector.
87+
The addition vector of the self and the other vector.
9588
"""
96-
if not isinstance(other_vector, VectorND):
97-
raise TypeError('You must pass in a VectorND instance!')
98-
result = [v1 + v2 for v1, v2 in zip(self.values, other_vector.values)]
99-
return VectorND(result)
89+
if not isinstance(other_vector, Vector2D):
90+
raise TypeError('You must pass in a Vector2D instance!')
91+
x = self.x + other_vector.x
92+
y = self.y + other_vector.y
93+
return Vector2D(x, y)
10094

101-
def __sub__(self, other_vector: VectorND) -> VectorND:
95+
def __sub__(self, other_vector: Vector2D) -> Vector2D:
10296
"""Return the subtraction vector of the self and the other vector.
10397
10498
Args:
@@ -107,14 +101,15 @@ def __sub__(self, other_vector: VectorND) -> VectorND:
107101
Returns:
108102
The subtraction vector of the self and the other vector.
109103
"""
110-
if not isinstance(other_vector, VectorND):
111-
raise TypeError('You must pass in a VectorND instance!')
112-
result = [v1 - v2 for v1, v2 in zip(self.values, other_vector.values)]
113-
return VectorND(result)
104+
if not isinstance(other_vector, Vector2D):
105+
raise TypeError('You must pass in a Vector2D instance!')
106+
x = self.x - other_vector.x
107+
y = self.y - other_vector.y
108+
return Vector2D(x, y)
114109

115110
def __mul__(
116-
self, other: Union[VectorND, Number]
117-
) -> Union[VectorND, Number]:
111+
self, other: Union[Vector2D, SupportsFloat]
112+
) -> Union[Vector2D, SupportsFloat]:
118113
"""Return the multiplication of self and the other vector/number.
119114
120115
Args:
@@ -126,13 +121,14 @@ def __mul__(
126121
Returns:
127122
The multiplication of self and the other vector/number.
128123
"""
129-
if isinstance(other, VectorND):
130-
return sum([v1 * v2 for v1, v2 in zip(self.values, other.values)])
131-
if not isinstance(other, int) and not isinstance(other, float):
124+
if isinstance(other, Vector2D):
125+
result: SupportsFloat = self.x * other.x + self.y * other.y
126+
return result
127+
if not isinstance(other, numbers.Real):
132128
raise TypeError('You must pass in an int/float!')
133-
return VectorND([v * other for v in self.values])
129+
return Vector2D(self.x * other, self.y * other)
134130

135-
def __truediv__(self, other: Number) -> VectorND:
131+
def __truediv__(self, other: SupportsFloat) -> Vector2D:
136132
"""Return the multiplication of self and the other vector/number.
137133
138134
Args:
@@ -145,35 +141,6 @@ def __truediv__(self, other: Number) -> VectorND:
145141
Returns:
146142
The multiplication of self and the other vector/number.
147143
"""
148-
if not isinstance(other, int) and not isinstance(other, float):
144+
if not isinstance(other, numbers.Real):
149145
raise TypeError('You must pass in an int/float!')
150-
return VectorND([v / other for v in self.values])
151-
152-
def __len__(self) -> int:
153-
"""Returns the length of the vector.
154-
155-
Returns:
156-
int: The length.
157-
"""
158-
return len(self.values)
159-
160-
def __getitem__(self, idx: int) -> Number:
161-
"""Returns the i-th component of the vector.
162-
163-
Args:
164-
idx (int): i-th component index
165-
166-
Returns:
167-
Number: The value at the i-th component
168-
"""
169-
result: Number = self.values[idx]
170-
return result
171-
172-
def __setitem__(self, idx: int, val: Number) -> None:
173-
"""Updates the i-th component of the vector.
174-
175-
Args:
176-
idx (int): i-th component index
177-
val (Number): The updated valued
178-
"""
179-
self.values[idx] = val
146+
return Vector2D(self.x / other, self.y / other)

fastvector/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '2.0.2'
1+
__version__ = '3.0.0'

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[tool.black]
2-
line-length = 120
2+
line-length = 80
33
skip-string-normalization = false
44
skip-magic-trailing-comma = false
55

66
[build-system]
7-
requires = ["setuptools>=45.0", "setuptools_scm[toml]>=6.3.1", "wheel", "Cython"]
7+
requires = ["setuptools>=45.0", "setuptools_scm[toml]>=6.3.1", "wheel"]
88
build-backend = "setuptools.build_meta"
99

1010
[tool.pytest.ini_options]

requirements-dev.txt

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ pytest
66
codecov
77
pytest-cov
88
pre-commit
9-
pytest-benchmark
109

1110
# Linting/Tooling
1211
pylint

requirements.txt

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
# Runtime requirements
22
numpy
3-
scipy
4-
Cython

setup.cfg

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
12
[metadata]
23
name = fastvector
3-
version = 2.0.2
4+
version = 3.0.0
45
description = This is a simple vector python package.
56
long_description = file: README.md
67
long_description_content_type = text/markdown
@@ -21,8 +22,6 @@ classifiers =
2122
packages = fastvector
2223
install_requires =
2324
numpy
24-
scipy
25-
Cython
2625
python_requires = >=3.7
2726
package_dir =
2827
=.
@@ -34,7 +33,6 @@ testing =
3433
codecov
3534
pytest-cov
3635
pre-commit
37-
pytest-benchmark
3836
tox
3937
pylint
4038
flake8
@@ -66,7 +64,6 @@ omit =
6664
setup.py
6765
fastvector/__init__.py
6866
fastvector/version.py
69-
fastvector/dtypes.py
7067

7168
[isort]
7269
sections =
@@ -95,7 +92,7 @@ line_length = 80
9592
[flake8]
9693
exclude = .git,__pycache__,docs,old,build,dist
9794
max-complexity = 30
98-
max-line-length = 120
95+
max-line-length = 80
9996
ignore=W504,F401,E402,E266,E203,W503,C408,C416,B001
10097

10198

@@ -135,5 +132,5 @@ confidence=HIGH
135132

136133
[FORMAT]
137134

138-
max-line-length = 120
135+
max-line-length = 80
139136
max-module-lines = 2000

0 commit comments

Comments
 (0)