stream2py 1.0.48__tar.gz → 1.0.49__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. {stream2py-1.0.48 → stream2py-1.0.49}/PKG-INFO +1 -1
  2. {stream2py-1.0.48 → stream2py-1.0.49}/pyproject.toml +1 -1
  3. stream2py-1.0.49/stream2py/examples/tag_events.py +119 -0
  4. stream2py-1.0.49/stream2py/tests/test_tag_events.py +38 -0
  5. {stream2py-1.0.48 → stream2py-1.0.49}/.gitattributes +0 -0
  6. {stream2py-1.0.48 → stream2py-1.0.49}/.github/workflows/ci.yml +0 -0
  7. {stream2py-1.0.48 → stream2py-1.0.49}/.gitignore +0 -0
  8. {stream2py-1.0.48 → stream2py-1.0.49}/ISSUE_ANALYSIS.md +0 -0
  9. {stream2py-1.0.48 → stream2py-1.0.49}/ISSUE_COMMENTS.md +0 -0
  10. {stream2py-1.0.48 → stream2py-1.0.49}/LICENSE +0 -0
  11. {stream2py-1.0.48 → stream2py-1.0.49}/PR_DESCRIPTION.md +0 -0
  12. {stream2py-1.0.48 → stream2py-1.0.49}/README.md +0 -0
  13. {stream2py-1.0.48 → stream2py-1.0.49}/README.rst +0 -0
  14. {stream2py-1.0.48 → stream2py-1.0.49}/RESOLVED_ISSUES.md +0 -0
  15. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/.gitignore +0 -0
  16. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/Makefile +0 -0
  17. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/conf.py +0 -0
  18. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/index.rst +0 -0
  19. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/make.bat +0 -0
  20. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/buffer_reader.rst +0 -0
  21. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/protocols.rst +0 -0
  22. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/simply.rst +0 -0
  23. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/source_reader.rst +0 -0
  24. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/sources/http.rst +0 -0
  25. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/sources.rst +0 -0
  26. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/stream_buffer.rst +0 -0
  27. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/stream_source.rst +0 -0
  28. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/tests/test_buffer_reader.rst +0 -0
  29. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/tests/test_lock_sorted_deque.rst +0 -0
  30. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/tests/test_source_reader.rst +0 -0
  31. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/tests/test_stream_buffer.rst +0 -0
  32. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/tests/test_stream_source.rst +0 -0
  33. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/tests/test_util.rst +0 -0
  34. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/tests/utils_for_testing.rst +0 -0
  35. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/util.rst +0 -0
  36. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/utility/locked_sorted_deque.rst +0 -0
  37. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/utility/reader_writer_lock.rst +0 -0
  38. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/utility/sorted_collection.rst +0 -0
  39. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/utility/sorted_deque.rst +0 -0
  40. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py/utility/typing_hints.rst +0 -0
  41. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/module_docs/stream2py.rst +0 -0
  42. {stream2py-1.0.48 → stream2py-1.0.49}/docsrc/table_of_contents.rst +0 -0
  43. {stream2py-1.0.48 → stream2py-1.0.49}/pack.py +0 -0
  44. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/__init__.py +0 -0
  45. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/buffer_reader.py +0 -0
  46. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/examples/source_reader.py +0 -0
  47. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/examples/stream_source.py +0 -0
  48. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/exceptions.py +0 -0
  49. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/protocols.py +0 -0
  50. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/simply.py +0 -0
  51. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/source_reader.py +0 -0
  52. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/sources/__init__.py +0 -0
  53. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/sources/http.py +0 -0
  54. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/stream_buffer.py +0 -0
  55. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/stream_source.py +0 -0
  56. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/__init__.py +0 -0
  57. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_buffer_reader.py +0 -0
  58. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_buffer_reader_blocking.py +0 -0
  59. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_buffer_reader_stopiteration.py +0 -0
  60. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_lock_sorted_deque.py +0 -0
  61. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_quick_source_reader.py +0 -0
  62. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_reader_without_context.py +0 -0
  63. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_source_reader.py +0 -0
  64. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_stream_buffer.py +0 -0
  65. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_stream_source.py +0 -0
  66. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_util.py +0 -0
  67. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/test_zz_range_join.py +0 -0
  68. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/tests/utils_for_testing.py +0 -0
  69. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/util.py +0 -0
  70. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/utility/__init__.py +0 -0
  71. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/utility/locked_sorted_deque.py +0 -0
  72. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/utility/reader_writer_lock.py +0 -0
  73. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/utility/sorted_collection.py +0 -0
  74. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/utility/sorted_deque.py +0 -0
  75. {stream2py-1.0.48 → stream2py-1.0.49}/stream2py/utility/typing_hints.py +0 -0
  76. {stream2py-1.0.48 → stream2py-1.0.49}/things_you_can_do.md +0 -0
  77. {stream2py-1.0.48 → stream2py-1.0.49}/use_cases.md +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: stream2py
