Skip to content

Commit

Permalink
Merge pull request #6 from amirsoroush/using-linked-list
Browse files Browse the repository at this point in the history
Using linked list for ListStrategy
  • Loading branch information
hamidrabedi authored Apr 3, 2024
2 parents f8ec7e3 + 6933355 commit c775902
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 26 deletions.
1 change: 0 additions & 1 deletion pyinmem/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from .core import PyInMemStore


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

Expand Down
2 changes: 2 additions & 0 deletions pyinmem/strategy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
from .set import SetStrategy
from .sorted_set import SortedSetStrategy
from .string import StringStrategy

__all__ = ("ListStrategy", "SetStrategy", "SortedSetStrategy", "StringStrategy")
5 changes: 2 additions & 3 deletions pyinmem/strategy/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from abc import ABC, abstractmethod
from typing import Any


class DataTypeStrategy(ABC):
Expand All @@ -8,8 +7,8 @@ class DataTypeStrategy(ABC):
"""

@abstractmethod
def is_valid_type(value: Any) -> bool:
"""An abstract method to determine if the data type is valid"""
def is_valid_type(self, value: object) -> bool:
"""Determine if the data type is valid"""

@classmethod
def supports_operation(cls, operation: str) -> bool:
Expand Down
29 changes: 16 additions & 13 deletions pyinmem/strategy/list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import deque
from typing import Any, Dict

from .base import DataTypeStrategy
Expand All @@ -6,29 +7,31 @@
class ListStrategy(DataTypeStrategy):
"""Strategy for handling list data types"""

@classmethod
def is_valid_type(cls, value: Any) -> bool:
def is_valid_type(self, value: Any) -> bool:
"""Check if the given value is a list."""
return isinstance(value, list)
return isinstance(value, deque)

def ensure_list(self, store: Dict[str, Any], key: str) -> None:
def ensure_linked_list(self, store: Dict[str, Any], key: str) -> None:
"""Ensure the value for the given key is a list in the store."""
if key not in store or not isinstance(store[key], list):
store[key] = []
if key not in store or not isinstance(store[key], deque):
store[key] = deque()

def lpush(self, store: Dict[str, Any], key: str, value: Any) -> int:
"""Push a value to the beginning of the list at the given key."""
self.ensure_list(store, key)
store[key].insert(0, value)
return len(store[key])
self.ensure_linked_list(store, key)
linked_list: deque = store[key]
linked_list.appendleft(value)
return len(linked_list)

def rpop(self, store: Dict[str, Any], key: str) -> Any:
"""Pop a value from the end of the list at the given key."""
self.ensure_list(store, key)
if store[key]:
return store[key].pop()
self.ensure_linked_list(store, key)
linked_list: deque = store[key]
if linked_list:
return linked_list.pop()
return None

def llen(self, store: Dict[str, Any], key: str) -> int:
"""Return the length of the list at the given key."""
self.ensure_list(store, key)
self.ensure_linked_list(store, key)
return len(store[key])
3 changes: 1 addition & 2 deletions pyinmem/strategy/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
class SetStrategy(DataTypeStrategy):
"""Strategy for handling set data types in PyInMemStore."""

@classmethod
def is_valid_type(cls, value: Any) -> bool:
def is_valid_type(self, value: Any) -> bool:
"""Check if the given value is a set."""
return isinstance(value, set)

Expand Down
3 changes: 1 addition & 2 deletions pyinmem/strategy/sorted_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
class SortedSetStrategy(DataTypeStrategy):
"""Strategy for handling sorted set data types in PyInMemStore."""

@classmethod
def is_valid_type(cls, value: Any) -> bool:
def is_valid_type(self, value: Any) -> bool:
"""Check if the given value is suitable for a sorted set."""
return isinstance(value, dict)

Expand Down
3 changes: 1 addition & 2 deletions pyinmem/strategy/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
class StringStrategy(DataTypeStrategy):
"""Strategy for handling string data types in PyInMemStore."""

@classmethod
def is_valid_type(cls, value: Any) -> bool:
def is_valid_type(self, value: Any) -> bool:
"""Check if the given value is a string."""
return isinstance(value, str)
7 changes: 4 additions & 3 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import time
from collections import deque

from pyinmem.core import PyInMemStore
from pyinmem.strategy import SortedSetStrategy
Expand All @@ -20,9 +21,9 @@ def test_list_strategy():
value = "item1"
store.lpush(key, value)
assert store.llen(key) == 1
assert store.get(key) == [value]
assert store.get(key) == deque([value])
assert store.rpop(key) == value
assert store.get(key) == []
assert store.get(key) == deque()


def test_string_operations():
Expand All @@ -45,7 +46,7 @@ def test_list_operations():
for v in values:
store.lpush(key, v)

assert store.get(key) == list(reversed(values))
assert store.get(key) == deque(reversed(values))
assert store.rpop(key) == values[0]
assert store.llen(key) == 1

Expand Down

0 comments on commit c775902

Please sign in to comment.