File: crunch.py

package info (click to toggle)
python-graphene 3.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,124 kB
  • sloc: python: 8,935; makefile: 212; sh: 18
file content (35 lines) | stat: -rw-r--r-- 756 bytes parent folder | download | duplicates (2)
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
import json
from collections.abc import Mapping


def to_key(value):
    return json.dumps(value)


def insert(value, index, values):
    key = to_key(value)

    if key not in index:
        index[key] = len(values)
        values.append(value)
        return len(values) - 1

    return index.get(key)


def flatten(data, index, values):
    if isinstance(data, (list, tuple)):
        flattened = [flatten(child, index, values) for child in data]
    elif isinstance(data, Mapping):
        flattened = {key: flatten(child, index, values) for key, child in data.items()}
    else:
        flattened = data
    return insert(flattened, index, values)


def crunch(data):
    index = {}
    values = []

    flatten(data, index, values)
    return values