Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code smells and other issues #80

Merged
merged 2 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pystreamapi/_parallel/fork_and_join.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# pylint: disable=protected-access
import os

from typing import Callable, Any, Optional

from pystreamapi._parallel.parallelizer import Parallel, delayed
from joblib import delayed

from pystreamapi._itertools.tools import reduce
from pystreamapi._parallel.parallelizer import Parallel
from pystreamapi._streams.error.__error import ErrorHandler
from pystreamapi._streams.error.__levels import ErrorLevel
from pystreamapi._itertools.tools import reduce


class Parallelizer:
Expand Down
4 changes: 2 additions & 2 deletions pystreamapi/_parallel/parallelizer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from joblib import Parallel as _JoblibParallel, delayed # pylint: disable=unused-import
from joblib import Parallel as _JoblibParallel # pylint: disable=unused-import

from pystreamapi._streams.error.__error import ErrorHandler
from pystreamapi._streams.error.__levels import ErrorLevel
Expand All @@ -7,7 +7,7 @@
class Parallel:
"""Wrapper for joblib.Parallel supporting error handling"""

def __init__(self, n_jobs=-1, prefer="processes", handler: ErrorHandler=None):
def __init__(self, n_jobs=-1, prefer="processes", handler: ErrorHandler = None):
self.n_jobs = n_jobs
self.prefer = prefer
self.handler = handler
Expand Down
8 changes: 4 additions & 4 deletions pystreamapi/_streams/__parallel_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from functools import reduce as seq_reduce
from typing import Callable, Any, Iterable

from pystreamapi._streams.__base_stream import terminal
from pystreamapi._parallel.parallelizer import Parallel, delayed

from pystreamapi.__optional import Optional
from joblib import delayed

import pystreamapi._streams.__base_stream as stream
from pystreamapi.__optional import Optional
from pystreamapi._parallel.fork_and_join import Parallelizer
from pystreamapi._parallel.parallelizer import Parallel
from pystreamapi._streams.__base_stream import terminal

_identity_missing = object()

Expand Down
3 changes: 3 additions & 0 deletions tests/file_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OPEN = 'builtins.open'
PATH_EXISTS = 'os.path.exists'
PATH_ISFILE = 'os.path.isfile'
10 changes: 5 additions & 5 deletions tests/test_error_handler_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from parameterized import parameterized_class

from pystreamapi._streams.error.__levels import ErrorLevel
from pystreamapi._streams.__parallel_stream import ParallelStream
from pystreamapi._streams.__sequential_stream import SequentialStream
from pystreamapi._streams.error.__levels import ErrorLevel
from pystreamapi._streams.numeric.__parallel_numeric_stream import ParallelNumericStream
from pystreamapi._streams.numeric.__sequential_numeric_stream import SequentialNumericStream

Expand Down Expand Up @@ -95,12 +95,12 @@ def test_map_to_str_ignore(self):
.error_level(ErrorLevel.IGNORE).map_to_str().to_list()
self.assertListEqual(result, ["1", "2", "a"])

def peek_raise(self):
def test_peek_raise(self):
with self.assertRaises(ValueError):
self.stream([1, 2, 3, "a"])\
.error_level(ErrorLevel.RAISE).peek(int)
self.stream([1, 2, 3, "a"]) \
.error_level(ErrorLevel.RAISE).peek(int).to_list()

def peek_ignore(self):
def test_peek_ignore(self):
result = self.stream([1, 2, 3, "a"])\
.error_level(ErrorLevel.IGNORE).peek(int).to_list()
self.assertListEqual(result, [1, 2, 3, "a"])
Expand Down
19 changes: 10 additions & 9 deletions tests/test_json_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest import TestCase
from unittest.mock import patch, mock_open

from file_test import OPEN, PATH_EXISTS, PATH_ISFILE
from pystreamapi.loaders import json

