Skip to content

ucloud: Fix set/get attr compatibility issue with CPython/MicroPython. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions arduino_iot_cloud/ucloud.py
Original file line number Diff line number Diff line change
@@ -112,23 +112,28 @@ def value(self, value):
)
self._value = value

def __is_subrecord(self, attr):
return (hasattr(super(), '__dict__')
and isinstance(super().__dict__.get("_value", None), dict)
and attr in super().value)

def __getattr__(self, attr):
if isinstance(getattr(super(), "_value", None), dict) and attr in super().value:
if self.__is_subrecord(attr):
return super().value[attr].value
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{attr}'")

def __setattr__(self, name, value):
if isinstance(getattr(super(), "_value", None), dict) and name in super().value:
self.value[name].value = value
def __setattr__(self, attr, value):
if self.__is_subrecord(attr):
self.value[attr].value = value
else:
super().__setattr__(name, value)
super().__setattr__(attr, value)

def _build_rec_dict(self, naming_map, appendTo):
if isinstance(self.value, dict):
for r in self.value.values():
if r.value is not None: # NOTE: should filter by updated when it's supported.
r._build_rec_dict(naming_map, appendTo)
else:
elif self._value is not None:
super()._build_rec_dict(naming_map, appendTo)

def add_to_pack(self, pack):