From f444a5357735497206a4dd44d7e5acd78df85576 Mon Sep 17 00:00:00 2001 From: slowy07 Date: Wed, 24 Jan 2024 12:10:11 +0700 Subject: [PATCH 1/2] chore(dev): menambahkan utilites development lib menambahkan beberapa konfigurasi dari pre-commit untuk menambah kualitas kode dari project Signed-off-by: slowy07 --- .github/workflows/pythontesting.yml | 4 --- .gitignore | 5 ++++ .pre-commit-config.yaml | 44 +++++++++++++++++++++++++++++ OpenSeries/matematika.py | 35 +++++++++++++++++------ OpenSeries/statistika.py | 2 +- dev-requirements.txt | 2 -- 6 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/pythontesting.yml b/.github/workflows/pythontesting.yml index b281c67..687ef11 100644 --- a/.github/workflows/pythontesting.yml +++ b/.github/workflows/pythontesting.yml @@ -27,10 +27,6 @@ jobs: python -m pip install -r requirements.txt python -m pip install -r dev-requirements.txt - - name: cek kode dengan flake8 - run: | - flake8 . --max-line-length=100 --exclude=example - - name: testing kode dengan pytest run: | pytest testing/main_test.py --verbose diff --git a/.gitignore b/.gitignore index 6a4cd49..c51f3be 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,8 @@ node_modules next-* next-env.d.ts +# hasil dari format ruff +.ruff_cache + +# hasil cache dari mypy +.mypy_cache diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..f308e26 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,44 @@ +# konfigurasi pre commit +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-executables-have-shebangs + - id: check-toml + - id: check-yaml + - id: end-of-file-fixer + types: [python] + - id: trailing-whitespace + - id: requirements-txt-fixer + + # auto walrus python + # operator yang memungkinkan menggabungkan penugasan variabel dan evaluasi ekspresi dalam satu baris + # contoh + # total = sum(angka := num for num in nums ) + - repo: https://github.com/MarcoGorelli/auto-walrus + rev: v0.2.2 + hooks: + - id: auto-walrus + + # ruff adalah salah satu linter dan formatter kode + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.1.14 + hooks: + - id: ruff + - id: ruff-format + + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.8.0 + hooks: + - id: mypy + args: + - --ignore-missing-imports + - --non-interactive + - --install-types + additional_dependencies: [types-requests] + + - repo: https://github.com/pre-commit/mirrors-prettier + rev: "v4.0.0-alpha.8" + hooks: + - id: prettier + types_or: [toml, yaml] diff --git a/OpenSeries/matematika.py b/OpenSeries/matematika.py index c0c8f54..53b2209 100644 --- a/OpenSeries/matematika.py +++ b/OpenSeries/matematika.py @@ -1,6 +1,6 @@ from OpenSeries.util import constant as constant from OpenSeries.util import error as pesan_error -from typing import Union +from typing import Union, Sequence import math @@ -132,7 +132,7 @@ def persamaan_kuadrat( return pesan_error.error_tipe_data(["float", "int"]) -def rata_rata(nilai: list[Union[int, float]]) -> Union[int, float, str]: +def rata_rata(nilai: Sequence[Union[int, float]]) -> Union[int, float, str]: """ menghitung nilai rata-rata @@ -157,7 +157,7 @@ def rata_rata(nilai: list[Union[int, float]]) -> Union[int, float, str]: return pesan_error.error_tipe_data(["float", "int"]) -def faktorial(nilai: int) -> Union[int, str]: +def faktorial(nilai: int) -> Union[int, float, str]: """ menghitung produk dari semua bilangan bulat positif contoh `4! = 24 = 4 x 3 x 2 x 1` @@ -173,7 +173,7 @@ def faktorial(nilai: int) -> Union[int, str]: elif nilai < 0: return pesan_error.error_format("Tidak bisa menggunakan angka negatif") else: - return nilai * faktorial(nilai - 1) + return int(nilai * faktorial(nilai - 1)) else: return pesan_error.error_tipe_data(["int"]) @@ -191,7 +191,13 @@ def permutasi(nilai: int, r: int) -> Union[int, float, str]: if not isinstance(nilai, int) or not isinstance(r, int): return pesan_error.error_tipe_data(["int"]) else: - return faktorial(nilai) / faktorial(nilai - r) + faktorial_nilai = faktorial(nilai) + faktorial_nilai_r = faktorial(nilai - r) + + if isinstance(faktorial_nilai, str) or isinstance(faktorial_nilai_r, str): + return pesan_error.error_tipe_data(["int"]) + else: + return faktorial_nilai_r / faktorial_nilai_r def kombinasi(nilai: int, r: int) -> Union[int, float, str]: @@ -204,8 +210,19 @@ def kombinasi(nilai: int, r: int) -> Union[int, float, str]: """ # mengecek apakah variable tersebut bertipe data int atau float # jika tidak maka error - if isinstance(nilai, (int)): - return faktorial(nilai) / (faktorial(r) * faktorial(nilai - r)) + if isinstance(nilai, (int)) and isinstance(r, int): + faktorial_nilai = faktorial(nilai) + faktorial_r = faktorial(r) + faktorial_nilai_r = faktorial(nilai - r) + + if ( + isinstance(faktorial_nilai, str) + or isinstance(faktorial_r, str) + or isinstance(faktorial_nilai_r, str) + ): + return pesan_error.error_tipe_data(["int"]) + else: + return faktorial_nilai / (faktorial_r * faktorial_nilai_r) else: return pesan_error.error_tipe_data(["int"]) @@ -314,7 +331,9 @@ def hitung_jumlah_deret( return 0.5 * n * (2 * a + (n - 1) * b) -def transpose_matriks(matriks: list[Union[float, int]]) -> Union[list[int, float], str]: +def transpose_matriks( + matriks: list[list[Union[float, int]]], +) -> Union[list[list[Union[float, int]]], str]: """ fungsi untuk transpose matrix diff --git a/OpenSeries/statistika.py b/OpenSeries/statistika.py index 21c39f0..38af6f7 100644 --- a/OpenSeries/statistika.py +++ b/OpenSeries/statistika.py @@ -3,7 +3,7 @@ from OpenSeries.util import error as pesan_error -def entropy(label: list[int], base: int = None) -> Union[float, int, str]: +def entropy(label: list[int], base: int) -> Union[float, int, str]: """ fungsi menghitung entropy dari suatu fitur pada suatu dataset diff --git a/dev-requirements.txt b/dev-requirements.txt index a77e5b7..e079f8a 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,3 +1 @@ -black pytest -flake8 From a0a63c4d5bfd5bd893a83d3c2c5eec249bb39073 Mon Sep 17 00:00:00 2001 From: slowy07 Date: Wed, 24 Jan 2024 12:17:15 +0700 Subject: [PATCH 2/2] fix: amend message commit Signed-off-by: slowy07 --- OpenSeries/matematika.py | 2 +- testing/statistika_test.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/OpenSeries/matematika.py b/OpenSeries/matematika.py index 53b2209..2672dc3 100644 --- a/OpenSeries/matematika.py +++ b/OpenSeries/matematika.py @@ -197,7 +197,7 @@ def permutasi(nilai: int, r: int) -> Union[int, float, str]: if isinstance(faktorial_nilai, str) or isinstance(faktorial_nilai_r, str): return pesan_error.error_tipe_data(["int"]) else: - return faktorial_nilai_r / faktorial_nilai_r + return faktorial_nilai / faktorial_nilai_r def kombinasi(nilai: int, r: int) -> Union[int, float, str]: diff --git a/testing/statistika_test.py b/testing/statistika_test.py index c12edea..9fba2b9 100644 --- a/testing/statistika_test.py +++ b/testing/statistika_test.py @@ -7,22 +7,22 @@ class TestFungsiEntropy(unittest.TestCase): def test_entropy_dengan_none(self): label = [1, 1, 2, 2, 3, 3] - hasil = statistika.entropy(label) - self.assertFalse(np.allclose(hasil, 1.584962500721156)) + hasil = statistika.entropy(label, base=2) + self.assertTrue(np.allclose(hasil, 1.584962500721156)) def test_nilai_kosong(self): label = [] - hasil = statistika.entropy(label) + hasil = statistika.entropy(label, base=2) self.assertEqual(hasil, pesan_error.error_format("label tidak boleh kosong")) def test_nilai_element_dalam_label(self): label = [1, 2, "3", 4] - hasil = statistika.entropy(label) + hasil = statistika.entropy(label, base=2) self.assertEqual(hasil, pesan_error.error_tipe_data(["int"])) def test_tipe_data_tuple(self): label = (1, 2, 3) - hasil = statistika.entropy(label) + hasil = statistika.entropy(label, base=2) self.assertEqual(hasil, pesan_error.error_tipe_data(["list"]))