Skip to content

Commit 019e8a4

Browse files
authored
Implement conversions (#5)
* update suported python to >=3.10 * add basic conversion logic * refactor convbase * remove __main__ * add hexto* methods * add decto* methods * add octto* methods * add binto* methods * add version and hexto* tests * linting * update pre-commit * remove unsupported python versions * add nox support * add remaining conversion tests * add docstrings
1 parent deea8bf commit 019e8a4

File tree

7 files changed

+655
-32
lines changed

7 files changed

+655
-32
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
17-
16+
python-version: ["3.10", "3.11", "3.12"]
17+
1818
steps:
1919
- uses: actions/checkout@v4
2020
- name: Set up Python ${{ matrix.python-version }}

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ repos:
1010
- id: check-added-large-files
1111
- repo: https://github.com/astral-sh/ruff-pre-commit
1212
# Ruff version.
13-
rev: v0.3.2
13+
rev: v0.3.4
1414
hooks:
1515
# Run the linter.
1616
- id: ruff

noxfile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import nox
2+
3+
4+
@nox.session(python="3.10", venv_backend="uv")
5+
def lint(session):
6+
session.install("ruff")
7+
session.run("ruff", "check", "--fix")
8+
session.run("ruff", "format")
9+
10+
11+
@nox.session(python=["3.10", "3.11", "3.12"], venv_backend="uv")
12+
def test(session):
13+
session.install("pytest")
14+
session.install(".")
15+
session.run("pytest", "-q")

pyproject.toml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "convbase"
77
dynamic = ["version"]
88
description = 'Base conversion command line utility.'
99
readme = "README.md"
10-
requires-python = ">=3.8"
10+
requires-python = ">=3.10"
1111
keywords = []
1212
authors = [
1313
{ name = "Kyle O'Malley", email = "[email protected]" },
@@ -16,8 +16,6 @@ classifiers = [
1616
"Development Status :: 1 - Planning",
1717
"License :: OSI Approved :: MIT License",
1818
"Programming Language :: Python",
19-
"Programming Language :: Python :: 3.8",
20-
"Programming Language :: Python :: 3.9",
2119
"Programming Language :: Python :: 3.10",
2220
"Programming Language :: Python :: 3.11",
2321
"Programming Language :: Python :: 3.12",
@@ -32,20 +30,22 @@ Documentation = "https://github.com/jkomalley/convbase#readme"
3230
Issues = "https://github.com/jkomalley/convbase/issues"
3331
Source = "https://github.com/jkomalley/convbase"
3432

35-
[project.optional-dependencies]
36-
dev = [
37-
"pytest",
38-
"ruff",
39-
"mypy",
40-
"pre-commit",
41-
]
42-
publish = [
43-
"twine",
44-
"build"
45-
]
46-
4733
[project.scripts]
48-
convbase = "convbase.convbase:run"
34+
hextodec = "convbase.convbase:hextodec"
35+
hextooct = "convbase.convbase:hextooct"
36+
hextobin = "convbase.convbase:hextobin"
37+
38+
dectohex = "convbase.convbase:dectohex"
39+
dectooct = "convbase.convbase:dectooct"
40+
dectobin = "convbase.convbase:dectobin"
41+
42+
octtohex = "convbase.convbase:octtohex"
43+
octtodec = "convbase.convbase:octtodec"
44+
octtobin = "convbase.convbase:octtobin"
45+
46+
bintohex = "convbase.convbase:bintohex"
47+
bintodec = "convbase.convbase:bintodec"
48+
bintooct = "convbase.convbase:bintooct"
4949

5050
[tool.setuptools.dynamic]
5151
version = {attr = "convbase.__version__"}
@@ -58,6 +58,6 @@ line-ending = "lf"
5858
skip-magic-trailing-comma = true
5959

6060
[tool.mypy]
61-
python_version = "3.8"
61+
python_version = "3.10"
6262
warn_return_any = true
6363
warn_unused_configs = true

src/convbase/__main__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/convbase/convbase.py

Lines changed: 173 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,179 @@
22

33
import click
44

5-
__version__ = version(__package__)
5+
version_option = click.version_option(
6+
version(__package__), package_name=__package__, message="%(package)s %(version)s"
7+
)
8+
9+
HEXBASE = 16
10+
DECBASE = 10
11+
OCTBASE = 8
12+
BINBASE = 2
613

714

815
@click.command()
9-
@click.version_option(
10-
__version__, package_name=__package__, message="%(package)s %(version)s"
11-
)
12-
def run():
13-
pass
16+
@click.argument("value")
17+
@version_option
18+
def hextodec(value):
19+
"""Convert a hexadecimal value to decimal."""
20+
try:
21+
dec_value = int(value, HEXBASE)
22+
except ValueError:
23+
click.echo("Error: not hex value")
24+
exit(1)
25+
else:
26+
print(dec_value)
27+
28+
29+
@click.command()
30+
@click.argument("value")
31+
@version_option
32+
def hextooct(value):
33+
"""Convert a hexadecimal value to octal."""
34+
try:
35+
dec_value = int(value, HEXBASE)
36+
except ValueError:
37+
click.echo("Error: not hex value")
38+
exit(1)
39+
else:
40+
print(oct(dec_value))
41+
42+
43+
@click.command()
44+
@click.argument("value")
45+
@version_option
46+
def hextobin(value):
47+
"""Convert a hexadecimal value to binary."""
48+
try:
49+
dec_value = int(value, HEXBASE)
50+
except ValueError:
51+
click.echo("Error: not hex value")
52+
exit(1)
53+
else:
54+
print(bin(dec_value))
55+
56+
57+
@click.command()
58+
@click.argument("value")
59+
@version_option
60+
def dectohex(value):
61+
"""Convert a decimal value to hexadecimal."""
62+
try:
63+
dec_value = int(value, DECBASE)
64+
except ValueError:
65+
click.echo("Error: not decimal value")
66+
exit(1)
67+
else:
68+
print(hex(dec_value))
69+
70+
71+
@click.command()
72+
@click.argument("value")
73+
@version_option
74+
def dectooct(value):
75+
"""Convert a decimal value to octal."""
76+
try:
77+
dec_value = int(value, DECBASE)
78+
except ValueError:
79+
click.echo("Error: not decimal value")
80+
exit(1)
81+
else:
82+
print(oct(dec_value))
83+
84+
85+
@click.command()
86+
@click.argument("value")
87+
@version_option
88+
def dectobin(value):
89+
"""Convert a decimal value to binary."""
90+
try:
91+
dec_value = int(value, DECBASE)
92+
except ValueError:
93+
click.echo("Error: not decimal value")
94+
exit(1)
95+
else:
96+
print(bin(dec_value))
97+
98+
99+
@click.command()
100+
@click.argument("value")
101+
@version_option
102+
def octtohex(value):
103+
"""Convert an octal value to hexadecimal."""
104+
try:
105+
dec_value = int(value, OCTBASE)
106+
except ValueError:
107+
click.echo("Error: not octal value")
108+
exit(1)
109+
else:
110+
print(hex(dec_value))
111+
112+
113+
@click.command()
114+
@click.argument("value")
115+
@version_option
116+
def octtodec(value):
117+
"""Convert an octal value to decimal."""
118+
try:
119+
dec_value = int(value, OCTBASE)
120+
except ValueError:
121+
click.echo("Error: not octal value")
122+
exit(1)
123+
else:
124+
print(dec_value)
125+
126+
127+
@click.command()
128+
@click.argument("value")
129+
@version_option
130+
def octtobin(value):
131+
"""Convert an octal value to binary."""
132+
try:
133+
dec_value = int(value, OCTBASE)
134+
except ValueError:
135+
click.echo("Error: not octal value")
136+
exit(1)
137+
else:
138+
print(bin(dec_value))
139+
140+
141+
@click.command()
142+
@click.argument("value")
143+
@version_option
144+
def bintohex(value):
145+
"""Convert a binary value to hexadecimal."""
146+
try:
147+
dec_value = int(value, BINBASE)
148+
except ValueError:
149+
click.echo("Error: not binary value")
150+
exit(1)
151+
else:
152+
print(hex(dec_value))
153+
154+
155+
@click.command()
156+
@click.argument("value")
157+
@version_option
158+
def bintodec(value):
159+
"""Convert a binnary value to decimal."""
160+
try:
161+
dec_value = int(value, BINBASE)
162+
except ValueError:
163+
click.echo("Error: not binary value")
164+
exit(1)
165+
else:
166+
print(dec_value)
167+
168+
169+
@click.command()
170+
@click.argument("value")
171+
@version_option
172+
def bintooct(value):
173+
"""Convert a binary value to octal."""
174+
try:
175+
dec_value = int(value, BINBASE)
176+
except ValueError:
177+
click.echo("Error: not binary value")
178+
exit(1)
179+
else:
180+
print(oct(dec_value))

0 commit comments

Comments
 (0)