Skip to content

Commit 4c5afee

Browse files
committed
add to_clp support
1 parent d6d0f2f commit 4c5afee

File tree

6 files changed

+66
-2
lines changed

6 files changed

+66
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Convert a `numpy.NDArray` to various LaTeX forms.
3434
\end{bmatrix}
3535
```
3636

37-
Inspired by @josephcslater's [array_to_latex](https://github.com/josephcslater/array_to_latex).
37+
Inspired by [@josephcslater](https://github.com/josephcslater)'s
38+
[array_to_latex](https://github.com/josephcslater/array_to_latex).
3839

3940
## Features
4041

poetry.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Changelog = "https://github.com/dbatten5/arraytex/releases"
1919
python = "^3.8"
2020
click = ">=8.0.1"
2121
numpy = "^1.24.2"
22+
pyperclip = "^1.8.2"
2223

2324
[tool.poetry.group.test.dependencies]
2425
pytest = "^6.2.4"

src/arraytex/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
from numpy.typing import NDArray
1010

1111
from .errors import TooManyDimensionsError
12+
from .utils import use_clipboard
1213

1314

15+
@use_clipboard
1416
def to_matrix(
1517
arr: NDArray[Any],
1618
style: str = "b",
1719
num_format: Optional[str] = None,
1820
scientific_notation: bool = False,
21+
to_clp: bool = False,
1922
) -> str:
2023
"""Convert a numpy.NDArray to LaTeX matrix.
2124
@@ -25,6 +28,7 @@ def to_matrix(
2528
num_format: a number formatter string, e.g. ".2f"
2629
scientific_notation: a flag to determine whether 1 x 10^3 should be used,
2730
otherwise e-notation is used (1e3)
31+
to_clp: copy the output to the system clipboard
2832
2933
Returns:
3034
the string representation of the array

src/arraytex/utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Utils module."""
2+
3+
from functools import wraps
4+
from typing import Any
5+
from typing import Callable
6+
from typing import TypeVar
7+
from typing import cast
8+
9+
import pyperclip
10+
11+
12+
F = TypeVar("F", bound=Callable[..., str])
13+
14+
15+
def use_clipboard(func: F) -> F:
16+
"""Augument decorated functions argument to copy the output to the clipboard."""
17+
18+
@wraps(func)
19+
def wrapper_func(*args: Any, **kwargs: Any) -> str:
20+
"""Wrapped function."""
21+
out = func(*args, **kwargs)
22+
23+
if kwargs.get("to_clp"):
24+
pyperclip.copy(out)
25+
print("ArrayTeX: copied to clipboard")
26+
27+
return out
28+
29+
return cast(F, wrapper_func)

tests/test_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
"""Tests for the main API."""
2+
from unittest import mock
3+
from unittest.mock import MagicMock
4+
25
import numpy as np
36
import pytest
47

@@ -136,3 +139,18 @@ def test_3_d(self) -> None:
136139

137140
with pytest.raises(TooManyDimensionsError):
138141
to_matrix(mat)
142+
143+
144+
class TestClipboard:
145+
"""Tests for the `to_clp` arg."""
146+
147+
@mock.patch("arraytex.utils.pyperclip", autospec=True)
148+
def test_success(self, mock_pyperclip: MagicMock) -> None:
149+
"""Outputs are copied to the clipboard."""
150+
mat = np.array(1)
151+
152+
to_matrix(mat, to_clp=True)
153+
154+
mock_pyperclip.copy.assert_called_once_with(
155+
"\\begin{bmatrix}\n1 \\\\\n\\end{bmatrix}"
156+
)

0 commit comments

Comments
 (0)