some cleanup

This commit is contained in:
NichtJens
2021-06-01 09:30:43 +02:00
parent 650f7be38d
commit f36e2534c4
2 changed files with 11 additions and 12 deletions

View File

@ -1,5 +1,6 @@
import functools
from typing import get_type_hints
from .utils import typename
def typechecked(func):
@ -13,7 +14,7 @@ def typechecked(func):
return typechecked_call
def property_maker(cls, name, type_name):
def property_maker(name, type_name):
storage_name = '_' + name
@property
@ -31,6 +32,7 @@ def property_maker(cls, name, type_name):
class Snippet:
def __init__(self, **kwargs):
super().__init__()
self._properties = []
self.set_properties(**kwargs)
@ -38,12 +40,12 @@ class Snippet:
for key, value in kwargs.items():
storage_name = '_' + key
setattr(Snippet, storage_name, None)
setattr(Snippet, key, property_maker(self, key, value))
setattr(Snippet, key, property_maker(key, value))
self._properties.append(key)
def to_dict(self, include_none=True):
if include_none:
return {key: getattr(self, key) for key in self._properties }
return {key: getattr(self, key) for key in self._properties}
else:
return {key: getattr(self, key) for key in self._properties if getattr(self, key) is not None}
@ -53,16 +55,16 @@ class Snippet:
@classmethod
def from_dict(cls, properties):
tmp = cls()
tmp.import_dict(properties)
return tmp
new = cls()
new.import_dict(properties)
return new
def __str__(self):
return f"{type(self).__name__}"
return typename(self)
@classmethod
def from_http_response(cls, response):
if type(response) == list:
if isinstance(response, list):
return [cls.from_dict(resp) for resp in response]
else:
return cls.from_dict(response)