csdigit 0.1__tar.gz → 0.3__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.
- {csdigit-0.1 → csdigit-0.3}/.github/workflows/ci.yml +3 -3
- {csdigit-0.1 → csdigit-0.3}/.github/workflows/multi-platforms.yml +6 -4
- {csdigit-0.1 → csdigit-0.3}/.github/workflows/python-app.yml +2 -2
- {csdigit-0.1 → csdigit-0.3}/.github/workflows/python-package.yml +4 -3
- {csdigit-0.1 → csdigit-0.3}/.pre-commit-config.yaml +3 -1
- csdigit-0.3/.vscode/settings.json +3 -0
- {csdigit-0.1 → csdigit-0.3}/CONTRIBUTING.md +20 -18
- csdigit-0.3/GEMINI.md +84 -0
- csdigit-0.3/IFLOW.md +75 -0
- {csdigit-0.1 → csdigit-0.3}/LICENSE.txt +1 -1
- {csdigit-0.1/src/csdigit.egg-info → csdigit-0.3}/PKG-INFO +4 -2
- {csdigit-0.1 → csdigit-0.3}/README.md +3 -1
- {csdigit-0.1 → csdigit-0.3}/benches/test_bench.py +2 -2
- {csdigit-0.1 → csdigit-0.3}/docs/conf.py +1 -4
- csdigit-0.3/mypy.ini +21 -0
- {csdigit-0.1 → csdigit-0.3}/src/csdigit/cli.py +46 -34
- csdigit-0.3/src/csdigit/csd.py +387 -0
- csdigit-0.3/src/csdigit/csd_multiplier.py +413 -0
- csdigit-0.3/src/csdigit/lcsre.py +82 -0
- csdigit-0.3/src/csdigit/py.typed +0 -0
- {csdigit-0.1 → csdigit-0.3/src/csdigit.egg-info}/PKG-INFO +4 -2
- {csdigit-0.1 → csdigit-0.3}/src/csdigit.egg-info/SOURCES.txt +7 -0
- csdigit-0.3/src/pycsd/csd/__init__.py +3 -0
- csdigit-0.3/src/pycsd/csd/csd.py +160 -0
- csdigit-0.3/src/pycsd/csd_orig.py +129 -0
- csdigit-0.3/src/pycsd/dev/csd_numbers.py +89 -0
- csdigit-0.3/src/pycsd/test/decimal_tests.py +34 -0
- {csdigit-0.1 → csdigit-0.3}/src/pycsd/test/test_csd_decimals.py +7 -7
- {csdigit-0.1 → csdigit-0.3}/src/pycsd/test/test_csd_integers.py +3 -3
- {csdigit-0.1 → csdigit-0.3}/src/pycsd/test/test_to_and_fro_10bits.py +3 -3
- csdigit-0.3/tests/test_cli.py +83 -0
- csdigit-0.3/tests/test_csd.py +339 -0
- csdigit-0.3/tests/test_csd_multiplier.py +696 -0
- csdigit-0.3/tests/test_lcsre.py +164 -0
- csdigit-0.1/src/csdigit/csd.py +0 -309
- csdigit-0.1/src/csdigit/lcsre.py +0 -87
- csdigit-0.1/src/pycsd/csd/__init__.py +0 -1
- csdigit-0.1/src/pycsd/csd/csd.py +0 -118
- csdigit-0.1/src/pycsd/csd_orig.py +0 -99
- csdigit-0.1/src/pycsd/dev/csd_numbers.py +0 -86
- csdigit-0.1/src/pycsd/test/decimal_tests.py +0 -33
- csdigit-0.1/tests/test_cli.py +0 -23
- csdigit-0.1/tests/test_csd.py +0 -61
- csdigit-0.1/tests/test_lcsre.py +0 -23
- {csdigit-0.1 → csdigit-0.3}/.coveragerc +0 -0
- {csdigit-0.1 → csdigit-0.3}/.github/workflows/codecov.yml +0 -0
- {csdigit-0.1 → csdigit-0.3}/.github/workflows/jekyll-gh-pages.yml +0 -0
- {csdigit-0.1 → csdigit-0.3}/.github/workflows/python-publish.yml +0 -0
- {csdigit-0.1 → csdigit-0.3}/.gitignore +0 -0
- {csdigit-0.1 → csdigit-0.3}/.isort.cfg +0 -0
- {csdigit-0.1 → csdigit-0.3}/.readthedocs.yml +0 -0
- {csdigit-0.1 → csdigit-0.3}/AUTHORS.md +0 -0
- {csdigit-0.1 → csdigit-0.3}/CHANGELOG.md +0 -0
- {csdigit-0.1 → csdigit-0.3}/_config.yml +0 -0
- {csdigit-0.1 → csdigit-0.3}/docs/Makefile +0 -0
- {csdigit-0.1 → csdigit-0.3}/docs/_static/.gitignore +0 -0
- {csdigit-0.1 → csdigit-0.3}/docs/authors.md +0 -0
- {csdigit-0.1 → csdigit-0.3}/docs/changelog.md +0 -0
- {csdigit-0.1 → csdigit-0.3}/docs/contributing.md +0 -0
- {csdigit-0.1 → csdigit-0.3}/docs/index.md +0 -0
- {csdigit-0.1 → csdigit-0.3}/docs/license.md +0 -0
- {csdigit-0.1 → csdigit-0.3}/docs/readme.md +0 -0
- {csdigit-0.1 → csdigit-0.3}/docs/requirements.txt +0 -0
- {csdigit-0.1 → csdigit-0.3}/environment.yml +0 -0
- {csdigit-0.1 → csdigit-0.3}/experiments/notes.md +0 -0
- {csdigit-0.1 → csdigit-0.3}/experiments/random_gen.py +0 -0
- {csdigit-0.1 → csdigit-0.3}/pyproject.toml +0 -0
- {csdigit-0.1 → csdigit-0.3}/requirements/README.md +0 -0
- {csdigit-0.1 → csdigit-0.3}/requirements/default.txt +0 -0
- {csdigit-0.1 → csdigit-0.3}/requirements/doc.txt +0 -0
- {csdigit-0.1 → csdigit-0.3}/requirements/extras.txt +0 -0
- {csdigit-0.1 → csdigit-0.3}/requirements/test.txt +0 -0
- {csdigit-0.1 → csdigit-0.3}/requirements.txt +0 -0
- {csdigit-0.1 → csdigit-0.3}/setup.cfg +0 -0
- {csdigit-0.1 → csdigit-0.3}/setup.py +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/csdigit/__init__.py +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/csdigit.egg-info/dependency_links.txt +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/csdigit.egg-info/not-zip-safe +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/csdigit.egg-info/requires.txt +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/csdigit.egg-info/top_level.txt +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/pycsd/README +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/pycsd/__init__.py +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/pycsd/from_csd +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/pycsd/run_tests.py +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/pycsd/test/__init__.py +0 -0
- {csdigit-0.1 → csdigit-0.3}/src/pycsd/to_csd +0 -0
- {csdigit-0.1 → csdigit-0.3}/tests/conftest.py +0 -0
- {csdigit-0.1 → csdigit-0.3}/tox.ini +0 -0
|
@@ -8,7 +8,7 @@ on:
|
|
|
8
8
|
push:
|
|
9
9
|
# Avoid using all the resources/limits available by checking only
|
|
10
10
|
# relevant branches and tags. Other branches can be checked via PRs.
|
|
11
|
-
branches: [main]
|
|
11
|
+
branches: [ main, dev ]
|
|
12
12
|
tags: ["v[0-9]*", "[0-9]+.[0-9]+*"] # Match tags that resemble a version
|
|
13
13
|
pull_request: # Run in every PR
|
|
14
14
|
workflow_dispatch: # Allow manually triggering the workflow
|
|
@@ -60,8 +60,8 @@ jobs:
|
|
|
60
60
|
strategy:
|
|
61
61
|
matrix:
|
|
62
62
|
python:
|
|
63
|
-
- "3.
|
|
64
|
-
- "3.
|
|
63
|
+
- "3.10" # oldest Python supported by PSF
|
|
64
|
+
- "3.12" # newest Python that is stable
|
|
65
65
|
platform:
|
|
66
66
|
- ubuntu-latest
|
|
67
67
|
- macos-latest
|
|
@@ -8,10 +8,10 @@ jobs:
|
|
|
8
8
|
strategy:
|
|
9
9
|
matrix:
|
|
10
10
|
os: ["ubuntu-latest", "windows-latest"]
|
|
11
|
-
version: ["3.
|
|
11
|
+
version: ["3.10", "3.12"]
|
|
12
12
|
steps:
|
|
13
|
-
- uses: actions/checkout@
|
|
14
|
-
- uses: conda-incubator/setup-miniconda@
|
|
13
|
+
- uses: actions/checkout@v3
|
|
14
|
+
- uses: conda-incubator/setup-miniconda@v3
|
|
15
15
|
with:
|
|
16
16
|
activate-environment: test
|
|
17
17
|
environment-file: environment.yml
|
|
@@ -31,5 +31,7 @@ jobs:
|
|
|
31
31
|
shell: bash -l {0}
|
|
32
32
|
run: |
|
|
33
33
|
conda install pytest
|
|
34
|
-
|
|
34
|
+
conda install pip
|
|
35
|
+
conda install setuptools wheel setuptools_scm
|
|
36
|
+
pip install -e .
|
|
35
37
|
pytest
|
|
@@ -5,9 +5,9 @@ name: Python package
|
|
|
5
5
|
|
|
6
6
|
on:
|
|
7
7
|
push:
|
|
8
|
-
branches: [main]
|
|
8
|
+
branches: [ main, dev ]
|
|
9
9
|
pull_request:
|
|
10
|
-
branches: [main]
|
|
10
|
+
branches: [ main, dev ]
|
|
11
11
|
|
|
12
12
|
jobs:
|
|
13
13
|
build:
|
|
@@ -15,7 +15,7 @@ jobs:
|
|
|
15
15
|
strategy:
|
|
16
16
|
fail-fast: false
|
|
17
17
|
matrix:
|
|
18
|
-
python-version: ["3.
|
|
18
|
+
python-version: ["3.10", "3.12"]
|
|
19
19
|
|
|
20
20
|
steps:
|
|
21
21
|
- uses: actions/checkout@v2
|
|
@@ -26,6 +26,7 @@ jobs:
|
|
|
26
26
|
- name: Install dependencies
|
|
27
27
|
run: |
|
|
28
28
|
python -m pip install --upgrade pip
|
|
29
|
+
python -m pip install setuptools wheel setuptools_scm
|
|
29
30
|
python -m pip install flake8 pytest
|
|
30
31
|
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
|
31
32
|
python setup.py develop
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
exclude: "^docs/conf.py"
|
|
1
|
+
exclude: "^docs/conf.py|src/pycsd/"
|
|
2
2
|
|
|
3
3
|
repos:
|
|
4
4
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
@@ -14,8 +14,10 @@ repos:
|
|
|
14
14
|
- id: debug-statements
|
|
15
15
|
- id: end-of-file-fixer
|
|
16
16
|
- id: requirements-txt-fixer
|
|
17
|
+
exclude: "requirements[.]txt$"
|
|
17
18
|
- id: mixed-line-ending
|
|
18
19
|
args: ["--fix=auto"] # replace 'auto' with 'lf' to enforce Linux/Mac line endings or 'crlf' for Windows
|
|
20
|
+
exclude: "requirements[.]txt$"
|
|
19
21
|
|
|
20
22
|
## If you want to automatically "modernize" your Python code:
|
|
21
23
|
# - repo: https://github.com/asottile/pyupgrade
|
|
@@ -69,27 +69,29 @@ by adding missing information and correcting mistakes.
|
|
|
69
69
|
This means that the docs are kept in the same repository as the project code, and
|
|
70
70
|
that any documentation update is done in the same way was a code contribution.
|
|
71
71
|
|
|
72
|
-
```{todo}
|
|
72
|
+
```{todo}
|
|
73
|
+
Don't forget to mention which markup language you are using.
|
|
73
74
|
|
|
74
|
-
|
|
75
|
+
e.g., [reStructuredText] or [CommonMark] with [MyST] extensions.
|
|
75
76
|
```
|
|
76
77
|
|
|
77
|
-
```{todo}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
78
|
+
```{todo}
|
|
79
|
+
If your project is hosted on GitHub, you can also mention the following tip:
|
|
80
|
+
|
|
81
|
+
:::{tip}
|
|
82
|
+
Please notice that the [GitHub web interface] provides a quick way of
|
|
83
|
+
propose changes in `csdigit`'s files. While this mechanism can
|
|
84
|
+
be tricky for normal code contributions, it works perfectly fine for
|
|
85
|
+
contributing to the docs, and can be quite handy.
|
|
86
|
+
|
|
87
|
+
If you are interested in trying this method out, please navigate to
|
|
88
|
+
the `docs` folder in the source [repository], find which file you
|
|
89
|
+
would like to propose changes and click in the little pencil icon at the
|
|
90
|
+
top, to open [GitHub's code editor]. Once you finish editing the file,
|
|
91
|
+
please write a message in the form at the bottom of the page describing
|
|
92
|
+
which changes have you made and what are the motivations behind them and
|
|
93
|
+
submit your proposal.
|
|
94
|
+
:::
|
|
93
95
|
```
|
|
94
96
|
|
|
95
97
|
When working on documentation changes in your local machine, you can
|
csdigit-0.3/GEMINI.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# GEMINI.md
|
|
2
|
+
|
|
3
|
+
## Project Overview
|
|
4
|
+
|
|
5
|
+
This project, `csdigit`, is a Python library for Canonical Signed Digit (CSD) conversion. CSD is a number representation system that uses only three symbols: `+`, `-`, and `0`. It is used in digital signal processing and other areas of computer science for efficient arithmetic operations.
|
|
6
|
+
|
|
7
|
+
The library provides functions to convert numbers between decimal format and CSD format. It supports both integers and floating-point numbers.
|
|
8
|
+
|
|
9
|
+
The main functions are:
|
|
10
|
+
* `to_csd`: Converts a decimal number to a CSD string.
|
|
11
|
+
* `to_csd_i`: Converts an integer to a CSD string.
|
|
12
|
+
* `to_decimal`: Converts a CSD string to a decimal number.
|
|
13
|
+
* `to_csdnnz`: Converts a decimal number to a CSD string with a specified maximum number of non-zero digits.
|
|
14
|
+
* `to_csdnnz_i`: Converts an integer to a CSD string with a specified maximum number of non-zero digits.
|
|
15
|
+
|
|
16
|
+
The project is structured as a standard Python package using `setuptools` and `PyScaffold`.
|
|
17
|
+
|
|
18
|
+
## Building and Running
|
|
19
|
+
|
|
20
|
+
### Dependencies
|
|
21
|
+
|
|
22
|
+
The project's dependencies are managed in `requirements/` folder.
|
|
23
|
+
|
|
24
|
+
* **Runtime dependencies:** `decorator`
|
|
25
|
+
* **Testing dependencies:** `codecov`, `coverage`, `hypothesis`, `pytest`, `pytest-benchmark`, `pytest-cov`
|
|
26
|
+
|
|
27
|
+
### Testing
|
|
28
|
+
|
|
29
|
+
The project uses `tox` for managing test environments and `pytest` for running tests.
|
|
30
|
+
|
|
31
|
+
To run the tests, execute the following command:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
tox
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
This will run the tests in an isolated environment.
|
|
38
|
+
|
|
39
|
+
### Linting and Formatting
|
|
40
|
+
|
|
41
|
+
The project uses `pre-commit` with `isort`, `black`, and `flake8` to enforce code style and quality.
|
|
42
|
+
|
|
43
|
+
To run the linters and formatters, execute the following command:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pre-commit run --all-files
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Building the Package
|
|
50
|
+
|
|
51
|
+
The project uses `tox` to manage the build process.
|
|
52
|
+
|
|
53
|
+
To build the source distribution and wheel, run:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
tox -e build
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The distributable files will be located in the `dist/` directory.
|
|
60
|
+
|
|
61
|
+
To clean the build artifacts, run:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
tox -e clean
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Documentation
|
|
68
|
+
|
|
69
|
+
The project's documentation is built using `Sphinx`.
|
|
70
|
+
|
|
71
|
+
To build the documentation, run:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
tox -e docs
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The generated documentation will be in `docs/_build/html/`.
|
|
78
|
+
|
|
79
|
+
## Development Conventions
|
|
80
|
+
|
|
81
|
+
* **Code Style:** The project follows the `black` code style and uses `isort` to sort imports. `flake8` is used for linting.
|
|
82
|
+
* **Testing:** The project uses `pytest` for testing. Tests are located in the `tests/` directory.
|
|
83
|
+
* **Commits:** The project uses `pre-commit` to run checks before each commit.
|
|
84
|
+
* **Versioning:** The project uses `setuptools_scm` for automatic versioning based on Git tags.
|
csdigit-0.3/IFLOW.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# CSDigit iFlow Context
|
|
2
|
+
|
|
3
|
+
## 项目概述
|
|
4
|
+
|
|
5
|
+
CSDigit 是一个 Python 库,用于在十进制数和规范符号数字 (Canonical Signed Digit, CSD) 表示之间进行转换。CSD 是一种特殊的符号数字表示法,其中每个数字被限制为 -1、0 或 1,且不允许连续的非零数字。这种表示法在数字信号处理应用中特别有用,例如滤波器设计,因为它能通过使用简单的加法器和减法器来实现高效算术运算。
|
|
6
|
+
|
|
7
|
+
## 主要功能
|
|
8
|
+
|
|
9
|
+
- `to_csd`: 将十进制数转换为 CSD 字符串
|
|
10
|
+
- `to_csd_i`: 将整数转换为 CSD 字符串
|
|
11
|
+
- `to_decimal`: 将 CSD 字符串转换为十进制数
|
|
12
|
+
- `to_csdnnz`: 具有最大非零位数限制的 CSD 转换
|
|
13
|
+
- `to_csdnnz_i`: 整数版本的 CSD 转换,带非零位数限制
|
|
14
|
+
- `longest_repeated_substring`: 查找 CSD 字符串中的最长重复子串
|
|
15
|
+
- `generate_csd_multiplier`: 生成 CSD 乘法器的 Verilog 代码
|
|
16
|
+
|
|
17
|
+
## 项目结构
|
|
18
|
+
|
|
19
|
+
- `src/csdigit/`: 源代码目录
|
|
20
|
+
- `csd.py`: 核心 CSD 转换函数
|
|
21
|
+
- `cli.py`: 命令行接口
|
|
22
|
+
- `csd_multiplier.py`: CSD 乘法器 Verilog 代码生成
|
|
23
|
+
- `lcsre.py`: 最长重复子串查找算法
|
|
24
|
+
- `tests/`: 测试代码
|
|
25
|
+
- `docs/`: 文档
|
|
26
|
+
- `requirements/`: 依赖配置
|
|
27
|
+
|
|
28
|
+
## 构建和运行
|
|
29
|
+
|
|
30
|
+
### 安装依赖
|
|
31
|
+
```bash
|
|
32
|
+
pip install -r requirements.txt
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 开发安装
|
|
36
|
+
```bash
|
|
37
|
+
pip install -e .
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 运行测试
|
|
41
|
+
```bash
|
|
42
|
+
pytest tests/
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 使用命令行工具
|
|
46
|
+
```bash
|
|
47
|
+
# 转换十进制到 CSD
|
|
48
|
+
python -m csdigit.cli -c 28.5 -p 2
|
|
49
|
+
|
|
50
|
+
# 转换 CSD 到十进制
|
|
51
|
+
python -m csdigit.cli -d "+00-00.+"
|
|
52
|
+
|
|
53
|
+
# 使用非零位数限制
|
|
54
|
+
python -m csdigit.cli -f 28.5 -z 4
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## 开发约定
|
|
58
|
+
|
|
59
|
+
- 遵循 Python PEP 8 代码风格
|
|
60
|
+
- 使用 pytest 进行测试
|
|
61
|
+
- 使用 doctest 进行函数示例测试
|
|
62
|
+
- 使用 type hints 提供类型信息
|
|
63
|
+
- 遵循语义化版本控制
|
|
64
|
+
|
|
65
|
+
## 测试
|
|
66
|
+
|
|
67
|
+
项目包含全面的单元测试,使用 pytest 和 hypothesis 进行属性测试。主要测试包括:
|
|
68
|
+
- CSD 转换的正确性
|
|
69
|
+
- 反向转换验证 (decimal -> CSD -> decimal)
|
|
70
|
+
- 特殊情况处理 (零值、负数)
|
|
71
|
+
- 边界条件测试
|
|
72
|
+
|
|
73
|
+
## 许可证
|
|
74
|
+
|
|
75
|
+
MIT 许可证
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: csdigit
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3
|
|
4
4
|
Summary: Add a short description here!
|
|
5
5
|
Home-page: https://github.com/pyscaffold/pyscaffold/
|
|
6
6
|
Author: Wai-Shing Luk
|
|
@@ -54,7 +54,9 @@ The library contains a number of functions, each with a specific role.
|
|
|
54
54
|
|
|
55
55
|
3. The functions to_decimal_using_pow and to_decimal perform the inverse of the to_csd function. They accept a CSD string as input and return a decimal number.
|
|
56
56
|
|
|
57
|
-
4. The to_csdnnz function is a variant of the to_csd function that allows the user to specify the maximum number of non-zero digits in the result.
|
|
57
|
+
4. The to_csdnnz function is a variant of the to_csd function that allows the user to specify the maximum number of non-zero digits in the result.
|
|
58
|
+
|
|
59
|
+
5. The to_csdnnz_i function is a variant of the to_csd_i function that allows the user to specify the maximum number of non-zero digits in the result.
|
|
58
60
|
|
|
59
61
|
The library fulfills its intended function through a sequence of mathematical operations and logical tests. In order to effect a conversion from decimal to CSD, the system employs the use of powers of 2 in order to ascertain which of the three symbols (+, -, or 0) is to be used at each position within the CSD string. The algorithm then performs repeated divisions of the input number by two and compares the result to specific thresholds to determine the appropriate symbol to use.
|
|
60
62
|
|
|
@@ -31,7 +31,9 @@ The library contains a number of functions, each with a specific role.
|
|
|
31
31
|
|
|
32
32
|
3. The functions to_decimal_using_pow and to_decimal perform the inverse of the to_csd function. They accept a CSD string as input and return a decimal number.
|
|
33
33
|
|
|
34
|
-
4. The to_csdnnz function is a variant of the to_csd function that allows the user to specify the maximum number of non-zero digits in the result.
|
|
34
|
+
4. The to_csdnnz function is a variant of the to_csd function that allows the user to specify the maximum number of non-zero digits in the result.
|
|
35
|
+
|
|
36
|
+
5. The to_csdnnz_i function is a variant of the to_csd_i function that allows the user to specify the maximum number of non-zero digits in the result.
|
|
35
37
|
|
|
36
38
|
The library fulfills its intended function through a sequence of mathematical operations and logical tests. In order to effect a conversion from decimal to CSD, the system employs the use of powers of 2 in order to ascertain which of the three symbols (+, -, or 0) is to be used at each position within the CSD string. The algorithm then performs repeated divisions of the input number by two and compares the result to specific thresholds to determine the appropriate symbol to use.
|
|
37
39
|
|
|
@@ -28,10 +28,7 @@ sys.path.insert(0, os.path.join(__location__, "../src"))
|
|
|
28
28
|
# setup.py install" in the RTD Advanced Settings.
|
|
29
29
|
# Additionally it helps us to avoid running apidoc manually
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
from sphinx.ext import apidoc
|
|
33
|
-
except ImportError:
|
|
34
|
-
from sphinx import apidoc
|
|
31
|
+
from sphinx.ext import apidoc
|
|
35
32
|
|
|
36
33
|
output_dir = os.path.join(__location__, "api")
|
|
37
34
|
module_dir = os.path.join(__location__, "../src/csdigit")
|
csdigit-0.3/mypy.ini
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[mypy]
|
|
2
|
+
python_version = 3.12
|
|
3
|
+
warn_unused_configs = True
|
|
4
|
+
|
|
5
|
+
[mypy-setuptools.*]
|
|
6
|
+
ignore_missing_imports = True
|
|
7
|
+
|
|
8
|
+
[mypy-networkx.*]
|
|
9
|
+
ignore_missing_imports = True
|
|
10
|
+
|
|
11
|
+
[mypy-numpy.*]
|
|
12
|
+
ignore_missing_imports = True
|
|
13
|
+
|
|
14
|
+
[mypy-scipy.*]
|
|
15
|
+
ignore_missing_imports = True
|
|
16
|
+
|
|
17
|
+
[mypy-Cython.*]
|
|
18
|
+
ignore_missing_imports = True
|
|
19
|
+
|
|
20
|
+
[mypy-json.*]
|
|
21
|
+
ignore_missing_imports = True
|
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
"""
|
|
2
|
-
|
|
3
|
-
console script. To run this script uncomment the following lines in the
|
|
4
|
-
``[options.entry_points]`` section in ``setup.cfg``::
|
|
2
|
+
Command-line interface for CSD conversion.
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
Provides a CLI wrapper around the csdigit library, allowing conversion
|
|
5
|
+
between decimal numbers and Canonical Signed Digit (CSD) representation
|
|
6
|
+
from the terminal.
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
Supported conversions:
|
|
9
|
+
- Decimal float to CSD (--to_csd / -c)
|
|
10
|
+
- Decimal float to CSD with limited non-zero digits (--to_csdnnz / -f)
|
|
11
|
+
- CSD string to decimal (--to_decimal / -d)
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
- https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html
|
|
17
|
-
- https://pip.pypa.io/en/stable/reference/pip_install
|
|
13
|
+
Usage:
|
|
14
|
+
python -m csdigit.cli --to_csd 28.5
|
|
15
|
+
python -m csdigit.cli --to_decimal "+00-00.+"
|
|
16
|
+
python -m csdigit.cli --to_csdnnz 28.5 --nnz 4
|
|
18
17
|
"""
|
|
19
18
|
|
|
20
19
|
import argparse
|
|
21
20
|
import logging
|
|
22
21
|
import sys
|
|
22
|
+
from typing import List
|
|
23
23
|
|
|
24
24
|
from csdigit import __version__
|
|
25
25
|
from csdigit.csd import to_csd, to_csdnnz, to_decimal
|
|
@@ -28,6 +28,7 @@ __author__ = "Wai-Shing Luk"
|
|
|
28
28
|
__copyright__ = "Wai-Shing Luk"
|
|
29
29
|
__license__ = "MIT"
|
|
30
30
|
|
|
31
|
+
# Initialize logger for the module
|
|
31
32
|
_logger = logging.getLogger(__name__)
|
|
32
33
|
|
|
33
34
|
|
|
@@ -37,7 +38,7 @@ _logger = logging.getLogger(__name__)
|
|
|
37
38
|
# executable/script.
|
|
38
39
|
|
|
39
40
|
|
|
40
|
-
def parse_args(args):
|
|
41
|
+
def parse_args(args: List[str]) -> argparse.Namespace:
|
|
41
42
|
"""Parse command line parameters
|
|
42
43
|
|
|
43
44
|
Args:
|
|
@@ -47,12 +48,17 @@ def parse_args(args):
|
|
|
47
48
|
Returns:
|
|
48
49
|
:obj:`argparse.Namespace`: command line parameters namespace
|
|
49
50
|
"""
|
|
51
|
+
# Create argument parser with program description
|
|
50
52
|
parser = argparse.ArgumentParser(description="Converts a decimal to a CSD format")
|
|
53
|
+
|
|
54
|
+
# Add version information argument
|
|
51
55
|
parser.add_argument(
|
|
52
56
|
"--version",
|
|
53
57
|
action="version",
|
|
54
58
|
version="csdigit {ver}".format(ver=__version__),
|
|
55
59
|
)
|
|
60
|
+
|
|
61
|
+
# Add conversion options with type checking and default values
|
|
56
62
|
parser.add_argument(
|
|
57
63
|
"-c",
|
|
58
64
|
"--to_csd",
|
|
@@ -60,7 +66,7 @@ def parse_args(args):
|
|
|
60
66
|
help="a decimal number",
|
|
61
67
|
type=float,
|
|
62
68
|
metavar="FLOAT",
|
|
63
|
-
default=float("Inf"),
|
|
69
|
+
default=float("Inf"), # Use infinity as sentinel value
|
|
64
70
|
)
|
|
65
71
|
parser.add_argument(
|
|
66
72
|
"-f",
|
|
@@ -78,8 +84,10 @@ def parse_args(args):
|
|
|
78
84
|
help="a CSD string",
|
|
79
85
|
type=str,
|
|
80
86
|
metavar="STR",
|
|
81
|
-
default="",
|
|
87
|
+
default="", # Empty string as sentinel value
|
|
82
88
|
)
|
|
89
|
+
|
|
90
|
+
# Add precision control parameters
|
|
83
91
|
parser.add_argument(
|
|
84
92
|
"-p",
|
|
85
93
|
"--places",
|
|
@@ -98,6 +106,8 @@ def parse_args(args):
|
|
|
98
106
|
metavar="INT",
|
|
99
107
|
default=4,
|
|
100
108
|
)
|
|
109
|
+
|
|
110
|
+
# Add logging level control arguments
|
|
101
111
|
parser.add_argument(
|
|
102
112
|
"-v",
|
|
103
113
|
"--verbose",
|
|
@@ -117,12 +127,13 @@ def parse_args(args):
|
|
|
117
127
|
return parser.parse_args(args)
|
|
118
128
|
|
|
119
129
|
|
|
120
|
-
def setup_logging(loglevel) -> None:
|
|
130
|
+
def setup_logging(loglevel: int) -> None:
|
|
121
131
|
"""Setup basic logging
|
|
122
132
|
|
|
123
133
|
Args:
|
|
124
134
|
loglevel (int): minimum loglevel for emitting messages
|
|
125
135
|
"""
|
|
136
|
+
# Configure logging format and output stream
|
|
126
137
|
logformat = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
|
|
127
138
|
logging.basicConfig(
|
|
128
139
|
level=loglevel,
|
|
@@ -132,28 +143,30 @@ def setup_logging(loglevel) -> None:
|
|
|
132
143
|
)
|
|
133
144
|
|
|
134
145
|
|
|
135
|
-
def main(args) -> None:
|
|
136
|
-
"""Wrapper allowing :func:`
|
|
146
|
+
def main(args: List[str]) -> None:
|
|
147
|
+
"""Wrapper allowing :func:`main` to be called with string arguments in a CLI fashion
|
|
137
148
|
|
|
138
|
-
Instead of returning the value from :func:`
|
|
149
|
+
Instead of returning the value from :func:`main`, it prints the result to the
|
|
139
150
|
``stdout`` in a nicely formatted message.
|
|
140
151
|
|
|
141
152
|
Args:
|
|
142
153
|
args (List[str]): command line parameters as list of strings
|
|
143
154
|
(for example ``["--verbose", "42"]``).
|
|
144
155
|
"""
|
|
145
|
-
|
|
146
|
-
setup_logging(
|
|
156
|
+
parsed_args = parse_args(args)
|
|
157
|
+
setup_logging(parsed_args.loglevel)
|
|
147
158
|
_logger.debug("Starting crazy calculations...")
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
ans = to_csdnnz(args.decimal2, args.nnz)
|
|
159
|
+
|
|
160
|
+
# Check which conversion option was specified and execute corresponding function
|
|
161
|
+
if parsed_args.decimal != float("Inf"):
|
|
162
|
+
ans = to_csd(parsed_args.decimal, parsed_args.places)
|
|
153
163
|
print(f"{ans}")
|
|
154
|
-
if
|
|
155
|
-
ans =
|
|
164
|
+
if parsed_args.decimal2 != float("Inf"):
|
|
165
|
+
ans = to_csdnnz(parsed_args.decimal2, parsed_args.nnz)
|
|
156
166
|
print(f"{ans}")
|
|
167
|
+
if parsed_args.csdstr != "":
|
|
168
|
+
print(f"{to_decimal(parsed_args.csdstr)}")
|
|
169
|
+
|
|
157
170
|
_logger.info("Script ends here")
|
|
158
171
|
|
|
159
172
|
|
|
@@ -162,18 +175,17 @@ def run() -> None:
|
|
|
162
175
|
|
|
163
176
|
This function can be used as entry point to create console scripts with setuptools.
|
|
164
177
|
"""
|
|
178
|
+
# Pass all arguments except the script name
|
|
165
179
|
main(sys.argv[1:])
|
|
166
180
|
|
|
167
181
|
|
|
168
182
|
if __name__ == "__main__":
|
|
169
|
-
#
|
|
170
|
-
#
|
|
171
|
-
# executing it as a script.
|
|
172
|
-
# https://docs.python.org/3/library/__main__.html
|
|
183
|
+
# Guard clause to prevent script execution when imported as module
|
|
184
|
+
# https://docs.python.org/3/library/__main__.html
|
|
173
185
|
|
|
174
186
|
# After installing your project with pip, users can also run your Python
|
|
175
187
|
# modules as scripts via the ``-m`` flag, as defined in PEP 338::
|
|
176
188
|
#
|
|
177
|
-
# python -m csdigit.to_csd 42
|
|
189
|
+
# python -m csdigit.cli --to_csd 42
|
|
178
190
|
#
|
|
179
191
|
run()
|