diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml new file mode 100644 index 0000000..ec70354 --- /dev/null +++ b/.github/workflows/python-publish.yml @@ -0,0 +1,39 @@ +# 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: + 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..3f7c726 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,10 @@ +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])) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..888412f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,22 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "converter_package_Louise" +version = "0.0.1" +authors = [ + { name="Louise", email="author@example.com" }, +] +description = "A small example package" +readme = "README.md" +requires-python = ">=3.7" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", +] + +[project.urls] +"Homepage" = "https://github.com/Lou13213/M1-2022-git-workflow" +"Bug Tracker" = "https://github.com/Lou13213/M1-2022-git-workflow/issues" diff --git a/src/converter_package_Louise/__init__.py b/src/converter_package_Louise/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/converter_package_Louise/__init__.py @@ -0,0 +1 @@ + diff --git a/src/converter_package_Louise/converter.py b/src/converter_package_Louise/converter.py new file mode 100644 index 0000000..5735f4e --- /dev/null +++ b/src/converter_package_Louise/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/__init__.py b/test/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/__init__.py @@ -0,0 +1 @@ + diff --git a/test_converter.py b/test_converter.py new file mode 100644 index 0000000..b16992d --- /dev/null +++ b/test_converter.py @@ -0,0 +1,29 @@ +from unittest import TestCase + +from src.converter_package_Louise.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)))