Skip to content

Commit

Permalink
Merge pull request #220 from mhvk/phase-str-scalar
Browse files Browse the repository at this point in the history
Also fix __str__ for scalar case
  • Loading branch information
mhvk authored Mar 13, 2021
2 parents f0f08cc + a67ca1f commit 09112e1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion baseband_tasks/phases/phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ def __str__(self):
# Override Angle, since one cannot override the formatter for
# structured dtype in array2string.
def formatter(x):
return x.view(self.dtype).to_string()
# [...] and view as self.__class__ are needed for scalar case.
return x[...].view(self.dtype, self.__class__).to_string()

return np.array2string(self.view(f"V{self.dtype.itemsize}"),
formatter={'all': formatter}) + self._unitstr
Expand Down
10 changes: 6 additions & 4 deletions baseband_tasks/tests/test_phase_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,15 @@ def test_from_string_invalid(self, item):
with pytest.raises(ValueError):
Phase.from_string(item)

@pytest.mark.parametrize('item', (0, (), (0, 0)))
@pytest.mark.parametrize('imag', (True, False))
def test_str_works(self, imag):
str(self.phase * 1j if imag else self.phase)
def test_str_works(self, imag, item):
str((self.phase * 1j if imag else self.phase)[item])

@pytest.mark.parametrize('item', (0, (), (0, 0)))
@pytest.mark.parametrize('imag', (True, False))
def test_repr_works(self, imag):
repr(self.phase * 1j if imag else self.phase)
def test_repr_works(self, imag, item):
repr((self.phase * 1j if imag else self.phase)[item])

@pytest.mark.parametrize('imag', (True, False))
def test_str(self, imag):
Expand Down

0 comments on commit 09112e1

Please sign in to comment.