File: containers.py

package info (click to toggle)
python-utils 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 396 kB
  • sloc: python: 2,135; makefile: 19; sh: 5
file content (623 lines) | stat: -rw-r--r-- 19,099 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
"""
This module provides custom container classes with enhanced functionality.

Classes:
    CastedDictBase: Abstract base class for dictionaries that cast keys and
        values.
    CastedDict: Dictionary that casts keys and values to specified types.
    LazyCastedDict: Dictionary that lazily casts values to specified types upon
        access.
    UniqueList: List that only allows unique values, with configurable behavior
        on duplicates.
    SliceableDeque: Deque that supports slicing and enhanced equality checks.

Type Aliases:
    KT: Type variable for dictionary keys.
    VT: Type variable for dictionary values.
    DT: Type alias for a dictionary with keys of type KT and values of type VT.
    KT_cast: Type alias for a callable that casts dictionary keys.
    VT_cast: Type alias for a callable that casts dictionary values.
    HT: Type variable for hashable values in UniqueList.
    T: Type variable for generic types.
    DictUpdateArgs: Union type for arguments that can be used to update a
        dictionary.
    OnDuplicate: Literal type for handling duplicate values in UniqueList.

Usage:
    - CastedDict and LazyCastedDict can be used to create dictionaries with
        automatic type casting.
    - UniqueList ensures all elements are unique and can raise an error on
        duplicates.
    - SliceableDeque extends deque with slicing support and enhanced equality
        checks.

Examples:
    >>> d = CastedDict(int, int)
    >>> d[1] = 2
    >>> d['3'] = '4'
    >>> d.update({'5': '6'})
    >>> d.update([('7', '8')])
    >>> d
    {1: 2, 3: 4, 5: 6, 7: 8}

    >>> l = UniqueList(1, 2, 3)
    >>> l.append(4)
    >>> l.append(4)
    >>> l.insert(0, 4)
    >>> l.insert(0, 5)
    >>> l[1] = 10
    >>> l
    [5, 10, 2, 3, 4]

    >>> d = SliceableDeque([1, 2, 3, 4, 5])
    >>> d[1:4]
    SliceableDeque([2, 3, 4])
"""

# pyright: reportIncompatibleMethodOverride=false
import abc
import collections
import typing

from . import types

if typing.TYPE_CHECKING:
    import _typeshed  # noqa: F401

#: A type alias for a type that can be used as a key in a dictionary.
KT = types.TypeVar('KT')
#: A type alias for a type that can be used as a value in a dictionary.
VT = types.TypeVar('VT')
#: A type alias for a dictionary with keys of type KT and values of type VT.
DT = types.Dict[KT, VT]
#: A type alias for the casted type of a dictionary key.
KT_cast = types.Optional[types.Callable[..., KT]]
#: A type alias for the casted type of a dictionary value.
VT_cast = types.Optional[types.Callable[..., VT]]
#: A type alias for the hashable values of the `UniqueList`
HT = types.TypeVar('HT', bound=types.Hashable)
#: A type alias for a regular generic type
T = types.TypeVar('T')

# Using types.Union instead of | since Python 3.7 doesn't fully support it
DictUpdateArgs = types.Union[
    types.Mapping[KT, VT],
    types.Iterable[types.Tuple[KT, VT]],
    types.Iterable[types.Mapping[KT, VT]],
    '_typeshed.SupportsKeysAndGetItem[KT, VT]',
]

OnDuplicate = types.Literal['ignore', 'raise']


