bin-dec-conversions 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of bin-dec-conversions might be problematic. Click here for more details.
- bin_dec_conversions-0.1.0/PKG-INFO +12 -0
- bin_dec_conversions-0.1.0/README.md +138 -0
- bin_dec_conversions-0.1.0/pyproject.toml +33 -0
- bin_dec_conversions-0.1.0/setup.cfg +4 -0
- bin_dec_conversions-0.1.0/src/bin_dec_conversions.egg-info/PKG-INFO +12 -0
- bin_dec_conversions-0.1.0/src/bin_dec_conversions.egg-info/SOURCES.txt +10 -0
- bin_dec_conversions-0.1.0/src/bin_dec_conversions.egg-info/dependency_links.txt +1 -0
- bin_dec_conversions-0.1.0/src/bin_dec_conversions.egg-info/requires.txt +7 -0
- bin_dec_conversions-0.1.0/src/bin_dec_conversions.egg-info/top_level.txt +1 -0
- bin_dec_conversions-0.1.0/src/conversions/__init__.py +3 -0
- bin_dec_conversions-0.1.0/src/conversions/conversions.py +30 -0
- bin_dec_conversions-0.1.0/tests/test_conversions.py +27 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bin-dec-conversions
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python package for binary and decimal conversions implemented from scratch.
|
|
5
|
+
Author: Marco Salvatore Tornabene
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Provides-Extra: dev
|
|
8
|
+
Requires-Dist: pytest; extra == "dev"
|
|
9
|
+
Requires-Dist: ruff; extra == "dev"
|
|
10
|
+
Requires-Dist: black; extra == "dev"
|
|
11
|
+
Requires-Dist: build; extra == "dev"
|
|
12
|
+
Requires-Dist: twine; extra == "dev"
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Binary/Decimal Conversions
|
|
2
|
+
|
|
3
|
+
A small Python package for converting non-negative integers between binary and decimal representations. The conversion algorithms are implemented manually, without using Python's `bin()` or `int(value, 2)` for the core conversions.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Convert a binary string to a decimal integer with `binary_to_decimal()`.
|
|
8
|
+
- Convert a non-negative decimal integer to a binary string with `decimal_to_binary()`.
|
|
9
|
+
- Reject invalid binary input and negative decimal values with `ValueError`.
|
|
10
|
+
- Include tests written with `pytest`.
|
|
11
|
+
|
|
12
|
+
## Requirements
|
|
13
|
+
|
|
14
|
+
- Python 3.10 or newer
|
|
15
|
+
- `pytest` to run the tests
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
From the project root, create and activate a virtual environment, then install the package in editable mode:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
python -m venv .venv
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
On Linux/macOS:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
source .venv/bin/activate
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
On Windows PowerShell:
|
|
32
|
+
|
|
33
|
+
```powershell
|
|
34
|
+
.venv\Scripts\Activate.ps1
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Install the package:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
python -m pip install -e .
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
If the development dependencies are configured in `pyproject.toml`, you can install them as well:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
python -m pip install -e ".[dev]"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
The package exposes two functions:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from bin_dec_conversions import binary_to_decimal, decimal_to_binary
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Binary to decimal
|
|
58
|
+
|
|
59
|
+
Pass the binary number as a string:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from bin_dec_conversions import binary_to_decimal
|
|
63
|
+
|
|
64
|
+
result = binary_to_decimal("1010")
|
|
65
|
+
print(result) # 10
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The function evaluates each bit according to its position and adds the corresponding power of two.
|
|
69
|
+
|
|
70
|
+
More examples:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
binary_to_decimal("0") # 0
|
|
74
|
+
binary_to_decimal("1010") # 10
|
|
75
|
+
binary_to_decimal("10000000") # 128
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Decimal to binary
|
|
79
|
+
|
|
80
|
+
Pass a non-negative integer:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from bin_dec_conversions import decimal_to_binary
|
|
84
|
+
|
|
85
|
+
result = decimal_to_binary(10)
|
|
86
|
+
print(result) # "1010"
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The function repeatedly divides the number by two and collects the remainders. Reading the remainders in reverse order gives the binary representation.
|
|
90
|
+
|
|
91
|
+
More examples:
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
decimal_to_binary(0) # "0"
|
|
95
|
+
decimal_to_binary(10) # "1010"
|
|
96
|
+
decimal_to_binary(128) # "10000000"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Error handling
|
|
100
|
+
|
|
101
|
+
Invalid inputs raise `ValueError`.
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
binary_to_decimal("20") # raises ValueError
|
|
105
|
+
decimal_to_binary(-2) # raises ValueError
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
For binary input, use a string containing only the digits `0` and `1`. For decimal input, use a non-negative integer.
|
|
109
|
+
|
|
110
|
+
## Running the tests
|
|
111
|
+
|
|
112
|
+
Run the test suite from the project root:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
python -m pytest
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The tests cover common conversions and some invalid inputs, including invalid binary strings and negative decimal values.
|
|
119
|
+
|
|
120
|
+
## Project structure
|
|
121
|
+
|
|
122
|
+
```text
|
|
123
|
+
bin-dec-conversions/
|
|
124
|
+
├── pyproject.toml
|
|
125
|
+
├── README.md
|
|
126
|
+
├── src/
|
|
127
|
+
│ └── bin_dec_conversions/
|
|
128
|
+
│ ├── __init__.py
|
|
129
|
+
│ └── conversions.py
|
|
130
|
+
└── tests/
|
|
131
|
+
└── test_conversions.py
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The `conversions.py` module contains the conversion functions. The package's `__init__.py` should re-export `binary_to_decimal` and `decimal_to_binary` so they can be imported directly from `bin_dec_conversions`.
|
|
135
|
+
|
|
136
|
+
## Scope
|
|
137
|
+
|
|
138
|
+
This initial version focuses on non-negative whole numbers. Fractions, negative binary representations, hexadecimal conversion, and a command-line interface are outside the current scope.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
|
|
2
|
+
[build-system]
|
|
3
|
+
requires = ["setuptools>=68"]
|
|
4
|
+
build-backend = "setuptools.build_meta"
|
|
5
|
+
|
|
6
|
+
[project]
|
|
7
|
+
name = "bin-dec-conversions"
|
|
8
|
+
version = "0.1.0"
|
|
9
|
+
description = "A Python package for binary and decimal conversions implemented from scratch."
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "Marco Salvatore Tornabene" }
|
|
13
|
+
]
|
|
14
|
+
dependencies = []
|
|
15
|
+
|
|
16
|
+
[project.optional-dependencies]
|
|
17
|
+
dev = [
|
|
18
|
+
"pytest",
|
|
19
|
+
"ruff",
|
|
20
|
+
"black",
|
|
21
|
+
"build",
|
|
22
|
+
"twine"
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[tool.setuptools.packages.find]
|
|
26
|
+
where = ["src"]
|
|
27
|
+
|
|
28
|
+
[tool.pytest.ini_options]
|
|
29
|
+
testpaths = ["tests"]
|
|
30
|
+
|
|
31
|
+
[tool.ruff]
|
|
32
|
+
line-length = 88
|
|
33
|
+
target-version = "py310"
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bin-dec-conversions
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python package for binary and decimal conversions implemented from scratch.
|
|
5
|
+
Author: Marco Salvatore Tornabene
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Provides-Extra: dev
|
|
8
|
+
Requires-Dist: pytest; extra == "dev"
|
|
9
|
+
Requires-Dist: ruff; extra == "dev"
|
|
10
|
+
Requires-Dist: black; extra == "dev"
|
|
11
|
+
Requires-Dist: build; extra == "dev"
|
|
12
|
+
Requires-Dist: twine; extra == "dev"
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/bin_dec_conversions.egg-info/PKG-INFO
|
|
4
|
+
src/bin_dec_conversions.egg-info/SOURCES.txt
|
|
5
|
+
src/bin_dec_conversions.egg-info/dependency_links.txt
|
|
6
|
+
src/bin_dec_conversions.egg-info/requires.txt
|
|
7
|
+
src/bin_dec_conversions.egg-info/top_level.txt
|
|
8
|
+
src/conversions/__init__.py
|
|
9
|
+
src/conversions/conversions.py
|
|
10
|
+
tests/test_conversions.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
conversions
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def binary_to_decimal(binary: str) -> int:
|
|
5
|
+
if not re.fullmatch(r"[01]+", binary):
|
|
6
|
+
raise ValueError
|
|
7
|
+
|
|
8
|
+
decimal = 0
|
|
9
|
+
reversed_binary = binary[::-1]
|
|
10
|
+
for i, bit in enumerate(reversed_binary):
|
|
11
|
+
if bit == "1":
|
|
12
|
+
decimal += 2**i
|
|
13
|
+
|
|
14
|
+
return decimal
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def decimal_to_binary(decimal: int) -> str:
|
|
18
|
+
if decimal < 0:
|
|
19
|
+
raise ValueError
|
|
20
|
+
|
|
21
|
+
if decimal == 0:
|
|
22
|
+
return "0"
|
|
23
|
+
|
|
24
|
+
bits = []
|
|
25
|
+
|
|
26
|
+
while decimal > 0:
|
|
27
|
+
bits.append(str(decimal % 2))
|
|
28
|
+
decimal //= 2
|
|
29
|
+
|
|
30
|
+
return "".join(bits[::-1])
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from conversions import binary_to_decimal, decimal_to_binary
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_bin2dec() -> None:
|
|
7
|
+
assert binary_to_decimal("0") == 0
|
|
8
|
+
assert binary_to_decimal("1010") == 10
|
|
9
|
+
assert binary_to_decimal("10000000") == 128
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_dec2bin() -> None:
|
|
13
|
+
assert decimal_to_binary(0) == "0"
|
|
14
|
+
assert decimal_to_binary(10) == "1010"
|
|
15
|
+
assert decimal_to_binary(128) == "10000000"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_invalid_binary():
|
|
19
|
+
with pytest.raises(ValueError):
|
|
20
|
+
binary_to_decimal("Cat")
|
|
21
|
+
with pytest.raises(ValueError):
|
|
22
|
+
binary_to_decimal("20")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_invalid_decimal():
|
|
26
|
+
with pytest.raises(ValueError):
|
|
27
|
+
decimal_to_binary(-2)
|