Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.
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
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
test = pytest

[tool:pytest]
automark_dependency = true
addopts = -ra
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
'pytest>=4',
'pytest-runner>=4',
'pytest-cov>=2',
'pytest-dependency @ https://github.com/SelfHacked/pytest-dependency/archive/master.zip',
'cached-property',
] + extra_sql
extra_dev = extra_all + extra_test
Expand Down
25 changes: 23 additions & 2 deletions stream/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ def stream(self) -> _Stream[_typing.AnyStr]:
raise self.NotSupported
return _IterStream(self)

def input(self, lines: _typing.Iterable[_typing.AnyStr]) -> None:
"""
Write a sequence of lines.
Line endings are added.
"""
for line in lines:
self.write(line)
self.write(self.newline_str)

class SameFile(ValueError):
pass

Expand Down Expand Up @@ -107,6 +116,10 @@ def _read_character(self) -> Character:
def newline(self) -> Character:
raise NotImplementedError # pragma: no cover

@property
def newline_str(self) -> _typing.AnyStr:
raise NotImplementedError # pragma: no cover

def _read_iter(
self,
*,
Expand Down Expand Up @@ -204,7 +217,11 @@ def flush(self) -> None:

class TextFile(File[str], _typing.TextIO):
@property
def newline(self) -> Character:
def newline(self) -> str:
return '\n'

@property
def newline_str(self) -> str:
return '\n'

def _read(
Expand Down Expand Up @@ -240,9 +257,13 @@ def newlines(self) -> _typing.Union[str, _typing.Tuple[str, ...], None]:

class BinaryFile(File[bytes], _typing.BinaryIO):
@property
def newline(self) -> Character:
def newline(self) -> int:
return 10 # b'\n'

@property
def newline_str(self) -> bytes:
return b'\n'

def _read(
self,
*,
Expand Down
14 changes: 14 additions & 0 deletions stream/io/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ def path(self) -> str:
def text(self) -> bool:
return 'b' not in self.__mode

@cached_property
def newline(self):
if self.text:
return '\n'
else:
return 10

@cached_property
def newline_str(self):
if self.text:
return '\n'
else:
return b'\n'

def __eq__(self, other: _File):
if not isinstance(other, LocalFile):
return False
Expand Down
5 changes: 5 additions & 0 deletions stream/io/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ def __eq__(self, other):
def __exit__(self, exc_type, exc_val, exc_tb):
# don't close
pass


stdin = StdIn()
stdout = StdOut()
stderr = StdErr()
1 change: 0 additions & 1 deletion stream/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ def assert_time(
yield
finally:
t = _time() - start
print(t)
assert -margin <= t - expected_time <= margin
7 changes: 0 additions & 7 deletions tests/functions/control/test_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from time import sleep

import pytest

from stream.functions.control import Preload
from stream.util.testing import assert_time

Expand All @@ -11,11 +9,6 @@ def dummy_iterable():
yield 0


@pytest.mark.dependency(
depends=[
('session', 'tests/util/test_testing.py::test_assert_time'),
],
)
def test_preload_all():
with assert_time(0.1):
iterator = Preload(n=None)(dummy_iterable())
Expand Down
15 changes: 0 additions & 15 deletions tests/functions/control/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,14 @@ def _test_prefetch(prefetch):
next(iterator)


@pytest.mark.dependency(
depends=[
('session', 'tests/util/test_testing.py::test_assert_time'),
],
)
def test_prefetch_one():
_test_prefetch(Prefetch())


@pytest.mark.dependency(
depends=[
('session', 'tests/util/test_testing.py::test_assert_time'),
],
)
def test_prefetch_all():
_test_prefetch(Prefetch(n=None))


@pytest.mark.dependency(
depends=[
('session', 'tests/util/test_testing.py::test_assert_time'),
],
)
def test_timeout():
iterator = Prefetch(n=None, timeout=1)(dummy_iterable())
tuple(iterator)
Expand Down
5 changes: 0 additions & 5 deletions tests/functions/test_filter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import pytest

from stream.functions.filter import Filter, remove_empty


Expand All @@ -8,9 +6,6 @@ def test_filter():
assert tuple(f(range(10))) == tuple(range(0, 10, 2))


@pytest.mark.dependency(
depends=['test_filter'],
)
def test_filter_logical():
f1 = Filter(lambda x: x % 2 == 0)
f2 = Filter(lambda x: x % 3 == 0)
Expand Down
9 changes: 0 additions & 9 deletions tests/functions/test_unzip.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import gzip

import pytest

from stream.functions.bytes import un_gzip
from stream.io.local import LocalFile


@pytest.mark.dependency(
scope='session',
depends=[
'tests/io/test_base.py::test_stream',
'tests/test_operators.py::test_or',
],
)
def test_un_gzip(tmpdir):
file = str(tmpdir / '0.txt.gz')
with gzip.open(file, mode='wb') as f:
Expand Down
16 changes: 9 additions & 7 deletions tests/io/test_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from stream.io import BinaryFile, TextFile
from .util import depends_with


class DummyFile(BinaryFile):
Expand Down Expand Up @@ -78,7 +77,6 @@ def test_with():
pass


@depends_with()
def test_read():
with DummyFile() as f:
assert f.read(0) == b''
Expand All @@ -87,7 +85,6 @@ def test_read():
assert f.read() == b'45\n\n6\n'


@depends_with()
def test_readline():
with DummyFile() as f:
assert f.readline(0) == b''
Expand All @@ -96,7 +93,6 @@ def test_readline():
assert f.readline(100) == b'45\n'


@depends_with()
def test_readlines():
with DummyFile() as f:
assert f.readlines(0) == []
Expand All @@ -105,21 +101,27 @@ def test_readlines():
assert f.readlines() == [b'6\n']


@depends_with()
def test_iter():
with DummyFile() as f:
assert next(f) == b'123\n'
assert tuple(f) == (b'45\n', b'\n', b'6\n')


@depends_with()
def test_stream():
with DummyTextFile() as f:
assert tuple(f.stream) == ('123\n', 'abc\n')


@depends_with()
def test_stream_not_readable():
with DummyNotReadable() as f:
with pytest.raises(DummyNotReadable.NotSupported):
f.stream


def test_newline():
bin = DummyFile()
assert bin.newline == 10
assert bin.newline_str == b'\n'
txt = DummyTextFile()
assert txt.newline == '\n'
assert txt.newline_str == '\n'
6 changes: 0 additions & 6 deletions tests/io/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

from stream.io.local import LocalFile
from stream.io.std import StdIn, StdOut, StdErr
from .util import depends_with

txt = """123
abc
"""


@depends_with()
def test_copy_local(tmpdir):
file = tmpdir / '0.txt'
file.write_text(txt, encoding='utf-8')
Expand All @@ -24,7 +22,6 @@ def test_copy_local(tmpdir):
assert file2.read_text(encoding='utf-8') == txt


@depends_with()
def test_same_file(tmpdir):
file = tmpdir / '0.txt'
file.write_text(txt, encoding='utf-8')
Expand All @@ -35,7 +32,6 @@ def test_same_file(tmpdir):
f1.copy_to(f2)


@depends_with()
def test_from_stdin(tmpdir, monkeypatch):
monkeypatch.setattr('sys.stdin', StringIO(txt))
file = tmpdir / '0.txt'
Expand All @@ -47,7 +43,6 @@ def test_from_stdin(tmpdir, monkeypatch):
assert file.read_text(encoding='utf-8') == txt


@depends_with()
def test_to_stdout(tmpdir, capsys):
file = tmpdir / '0.txt'
file.write_text(txt, encoding='utf-8')
Expand All @@ -61,7 +56,6 @@ def test_to_stdout(tmpdir, capsys):
assert captured.err == ''


@depends_with()
def test_to_stderr(tmpdir, capsys):
file = tmpdir / '0.txt'
file.write_text(txt, encoding='utf-8')
Expand Down
13 changes: 0 additions & 13 deletions tests/io/test_iterable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest

from stream.io.iterable import IterableFile
from .util import depends_with


def get() -> IterableFile:
Expand All @@ -14,7 +13,6 @@ def get() -> IterableFile:
"""])


@depends_with()
def test_readonly():
with get() as f:
assert f.readable()
Expand All @@ -27,9 +25,6 @@ def test_readonly():
f.flush()


@depends_with(
('session', 'tests/io/test_base.py::test_read'),
)
def test_tell():
with get() as f:
assert f.tell() == 0
Expand All @@ -41,15 +36,13 @@ def test_tell():
assert f.tell() == 10


@depends_with()
def test_seekable():
with get() as f:
assert f.seekable()
with pytest.raises(OSError):
f.truncate()


@depends_with()
def test_seek_forward_only():
with get() as f:
with pytest.raises(OSError):
Expand All @@ -61,10 +54,6 @@ def test_seek_forward_only():
f.seek(-1, 1)


@depends_with(
('session', 'tests/io/test_base.py::test_read'),
'test_tell',
)
def test_seek():
with get() as f:
f.seek(2)
Expand All @@ -75,7 +64,6 @@ def test_seek():
assert f.read(1) == b'5'


@depends_with()
def test_os():
with get() as f:
assert f.mode == 'rb'
Expand All @@ -85,7 +73,6 @@ def test_os():
assert not f.isatty()


@depends_with()
def test_eq():
with get() as f1, get() as f2:
assert f1 != f2
Loading