class CastedDictBase(types.Dict[KT, VT], abc.ABC):
    """
    Abstract base class for dictionaries that cast keys and values.

    Attributes:
        _key_cast (KT_cast[KT]): Callable to cast dictionary keys.
        _value_cast (VT_cast[VT]): Callable to cast dictionary values.

    Methods:
        __init__(key_cast: KT_cast[KT] = None, value_cast: VT_cast[VT] = None,
            *args: DictUpdateArgs[KT, VT], **kwargs: VT) -> None:
            Initializes the dictionary with optional key and value casting
            callables.
        update(*args: DictUpdateArgs[types.Any, types.Any],
            **kwargs: types.Any) -> None:
            Updates the dictionary with the given arguments.
        __setitem__(key: types.Any, value: types.Any) -> None:
            Sets the item in the dictionary, casting the key if a key cast
            callable is provided.
    """

    _key_cast: KT_cast[KT]
    _value_cast: VT_cast[VT]

    def __init__(
        self,
        key_cast: KT_cast[KT] = None,
        value_cast: VT_cast[VT] = None,
        *args: DictUpdateArgs[KT, VT],
        **kwargs: VT,
    ) -> None:
        """
        Initializes the CastedDictBase with optional key and value
        casting callables.

        Args:
            key_cast (KT_cast[KT], optional): Callable to cast
                dictionary keys. Defaults to None.
            value_cast (VT_cast[VT], optional): Callable to cast
                dictionary values. Defaults to None.
            *args (DictUpdateArgs[KT, VT]): Arguments to initialize
                the dictionary.
            **kwargs (VT): Keyword arguments to initialize the
                dictionary.
        """
        self._value_cast = value_cast
        self._key_cast = key_cast
        self.update(*args, **kwargs)

    def update(
        self, *args: DictUpdateArgs[types.Any, types.Any], **kwargs: types.Any
    ) -> None:
        """
        Updates the dictionary with the given arguments.

        Args:
            *args (DictUpdateArgs[types.Any, types.Any]): Arguments to update
                the dictionary.
            **kwargs (types.Any): Keyword arguments to update the dictionary.
        """
        if args:
            kwargs.update(*args)

        if kwargs:
            for key, value in kwargs.items():
                self[key] = value

    def __setitem__(self, key: types.Any, value: types.Any) -> None:
        """
        Sets the item in the dictionary, casting the key if a key cast
        callable is provided.

        Args:
            key (types.Any): The key to set in the dictionary.
            value (types.Any): The value to set in the dictionary.
        """
        if self._key_cast is not None:
            key = self._key_cast(key)

        return super().__setitem__(key, value)


class CastedDict(CastedDictBase[KT, VT]):
    """
    Custom dictionary that casts keys and values to the specified typing.

    Note that you can specify the types for mypy and type hinting with:
    CastedDict[int, int](int, int)

    >>> d: CastedDict[int, int] = CastedDict(int, int)
    >>> d[1] = 2
    >>> d['3'] = '4'
    >>> d.update({'5': '6'})
    >>> d.update([('7', '8')])
    >>> d
    {1: 2, 3: 4, 5: 6, 7: 8}
    >>> list(d.keys())
    [1, 3, 5, 7]
    >>> list(d)
    [1, 3, 5, 7]
    >>> list(d.values())
    [2, 4, 6, 8]
    >>> list(d.items())
    [(1, 2), (3, 4), (5, 6), (7, 8)]
    >>> d[3]
    4

    # Casts are optional and can be disabled by passing None as the cast
    >>> d = CastedDict()
    >>> d[1] = 2
    >>> d['3'] = '4'
    >>> d.update({'5': '6'})
    >>> d.update([('7', '8')])
    >>> d
    {1: 2, '3': '4', '5': '6', '7': '8'}
    """

    def __setitem__(self, key: typing.Any, value: typing.Any) -> None:
        """Sets `key` to `cast(value)` in the dictionary."""
        if self._value_cast is not None:
            value = self._value_cast(value)

        super().__setitem__(key, value)


class LazyCastedDict(CastedDictBase[KT, VT]):
    """
    Custom dictionary that casts keys and lazily casts values to the specified
    typing. Note that the values are cast only when they are accessed and
    are not cached between executions.

    Note that you can specify the types for mypy and type hinting with:
    LazyCastedDict[int, int](int, int)

    >>> d: LazyCastedDict[int, int] = LazyCastedDict(int, int)
    >>> d[1] = 2
    >>> d['3'] = '4'
    >>> d.update({'5': '6'})
    >>> d.update([('7', '8')])
    >>> d
    {1: 2, 3: '4', 5: '6', 7: '8'}
    >>> list(d.keys())
    [1, 3, 5, 7]
    >>> list(d)
    [1, 3, 5, 7]
    >>> list(d.values())
    [2, 4, 6, 8]
    >>> list(d.items())
    [(1, 2), (3, 4), (5, 6), (7, 8)]
    >>> d[3]
    4

    # Casts are optional and can be disabled by passing None as the cast
    >>> d = LazyCastedDict()
    >>> d[1] = 2
    >>> d['3'] = '4'
    >>> d.update({'5': '6'})
    >>> d.update([('7', '8')])
    >>> d
    {1: 2, '3': '4', '5': '6', '7': '8'}
    >>> list(d.keys())
    [1, '3', '5', '7']
    >>> list(d.values())
    [2, '4', '6', '8']

    >>> list(d.items())
    [(1, 2), ('3', '4'), ('5', '6'), ('7', '8')]
    >>> d['3']
    '4'
    """

    def __setitem__(self, key: types.Any, value: types.Any) -> None:
        """
        Sets the item in the dictionary, casting the key if a key cast
        callable is provided.

        Args:
            key (types.Any): The key to set in the dictionary.
            value (types.Any): The value to set in the dictionary.
        """
        if self._key_cast is not None:
            key = self._key_cast(key)

        super().__setitem__(key, value)

    def __getitem__(self, key: types.Any) -> VT:
        """
        Gets the item from the dictionary, casting the value if a value cast
        callable is provided.

        Args:
            key (types.Any): The key to get from the dictionary.

        Returns:
            VT: The value from the dictionary.
        """
        if self._key_cast is not None:
            key = self._key_cast(key)

        value = super().__getitem__(key)

        if self._value_cast is not None:
            value = self._value_cast(value)

        return value

    def items(  # type: ignore[override]
        self,
    ) -> types.Generator[types.Tuple[KT, VT], None, None]:
        """
        Returns a generator of the dictionary's items, casting the values if a
        value cast callable is provided.

        Yields:
            types.Generator[types.Tuple[KT, VT], None, None]: A generator of
                the dictionary's items.
        """
        if self._value_cast is None:
            yield from super().items()
        else:
            for key, value in super().items():
                yield key, self._value_cast(value)

    def values(self) -> types.Generator[VT, None, None]:  # type: ignore[override]
        """
        Returns a generator of the dictionary's values, casting the values if a
        value cast callable is provided.

        Yields:
            types.Generator[VT, None, None]: A generator of the dictionary's
                values.
        """
        if self._value_cast is None:
            yield from super().values()
        else:
            for value in super().values():
                yield self._value_cast(value)


