File tree Expand file tree Collapse file tree 6 files changed +66
-2
lines changed
Expand file tree Collapse file tree 6 files changed +66
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ Changelog = "https://github.com/dbatten5/arraytex/releases"
1919python = " ^3.8"
2020click = " >=8.0.1"
2121numpy = " ^1.24.2"
22+ pyperclip = " ^1.8.2"
2223
2324[tool .poetry .group .test .dependencies ]
2425pytest = " ^6.2.4"
Original file line number Diff line number Diff line change 99from numpy .typing import NDArray
1010
1111from .errors import TooManyDimensionsError
12+ from .utils import use_clipboard
1213
1314
15+ @use_clipboard
1416def 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
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 11"""Tests for the main API."""
2+ from unittest import mock
3+ from unittest .mock import MagicMock
4+
25import numpy as np
36import 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}\n 1 \\ \\ \n \\ end{bmatrix}"
156+ )
You can’t perform that action at this time.
0 commit comments