stringcalc 0.1.0.dev3__tar.gz → 0.1.0.dev4__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.
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/.github/workflows/ci.yml +1 -1
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/PKG-INFO +1 -1
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/pyproject.toml +2 -0
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/stringcalc/__init__.py +1 -1
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/stringcalc/cli.py +41 -9
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/stringcalc/frets.py +1 -1
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/stringcalc/tension.py +9 -2
- stringcalc-0.1.0.dev4/tests/test_frets.py +90 -0
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/tests/test_string.py +16 -0
- stringcalc-0.1.0.dev3/tests/test_frets.py +0 -42
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/.gitignore +0 -0
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/.pre-commit-config.yaml +0 -0
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/LICENSE +0 -0
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/README.md +0 -0
- {stringcalc-0.1.0.dev3 → stringcalc-0.1.0.dev4}/stringcalc/data/daddario-tension.csv +0 -0
|
@@ -3,6 +3,7 @@ CLI
|
|
|
3
3
|
"""
|
|
4
4
|
from __future__ import annotations
|
|
5
5
|
|
|
6
|
+
import functools
|
|
6
7
|
import re
|
|
7
8
|
from pathlib import Path
|
|
8
9
|
|
|
@@ -31,9 +32,9 @@ def _to_fancy_sci(s: str) -> str:
|
|
|
31
32
|
return f"{a}×10{b}"
|
|
32
33
|
|
|
33
34
|
|
|
34
|
-
def error(s: str, *, rc: int = 1) -> None:
|
|
35
|
+
def error(s: str, *, rc: int = 1, markup: bool = True) -> None:
|
|
35
36
|
"""Print error message."""
|
|
36
|
-
console.print(s, style=Style(color="red", bold=True), highlight=False)
|
|
37
|
+
console.print(s, style=Style(color="red", bold=True), highlight=False, markup=markup)
|
|
37
38
|
assert rc != 0
|
|
38
39
|
raise typer.Exit(rc)
|
|
39
40
|
|
|
@@ -43,6 +44,26 @@ def info(s: str) -> None:
|
|
|
43
44
|
console.print(s, style=Style(color="cyan", bold=True), highlight=False)
|
|
44
45
|
|
|
45
46
|
|
|
47
|
+
def warning(s: str) -> None:
|
|
48
|
+
"""Print warning message."""
|
|
49
|
+
console.print(s, style=Style(color="red", bold=False), highlight=False)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def pretty_warnings(f):
|
|
53
|
+
"""Decorator to catch warnings and pretty-print them with Rich after running `f`."""
|
|
54
|
+
import warnings
|
|
55
|
+
|
|
56
|
+
@functools.wraps(f)
|
|
57
|
+
def inner(*args, **kwargs):
|
|
58
|
+
with warnings.catch_warnings(record=True) as w:
|
|
59
|
+
f(*args, **kwargs)
|
|
60
|
+
|
|
61
|
+
for i in range(len(w)):
|
|
62
|
+
warning(f"- {w[i].message}")
|
|
63
|
+
|
|
64
|
+
return inner
|
|
65
|
+
|
|
66
|
+
|
|
46
67
|
def _version_callback(show: bool):
|
|
47
68
|
if show:
|
|
48
69
|
import subprocess
|
|
@@ -155,7 +176,7 @@ def frets(
|
|
|
155
176
|
pprint_table(df, title=f"Fret distances for L={L}", float_format=float_format)
|
|
156
177
|
|
|
157
178
|
|
|
158
|
-
_RE_LENGTH_SPEC = re.compile(r"(?P<a>[0-
|
|
179
|
+
_RE_LENGTH_SPEC = re.compile(r"(?P<a>[0-9]+|[ns])-(?P<b>[0-9]+|[ns])=(?P<d>[0-9e\.\+\-\.]+)")
|
|
159
180
|
|
|
160
181
|
|
|
161
182
|
def _ab_interp(s: str) -> int | None:
|
|
@@ -192,15 +213,25 @@ def length(
|
|
|
192
213
|
|
|
193
214
|
m = _RE_LENGTH_SPEC.fullmatch(spec)
|
|
194
215
|
if m is None:
|
|
195
|
-
error(
|
|
216
|
+
error(
|
|
217
|
+
f"Input failed to match a-b=d spec format regex {_RE_LENGTH_SPEC.pattern!r}",
|
|
218
|
+
rc=2,
|
|
219
|
+
markup=False,
|
|
220
|
+
)
|
|
196
221
|
|
|
197
222
|
assert m is not None
|
|
198
223
|
dct = m.groupdict()
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
224
|
+
try:
|
|
225
|
+
a = _ab_interp(dct["a"])
|
|
226
|
+
b = _ab_interp(dct["b"])
|
|
227
|
+
d = float(dct["d"])
|
|
228
|
+
except ValueError as e:
|
|
229
|
+
error(f"Failed to convert input to frets. Message: {e}", rc=2)
|
|
230
|
+
|
|
231
|
+
try:
|
|
232
|
+
res = length_from_distance((a, b), d)
|
|
233
|
+
except ValueError as e:
|
|
234
|
+
error(f"Failed to compute scale length. Message: {e}", rc=2)
|
|
204
235
|
|
|
205
236
|
if round_ is not None:
|
|
206
237
|
res = round(float(res), round_)
|
|
@@ -218,6 +249,7 @@ def length(
|
|
|
218
249
|
|
|
219
250
|
|
|
220
251
|
@app.command(name="gauge")
|
|
252
|
+
@pretty_warnings
|
|
221
253
|
def gauge_(
|
|
222
254
|
T: float = typer.Option(..., "-T", "--tension", help="Desired tension"),
|
|
223
255
|
L: float = typer.Option(..., "-L", "--length", help="String length (scale length)."),
|
|
@@ -9,6 +9,7 @@ from __future__ import annotations
|
|
|
9
9
|
|
|
10
10
|
import math
|
|
11
11
|
import re
|
|
12
|
+
import warnings
|
|
12
13
|
from functools import lru_cache
|
|
13
14
|
from pathlib import Path
|
|
14
15
|
from typing import NamedTuple
|
|
@@ -254,9 +255,15 @@ def suggest_gauge(
|
|
|
254
255
|
data_sort["T"] = T_all[data_sort.index]
|
|
255
256
|
data_sort["dT"] = data_sort["T"] - T
|
|
256
257
|
|
|
257
|
-
# TODO:
|
|
258
|
+
b = 1.7 # TODO: allow to set this?
|
|
259
|
+
if (data_sort["dT"] > b).all() or (data_sort["dT"] < -b).all():
|
|
260
|
+
warnings.warn(
|
|
261
|
+
f"You are outside the range of what string type group(s) {types} can provide. "
|
|
262
|
+
"Maybe a different string type can give the tension/pitch/length you desire.",
|
|
263
|
+
stacklevel=2,
|
|
264
|
+
)
|
|
258
265
|
|
|
259
|
-
df = data_sort[["id", "T", "dT"]].sort_values(by="
|
|
266
|
+
df = data_sort[["id", "T", "dT"]].sort_values(by="dT").reset_index(drop=True)
|
|
260
267
|
desc = {
|
|
261
268
|
"id": "Product ID",
|
|
262
269
|
"T": "Tension",
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from stringcalc import frets
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.mark.parametrize(
|
|
8
|
+
"n, expected",
|
|
9
|
+
[
|
|
10
|
+
(12, 0.5),
|
|
11
|
+
(24, 0.75),
|
|
12
|
+
([12, 24], np.array([0.5, 0.75], dtype=float)),
|
|
13
|
+
(1, 0.05613),
|
|
14
|
+
(5, 0.25085),
|
|
15
|
+
],
|
|
16
|
+
)
|
|
17
|
+
def test_d_et_rel(n, expected):
|
|
18
|
+
np.testing.assert_allclose(frets.distance_et(n, L=1), expected, rtol=1e-4)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_ds_et_rel():
|
|
22
|
+
ret = frets.distances(N=24, L=1)
|
|
23
|
+
assert ret.loc[12].d == 0.5
|
|
24
|
+
assert ret.loc[24].d == 0.75
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_ds_invalid():
|
|
28
|
+
with pytest.raises(ValueError, match=r"invalid `method` 'asdf'"):
|
|
29
|
+
frets.distances(N=17, L=21, method="asdf")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@pytest.mark.parametrize(
|
|
33
|
+
"n",
|
|
34
|
+
[
|
|
35
|
+
-1,
|
|
36
|
+
-0.1,
|
|
37
|
+
[-2, -1],
|
|
38
|
+
[-1, 0, 1],
|
|
39
|
+
],
|
|
40
|
+
)
|
|
41
|
+
def test_d_et_invalid(n):
|
|
42
|
+
with pytest.raises(ValueError, match="input fret numbers should be positive"):
|
|
43
|
+
frets.distance_et(n=-1, L=25)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@pytest.mark.parametrize(
|
|
47
|
+
"ab, d, expected",
|
|
48
|
+
[
|
|
49
|
+
((0, 2), 2, 18.332),
|
|
50
|
+
((2, None), 16.3316, 18.332),
|
|
51
|
+
((2, None), 21, 23.572),
|
|
52
|
+
((0, 1), 1, 17.817),
|
|
53
|
+
((1, 0), 1, 17.817),
|
|
54
|
+
],
|
|
55
|
+
)
|
|
56
|
+
def test_l_from_d(ab, d, expected):
|
|
57
|
+
assert frets.length_from_distance(ab, d) == pytest.approx(expected, abs=5e-4)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_l_from_d_pint():
|
|
61
|
+
# Seems not to work with `pytest.approx`
|
|
62
|
+
ret = frets.length_from_distance(ab=(1, 0), d="1in")
|
|
63
|
+
expected = 17.817 * frets.ureg["in"]
|
|
64
|
+
delta = 5e-4 * frets.ureg["in"]
|
|
65
|
+
assert expected - delta <= ret <= expected + delta
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_l_from_d_invalid_ab():
|
|
69
|
+
with pytest.raises(ValueError, match="must be different"):
|
|
70
|
+
frets.length_from_distance((0, 0), 10)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_l_from_d_invalid_d():
|
|
74
|
+
with pytest.raises(ValueError, match="must be positive"):
|
|
75
|
+
frets.length_from_distance((0, 1), -10)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_main():
|
|
79
|
+
import runpy
|
|
80
|
+
import warnings
|
|
81
|
+
|
|
82
|
+
mod_name = "stringcalc.frets"
|
|
83
|
+
with warnings.catch_warnings():
|
|
84
|
+
warnings.filterwarnings(
|
|
85
|
+
"ignore",
|
|
86
|
+
category=RuntimeWarning,
|
|
87
|
+
message=rf"{mod_name!r} found in sys\.modules after import of package",
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
runpy.run_module(mod_name, run_name="__main__")
|
|
@@ -60,6 +60,22 @@ def test_suggest_gauge():
|
|
|
60
60
|
assert ret.id.tolist() == ["NYL031", "NYL032", "NYL033"]
|
|
61
61
|
|
|
62
62
|
|
|
63
|
+
@pytest.mark.parametrize(
|
|
64
|
+
"pitch",
|
|
65
|
+
["G1", "G4"],
|
|
66
|
+
)
|
|
67
|
+
def test_suggest_gauge_bounds_warning(pitch):
|
|
68
|
+
T = 15
|
|
69
|
+
L = 21
|
|
70
|
+
types = {"PB"}
|
|
71
|
+
|
|
72
|
+
with pytest.warns(
|
|
73
|
+
UserWarning,
|
|
74
|
+
match=r"You are outside the range of what string type group\(s\) \{'PB'\} can provide\.",
|
|
75
|
+
):
|
|
76
|
+
suggest_gauge(T=T, L=L, pitch=pitch, types=types)
|
|
77
|
+
|
|
78
|
+
|
|
63
79
|
@pytest.mark.parametrize(
|
|
64
80
|
"T,pitch,expected",
|
|
65
81
|
[
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
import pytest
|
|
3
|
-
|
|
4
|
-
from stringcalc import frets
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
@pytest.mark.parametrize(
|
|
8
|
-
"n, expected",
|
|
9
|
-
[
|
|
10
|
-
(12, 0.5),
|
|
11
|
-
(24, 0.75),
|
|
12
|
-
([12, 24], np.array([0.5, 0.75], dtype=float)),
|
|
13
|
-
(1, 0.05613),
|
|
14
|
-
(5, 0.25085),
|
|
15
|
-
],
|
|
16
|
-
)
|
|
17
|
-
def test_d_et_rel(n, expected):
|
|
18
|
-
np.testing.assert_allclose(frets.distance_et(n, L=1), expected, rtol=1e-4)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
@pytest.mark.parametrize(
|
|
22
|
-
"ab, d, expected",
|
|
23
|
-
[
|
|
24
|
-
((0, 2), 2, 18.332),
|
|
25
|
-
((2, None), 16.3316, 18.332),
|
|
26
|
-
((2, None), 21, 23.572),
|
|
27
|
-
((0, 1), 1, 17.817),
|
|
28
|
-
((1, 0), 1, 17.817),
|
|
29
|
-
],
|
|
30
|
-
)
|
|
31
|
-
def test_l_from_d(ab, d, expected):
|
|
32
|
-
assert frets.length_from_distance(ab, d) == pytest.approx(expected, abs=5e-4)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def test_l_from_d_invalid_ab():
|
|
36
|
-
with pytest.raises(ValueError, match="must be different"):
|
|
37
|
-
frets.length_from_distance((0, 0), 10)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def test_l_from_d_invalid_d():
|
|
41
|
-
with pytest.raises(ValueError, match="must be positive"):
|
|
42
|
-
frets.length_from_distance((0, 1), -10)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|