class UniqueList(types.List[HT]):
    """
    A list that only allows unique values. Duplicate values are ignored by
    default, but can be configured to raise an exception instead.

    >>> l = UniqueList(1, 2, 3)
    >>> l.append(4)
    >>> l.append(4)
    >>> l.insert(0, 4)
    >>> l.insert(0, 5)
    >>> l[1] = 10
    >>> l
    [5, 10, 2, 3, 4]

    >>> l = UniqueList(1, 2, 3, on_duplicate='raise')
    >>> l.append(4)
    >>> l.append(4)
    Traceback (most recent call last):
    ...
    ValueError: Duplicate value: 4
    >>> l.insert(0, 4)
    Traceback (most recent call last):
    ...
    ValueError: Duplicate value: 4
    >>> 4 in l
    True
    >>> l[0]
    1
    >>> l[1] = 4
    Traceback (most recent call last):
    ...
    ValueError: Duplicate value: 4
    """

    _set: types.Set[HT]

    def __init__(
        self,
        *args: HT,
        on_duplicate: OnDuplicate = 'ignore',
    ):
        """
        Initializes the UniqueList with optional duplicate handling behavior.

        Args:
            *args (HT): Initial values for the list.
            on_duplicate (OnDuplicate, optional): Behavior on duplicates.
                Defaults to 'ignore'.
        """
        self.on_duplicate = on_duplicate
        self._set = set()
        super().__init__()
        for arg in args:
            self.append(arg)

    def insert(self, index: types.SupportsIndex, value: HT) -> None:
        """
        Inserts a value at the specified index, ensuring uniqueness.

        Args:
            index (types.SupportsIndex): The index to insert the value at.
            value (HT): The value to insert.

        Raises:
            ValueError: If the value is a duplicate and `on_duplicate` is set
                to 'raise'.
        """
        if value in self._set:
            if self.on_duplicate == 'raise':
                raise ValueError(f'Duplicate value: {value}')
            else:
                return

        self._set.add(value)
        super().insert(index, value)

    def append(self, value: HT) -> None:
        """
        Appends a value to the list, ensuring uniqueness.

        Args:
            value (HT): The value to append.

        Raises:
            ValueError: If the value is a duplicate and `on_duplicate` is set
                to 'raise'.
        """
        if value in self._set:
            if self.on_duplicate == 'raise':
                raise ValueError(f'Duplicate value: {value}')
            else:
                return

        self._set.add(value)
        super().append(value)

    def __contains__(self, item: HT) -> bool:  # type: ignore[override]
        """
        Checks if the list contains the specified item.

        Args:
            item (HT): The item to check for.

        Returns:
            bool: True if the item is in the list, False otherwise.
        """
        return item in self._set

    @typing.overload
    def __setitem__(
        self, indices: types.SupportsIndex, values: HT
    ) -> None: ...

    @typing.overload
    def __setitem__(
        self, indices: slice, values: types.Iterable[HT]
    ) -> None: ...

    def __setitem__(
        self,
        indices: types.Union[slice, types.SupportsIndex],
        values: types.Union[types.Iterable[HT], HT],
    ) -> None:
        """
        Sets the item(s) at the specified index/indices, ensuring uniqueness.

        Args:
            indices (types.Union[slice, types.SupportsIndex]): The index or
                slice to set the value(s) at.
            values (types.Union[types.Iterable[HT], HT]): The value(s) to set.

        Raises:
            RuntimeError: If `on_duplicate` is 'ignore' and setting slices.
            ValueError: If the value(s) are duplicates and `on_duplicate` is
                set to 'raise'.
        """
        if isinstance(indices, slice):
            values = types.cast(types.Iterable[HT], values)
            if self.on_duplicate == 'ignore':
                raise RuntimeError(
                    'ignore mode while setting slices introduces ambiguous '
                    'behaviour and is therefore not supported'
                )

            duplicates: types.Set[HT] = set(values) & self._set
            if duplicates and values != list(self[indices]):
                raise ValueError(f'Duplicate values: {duplicates}')

            self._set.update(values)
        else:
            values = types.cast(HT, values)
            if values in self._set and values != self[indices]:
                if self.on_duplicate == 'raise':
                    raise ValueError(f'Duplicate value: {values}')
                else:
                    return

            self._set.add(values)

        super().__setitem__(
            types.cast(slice, indices), types.cast(types.List[HT], values)
        )

    def __delitem__(
        self, index: types.Union[types.SupportsIndex, slice]
    ) -> None:
        """
        Deletes the item(s) at the specified index/indices.

        Args:
            index (types.Union[types.SupportsIndex, slice]): The index or slice
                to delete the item(s) at.
        """
        if isinstance(index, slice):
            for value in self[index]:
                self._set.remove(value)
        else:
            self._set.remove(self[index])

        super().__delitem__(index)


