|
48 | 48 | } |
49 | 49 |
|
50 | 50 |
|
| 51 | +def _coerce_wi_int(value): |
| 52 | + if value is None or (isinstance(value, str) and not value.strip()): |
| 53 | + return None |
| 54 | + if isinstance(value, bool): |
| 55 | + return int(value) |
| 56 | + try: |
| 57 | + if pd.isna(value): |
| 58 | + return None |
| 59 | + except TypeError: |
| 60 | + pass |
| 61 | + try: |
| 62 | + return int(float(value)) |
| 63 | + except (TypeError, ValueError): |
| 64 | + return None |
| 65 | + |
| 66 | + |
51 | 67 | def _coerce_wi_mic_gain(value): |
52 | 68 | if value is None or (isinstance(value, str) and not value.strip()): |
53 | 69 | return None |
54 | 70 | if isinstance(value, str): |
55 | 71 | value = value.strip() |
56 | | - if pd.isna(value): |
57 | | - return None |
58 | | - if isinstance(value, bool): |
59 | | - return value |
| 72 | + try: |
| 73 | + if pd.isna(value): |
| 74 | + return None |
| 75 | + except TypeError: |
| 76 | + pass |
60 | 77 | try: |
61 | 78 | return bool(int(float(value))) |
62 | | - except (ValueError, TypeError): |
63 | | - lowered = str(value).strip().lower() |
64 | | - if lowered in {"true", "t", "yes", "y"}: |
65 | | - return True |
66 | | - if lowered in {"false", "f", "no", "n"}: |
67 | | - return False |
68 | | - return None |
| 79 | + except (TypeError, ValueError): |
| 80 | + return None |
69 | 81 |
|
70 | 82 |
|
71 | 83 | class SensorTransferer(ThingBasedTransferer): |
@@ -238,12 +250,12 @@ def _group_step(self, session: Session, row: pd.Series, db_item: Base): |
238 | 250 | hanging_cable_length=row.HangingCableLength, |
239 | 251 | hanging_point_height=row.HangingPointHgt, |
240 | 252 | hanging_point_description=row.HangingPointDescription, |
241 | | - nma_WI_Duration=row.WI_Duration, |
242 | | - nma_WI_EndFrequency=row.WI_EndFrequency, |
243 | | - nma_WI_Magnitude=row.WI_Magnitude, |
| 253 | + nma_WI_Duration=_coerce_wi_int(row.WI_Duration), |
| 254 | + nma_WI_EndFrequency=_coerce_wi_int(row.WI_EndFrequency), |
| 255 | + nma_WI_Magnitude=_coerce_wi_int(row.WI_Magnitude), |
244 | 256 | nma_WI_MicGain=_coerce_wi_mic_gain(row.WI_MicGain), |
245 | | - nma_WI_MinSoundDepth=row.WI_MinSoundDepth, |
246 | | - nma_WI_StartFrequency=row.WI_StartFrequency, |
| 257 | + nma_WI_MinSoundDepth=_coerce_wi_int(row.WI_MinSoundDepth), |
| 258 | + nma_WI_StartFrequency=_coerce_wi_int(row.WI_StartFrequency), |
247 | 259 | ) |
248 | 260 | session.add(deployment) |
249 | 261 | logger.info( |
|
0 commit comments