File: utils.py

package info (click to toggle)
jsonhyperschema-codec 1.0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 96 kB
  • sloc: python: 158; makefile: 3
file content (34 lines) | stat: -rw-r--r-- 660 bytes parent folder | download | duplicates (3)
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
from coreapi.compat import string_types


def _get_string(item, key):
    value = item.get(key)
    if isinstance(value, string_types):
        return value
    return ''


def _get_dict(item, key):
    value = item.get(key)
    if isinstance(value, dict):
        return value
    return {}


def _get_list(item, key):
    value = item.get(key)
    if isinstance(value, list):
        return value
    return []


def get_dicts(item):
    return [value for value in item if isinstance(value, dict)]


def _dereference(value, ref):
    keys = value.strip('#/').split('/')
    node = ref
    for key in keys:
        node = _get_dict(node, key)
    return node