fork download
  1. class CarModel:
  2. mapping = {}
  3.  
  4. @staticmethod
  5. def _key(key, mapping=mapping):
  6. def decorator(func):
  7. mapping[key] = func
  8. return decorator
  9.  
  10. @classmethod
  11. def from_dict(cls, d: dict):
  12. instance = cls()
  13. for key, value in d.items():
  14. cls.mapping[key](instance, value)
  15. return instance
  16.  
  17. def __init__(self):
  18. self.__wheel_count: int = 0
  19.  
  20. @_key('wheelCount')
  21. def set_wheel_count(self, value: int) -> None:
  22. self.__wheel_count = value
  23.  
  24. class TruckModel(CarModel):
  25. def __init__(self):
  26. super().__init__()
  27. self.__load_in_kg: int = 0
  28.  
  29. @CarModel._key('loadInKg')
  30. def set_load_in_kg(self, value: int) -> None:
  31. self.__load_in_kg= value
  32.  
  33. print(vars(CarModel.from_dict({'wheelCount': 4})))
  34. print(vars(TruckModel.from_dict({'wheelCount': 6, 'loadInKg': 8000})))
Runtime error #stdin #stdout #stderr 0.15s 25604KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
  File "./prog.py", line 20, in CarModel
TypeError: 'staticmethod' object is not callable