-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #529 from graphistry/dev/gfql-serialization
Dev/gfql serialization
- Loading branch information
Showing
26 changed files
with
678 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Dict | ||
import pandas as pd | ||
|
||
from graphistry.utils.json import JSONVal, serialize_to_json_val | ||
|
||
|
||
class ASTSerializable(ABC): | ||
""" | ||
Internal, not intended for use outside of this module. | ||
Class name becomes o['type'], and all non reserved_fields become JSON-typed key | ||
""" | ||
|
||
reserved_fields = ['type'] | ||
|
||
def validate(self) -> None: | ||
pass | ||
|
||
def to_json(self, validate=True) -> Dict[str, JSONVal]: | ||
""" | ||
Returns JSON-compatible dictionry {"type": "ClassName", "arg1": val1, ...} | ||
Emits all non-reserved instance fields | ||
""" | ||
if validate: | ||
self.validate() | ||
data: Dict[str, JSONVal] = {'type': self.__class__.__name__} | ||
for key, value in self.__dict__.items(): | ||
if key not in self.reserved_fields: | ||
data[key] = serialize_to_json_val(value) | ||
return data | ||
|
||
@classmethod | ||
def from_json(cls, d: Dict[str, JSONVal]) -> 'ASTSerializable': | ||
""" | ||
Given c.to_json(), hydrate back c | ||
Corresponding c.__class__.__init__ must accept all non-reserved instance fields | ||
""" | ||
constructor_args = {k: v for k, v in d.items() if k not in cls.reserved_fields} | ||
return cls(**constructor_args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.