Using instance method in data class #60
Replies: 2 comments 3 replies
-
I think the approach used to init and assign The following is how
The same process above is loosely applied to
@classmethod
def from_elem(cls, elem):
cls.something = float(elem.find('something')) # float in the case is the expected type
...
# do ipf specific things
return (time_stamp, cls)
with open_method(noise_annotation_path, 'r') as f_nads:
ET.parse(f_nads).find('noiseVectorList')
noise_list = [BurstNoise.from_elem(e) for e in et]
Let me know what you think. |
Beta Was this translation helpful? Give feedback.
-
Sorry I went way off topic with my 1st response. I really appreciate the simple example in your follow up. It made perfectly clear the matter at hand. |
Beta Was this translation helpful? Give feedback.
-
Here is some context to understand the situation:
In the last PR #48, I've added few data classes into
Sentinel1BurstSlc
as its attributes (see below):https://github.com/opera-adt/s1-reader/blob/3c5ab381e647d9a93dc0924a49f460c411ddd3ef/src/s1reader/s1_burst_slc.py#L169-L172
They contain the burst-wise information, whose information are extracted subswath-wide annotation dataclasses (i.e.
CalibrationAnnotation
,NoiseAnnotation
, etc.) Below is the example forBurstCalibration.from_calibration_annotation()
.:https://github.com/opera-adt/s1-reader/blob/3c5ab381e647d9a93dc0924a49f460c411ddd3ef/src/s1reader/s1_annotation.py#L443-L444
As it can be shown above, the extractor has a decorator
@classmethod
Running s1reader.load_bursts() will give the list of
Sentinel1BurstSlc
instances. The information inside the attributes above has to be unique for each instances in the list, because each bursts have different parameters for noise correction, radiometric calibration, etc. However, making the extractor function as aclassmethod
make the values identical because all instances shares the attributes as class attributes.Such issue does not happen when the attributes are initialized when the instances are generated (see below). Same approaches can be borrowed for the burst-wise dataclass, but it will make the function
s1_reader.burst_from_xml()
too long.https://github.com/opera-adt/s1-reader/blob/3c5ab381e647d9a93dc0924a49f460c411ddd3ef/src/s1reader/s1_reader.py#L498-L510
One of the workaround I did in the PR #48 was to define the extractor as an instance method. Below is the example of
BurstNoise.from_noise_annotation()
as below:https://github.com/opera-adt/s1-reader/blob/3c5ab381e647d9a93dc0924a49f460c411ddd3ef/src/s1reader/s1_annotation.py#L367-L368
In that context, my questions are:
dataclass
, but is it a good practice?s1_reader.burst_from_xml()
simple?Beta Was this translation helpful? Give feedback.
All reactions