diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml new file mode 100644 index 0000000..e70619e --- /dev/null +++ b/.github/workflows/python-publish.yml @@ -0,0 +1,67 @@ +# This workflow will upload a Python Package using Twine when a release is created +# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Upload Python Package + +on: + release: + types: [published] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.8", "3.9", "3.10"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install flake8 pytest pytest-cov + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest + deploy: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install build + - name: Build package + run: python -m build + - name: Publish package + uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 + with: + user: __token__ + password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/main.py b/main.py index 991edee..903f663 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,11 @@ + +from converter import dec_to_bin, bin_to_dec + + def say_hello(): - print("Say Hello !") + print("Hello") + if __name__ == '__main__': - say_hello() \ No newline at end of file + print(*dec_to_bin(256)) + print(bin_to_dec([1, 0, 1, 0, 0, 1])) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..2caac20 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[project] +name = "TP_release_converter" +version = "0.0.2" +authors = [ + { name="Angileri", email="lorenzo.angileri@isen.yncrea.fr" }, +] +description = "apprentissage de relaese de package" +readme = "README.md" +requires-python = ">=3.7" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", +] diff --git a/src/dec_bin_converter_Angileri_lorenzo/__init__.py b/src/dec_bin_converter_Angileri_lorenzo/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/dec_bin_converter_Angileri_lorenzo/converter.py b/src/dec_bin_converter_Angileri_lorenzo/converter.py new file mode 100644 index 0000000..966f5a9 --- /dev/null +++ b/src/dec_bin_converter_Angileri_lorenzo/converter.py @@ -0,0 +1,35 @@ +from typing import List + + +def dec_to_bin(n: int) -> List[int]: + """ + Converts a positive integer to binary + :param n: int, decimal integer to convert to binary + :return: Binary translation of input number, as a list of 0s and 1 + """ + if n < 0: + raise ValueError("n should be positive") + + bit = [0 if n % 2 == 0 else 1] + return bit if n <= 1 else dec_to_bin(n >> 1) + bit + + +def bin_to_dec(bits_array: List[int]) -> int: + """ + Converts a list of 0s and 1s to its corresponding decimal representation + :param bits_array: list of 0s and 1s to convert + :return: int + """ + if not inputs_is_bits(bits_array): + raise ValueError("input should contain only bits (0 or 1)") + + ba = bits_array.copy() + ba.reverse() + return sum([(1 << i) * m for i, m in enumerate(ba)]) + + +def inputs_is_bits(bits_array): + for b in bits_array: + if b not in [0, 1]: + return False + return True diff --git a/test_converter.py b/test_converter.py new file mode 100644 index 0000000..65fdb7a --- /dev/null +++ b/test_converter.py @@ -0,0 +1,30 @@ + +from unittest import TestCase + +from src.dec_bin_converter_Angileri_lorenzo.converter import dec_to_bin, bin_to_dec + + +class TestConverter(TestCase): + def test_dec_to_bin_even(self): + n = 10 + expected = [1, 0, 1, 0] + self.assertListEqual(expected, dec_to_bin(n)) + + def test_dec_to_bin_odd(self): + n = 37 + expected = [1, 0, 0, 1, 0, 1] + self.assertListEqual(expected, dec_to_bin(n)) + + def test_bin_to_dec_odd(self): + bits = [1, 0, 0, 1, 0, 1] + expected = 37 + self.assertEqual(expected, bin_to_dec(bits)) + + def test_bin_to_dec_even(self): + bits = [1, 0, 1, 0] + expected = 10 + self.assertEqual(expected, bin_to_dec(bits)) + + def test_reverse(self): + n = 135 + self.assertEqual(n, bin_to_dec(dec_to_bin(n)))