跳转到主要内容

Dgraph到Python对象映射器

项目描述

重要通知:我仍在该项目上工作。缓慢,但希望它能在2019年中期发布。

PyDiggy

Dgraph到Python对象映射器

  • 免费软件:MIT许可证

示例

# ./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]

命令行界面

将命令行工具指向现有模块以生成Dgraph模式。

$ 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 .

~~~~~~~~

生成突变

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())

结果

{
    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> .
    }
}

从JSON到Python对象的HYDATE

给定一些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
                }
            }
        }
    }
}

我们可以将其转换为一些Python对象

>>> data = hydrate(retrieved_data)

{'allRegions': [<Region:17>, <Region:18>, <Region:19>, <Region:20>]}

历史

0.1.0 (2018-07-31)

  • 首次发布在PyPI上。

项目详情


下载文件

为您的平台下载文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源分发

pydiggy-0.1.0.tar.gz (22.8 kB 查看哈希值)

上传时间:

由以下支持