tksheet 7.0.5__py3-none-any.whl → 7.1.0__py3-none-any.whl

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.
tksheet/functions.py CHANGED
@@ -4,6 +4,7 @@ import bisect
4
4
  import pickle
5
5
  import re
6
6
  import zlib
7
+ from collections import deque
7
8
  from collections.abc import (
8
9
  Callable,
9
10
  Generator,
@@ -21,10 +22,6 @@ from .other_classes import (
21
22
  Span,
22
23
  )
23
24
 
24
- from .vars import (
25
- backwards_compatibility_keys,
26
- )
27
-
28
25
  compress = partial(zlib.compress, level=1)
29
26
  pickle_obj = partial(pickle.dumps, protocol=pickle.HIGHEST_PROTOCOL)
30
27
  unpickle_obj = pickle.loads
@@ -141,11 +138,9 @@ def event_dict(
141
138
  ),
142
139
  named_spans=DotDict() if named_spans is None else named_spans,
143
140
  options=DotDict(),
144
- selection_boxes={}
145
- if boxes is None
146
- else selection_box_tup_to_dict(boxes)
147
- if isinstance(boxes, tuple)
148
- else boxes,
141
+ selection_boxes=(
142
+ {} if boxes is None else selection_box_tup_to_dict(boxes) if isinstance(boxes, tuple) else boxes
143
+ ),
149
144
  selected=tuple() if selected is None else selected,
150
145
  being_selected=tuple() if being_selected is None else being_selected,
151
146
  data=[] if data is None else data,
@@ -250,6 +245,14 @@ def is_iterable(o: object) -> bool:
250
245
  return False
251
246
 
252
247
 
248
+ def unpack(t: tuple[object] | tuple[Iterator[object]]) -> tuple[object]:
249
+ if not len(t):
250
+ return t
251
+ if is_iterable(t[0]) and len(t) == 1:
252
+ return t[0]
253
+ return t
254
+
255
+
253
256
  def is_type_int(o: object) -> bool:
254
257
  return isinstance(o, int) and not isinstance(o, bool)
255
258
 
@@ -342,7 +345,7 @@ def get_seq_without_gaps_at_index(
342
345
  return seq
343
346
 
344
347
 
345
- def consecutive_chunks(seq: list[object]) -> list[object]:
348
+ def consecutive_chunks(seq: list[object]) -> Generator[object]:
346
349
  if not seq:
347
350
  yield seq
348
351
  start = 0
@@ -363,6 +366,21 @@ def is_contiguous(seq: list[int]) -> bool:
363
366
  return all(i == (prev := prev + 1) for i in itr)
364
367
 
365
368
 
369
+ def get_last(
370
+ it: Iterator,
371
+ ) -> object:
372
+ if hasattr(it, "__reversed__"):
373
+ try:
374
+ return next(reversed(it))
375
+ except Exception:
376
+ return None
377
+ else:
378
+ try:
379
+ return deque(it, maxlen=1)[0]
380
+ except Exception:
381
+ return None
382
+
383
+
366
384
  def index_exists(seq: Sequence[object], index: int) -> bool:
367
385
  try:
368
386
  seq[index]
@@ -662,12 +680,14 @@ def coords_to_span(
662
680
 
663
681
 
664
682
  def key_to_span(
665
- key: str
666
- | int
667
- | slice
668
- | Sequence[int | None, int | None]
669
- | Sequence[int | None, int | None, int | None, int | None]
670
- | Sequence[Sequence[int | None, int | None], Sequence[int | None, int | None]],
683
+ key: (
684
+ str
685
+ | int
686
+ | slice
687
+ | Sequence[int | None, int | None]
688
+ | Sequence[int | None, int | None, int | None, int | None]
689
+ | Sequence[Sequence[int | None, int | None], Sequence[int | None, int | None]]
690
+ ),
671
691
  spans: dict[str, Span],
672
692
  widget: object = None,
673
693
  ) -> Span:
@@ -1084,14 +1104,6 @@ def del_named_span_options_nested(options: dict, itr1: Iterator, itr2: Iterator,
1084
1104
  del options[k][type_]
1085
1105
 
1086
1106
 
1087
- def coords_tag_to_int_tuple(s: str) -> tuple[int, int, int, int] | tuple[int, int]:
1088
- return tuple(map(int, filter(None, s.split("_"))))
1089
-
1090
-
1091
- def coords_tag_to_box_nt(s: str) -> Box_nt[int, int, int, int]:
1092
- return Box_nt(*(map(int, filter(None, s.split("_")))))
1093
-
1094
-
1095
1107
  def add_highlight(
1096
1108
  options: dict,
1097
1109
  key: int | tuple[int, int],
@@ -1252,9 +1264,13 @@ def mod_event_val(
1252
1264
  return event_data
1253
1265
 
1254
1266
 
1255
- def backwards_compatibility_x(
1256
- key: str,
1257
- ) -> str:
1258
- if key in backwards_compatibility_keys:
1259
- return backwards_compatibility_keys[key]
1260
- return key
1267
+ def pop_positions(
1268
+ itr: Iterator[int],
1269
+ to_pop: dict[int, int], # displayed index: data index
1270
+ save_to: dict[int, int],
1271
+ ) -> Iterator[int]:
1272
+ for i, pos in enumerate(itr()):
1273
+ if i in to_pop:
1274
+ save_to[to_pop[i]] = pos
1275
+ else:
1276
+ yield pos