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

Mathias COUSSEAU pull request #37

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e29d028
Adds function to "say hello"
rgt-yncrea Sep 19, 2022
001ab4a
Creates a decimal to binary converter
rgt-yncrea Sep 19, 2022
cd3b000
Adds a binary to decimal converter
rgt-yncrea Sep 19, 2022
96a3f84
Improves speed of conversion
rgt-yncrea Sep 19, 2022
ff5f0fa
Adds some documentation to converters
rgt-yncrea Sep 19, 2022
4c1e33d
Adds some input values check
rgt-yncrea Sep 19, 2022
c098a3b
Checks that only bits are passed to conv
rgt-yncrea Sep 19, 2022
2435632
Refactors bits verification
rgt-yncrea Sep 19, 2022
cb3af7e
Fixes bits check
rgt-yncrea Sep 19, 2022
7a6358a
Adds UT for converter
rgt-yncrea Sep 22, 2022
9d3866f
Fix converter bugs
rgt-yncrea Sep 22, 2022
40845e5
Merge pull request #1 from Bajiio/decimal-binary-converter
Bajiio Sep 28, 2022
45b92a4
Create python-publish.yml
Bajiio Sep 28, 2022
20a136c
Ajout init.py et dossier src
Bajiio Sep 29, 2022
d9981a2
Moove converter.py to src/converter_package_Bajiio
Bajiio Sep 29, 2022
29c980b
Create test/ folder
Bajiio Sep 29, 2022
8fb1838
Create toml configuration file
Bajiio Sep 29, 2022
a36549f
Merge branch 'main' into dev
Bajiio Sep 29, 2022
48d9154
Delete pyproject.toml
Bajiio Sep 29, 2022
187ad04
Delete __init__.py
Bajiio Sep 29, 2022
bae45f9
moove back conervter.py into main folder
Bajiio Sep 29, 2022
ca531a8
Merge pull request #2 from Bajiio/dev
Bajiio Sep 29, 2022
fa7871e
create init.py to import package as directory
Bajiio Sep 29, 2022
e504b77
moove converter to src/
Bajiio Sep 29, 2022
b421e5a
moove into converter_package
Bajiio Sep 29, 2022
a3ea89e
moove converter to converter_package
Bajiio Sep 29, 2022
02946e5
Update test to import right place
Bajiio Sep 29, 2022
d743d9f
fix bug import
Bajiio Sep 29, 2022
77355ec
fix bug and add into test
Bajiio Sep 29, 2022
9d340a4
Rename testexample.py to __init__.py
Bajiio Sep 29, 2022
840ca27
Create pyproject.toml file
Bajiio Sep 29, 2022
fd57bef
Fix import bug
Bajiio Sep 29, 2022
216ad40
fix import bug
Bajiio Sep 29, 2022
6f4bcbd
try fixing import bug
Bajiio Sep 29, 2022
70ceb86
try fixing bug import
Bajiio Sep 29, 2022
61116ce
try fixing import bug
Bajiio Sep 29, 2022
03c563d
moove back test converter
Bajiio Sep 29, 2022
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
39 changes: 39 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -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 }}
9 changes: 7 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -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()
print(*dec_to_bin(256))
print(bin_to_dec([1, 0, 1, 0, 0, 1]))
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "converter_package_Bajiio"
version = "0.0.1"
authors = [
{ name="Mathias", email="[email protected]" },
]
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/Bajiio/tp2_git/"
"Bug Tracker" = "https://github.com/Bajiio/tp2_git/issues"
1 change: 1 addition & 0 deletions src/converter_package_Bajiio/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

35 changes: 35 additions & 0 deletions src/converter_package_Bajiio/converter.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions test_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from unittest import TestCase

from src.converter_package_Bajiio.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)))
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@