stringcalc 0.1.0.dev3__py3-none-any.whl → 0.1.0.dev4__py3-none-any.whl

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/__init__.py CHANGED
@@ -2,4 +2,4 @@
2
2
  Calculations for instrument strings
3
3
  """
4
4
 
5
- __version__ = "0.1.0.dev3"
5
+ __version__ = "0.1.0.dev4"
stringcalc/cli.py CHANGED
@@ -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-9ns])-(?P<b>[0-9ns])=(?P<d>[0-9e\.\+\-\.]+)")
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(f"Input failed to match a-b=d spec format regex {_RE_LENGTH_SPEC.pattern!r}", rc=2)
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
- a = _ab_interp(dct["a"])
200
- b = _ab_interp(dct["b"])
201
- d = float(dct["d"])
202
-
203
- res = length_from_distance((a, b), d)
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)."),
stringcalc/frets.py CHANGED
@@ -28,7 +28,7 @@ def distance_et(n: int | npt.ArrayLike, *, L: float) -> np.float_ | npt.NDArray[
28
28
  Parameters
29
29
  ----------
30
30
  n
31
- Fret number.
31
+ Fret number (or array-like of them).
32
32
  If floats are passed, they will be floored before computing.
33
33
  s
34
34
  Scale length.
stringcalc/tension.py CHANGED
@@ -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: warning if edge is one of the closest
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="id").reset_index(drop=True)
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",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: stringcalc
3
- Version: 0.1.0.dev3
3
+ Version: 0.1.0.dev4
4
4
  Summary: Calculations for instrument strings
5
5
  Author-email: zmoon <zmoon92@gmail.com>
6
6
  Requires-Python: ~=3.8
@@ -0,0 +1,10 @@
1
+ stringcalc/__init__.py,sha256=03RMhmcDxIWvr9JrJdiPNw2nVpTibhKaGfzqpVEFRLI,77
2
+ stringcalc/cli.py,sha256=7vIR_RTjpUCy9R1Be6KkqApqn1uTUdmxekAz-atQuhg,9724
3
+ stringcalc/frets.py,sha256=jypfP1CteKQR-8Q5v2_bB71zb7RDsWbn2HQBEYmFyRU,3545
4
+ stringcalc/tension.py,sha256=nIZWJtinswSPG0j2krr6MPJjPHpjACqE6k_xvWwofdw,8501
5
+ stringcalc/data/daddario-tension.csv,sha256=syYZsCI-btdGo85XrcbgsmwdMETPT9IgQ--uOuQhs_Y,124809
6
+ stringcalc-0.1.0.dev4.dist-info/entry_points.txt,sha256=0DV5ry2A4YzcRzPmWKPXWSFG_dd2rE1Q17UORWHN9vI,49
7
+ stringcalc-0.1.0.dev4.dist-info/LICENSE,sha256=QJZDkt5nq7Pto8WzgmPHKnpeTyEyyqscYUFlKoyXgZg,1090
8
+ stringcalc-0.1.0.dev4.dist-info/WHEEL,sha256=rSgq_JpHF9fHR1lx53qwg_1-2LypZE_qmcuXbVUq948,81
9
+ stringcalc-0.1.0.dev4.dist-info/METADATA,sha256=vtGmKCIOp-DhRKm5afbsGauJTeCLnQFMRdK-Fk-qLJk,4905
10
+ stringcalc-0.1.0.dev4.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- stringcalc/__init__.py,sha256=leIoGRZmOBoT4UpNrv0WEyeAajfLV9UpRJkl6mZkaMA,77
2
- stringcalc/cli.py,sha256=w0S3KheXJ4k76N-uwzr-JiMyeHcBmqd9OBvZfaYZ7zI,8804
3
- stringcalc/frets.py,sha256=sSfgdX3DCpx5gORzT759XH3mVahctQv09MgKVOBffeM,3521
4
- stringcalc/tension.py,sha256=U-nAM-mfYzn9ALaLCxL6K24SX26FnbiV9OQou--n72c,8178
5
- stringcalc/data/daddario-tension.csv,sha256=syYZsCI-btdGo85XrcbgsmwdMETPT9IgQ--uOuQhs_Y,124809
6
- stringcalc-0.1.0.dev3.dist-info/entry_points.txt,sha256=0DV5ry2A4YzcRzPmWKPXWSFG_dd2rE1Q17UORWHN9vI,49
7
- stringcalc-0.1.0.dev3.dist-info/LICENSE,sha256=QJZDkt5nq7Pto8WzgmPHKnpeTyEyyqscYUFlKoyXgZg,1090
8
- stringcalc-0.1.0.dev3.dist-info/WHEEL,sha256=rSgq_JpHF9fHR1lx53qwg_1-2LypZE_qmcuXbVUq948,81
9
- stringcalc-0.1.0.dev3.dist-info/METADATA,sha256=V0p8nNUAXUs_hvMhwDZgXWhRjYEIWfnJV03Uw0mjaSc,4905
10
- stringcalc-0.1.0.dev3.dist-info/RECORD,,