Skip to content

Commit

Permalink
feat: Add contains()
Browse files Browse the repository at this point in the history
  • Loading branch information
T-baby committed Nov 22, 2022
1 parent c8ae075 commit d41cbaa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions pond-stubs/pond_class.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Pond:
def clear(self, factory: Optional[PooledObjectFactory] = ..., name: Optional[str] = ...) -> None: ...
def size(self) -> int: ...
def count_total_objects(self) -> int: ...
def contains(self, factory: Optional[PooledObjectFactory] = ..., name: Optional[str] = ...) -> bool: ...
def pooled_object_size(self, factory: Optional[PooledObjectFactory] = ..., name: Optional[str] = ...) -> int: ...
def is_full(self, factory: Optional[PooledObjectFactory] = ..., name: Optional[str] = ...) -> bool: ...
def is_empty(self, factory: Optional[PooledObjectFactory] = ..., name: Optional[str] = ...) -> bool: ...
Expand Down
32 changes: 24 additions & 8 deletions pond/pond_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
from asyncio import AbstractEventLoop
from queue import Queue
from threading import Thread
from typing import (TYPE_CHECKING, Any, Coroutine, Dict, Final,
Optional)
from typing import TYPE_CHECKING, Any, Coroutine, Dict, Final, Optional

from .count_min_sketch import CountMinSketch
from .pooled_object import PooledObject
Expand Down Expand Up @@ -215,14 +214,31 @@ def count_total_objects(self) -> int:
total_number = total_number + v.qsize()
return total_number

def contains(
self, factory: Optional[PooledObjectFactory] = None, name: Optional[str] = None
) -> bool:
"""Return true if the specified factory has been registered in the pond.
Args:
factory (Optional[PooledObjectFactory], optional): The specified factory object. Defaults to None.
name (Optional[str], optional): The specified factory name. Defaults to None.
Returns:
bool: Return true if the specified factory has been registered in the pond.
"""
if name is None:
assert factory is not None
name = factory.factory_name()
return self.__pooled_object_tree.__contains__(name)

def pooled_object_size(
self, factory: Optional[PooledObjectFactory] = None, name: Optional[str] = None
) -> int:
"""Query how many objects there are in the specified object pool.
Args:
factory (Optional[PooledObjectFactory], optional): _description_. Defaults to None.
name (Optional[str], optional): _description_. Defaults to None.
factory (Optional[PooledObjectFactory], optional): The specified factory object. Defaults to None.
name (Optional[str], optional): The specified factory name. Defaults to None.
Returns:
int: Size of the specified object pool.
Expand All @@ -238,8 +254,8 @@ def is_full(
"""Check to see if the supplied object pool is filled.
Args:
factory (Optional[PooledObjectFactory], optional): _description_. Defaults to None.
name (Optional[str], optional): _description_. Defaults to None.
factory (Optional[PooledObjectFactory], optional): The specified factory object. Defaults to None.
name (Optional[str], optional): The specified factory name. Defaults to None.
Returns:
bool: Whether the object pool is full.
Expand All @@ -255,8 +271,8 @@ def is_empty(
"""Check to see if the supplied object pool is emptied.
Args:
factory (Optional[PooledObjectFactory], optional): _description_. Defaults to None.
name (Optional[str], optional): _description_. Defaults to None.
factory (Optional[PooledObjectFactory], optional): The specified factory object. Defaults to None.
name (Optional[str], optional): The specified factory name. Defaults to None.
Returns:
bool: Whether the object pool is empty.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license = {file = "LICENSE"}
name = "pondpond"
readme = "README.md"
requires-python = ">=3.8"
version = "1.2.0"
version = "1.2.1"

[project.urls]
"Blog" = "https://qin.news"
Expand Down
6 changes: 6 additions & 0 deletions tests/test_pond.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def test_register_by_name() -> None:
assert pond.size() == 2


@pytest.mark.run(order=1)
def test_contains() -> None:
assert pond.contains(name="PuppyFactory")
assert pond.contains(name="Notfound") is False


@pytest.mark.run(order=2)
def test_pooled_object_size() -> None:
assert pond.pooled_object_size(factory) == pooled_maxsize
Expand Down

0 comments on commit d41cbaa

Please sign in to comment.