The attribute samples is a public attribute of the Spectrum class, thus it is possible to directly assign the value to this attribute without safety checks. The following code breaks the integrity of the spectrum object because the sample length becomes inconsistent with the number of spectral bins, and the memoryview spectrum.samples_mv no longer points to the spectrum.samples buffer.
import numpy as np
from raysect.optical import Spectrum
spectrum = Spectrum(300, 500, 200)
spectrum.samples = np.ones(100)
print(spectrum.bins)
I suggest making samples a property because in this case any direct assignments spectrum.samples = new_samples in the user code will continue to work.
An alternative solution would be making samples a read-only attribute. In this case, the users must replace spectrum.samples = new_samples with spectrum.samples[:] = new_samples in their code.
@CnlPepper, I can make a patch. What is the best solution in your opinion?
The attribute
samples
is a public attribute of theSpectrum
class, thus it is possible to directly assign the value to this attribute without safety checks. The following code breaks the integrity of thespectrum
object because the sample length becomes inconsistent with the number of spectral bins, and the memoryviewspectrum.samples_mv
no longer points to thespectrum.samples
buffer.I suggest making
samples
a property because in this case any direct assignmentsspectrum.samples = new_samples
in the user code will continue to work.An alternative solution would be making
samples
a read-only attribute. In this case, the users must replacespectrum.samples = new_samples
withspectrum.samples[:] = new_samples
in their code.@CnlPepper, I can make a patch. What is the best solution in your opinion?