3
- Version: 1.0.48
3
+ Version: 1.0.49
4
4
  Summary: Bring data streams to python with ease
5
5
  Project-URL: Homepage, https://github.com/i2mint/stream2py
6
6
  Project-URL: Documentation, https://i2mint.github.io/stream2py/
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
6
6
 
7
7
  [project]
8
8
  name = "stream2py"
9
- version = "1.0.48"
9
+ version = "1.0.49"
10
10
  description = "Bring data streams to python with ease"
11
11
  readme = "README.md"
12
12
  requires-python = ">=3.10"
@@ -0,0 +1,119 @@
1
+ """Tag events on a timeline with key presses, and save the timestamped tags.
2
+
3
+ The smallest useful version of "tag sound events": while audio plays (live or from a file,
4
+ played by whatever you like), you press a key per event type and each press is stored as
5
+ ``{"t": <seconds>, "tag": <name>, "key": <key>}`` next to the media it refers to.
6
+
7
+ The two things that vary are injectable, so the tagger works with any player or key source:
8
+
9
+ * ``position`` - ``() -> float``, seconds into the media at the moment of a press. Default:
10
+ wall-clock seconds since the tagger was created (right for live sources). With a player,
11
+ pass its current-position getter; with a ``stream2py`` buffer, a function of the latest
12
+ item's timestamp.
13
+ * the keys themselves - anything that yields them (:func:`tag_keys`): ``sys.stdin`` lines in
14
+ the bundled CLI, a GUI callback, a MIDI pad.
15
+
16
+ Run it (type a key, then Enter; empty input or ``q`` ends)::
17
+
18
+ python -m stream2py.examples.tag_events --media talk.wav --out talk.tags.json \\
19
+ --keymap a=applause --keymap l=laughter
20
+
21
+ >>> now = iter([0.0, 1.5, 4.0])
22
+ >>> tagger = EventTagger({"a": "applause", "l": "laughter"}, position=lambda: next(now))
23
+ >>> [tagger.press(k) for k in "al"] == tagger.events
24
+ True
25
+ >>> tagger.events[1]
26
+ TagEvent(t=1.5, tag='laughter', key='l')
27
+ >>> tagger.press("x") is None # a key without a tag is ignored
28
+ True
29
+ """
30
+
31
+ from __future__ import annotations
32
+
33
+ import argparse
34
+ import json
35
+ import sys
36
+ import time
37
+ from collections.abc import Callable, Iterable, Mapping
38
+ from dataclasses import asdict, dataclass, field
39
+ from pathlib import Path
40
+
41
+ __all__ = ["TagEvent", "EventTagger", "tag_keys", "main"]
42
+
43
+
44
+ @dataclass(frozen=True)
45
+ class TagEvent:
46
+ """One tagged moment: ``t`` seconds into the media."""
47
+
48
+ t: float
49
+ tag: str
50
+ key: str
51
+
52
+
53
+ def _wall_clock_position() -> Callable[[], float]:
54
+ start = time.monotonic()
55
+ return lambda: time.monotonic() - start
56
+
57
+
58
+ @dataclass
59
+ class EventTagger:
60
+ """Turn key presses into timestamped :class:`TagEvent`s (``keymap``: key -> tag name)."""
61
+
62
+ keymap: Mapping[str, str]
63
+ position: Callable[[], float] = field(default_factory=_wall_clock_position)
64
+ events: list[TagEvent] = field(default_factory=list)
65
+
66
+ def press(self, key: str) -> TagEvent | None:
67
+ """Record ``key``'s tag at the current position; ``None`` if the key has no tag."""
68
+ tag = self.keymap.get(key)
69
+ if tag is None:
70
+ return None
71
+ event = TagEvent(round(float(self.position()), 3), tag, key)
72
+ self.events.append(event)
73
+ return event
74
+
75
+ def to_dict(self, *, media: str | None = None) -> dict:
76
+ return {"media": media, "events": [asdict(e) for e in self.events]}
77
+
78
+ def save(self, path: str | Path, *, media: str | None = None) -> Path:
79
+ """Write the events (and the ``media`` they refer to) as JSON."""
80
+ path = Path(path)
81
+ path.write_text(json.dumps(self.to_dict(media=media), indent=2))
82
+ return path
83
+
84
+
85
+ def tag_keys(tagger: EventTagger, keys: Iterable[str]) -> list[TagEvent]:
86
+ """Feed ``keys`` to ``tagger``; return the events recorded for them."""
87
+ return [e for k in keys if (e := tagger.press(k)) is not None]
88
+
89
+
90
+ def _parse_keymap(pairs: Iterable[str]) -> dict[str, str]:
91
+ keymap = {}
92
+ for pair in pairs:
93
+ key, sep, tag = pair.partition("=")
94
+ if not (sep and key and tag):
95
+ raise argparse.ArgumentTypeError(f"expected KEY=TAG, got {pair!r}")
96
+ keymap[key] = tag
97
+ return keymap
98
+
99
+
100
+ def main(argv: list[str] | None = None, *, stdin=None) -> int:
101
+ parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
102
+ parser.add_argument("--media", help="the audio file (or source name) the tags refer to")
103
+ parser.add_argument("--out", default="tags.json", help="where to save the tags")
104
+ parser.add_argument("--keymap", action="append", default=[], metavar="KEY=TAG")
105
+ args = parser.parse_args(argv)
106
+ keymap = _parse_keymap(args.keymap) or {"a": "event"}
107
+ tagger = EventTagger(keymap)
108
+ print(f"keys: {keymap}; key + Enter to tag, q to end", file=sys.stderr)
109
+ for line in stdin or sys.stdin:
110
+ key = line.strip()
111
+ if key in ("", "q"):
112
+ break
113
+ tagger.press(key)
114
+ print(tagger.save(args.out, media=args.media))
115
+ return 0
116
+
117
+
118
+ if __name__ == "__main__":
119
+ raise SystemExit(main())
@@ -0,0 +1,38 @@
1
+ """The tag-events example: key presses become timestamped tags saved as JSON."""
2
+
3
+ import io
4
+ import json
5
+
6
+ from stream2py.examples.tag_events import EventTagger, main, tag_keys
7
+
8
+
9
+ def _clock(*times):
10
+ it = iter(times)
11
+ return lambda: next(it)
12
+
13
+
14
+ def test_press_records_tag_at_position_and_ignores_unmapped_keys():
15
+ tagger = EventTagger({"a": "applause"}, position=_clock(1.23456, 9.0))
16
+ assert tag_keys(tagger, "xaa")[0].t == 1.235
17
+ assert [e.tag for e in tagger.events] == ["applause", "applause"]
18
+
19
+
20
+ def test_save_roundtrip(tmp_path):
21
+ tagger = EventTagger({"l": "laughter"}, position=_clock(2.0))
22
+ tagger.press("l")
23
+ path = tagger.save(tmp_path / "t.json", media="talk.wav")
24
+ assert json.loads(path.read_text()) == {
25
+ "media": "talk.wav",
26
+ "events": [{"t": 2.0, "tag": "laughter", "key": "l"}],
27
+ }
28
+
29
+
30
+ def test_cli_reads_keys_until_q(tmp_path):
31
+ out = tmp_path / "o.json"
32
+ rc = main(
33
+ ["--media", "m.wav", "--out", str(out), "--keymap", "a=applause"],
34
+ stdin=io.StringIO("a\nz\na\nq\na\n"),
35
+ )
36
+ saved = json.loads(out.read_text())
37
+ assert rc == 0 and saved["media"] == "m.wav"
38
+ assert [e["tag"] for e in saved["events"]] == ["applause", "applause"]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes