Created
November 25, 2019 21:25
-
-
Save formigone/600884d6912c484f218f1c5661b93309 to your computer and use it in GitHub Desktop.
A poor man's dataclass not using @DataClass for backwards compat with Python 3.6, based on namedtuples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
_Activity = namedtuple('Activity', ['activityId', 'studentId', 'timeRead']) | |
_Activity.__new__.__defaults__ = (None, None, 0.0) | |
class Activity(_Activity): | |
@staticmethod | |
def from_dict(args): | |
args = {k: v for k, v in args.items() if k in _Activity._fields} | |
return Activity(**args) | |
def as_seconds(self, attr) -> float: | |
return self.__getattribute__(attr) / 1000 / 60 | |
act = { | |
'xactivityId': 'ACT001', | |
'xstudentId': 'STU001', | |
'createdAt': 1234566, | |
} | |
item = Activity.from_dict(act) | |
print(item) | |
item = item._replace(timeRead=3105111) | |
print('---') | |
print(item) | |
print(item.as_seconds('timeRead')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment