Skip to content
This repository has been archived by the owner on Apr 21, 2021. It is now read-only.

WIP: Node split #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 6 additions & 23 deletions examples/subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ def get(
return NoneNode() if single else [NoneNode()]

if single and item.__class__.__name__ != cls.__name__:
raise Exception(
f"Found {item.__class__.__name__}, and not {cls.__name__}."
)
raise Exception(f"Found {item.__class__.__name__}, and not {cls.__name__}.")

return item

Expand All @@ -137,9 +135,7 @@ def make_filter(key, value):
else:
return fltr.format(value)

filters = [
make_filter(k, v) for k, v in kwargs.items() if k in cls._filters
]
filters = [make_filter(k, v) for k, v in kwargs.items() if k in cls._filters]
if hasattr(cls, "_required_filters"):
filters += list(cls._required_filters)
filters = " and ".join((filter(lambda x: x, filters)))
Expand Down Expand Up @@ -194,10 +190,7 @@ class Person(BaseAbstract):
"other": f'eq(gender, "{Gender.OTHER.value}")',
},
"family": ("family", "uid(family)"),
"living": {
"true": "(not has(death_year))",
"false": "has(death_year)",
},
"living": {"true": "(not has(death_year))", "false": "has(death_year)"},
}
_subqueries = {
"family": """
Expand Down Expand Up @@ -284,16 +277,8 @@ class Person(BaseAbstract):

@classmethod
def _get_parents(cls, person, step=1):
father = (
Person.get(person.father.uid)
if hasattr(person, "father")
else None
)
mother = (
Person.get(person.mother.uid)
if hasattr(person, "mother")
else None
)
father = Person.get(person.father.uid) if hasattr(person, "father") else None
mother = Person.get(person.mother.uid) if hasattr(person, "mother") else None
generation = [
{"person": father, "step": step},
{"person": mother, "step": step},
Expand All @@ -317,6 +302,4 @@ def _get_parents(cls, person, step=1):

@property
def ancestors(self):
return [{"person": self, "step": 0}] + self.__class__._get_parents(
self
)
return [{"person": self, "step": 0}] + self.__class__._get_parents(self)
5 changes: 3 additions & 2 deletions pydiggy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__email__ = "[email protected]"
__version__ = "0.1.0"

from pydiggy.node import Facets, Node, get_node, is_facets
from pydiggy.node import Facets, Node, is_facets, get_node_type, NodeTypeRegistry
from pydiggy.operations import generate_mutation, hydrate, query, run_mutation
from pydiggy._types import count, exact, geo, index, lang, reverse, uid, upsert

Expand All @@ -17,12 +17,13 @@
"Facets",
"generate_mutation",
"geo",
"get_node",
"get_node_type",
"hydrate",
"is_facets",
"index",
"lang",
"Node",
"NodeTypeRegistry",
"query",
"reverse",
"run_mutation",
Expand Down
7 changes: 6 additions & 1 deletion pydiggy/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class Tokenizer(DirectiveArgument):


class Directive:
"""
A directive adds extra instructions to a schema or query. Annotated
with the '@' symbol and optional arguments in parens.
"""

def __str__(self):
args = []
if "__annotations__" in self.__class__.__dict__:
Expand Down Expand Up @@ -78,7 +83,7 @@ def __init__(self, name=None, many=False, with_facets=False):
upsert = type("upsert", (Directive,), {})
lang = type("lang", (Directive,), {})

DGRAPH_TYPES = { # Unsupported dgraph type: password, geo
DGRAPH_TYPES = { # TODO: add dgraph type 'password'
"uid": "uid",
"geo": "geo",
"str": "string",
Expand Down
8 changes: 4 additions & 4 deletions pydiggy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pydgraph import Operation

from pydiggy.connection import get_client
from pydiggy.node import Node
from pydiggy.node import Node, NodeTypeRegistry


@click.group()
Expand Down Expand Up @@ -40,12 +40,12 @@ def generate(module, run, host, port):
click.echo(f"Generating schema for: {module}")
importlib.import_module(module)

num_nodes = len(Node._nodes)
num_nodes = len(NodeTypeRegistry._node_types)
click.echo(f"\nNodes found: ({num_nodes})")
for node in Node._nodes:
for node in NodeTypeRegistry._node_types:
click.echo(f" - {node._get_name()}")

schema, unknown = Node._generate_schema()
schema, unknown = NodeTypeRegistry._generate_schema()

if not run:
click.echo("\nYour schema:\n~~~~~~~~\n")
Expand Down
Loading