Skip to content

Commit

Permalink
#73 - Refactoring code and unit tests WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce Fenske committed Aug 6, 2022
1 parent 4942c55 commit 0cd1751
Show file tree
Hide file tree
Showing 9 changed files with 1,119 additions and 1,304 deletions.
22 changes: 19 additions & 3 deletions py_linq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,32 @@ def __next__(self):
return self.__current.value

@property
def current(self) -> Optional[Any]:
return self.__current.value if self.__current is not None else None
def current(self) -> Optional[TNode]:
return self.__current

@current.setter
def current(self, node: TNode) -> None:
self.__current = node

@property
def head(self) -> Optional[TNode]:
return self.__root

@head.setter
def head(self, node: TNode) -> None:
self.__root = node

@property
def tail(self) -> Optional[TNode]:
return self.__tail

@tail.setter
def tail(self, node: TNode) -> None:
self.__tail = node

def next(self):
return self.__next(self)
return self.__next__()

@property
def previous(self):
return self.__current.previous
42 changes: 5 additions & 37 deletions py_linq/py_linq.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ def skip(self, n):
:param n: Number of elements to skip as int
:return: new Enumerable object
"""
return SkipEnumerable(Enumerable(iter(self)), n)
return Enumerable(data=itertools.islice(self, n, None, 1))

def take(self, n):
"""
Return new Enumerable where first n elements are taken
:param n: Number of elements to take
:return: new Enumerable object
"""
return TakeEnumerable(Enumerable(iter(self)), n)
return Enumerable(data=itertools.islice(self, 0, n, 1))

def where(self, predicate):
"""
Expand Down Expand Up @@ -580,7 +580,7 @@ def reverse(self):
Inverts the order of the elements in a sequence
:return: Enumerable with elements in reversed order
"""
return ReversedEnumerable(Enumerable(iter(self)))
return Enumerable(data=reversed(self))

def skip_last(self, n):
"""
Expand Down Expand Up @@ -641,21 +641,6 @@ def zip(self, enumerable, func=lambda x: x):
return ZipEnumerable(Enumerable(iter(self)), enumerable, func)


class SkipEnumerable(Enumerable):
"""
Class to hold state for skipping elements in a collection
"""

def __init__(self, enumerable, n):
super(SkipEnumerable, self).__init__(enumerable)
self.n = n

def __iter__(self):
for index, element in enumerate(self._iterable):
if index >= self.n:
yield element


class SelectManyEnumerable(Enumerable):
"""
Class to hold state for flattening nested collections within a collection
Expand Down Expand Up @@ -713,22 +698,6 @@ def __iter__(self):
return itertools.takewhile(self.predicate, self._iterable)


class ReversedEnumerable(Enumerable):
"""
Class to hold state for reversing elements in a collection
"""

def __init__(self, enumerable):
super(ReversedEnumerable, self).__init__(enumerable)

def __iter__(self):
stack = LifoQueue()
for element in self._iterable:
stack.put(element)
while not stack.empty():
yield stack.get()


class IntersectEnumerable(Enumerable):
"""
Class to hold state for determining the intersection between two sets
Expand Down Expand Up @@ -851,7 +820,7 @@ def add(self, item):


class SortedEnumerable(Enumerable):
def __init__(self, enumerable, key_funcs):
def __init__(self, data: Iterable, key_funcs):
"""
Constructor
:param key_funcs: list of OrderingDirection instances in order of primary key --> less important keys
Expand All @@ -862,9 +831,8 @@ def __init__(self, enumerable, key_funcs):
if not isinstance(key_funcs, list):
raise TypeError(u"key_funcs should be a list instance")
self._key_funcs = [f for f in key_funcs if isinstance(f, OrderingDirection)]
data = enumerable
for o in reversed(self._key_funcs):
data = sorted(iter(data), key=o.key, reverse=o.descending)
data = sorted(data, key=o.key, reverse=o.descending)
super(SortedEnumerable, self).__init__(data)

def __getitem__(self, n):
Expand Down
18 changes: 0 additions & 18 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
_empty = []
_simple = [1, 2, 3]
_complex = [{"value": 1}, {"value": 2}, {"value": 3}]

_locations = [
("Scotland", "Edinburgh", "Branch1", 20000),
("Scotland", "Glasgow", "Branch1", 12500),
("Scotland", "Glasgow", "Branch2", 12000),
("Wales", "Cardiff", "Branch1", 29700),
("Wales", "Cardiff", "Branch2", 30000),
("Wales", "Bangor", "Branch1", 12800),
("England", "London", "Branch1", 90000),
("England", "London", "Branch2", 80000),
("England", "London", "Branch3", 70000),
("England", "Manchester", "Branch1", 45600),
("England", "Manchester", "Branch2", 50000),
("England", "Liverpool", "Branch1", 29700),
("England", "Liverpool", "Branch2", 25000),
]
42 changes: 42 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from py_linq import Enumerable
import pytest

_simple = [1, 2, 3]
_complex = [{"value": 1}, {"value": 2}, {"value": 3}]

_locations = [
("Scotland", "Edinburgh", "Branch1", 20000),
("Scotland", "Glasgow", "Branch1", 12500),
("Scotland", "Glasgow", "Branch2", 12000),
("Wales", "Cardiff", "Branch1", 29700),
("Wales", "Cardiff", "Branch2", 30000),
("Wales", "Bangor", "Branch1", 12800),
("England", "London", "Branch1", 90000),
("England", "London", "Branch2", 80000),
("England", "London", "Branch3", 70000),
("England", "Manchester", "Branch1", 45600),
("England", "Manchester", "Branch2", 50000),
("England", "Liverpool", "Branch1", 29700),
("England", "Liverpool", "Branch2", 25000),
]

@pytest.fixture
def empty() -> Enumerable:
return Enumerable([])


@pytest.fixture
def simple() -> Enumerable:
return Enumerable(_simple)


@pytest.fixture
def complex() -> Enumerable:
return Enumerable(_complex)


@pytest.fixture
def locations() -> Enumerable:
return Enumerable(_locations)


24 changes: 0 additions & 24 deletions tests/test_constructor.py

This file was deleted.

Loading

0 comments on commit 0cd1751

Please sign in to comment.