# Type hinting `collections.deque` does not work consistently between Python
# runtime, mypy and pyright currently so we have to ignore the errors
class SliceableDeque(types.Generic[T], collections.deque[T]):
    """
    A deque that supports slicing and enhanced equality checks.

    Methods:
        __getitem__(index: types.Union[types.SupportsIndex, slice]) ->
            types.Union[T, 'SliceableDeque[T]']:
            Returns the item or slice at the given index.
        __eq__(other: types.Any) -> bool:
            Checks equality with another object, allowing for comparison with
             lists, tuples, and sets.
        pop(index: int = -1) -> T:
            Removes and returns the item at the given index. Only supports
            index 0 and the last index.
    """

    @typing.overload
    def __getitem__(self, index: types.SupportsIndex) -> T: ...

    @typing.overload
    def __getitem__(self, index: slice) -> 'SliceableDeque[T]': ...

    def __getitem__(
        self, index: types.Union[types.SupportsIndex, slice]
    ) -> types.Union[T, 'SliceableDeque[T]']:
        """
        Return the item or slice at the given index.

        Args:
            index (types.Union[types.SupportsIndex, slice]): The index or
             slice to retrieve.

        Returns:
            types.Union[T, 'SliceableDeque[T]']: The item or slice at the
            given index.

        Examples:
            >>> d = SliceableDeque[int]([1, 2, 3, 4, 5])
            >>> d[1:4]
            SliceableDeque([2, 3, 4])

            >>> d = SliceableDeque[str](['a', 'b', 'c'])
            >>> d[-2:]
            SliceableDeque(['b', 'c'])
        """
        if isinstance(index, slice):
            start, stop, step = index.indices(len(self))
            return self.__class__(self[i] for i in range(start, stop, step))
        else:
            return super().__getitem__(index)

    def __eq__(self, other: types.Any) -> bool:
        """
        Checks equality with another object, allowing for comparison with
        lists, tuples, and sets.

        Args:
            other (types.Any): The object to compare with.

        Returns:
            bool: True if the objects are equal, False otherwise.
        """
        if isinstance(other, list):
            return list(self) == other
        elif isinstance(other, tuple):
            return tuple(self) == other
        elif isinstance(other, set):
            return set(self) == other
        else:
            return super().__eq__(other)

    def pop(self, index: int = -1) -> T:
        """
        Removes and returns the item at the given index. Only supports index 0
        and the last index.

        Args:
            index (int, optional): The index of the item to remove. Defaults to
            -1.

        Returns:
            T: The removed item.

        Raises:
            IndexError: If the index is not 0 or the last index.

        Examples:
            >>> d = SliceableDeque([1, 2, 3])
            >>> d.pop(0)
            1
            >>> d.pop()
            3
        """
        if index == 0:
            return super().popleft()
        elif index in {-1, len(self) - 1}:
            return super().pop()
        else:
            raise IndexError(
                'Only index 0 and the last index (`N-1` or `-1`) '
                'are supported'
            )


if __name__ == '__main__':
    import doctest

    doctest.testmod()