Skip to content
Dgraph to Python object mapper, and schema generator
Branch: master
Clone or download
Latest commit 9b6e1f0 May 28, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Get generate schema, flush, and data loader mvp operational Jul 31, 2018
docs Add context and comments to code, add subclass example, general outli… May 26, 2019
examples Add context and comments to code, add subclass example, general outli… May 26, 2019
pydiggy Fixed last test. May 27, 2019
tests Return to passing tests May 26, 2019
.coveragerc Add some unit tests Aug 1, 2018
.editorconfig Get generate schema, flush, and data loader mvp operational Jul 31, 2018
.gitignore Add some unit tests Aug 1, 2018
.travis.yml Get generate schema, flush, and data loader mvp operational Jul 31, 2018
AUTHORS.rst Get generate schema, flush, and data loader mvp operational Jul 31, 2018
CONTRIBUTING.rst Get generate schema, flush, and data loader mvp operational Jul 31, 2018
HISTORY.rst Get generate schema, flush, and data loader mvp operational Jul 31, 2018
LICENSE Get generate schema, flush, and data loader mvp operational Jul 31, 2018
MANIFEST.in Get generate schema, flush, and data loader mvp operational Jul 31, 2018
Makefile Get generate schema, flush, and data loader mvp operational Jul 31, 2018
Pipfile Return to passing tests May 26, 2019
Pipfile.lock Add directives Aug 8, 2018
README.rst Fix typo in README. May 28, 2019
pytest.ini Add some unit tests Aug 1, 2018
setup.cfg Get generate schema, flush, and data loader mvp operational Jul 31, 2018
setup.py Return to passing tests May 26, 2019
tox.ini Get generate schema, flush, and data loader mvp operational Jul 31, 2018

README.rst

IMPORTANT NOTICE: I am still working on this project. Slowly, but I hope that it should be releasable by mid-2019.

PyDiggy

Dgraph to Python object mapper

  • Free software: MIT license

Note

Python 3.7 only. Sorry.

EXAMPLE

# ./examples/__init__

from .basic import *  # noqa


# ./examples/basic.py

from __future__ import annotations


from pydiggy import Node
from typing import List


class Region(Node):
    area: int
    population: int
    name: str
    borders: List[Region]

CLI

Point the CLI utility at an existing module to generate a Dgraph schema.

$ python3 -m pydiggy generate examples

Generating schema for: examples

Nodes found: (1)
    - Region

Your schema:
~~~~~~~~

Region: bool @index(bool) .
_type: string .
area: int .
borders: uid .
name: string .
population: int .

~~~~~~~~

GENERATE MUTATIONS

from pydiggy import generate_mutation, Facets

por = Region(uid=0x11, name="Portugal")
spa = Region(uid=0x12, name="Spain")
gas = Region(uid=0x13, name="Gascony")
mar = Region(uid=0x14, name="Marseilles")

por.borders = [spa]
spa.borders = [por, gas, mar]
gas.borders = [Facets(spa, foo='bar', hello='world'), mar]
mar.borders = [spa, gas]

por.stage()
spa.stage()
gas.stage()
mar.stage()

print(generate_mutation())

The result:

{
    set {
        <0x11> <Region> "true" .
        <0x11> <_type> "Region" .
        <0x11> <name> "Portugal" .
        <0x11> <borders> <0x12> .
        <0x12> <Region> "true" .
        <0x12> <_type> "Region" .
        <0x12> <name> "Spain" .
        <0x12> <borders> <0x11> .
        <0x12> <borders> <0x13> .
        <0x12> <borders> <0x14> .
        <0x13> <Region> "true" .
        <0x13> <_type> "Region" .
        <0x13> <name> "Gascony" .
        <0x13> <borders> <0x12> (foo="bar", hello="world") .
        <0x13> <borders> <0x14> .
        <0x14> <Region> "true" .
        <0x14> <_type> "Region" .
        <0x14> <name> "Marseilles" .
        <0x14> <borders> <0x12> .
        <0x14> <borders> <0x13> .
    }
}

HYDRATE FROM JSON TO PYTHON OBJECTS

Given some response from Dgraph:

{
    "data": {
        "allRegions": [
            {
                "uid": "0x11",
                "_type": "Region",
                "name": "Portugal",
                "borders": [
                    {
                        "uid": "0x12",
                        "_type": "Region",
                        "name": "Spain"
                    }
                ]
            },
            {
                "uid": "0x12",
                "_type": "Region",
                "name": "Spain",
                "borders": [
                    {
                        "uid": "0x11",
                        "_type": "Region",
                        "name": "Portugal"
                    },
                    {
                        "uid": "0x13",
                        "_type": "Region",
                        "name": "Gascony"
                    },
                    {
                        "uid": "0x14",
                        "_type": "Region",
                        "name": "Marseilles"
                    }
                ]
            },
            {
                "uid": "0x13",
                "_type": "Region",
                "name": "Gascony",
                "borders": [
                    {
                        "uid": "0x12",
                        "_type": "Region",
                        "name": "Spain",
                        "borders|foo": "bar",
                        "borders|hello": "world"
                    },
                    {
                        "uid": "0x14",
                        "_type": "Region",
                        "name": "Marseilles"
                    }
                ]
            },
            {
                "uid": "0x14",
                "_type": "Region",
                "name": "Marseilles",
                "borders": [
                    {
                        "uid": "0x12",
                        "_type": "Region",
                        "name": "Spain"
                    },
                    {
                        "uid": "0x13",
                        "_type": "Region",
                        "name": "Gascony"
                    }
                ]
            }
        ]
    },
    "extensions": {
        "server_latency": {
            "parsing_ns": 23727,
            "processing_ns": 2000535,
            "encoding_ns": 7803450
        },
        "txn": {
            "start_ts": 117,
            "lin_read": {
                "ids": {
                    "1": 49
                }
            }
        }
    }
}

We can turn it into some Python objects:

>>> data = hydrate(retrieved_data)

{'allRegions': [<Region:17>, <Region:18>, <Region:19>, <Region:20>]}
You can’t perform that action at this time.