• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bogdandm / json2python-models / 11498135606

pending completion
11498135606

Pull #59

github

web-flow
Merge 2387ea51f into e2606e8f2
Pull Request #59: Modernize project setup and setup cron action job

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.67
/json_to_models/models/utils.py
1
from collections import defaultdict
2✔
2
from typing import Generic, TypeVar, Union
2✔
3

4
from . import INDENT
2✔
5

6
T = TypeVar('T')
2✔
7

8

9
class ListEx(list, Generic[T]):
2✔
10
    """
11
    Extended list with shortcut methods
12
    """
13

14
    def safe_index(self, value: T):
2✔
15
        try:
2✔
16
            return self.index(value)
2✔
17
        except ValueError:
2✔
18
            return None
19

20
    def _safe_indexes(self, *values: T):
2✔
21
        return [i for i in map(self.safe_index, values) if i is not None]
2✔
22

23
    def insert_before(self, value: T, *before: T):
2✔
24
        ix = self._safe_indexes(*before)
2✔
25
        if not ix:
2✔
26
            raise ValueError
2✔
27
        pos = min(ix)
2✔
28
        self.insert(pos, value)
2✔
29
        return pos
2✔
30

31
    def insert_after(self, value: T, *after: T):
2✔
32
        ix = self._safe_indexes(*after)
2✔
33
        if not ix:
2✔
34
            raise ValueError
×
35
        pos = max(ix) + 1
2✔
36
        self.insert(pos, value)
2✔
37
        return pos
2✔
38

39

40
class PositionsDict(defaultdict):
2✔
41
    # Dict contains mapping Index -> position, where position is list index to insert nested element of Index
42
    INC = object()
2✔
43

44
    def __init__(self, default_factory=int, **kwargs):
2✔
45
        super().__init__(default_factory, **kwargs)
2✔
46

47
    def update_position(self, key: str, value: Union[object, int]):
2✔
48
        """
49
        Shift all elements which are placed after updated one
50

51
        :param key: Index or "root"
52
        :param value: Could be position or PositionsDict.INC to perform quick increment (x+=1)
53
        :return:
54
        """
55
        if value is self.INC:
2✔
56
            value = self[key] + 1
2✔
57
        if key in self:
2✔
58
            old_value = self[key]
2✔
59
            delta = value - old_value
2✔
60
        else:
61
            old_value = value
2✔
62
            delta = 1
2✔
63
        for k, v in self.items():
2✔
64
            if k != key and v >= old_value:
2✔
65
                self[k] += delta
2✔
66
        self[key] = value
2✔
67

68

69
def indent(string: str, lvl: int = 1, indent: str = INDENT) -> str:
2✔
70
    """
71
    Indent all lines of string by ``indent * lvl``
72
    """
73
    return "\n".join(indent * lvl + line for line in string.split("\n"))
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc