python module
parent
c5a2dd4a84
commit
dcb3bdd033
@ -0,0 +1,140 @@
|
||||
### Python template
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "cozopy"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[lib]
|
||||
name = "cozopy"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1.0.81"
|
||||
anyhow = "1.0.58"
|
||||
pyo3 = { version = "0.16.5", features = ["extension-module"] }
|
||||
cozo = { path = ".." }
|
@ -0,0 +1,123 @@
|
||||
import json
|
||||
|
||||
from cozopy import CozoDbPy
|
||||
|
||||
|
||||
class CozoDb:
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.inner = CozoDbPy(*args, **kwargs)
|
||||
|
||||
def tx_attr(self, payload):
|
||||
return json.loads(self.inner.transact_attributes(json.dumps(payload, ensure_ascii=False)))
|
||||
|
||||
def tx(self, payload):
|
||||
return json.loads(self.inner.transact_triples(json.dumps(payload, ensure_ascii=False)))
|
||||
|
||||
def run(self, payload):
|
||||
return json.loads(self.inner.run_query(json.dumps(payload, ensure_ascii=False)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
db = CozoDb('_test', destroy_on_exit=True)
|
||||
res = db.tx_attr({"attrs": [
|
||||
{"put": {"keyword": "person.idd", "cardinality": "one", "type": "string", "index": "identity",
|
||||
"history": False}},
|
||||
{"put": {"keyword": "person.first_name", "cardinality": "one", "type": "string", "index": True}},
|
||||
{"put": {"keyword": "person.last_name", "cardinality": "one", "type": "string", "index": True}},
|
||||
{"put": {"keyword": "person.age", "cardinality": "one", "type": "int"}},
|
||||
{"put": {"keyword": "person.friend", "cardinality": "many", "type": "ref"}},
|
||||
{"put": {"keyword": "person.weight", "cardinality": "one", "type": "float"}},
|
||||
{"put": {"keyword": "person.covid", "cardinality": "one", "type": "bool"}},
|
||||
]
|
||||
})
|
||||
print(res)
|
||||
print(db.tx_attr({
|
||||
"attrs": [
|
||||
{"put": {"id": res["results"][0][0], "keyword": ":person.id", "cardinality": "one", "type": "string",
|
||||
"index": "identity", "history": False}},
|
||||
{"retract": {"id": res["results"][-1][0], "keyword": ":person.covid", "cardinality": "one", "type": "bool"}}
|
||||
]
|
||||
}))
|
||||
print(db.tx({
|
||||
"tx": [
|
||||
{"put": {
|
||||
"_temp_id": "alice",
|
||||
"person.first_name": "Alice",
|
||||
"person.age": 7,
|
||||
"person.last_name": "Amorist",
|
||||
"person.id": "alice_amorist",
|
||||
"person.weight": 25,
|
||||
"person.friend": "eve"}},
|
||||
{"put": {
|
||||
"_temp_id": "bob",
|
||||
"person.first_name": "Bob",
|
||||
"person.age": 70,
|
||||
"person.last_name": "Wonderland",
|
||||
"person.id": "bob_wonderland",
|
||||
"person.weight": 100,
|
||||
"person.friend": "alice"
|
||||
}},
|
||||
{"put": {
|
||||
"_temp_id": "eve",
|
||||
"person.first_name": "Eve",
|
||||
"person.age": 18,
|
||||
"person.last_name": "Faking",
|
||||
"person.id": "eve_faking",
|
||||
"person.weight": 50,
|
||||
"person.friend": [
|
||||
"alice",
|
||||
"bob",
|
||||
{
|
||||
"person.first_name": "Charlie",
|
||||
"person.age": 22,
|
||||
"person.last_name": "Goodman",
|
||||
"person.id": "charlie_goodman",
|
||||
"person.weight": 120,
|
||||
"person.friend": "eve"
|
||||
}
|
||||
]
|
||||
}},
|
||||
{"put": {
|
||||
"_temp_id": "david",
|
||||
"person.first_name": "David",
|
||||
"person.age": 7,
|
||||
"person.last_name": "Dull",
|
||||
"person.id": "david_dull",
|
||||
"person.weight": 25,
|
||||
"person.friend": {
|
||||
"_temp_id": "george",
|
||||
"person.first_name": "George",
|
||||
"person.age": 7,
|
||||
"person.last_name": "Geomancer",
|
||||
"person.id": "george_geomancer",
|
||||
"person.weight": 25,
|
||||
"person.friend": "george"}}},
|
||||
]
|
||||
}))
|
||||
res = db.run({
|
||||
"q": [
|
||||
{
|
||||
"rule": "ff",
|
||||
"args": [["?a", "?b"], ["?a", "person.friend", "?b"]]
|
||||
},
|
||||
{
|
||||
"rule": "ff",
|
||||
"args": [["?a", "?b"], ["?a", "person.friend", "?c"], {"rule": "ff", "args": ["?c", "?b"]}]
|
||||
},
|
||||
{
|
||||
"rule": "?",
|
||||
"args": [["?a"],
|
||||
{"not_exists": ["?a", "person.last_name", "Goodman"]},
|
||||
{"disj": [
|
||||
{"pred": "Eq", "args": ["?n", {"pred": "StrCat", "args": ["A", "l", "i", "c", "e"]}]},
|
||||
{"pred": "Eq", "args": ["?n", "Bob"]},
|
||||
{"pred": "Eq", "args": ["?n", 12345]},
|
||||
]},
|
||||
{"rule": "ff", "args": [{"person.id": "alice_amorist"}, "?a"]},
|
||||
["?a", "person.first_name", "?n"]
|
||||
]
|
||||
}
|
||||
],
|
||||
"out": {"friend": {"pull": "?a", "spec": ["person.first_name"]}}
|
||||
})
|
||||
print(res)
|
@ -0,0 +1,61 @@
|
||||
use pyo3::exceptions::PyException;
|
||||
use pyo3::prelude::*;
|
||||
|
||||
use cozo::{Db, DbBuilder};
|
||||
|
||||
#[pyclass(extends=PyException)]
|
||||
struct ErrorBridge(cozo::Error);
|
||||
|
||||
trait PyResultExt<T> {
|
||||
fn into_py_res(self) -> PyResult<T>;
|
||||
}
|
||||
|
||||
impl<T> PyResultExt<T> for anyhow::Result<T> {
|
||||
fn into_py_res(self) -> PyResult<T> {
|
||||
match self {
|
||||
Ok(t) => Ok(t),
|
||||
Err(e) => Err(PyException::new_err(e.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
struct CozoDbPy {
|
||||
db: Db,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl CozoDbPy {
|
||||
#[new]
|
||||
#[args(create_if_missing = true, destroy_on_exit = false)]
|
||||
fn new(path: &str, create_if_missing: bool, destroy_on_exit: bool) -> PyResult<Self> {
|
||||
let builder = DbBuilder::default()
|
||||
.path(path)
|
||||
.create_if_missing(create_if_missing)
|
||||
.destroy_on_exit(destroy_on_exit);
|
||||
let db = Db::build(builder).into_py_res()?;
|
||||
Ok(Self { db })
|
||||
}
|
||||
pub fn transact_attributes(&self, payload: &str) -> PyResult<String> {
|
||||
let payload: serde_json::Value = serde_json::from_str(payload).unwrap();
|
||||
let ret = self.db.transact_attributes(&payload).into_py_res()?;
|
||||
Ok(ret.to_string())
|
||||
}
|
||||
pub fn transact_triples(&self, payload: &str) -> PyResult<String> {
|
||||
let payload: serde_json::Value = serde_json::from_str(payload).unwrap();
|
||||
let ret = self.db.transact_triples(&payload).into_py_res()?;
|
||||
Ok(ret.to_string())
|
||||
}
|
||||
pub fn run_query(&self, payload: &str) -> PyResult<String> {
|
||||
let payload: serde_json::Value = serde_json::from_str(payload).unwrap();
|
||||
let ret = self.db.run_query(&payload).into_py_res()?;
|
||||
Ok(ret.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
fn cozopy(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<CozoDbPy>()?;
|
||||
m.add_class::<ErrorBridge>()?;
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue