Skip to content

Commit

Permalink
using __slots__ and info from iterable
Browse files Browse the repository at this point in the history
Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Apr 5, 2024
1 parent 2edba94 commit e162b2b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_resource_creation_with_info_needs_proper_id(client):
with pytest.raises(AssertionError, match="must contain 'id'"):
_ = client.tickets(1, info={"noid": True})

with pytest.raises(AssertionError, match="be equal with rid"):
with pytest.raises(AssertionError, match="equal to rid"):
_ = client.tickets(1, info={"id": 2})


Expand Down
6 changes: 2 additions & 4 deletions zammadoo/articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __repr__(self):
@property
def size(self) -> int:
"""attachment size in bytes"""
return int(self._info["size"])
return int(self["size"])

@staticmethod
def info_from_files(*paths: "PathType"):
Expand Down Expand Up @@ -81,9 +81,7 @@ def info_from_files(*paths: "PathType"):
def _response(self) -> requests.Response:
response = self._client.response("GET", self.url, stream=True)
response.raise_for_status()

preferences = self._info.get("preferences", {})
response.encoding = preferences.get("Charset") or response.apparent_encoding
response.encoding = response.apparent_encoding

return response

Expand Down
8 changes: 5 additions & 3 deletions zammadoo/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing_extensions import Self

from .users import User
from .utils import JsonDict, JsonType
from .utils import JsonDict, JsonMapping, JsonType

class TypedResourceDict(JsonDict):
@overload
Expand All @@ -26,6 +26,8 @@ def __getitem__(self, item) -> "JsonType": ...


class Resource(FrozenInfo):
__slots__ = ("id", "url", "parent")

EXPANDED_ATTRIBUTES: Tuple[str, ...] = () #: :meta private:

id: int #:
Expand All @@ -36,12 +38,12 @@ def __init__(
parent: ResourcesT[Any],
rid: int,
*,
info: Optional["JsonDict"] = None,
info: Optional["JsonMapping"] = None,
) -> None:
self.id = rid
self.parent = parent
self.url = f"{parent.url}/{rid}"
super().__init__(info)
super().__init__(info or ())

def __repr__(self):
return f"<{self.__class__.__qualname__} {self.url!r}>"
Expand Down
13 changes: 8 additions & 5 deletions zammadoo/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

# noinspection PyUnresolvedReferences
from .resource import Resource
from .utils import JsonDict
from .utils import JsonDict, JsonMapping


_T_co = TypeVar("_T_co", bound="Resource", covariant=True)


class ResourcesT(Generic[_T_co]):
__slots__ = ("_client", "endpoint", "url", "cache")

_RESOURCE_TYPE: Type[_T_co]
DEFAULT_CACHE_SIZE = -1
"""
Expand All @@ -47,12 +49,13 @@ def __init__(self, client: "Client", endpoint: str):
max_size=self.DEFAULT_CACHE_SIZE
) #: resource LRU cache

def __call__(self, rid: int, *, info: Optional["JsonDict"] = None) -> _T_co:
def __call__(self, rid: int, *, info: Optional["JsonMapping"] = None) -> _T_co:
if info is not None:
mapping = dict(info)
assert (
info.get("id") == rid
), "parameter info must contain 'id' and be equal with rid"
self.cache[f"{self.url}/{rid}"] = dict(info)
mapping.get("id") == rid
), "parameter info must contain 'id' equal to rid"
self.cache[f"{self.url}/{rid}"] = mapping

return self._RESOURCE_TYPE(self, rid, info=info)

Expand Down
20 changes: 17 additions & 3 deletions zammadoo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@
from datetime import datetime
from itertools import chain
from types import MappingProxyType
from typing import Any, Dict, Generic, Iterable, List, Mapping, Optional, TypeVar, Union
from typing import (
Any,
Dict,
Generic,
Iterable,
List,
Mapping,
Optional,
Tuple,
TypeVar,
Union,
)

JsonType = Union[None, bool, int, float, str, List["JsonType"], "JsonDict"]
JsonDict = Dict[str, JsonType]
StringKeyMapping = Mapping[str, Any]
JsonMapping = Union[JsonDict, Iterable[Tuple[str, JsonType]]]


class YieldCounter:
Expand All @@ -30,11 +42,13 @@ def __call__(self, itr: Iterable[_T]) -> Iterable[_T]:


class FrozenInfo:
__slots__ = ("_info", "_frozen")

def __init__(
self,
info=None,
info: JsonMapping,
) -> None:
self._info = dict(info) if info is not None else {}
self._info = dict(info)
self._frozen = True

def __getattr__(self, name: str) -> "JsonType":
Expand Down

0 comments on commit e162b2b

Please sign in to comment.