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

bogdandm / json2python-models / 3822431117

pending completion
3822431117

Pull #54

github

GitHub
Merge c7664efeb into cdd441319
Pull Request #54: SQLModel support

27 of 27 new or added lines in 4 files covered. (100.0%)

1577 of 1608 relevant lines covered (98.07%)

3.92 hits per line

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
4✔
2
from typing import Generic, TypeVar, Union
4✔
3

4
from . import INDENT
4✔
5

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

8

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

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

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

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

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

39

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

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

47
    def update_position(self, key: str, value: Union[object, int]):
4✔
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:
4✔
56
            value = self[key] + 1
4✔
57
        if key in self:
4✔
58
            old_value = self[key]
4✔
59
            delta = value - old_value
4✔
60
        else:
61
            old_value = value
4✔
62
            delta = 1
4✔
63
        for k, v in self.items():
4✔
64
            if k != key and v >= old_value:
4✔
65
                self[k] += delta
4✔
66
        self[key] = value
4✔
67

68

69
def indent(string: str, lvl: int = 1, indent: str = INDENT) -> str:
4✔
70
    """
71
    Indent all lines of string by ``indent * lvl``
72
    """
73
    return "\n".join(indent * lvl + line for line in string.split("\n"))
4✔
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