|
|
@ -1,7 +1,13 @@ |
|
|
|
from inspect import signature, Parameter |
|
|
|
from inspect import signature, Parameter |
|
|
|
from functools import wraps |
|
|
|
from functools import wraps |
|
|
|
|
|
|
|
from typing import Optional |
|
|
|
|
|
|
|
from re import match |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_field_private(field_name: str) -> Optional[str]: |
|
|
|
|
|
|
|
m = match(r'_[^(__)]+__', field_name) |
|
|
|
|
|
|
|
return m is not None |
|
|
|
|
|
|
|
|
|
|
|
def inited_params(target_init): |
|
|
|
def inited_params(target_init): |
|
|
|
target_params = signature(target_init).parameters.values() |
|
|
|
target_params = signature(target_init).parameters.values() |
|
|
|
if len(target_params) < 1: |
|
|
|
if len(target_params) < 1: |
|
|
@ -34,17 +40,15 @@ def JSONClass(target_class): |
|
|
|
returns a json representation of the class |
|
|
|
returns a json representation of the class |
|
|
|
where all None - values and private fileds are skipped |
|
|
|
where all None - values and private fileds are skipped |
|
|
|
""" |
|
|
|
""" |
|
|
|
private_prefix = '__' |
|
|
|
return { |
|
|
|
result = { |
|
|
|
|
|
|
|
k: v for k, v in self.__dict__.items() |
|
|
|
k: v for k, v in self.__dict__.items() |
|
|
|
if v is not None and not k.startswith(private_prefix) |
|
|
|
if v is not None and not is_field_private(k) |
|
|
|
} |
|
|
|
} |
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def from_json(json_object: dict) -> target_class: |
|
|
|
def from_json(json_object: dict) -> target_class: |
|
|
|
return target_class(**json_object) |
|
|
|
return target_class(**json_object) |
|
|
|
|
|
|
|
|
|
|
|
target_class.__init__ = inited_params(target_class.__init__) |
|
|
|
# target_class.__init__ = inited_params(target_class.__init__) |
|
|
|
target_class.to_json = to_json |
|
|
|
target_class.to_json = to_json |
|
|
|
target_class.from_json = from_json |
|
|
|
target_class.from_json = from_json |
|
|
|
return target_class |
|
|
|
return target_class |
|
|
|