Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
franneck94 committed Mar 11, 2023
1 parent 36cbc29 commit 99fd8d8
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 249 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ html/
.benchmarks/
reports/
lectures/
.mypy_cache/

################################
########### PYTHON #############
Expand Down
26 changes: 5 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

![Python](https://img.shields.io/badge/python-3.7+-blue)
![License](https://camo.githubusercontent.com/890acbdcb87868b382af9a4b1fac507b9659d9bf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)
[![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)
[![codecov](https://codecov.io/gh/franneck94/python-project-template/branch/master/graph/badge.svg)](https://codecov.io/gh/franneck94/python-project-template)
[![Documentation](https://img.shields.io/badge/ref-Documentation-blue)](https://franneck94.github.io/Python-Project-Template/)
[![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)
[![codecov](https://codecov.io/gh/franneck94/Python-Project-Template-Eng/branch/master/graph/badge.svg)](https://codecov.io/gh/franneck94/Python-Project-Template-Eng)
[![Documentation](https://img.shields.io/badge/ref-Documentation-blue)](https://franneck94.github.io/Python-Project-Template-Eng/)

## Template For Python Projects

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

## Structure
Expand All @@ -29,25 +27,16 @@ This is a template for Python projects. What you get:
├── ... other config files ...
├── tests
│ ├── __init__.py
│ ├── test_vector.py
│ ├── test_computations.py
│ └── conftest.py
│ └── test_vector.py
├── docs
│ ├── api.md
│ └── index.md
├── fastvector
│ ├── __init__.py
│ ├── vector.py
│ ├── dtypes.py
│ ├── version.py
│ └── cython_computations.pyx
│ └── computations.py
└── benchmarks
│ ├── __init__.py
│ └── test_computations.py
│ └── version.py
└── tests
├── __init__.py
├── test_computations.py
└── test_vector.py
```

Expand All @@ -67,8 +56,3 @@ pytest tests
# Code Coverage
pytest --cov=fastvector tests --cov-report=html
```

```bash
# Benchmarks
pytest --benchmark-columns=min,max,mean,stddev --benchmark-sort=mean benchmarks
```
1 change: 0 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ coverage:

ignore:
- "tests/*"
- "benchmarks/*"
- "docs/*"
32 changes: 4 additions & 28 deletions fastvector/__init__.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
from .computations import cython_clip_vector
from .computations import naive_cython_clip_vector
from .computations import python_clip_vector
from .dtypes import float32
from .dtypes import float64
from .dtypes import int8
from .dtypes import int16
from .dtypes import int32
from .dtypes import int64
from .dtypes import uint8
from .dtypes import uint16
from .dtypes import uint32
from .dtypes import uint64
from .vector import VectorND
from .vector import Vector2D
from .version import __version__


__all__ = [
'cython_clip_vector',
'naive_cython_clip_vector',
'python_clip_vector',
'float32',
'float64',
'uint8',
'int8',
'uint16',
'int16',
'uint32',
'int32',
'uint64',
'int64',
'VectorND'
'Vector2D'
'__version__'
]
115 changes: 41 additions & 74 deletions fastvector/vector.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
from __future__ import annotations

import array
import numbers
from functools import total_ordering
from math import sqrt
from typing import Any
from typing import SupportsFloat
from typing import Union

from .dtypes import Dtype
from .dtypes import Number
from .dtypes import float64


@total_ordering
class VectorND:
def __init__(self, *args: Any, dtype: Dtype = float64) -> None:
"""Create a vector with the given values.
class Vector2D:
def __init__(self, x: SupportsFloat = 0.0, y: SupportsFloat = 0.0) -> None:
"""Create a vector with the given x and y values.
Args:
args (Any): The vector values.
dtype (Dtype): The data type of the array.array.
x: x-Value.
y: y-Value.
Raises:
TypeError: If x or y are not a number.
"""
if len(args) == 1 and isinstance(args[0], list):
self.values = array.array(dtype, args[0])
elif len(args) > 1:
inputs = [val for val in args]
self.values = array.array(dtype, inputs)
if isinstance(x, numbers.Real) and isinstance(y, numbers.Real):
self.x = x
self.y = y
else:
raise TypeError('You must pass in int/float value for x and y!')

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

def __str__(self) -> str:
"""The vector as a string.
Returns:
The vector as a string.
"""
return f'({self.values})'
return f'({self.x}, {self.y})'

def __abs__(self) -> float:
"""Return the length (magnitude) of the vector.
Returns:
Length of the vector.
"""
return sqrt(sum(pow(val, 2) for val in self.values))
return sqrt(pow(self.x, 2) + pow(self.y, 2))

def __eq__(self, other_vector: object) -> bool:
"""Check if the vectors have the same values.
Expand All @@ -66,11 +59,11 @@ def __eq__(self, other_vector: object) -> bool:
True, if the both vectors have the same values.
False, else.
"""
if not isinstance(other_vector, VectorND):
if not isinstance(other_vector, Vector2D):
return False
return self.values == other_vector.values
return self.x == other_vector.x and self.y == other_vector.y

def __lt__(self, other_vector: VectorND) -> bool:
def __lt__(self, other_vector: Vector2D) -> bool:
"""Check if the self is less than the other vector.
Args:
Expand All @@ -80,25 +73,26 @@ def __lt__(self, other_vector: VectorND) -> bool:
True, if the self is less than the other vector.
False, else.
"""
if not isinstance(other_vector, VectorND):
raise TypeError('You must pass in a VectorND instance!')
if not isinstance(other_vector, Vector2D):
raise TypeError('You must pass in a Vector2D instance!')
return abs(self) < abs(other_vector)

def __add__(self, other_vector: VectorND) -> VectorND:
"""Returns the additon vector of the self and the other vector.
def __add__(self, other_vector: Vector2D) -> Vector2D:
"""Returns the addition vector of the self and the other vector.
Args:
other_vector: Other vector (rhs).
Returns:
The additon vector of the self and the other vector.
The addition vector of the self and the other vector.
"""
if not isinstance(other_vector, VectorND):
raise TypeError('You must pass in a VectorND instance!')
result = [v1 + v2 for v1, v2 in zip(self.values, other_vector.values)]
return VectorND(result)
if not isinstance(other_vector, Vector2D):
raise TypeError('You must pass in a Vector2D instance!')
x = self.x + other_vector.x
y = self.y + other_vector.y
return Vector2D(x, y)

def __sub__(self, other_vector: VectorND) -> VectorND:
def __sub__(self, other_vector: Vector2D) -> Vector2D:
"""Return the subtraction vector of the self and the other vector.
Args:
Expand All @@ -107,14 +101,15 @@ def __sub__(self, other_vector: VectorND) -> VectorND:
Returns:
The subtraction vector of the self and the other vector.
"""
if not isinstance(other_vector, VectorND):
raise TypeError('You must pass in a VectorND instance!')
result = [v1 - v2 for v1, v2 in zip(self.values, other_vector.values)]
return VectorND(result)
if not isinstance(other_vector, Vector2D):
raise TypeError('You must pass in a Vector2D instance!')
x = self.x - other_vector.x
y = self.y - other_vector.y
return Vector2D(x, y)

def __mul__(
self, other: Union[VectorND, Number]
) -> Union[VectorND, Number]:
self, other: Union[Vector2D, SupportsFloat]
) -> Union[Vector2D, SupportsFloat]:
"""Return the multiplication of self and the other vector/number.
Args:
Expand All @@ -126,13 +121,14 @@ def __mul__(
Returns:
The multiplication of self and the other vector/number.
"""
if isinstance(other, VectorND):
return sum([v1 * v2 for v1, v2 in zip(self.values, other.values)])
if not isinstance(other, int) and not isinstance(other, float):
if isinstance(other, Vector2D):
result: SupportsFloat = self.x * other.x + self.y * other.y
return result
if not isinstance(other, numbers.Real):
raise TypeError('You must pass in an int/float!')
return VectorND([v * other for v in self.values])
return Vector2D(self.x * other, self.y * other)

def __truediv__(self, other: Number) -> VectorND:
def __truediv__(self, other: SupportsFloat) -> Vector2D:
"""Return the multiplication of self and the other vector/number.
Args:
Expand All @@ -145,35 +141,6 @@ def __truediv__(self, other: Number) -> VectorND:
Returns:
The multiplication of self and the other vector/number.
"""
if not isinstance(other, int) and not isinstance(other, float):
if not isinstance(other, numbers.Real):
raise TypeError('You must pass in an int/float!')
return VectorND([v / other for v in self.values])

def __len__(self) -> int:
"""Returns the length of the vector.
Returns:
int: The length.
"""
return len(self.values)

def __getitem__(self, idx: int) -> Number:
"""Returns the i-th component of the vector.
Args:
idx (int): i-th component index
Returns:
Number: The value at the i-th component
"""
result: Number = self.values[idx]
return result

def __setitem__(self, idx: int, val: Number) -> None:
"""Updates the i-th component of the vector.
Args:
idx (int): i-th component index
val (Number): The updated valued
"""
self.values[idx] = val
return Vector2D(self.x / other, self.y / other)
2 changes: 1 addition & 1 deletion fastvector/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.2'
__version__ = '3.0.0'
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[tool.black]
line-length = 120
line-length = 80
skip-string-normalization = false
skip-magic-trailing-comma = false

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

[tool.pytest.ini_options]
Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pytest
codecov
pytest-cov
pre-commit
pytest-benchmark

# Linting/Tooling
pylint
Expand Down
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# Runtime requirements
numpy
scipy
Cython
11 changes: 4 additions & 7 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

[metadata]
name = fastvector
version = 2.0.2
version = 3.0.0
description = This is a simple vector python package.
long_description = file: README.md
long_description_content_type = text/markdown
Expand All @@ -21,8 +22,6 @@ classifiers =
packages = fastvector
install_requires =
numpy
scipy
Cython
python_requires = >=3.7
package_dir =
=.
Expand All @@ -34,7 +33,6 @@ testing =
codecov
pytest-cov
pre-commit
pytest-benchmark
tox
pylint
flake8
Expand Down Expand Up @@ -66,7 +64,6 @@ omit =
setup.py
fastvector/__init__.py
fastvector/version.py
fastvector/dtypes.py

[isort]
sections =
Expand Down Expand Up @@ -95,7 +92,7 @@ line_length = 80
[flake8]
exclude = .git,__pycache__,docs,old,build,dist
max-complexity = 30
max-line-length = 120
max-line-length = 80
ignore=W504,F401,E402,E266,E203,W503,C408,C416,B001


Expand Down Expand Up @@ -135,5 +132,5 @@ confidence=HIGH

[FORMAT]

max-line-length = 120
max-line-length = 80
max-module-lines = 2000
Loading

0 comments on commit 99fd8d8

Please sign in to comment.