file_content = """
Expand All @@ -22,9 +23,9 @@
class TestJsonLoader(TestCase):

def test_json_loader_from_file(self):
with (patch('builtins.open', mock_open(read_data=file_content)),
patch('os.path.exists', return_value=True),
patch('os.path.isfile', return_value=True)):
with (patch(OPEN, mock_open(read_data=file_content)),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = json('path/to/data.json')
self.assertEqual(len(data), 2)
self.assertEqual(data[0].attr1, 1)
Expand All @@ -35,16 +36,16 @@ def test_json_loader_from_file(self):
self.assertIsInstance(data[1].attr1, str)

def test_json_loader_is_iterable(self):
with (patch('builtins.open', mock_open(read_data=file_content)),
patch('os.path.exists', return_value=True),
patch('os.path.isfile', return_value=True)):
with (patch(OPEN, mock_open(read_data=file_content)),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = json('path/to/data.json')
self.assertEqual(len(list(iter(data))), 2)

def test_json_loader_with_empty_file(self):
with (patch('builtins.open', mock_open(read_data="")),
patch('os.path.exists', return_value=True),
patch('os.path.isfile', return_value=True)):
with (patch(OPEN, mock_open(read_data="")),
patch(PATH_EXISTS, return_value=True),
patch(PATH_ISFILE, return_value=True)):
data = json('path/to/data.json')
self.assertEqual(len(data), 0)

Expand Down
12 changes: 6 additions & 6 deletions tests/test_numeric_base_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_range(self):

def test_range_empty(self):
result = Stream([]).range()
self.assertEqual(result, None)
self.assertIsNone(result)

def test_range_negative(self):
result = Stream([-1, -2, -3, -4, -5]).range()
Expand All @@ -23,7 +23,7 @@ def test_interquartile_range(self):

def test_interquartile_range_empty(self):
result = Stream([]).interquartile_range()
self.assertEqual(result, None)
self.assertIsNone(result)

def test_interquartile_range_odd(self):
result = Stream([1, 2, 3, 4, 5, 6, 7, 8, 9]).interquartile_range()
Expand All @@ -39,15 +39,15 @@ def test_median_even(self):

def test_median_empty(self):
result = Stream([]).median()
self.assertEqual(result, None)
self.assertIsNone(result)

def test_first_quartile(self):
result = Stream([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).first_quartile()
self.assertEqual(result, 3)

def test_first_quartile_empty(self):
result = Stream([]).first_quartile()
self.assertEqual(result, None)
self.assertIsNone(result)

def test_first_quartile_odd(self):
result = Stream([1, 2, 3, 4, 5, 6, 7, 8, 9]).first_quartile()
Expand All @@ -59,7 +59,7 @@ def test_third_quartile(self):

def test_third_quartile_empty(self):
result = Stream([]).third_quartile()
self.assertEqual(result, None)
self.assertIsNone(result)

def test_third_quartile_odd(self):
result = Stream([1, 2, 3, 4, 5, 6, 7, 8, 9]).third_quartile()
Expand All @@ -75,7 +75,7 @@ def test_mode_multiple(self):

def test_mode_empty(self):
result = Stream([]).mode()
self.assertEqual(result, None)
self.assertIsNone(result)

def test_mode_negative(self):
result = Stream([-1, -2, -3, -3]).mode()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_numeric_stream_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_mean(self):

def test_mean_empty(self):
result = self.stream([]).mean()
self.assertEqual(result, None)
self.assertIsNone(result)

def test_mean_negative(self):
result = self.stream([-1, -2, -3, -4, -5]).mean()
Expand Down
12 changes: 6 additions & 6 deletions tests/test_stream_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from parameterized import parameterized_class

from pystreamapi.__optional import Optional
from pystreamapi._streams.__base_stream import BaseStream
from pystreamapi._streams.__parallel_stream import ParallelStream
from pystreamapi._streams.__sequential_stream import SequentialStream
from pystreamapi._streams.numeric.__numeric_base_stream import NumericBaseStream
from pystreamapi._streams.numeric.__parallel_numeric_stream import ParallelNumericStream
from pystreamapi._streams.numeric.__sequential_numeric_stream import SequentialNumericStream
from pystreamapi.__optional import Optional


@parameterized_class("stream", [
Expand Down Expand Up @@ -41,7 +41,7 @@ def test_map_to_int_empty(self):

def test_map_to_int_returns_numeric_stream(self):
result = self.stream(["1", "2", "3", "9"]).map_to_int()
self.assertTrue(isinstance(result, NumericBaseStream))
self.assertIsInstance(result, NumericBaseStream)

def test_map_to_float(self):
result = self.stream(["1", "2", "3", "9"]).map_to_float().to_list()
Expand All @@ -53,19 +53,19 @@ def test_map_to_float_empty(self):

def test_map_to_float_returns_numeric_stream(self):
result = self.stream(["1", "2", "3", "9"]).map_to_float()
self.assertTrue(isinstance(result, NumericBaseStream))
self.assertIsInstance(result, NumericBaseStream)

def test_map_to_str(self):
result = self.stream([1, 2, 3, 9]).map_to_str().to_list()
self.assertListEqual(result, ["1", "2", "3", "9"])

def test_convert_to_numeric_stream(self):
result = self.stream([1, 2, 3, 9]).numeric()
self.assertTrue(isinstance(result, NumericBaseStream))
self.assertIsInstance(result, NumericBaseStream)

def test_convert_to_numeric_stream_is_already_numeric(self):
result = self.stream([1.0, 2.0, 3.0, 9.0]).numeric()
self.assertTrue(isinstance(result, NumericBaseStream))
self.assertIsInstance(result, NumericBaseStream)

def test_flat_map(self):
result = self.stream([1, 2, 3, 9]).flat_map(lambda x: self.stream([x, x])).to_list()
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_filter_complex(self):
def test_filter_lazy(self):
result = self.stream([1, 2, 3]).filter(lambda x: x > 1)
self.assertListEqual(result.to_list(), [2, 3])
self.assertTrue(isinstance(result, BaseStream))
self.assertIsInstance(result, BaseStream)

def test_peek(self):
src = []
Expand Down
Loading