Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore(dev): Precommit config #15

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/pythontesting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ node_modules
next-*
next-env.d.ts

# hasil dari format ruff
.ruff_cache

# hasil cache dari mypy
.mypy_cache
44 changes: 44 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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]
35 changes: 27 additions & 8 deletions OpenSeries/matematika.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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

Expand All @@ -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`
Expand All @@ -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"])

Expand All @@ -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 / faktorial_nilai_r


def kombinasi(nilai: int, r: int) -> Union[int, float, str]:
Expand All @@ -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"])

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion OpenSeries/statistika.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
black
pytest
flake8
10 changes: 5 additions & 5 deletions testing/statistika_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))


Expand Down
Loading