primal-grakn
A convenience wrapper around the official grakn-python client.
Features
- Less code / boilerplate.
- Response data looks and acts like primitive data structures (python lists / dicts etc.). Thus more immediately intelligble, accessable, and JSON-serialisable.
- Some added conveniences such as match_or_insert function.
- Still access all underlying grakn-python client functionality where needed.
Why
The grakn-python client provides a complete and efficient object-oriented method of interaction with a Grakn instance. It can require a lot of code and recursion to get data. This extension aims to provide convenience through reducing code involved in connecting to Grakn and working with response data. It reflects a manner of working with Grakn through python that I have found to be preferrable.
Usage
Example
import primal_grakn.primal_grakn as grakn
with grakn.Graph(uri='myuri', keyspace='mykeyspace') as graph:
query = 'insert $a isa animal has name \"squirrel\";' # Escape your quotes, or use a raw string
concept_map = graph.execute('match $a isa animal; get;')
print(concept_map)
[{'a': {
'id': 'V4144',
'type': 'animal',
'base_type': 'entity',
'attributes': [{
'id': 'V4216',
'label': 'name',
'value': 'squirrel'
}]
}}]
print(concept_map.object) # Get the underlying ConceptMap object
print(concept_map['a'].object) # Get the underlying Concept object
graph.commit() # Don't forget to commit changes if you make them. N.B. this also closes the session
API
primal_grakn.Graph
| Name | Type | Description | Params | Example |
|---|---|---|---|---|
| Graph | Class | Initiates the session. |
|
|
| Graph.execute | Method | Executes a query. |
|
execute('match $a isa animal') |
| Graph.commit | Method | Commits the changes and ends the session. | ||
| Graph.match_or_insert | Method | Given a graql query string, match if it exists in the graph, or else insert it. |
|
match_or_insert('$a isa animal has name \"squirrel\";') |
primal_grakn.ConceptDict
Dictionary respresenation of a Grakn Concept object.
| Name | Type | Description | Params | Example |
|---|---|---|---|---|
| ConceptDict.object | Grakn Concept object | Corresponding grakn-python object |
An explanation about explanations
At the time of writing, the explanation data structures Grakn provides are undocumented. Briefly, the Grakn ConceptMap object exposes the set of facts as a tree. The top level of this tree includes the inferred facts from the response, and the compositional facts are nested within deeper levels.
At present, we provide two structures to access these facts: one is .explanation, which is the explanation tree as it is exposed by grakn-python, parsed into the form of a python dictionary. The second is .flat_explanation, where the tree is flattened into a list. I found this much more convenient for my purposes, as it meant I could filter the list for only the types of concepts I was interested in for my explanation, and then sort the list into the logical order (Grakn does not provide any ordering in its explanation output).
If you don't need information about the depth of inferences underlying your response, use .flat_explanation.
I feel this is an area where ripe for improvement both as regards the Grakn API and third party packages such as this.
Installation
Clone the repo
git clone https://github.com/cyclecycle/pygrakn.git
Requirements
-
Grakn running.
-
Official python-grakn client:
pip install grakn
Contributions
Are welcome :)
Metadata
Release files for primal-grakn 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| primal-grakn-0.0.1.tar.gz | 5.5 kB | Without provenance | Details for primal-grakn-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| primal_grakn-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for primal_grakn-0.0.1-py3-none-any.whl |
Total release size: 12.3 kB
Release files / primal-grakn-0.0.1.tar.gz
| Download URL | primal-grakn-0.0.1.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c7180741baa405df63e27c5a48ce8e0d833bfc6d6cba97943b63e46883500c11
|
|
BLAKE2b-256 checksum How to use checksums |
72c400d953fa66d7cef28a2f3a956fb7d56fd9de4010ce516a3a677abd9b4e75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6
|
Release files / primal_grakn-0.0.1-py3-none-any.whl
| Download URL | primal_grakn-0.0.1-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c00589897d5154fa2fd3513b1018b9345312094068ed101bf02f4f12b0e9e55a
|
|
BLAKE2b-256 checksum How to use checksums |
ea22b94f7ee586603da79783a262dd43e8c9eb9c011f8f751b468eec134ec89b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6
|
Security for primal-grakn 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.