Skip to content

Commit

Permalink
Clean up repository & fix workflows (#5)
Browse files Browse the repository at this point in the history
Update tripper folder:
Move files around, use imports from "tripper.*" and extend Python
support to include 3.7 and 3.8.

Update files according to pre-commit.

Update tests for better isolation:
Split up tests into separate test functions for better environment
isolation.

Use `tripperpy` as package name.

Use `s`, `p`, and `o` as variable names:
These represent the parts of an RDF Triple: subject, predicate, object.

Co-authored-by: Jesper Friis <jesper-friis@users.noreply.github.com>
  • Loading branch information
CasperWA and jesper-friis authored Oct 12, 2022
1 parent 799b246 commit 8ef8b4e
Show file tree
Hide file tree
Showing 30 changed files with 812 additions and 523 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/cd_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ jobs:
uses: SINTEF/ci-cd/.github/workflows/cd_release.yml@v1
if: github.repository == 'EMMC-ASBL/tripper' && startsWith(github.ref, 'refs/tags/v')
with:
# General
git_username: "TEAM 4.0[bot]"
git_email: "Team4.0@SINTEF.no"
package_dirs: tripper
release_branch: main
install_extras: "[dev]"
python_version: "3.9"

# Build package
python_package: true
package_dirs: tripper
python_version_build: "3.7"
build_cmd: "pip install -U flit && flit build"
publish_on_pypi: true

# Build & publish documentation
update_docs: true
python_version_docs: "3.7"
doc_extras: "[docs]"
changelog_exclude_labels: "skip_changelog,duplicate,question,invalid,wontfix"
secrets:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_cd_updated_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
update_docs: true
update_python_api_ref: true
update_docs_landing_page: true
python_version: "3.9"
python_version: "3.7"
doc_extras: "[docs]"
secrets:
PAT: ${{ secrets.RELEASE_PAT }}
2 changes: 1 addition & 1 deletion .github/workflows/ci_check_dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
git_username: "TEAM 4.0[bot]"
git_email: "Team4.0@SINTEF.no"
permanent_dependencies_branch: "ci/dependency-updates"
python_version: "3.9"
python_version: "3.7"
install_extras: "[dev]"
pr_labels: "CI/CD,skip_changelog"
secrets:
Expand Down
20 changes: 15 additions & 5 deletions .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,36 @@ jobs:

# pre-commit
run_pre-commit: true
python_version_pre-commit: "3.9"
python_version_pre-commit: "3.8"
skip_pre-commit_hooks: pylint,pylint-tests

# pylint & safety
python_version_pylint_safety: "3.9"
python_version_pylint_safety: "3.7"
run_pylint: true
pylint_runs: |
--rcfile=pyproject.toml tripper
--rcfile=pyproject.toml --disable=import-outside-toplevel,redefined-outer-name tests
run_safety: true
# 48547: RDFLib vulnerability: https://pyup.io/vulnerabilities/PVE-2022-48547/48547/
# 44715-44717: NumPy vulnerabilities:
# https://pyup.io/vulnerabilities/CVE-2021-41495/44715/
# https://pyup.io/vulnerabilities/CVE-2021-41496/44716/
# https://pyup.io/vulnerabilities/CVE-2021-34141/44717/
safety_options: |
--ignore=48547
--ignore=44715
--ignore=44716
--ignore=44717
# Build package
run_build_package: true
python_version_package: "3.9"
python_version_package: "3.7"
build_libs: flit
build_cmd: flit build

# Build documentation
run_build_docs: true
python_version_docs: "3.9"
python_version_docs: "3.7"
update_python_api_ref: true
update_docs_landing_page: true
package_dirs: tripper
Expand All @@ -47,7 +57,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10"]

steps:
- name: Checkout tripper
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_update_dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
pr_labels: "CI/CD,skip_changelog"
extra_to_dos: "- [ ] Make sure that the PR is **squash** merged, with a sensible commit message."
update_pre-commit: true
python_version: "3.9"
python_version: "3.7"
install_extras: "[pre-commit]"
skip_pre-commit_hooks: "pylint,pylint-tests"
secrets:
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ repos:
- "pydantic"

- repo: https://github.com/SINTEF/ci-cd
rev: b648cea02c07ef2c2a81b60696c0e3eb283fa3d0 # frozen: v1
rev: 6a258b3bce2d2db022908c127d0532c8a7591707 # frozen: v1
hooks:
- id: docs-api-reference
args:
- --package-dirs=tripper
- --package-dir=tripper
- id: docs-landing-page

- repo: local
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

_The best ride when handling any triplestore._

## Installation

The package can be installed from [PyPI](https://pypi.org/project/tripperpy) using `pip`:

```shell
pip install tripperpy
```

## License and copyright

All files in this repository are licensed under the [MIT license](LICENSE) with copyright &copy; 2022 European Material Modelling Council.
1 change: 1 addition & 0 deletions docs/api_reference/.pages
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: "API Reference"
1 change: 1 addition & 0 deletions docs/api_reference/backends/.pages
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: "backends"
3 changes: 3 additions & 0 deletions docs/api_reference/backends/ontopy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ontopy

::: tripper.backends.ontopy
3 changes: 3 additions & 0 deletions docs/api_reference/backends/rdflib.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# rdflib

::: tripper.backends.rdflib
3 changes: 3 additions & 0 deletions docs/api_reference/interface.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# interface

::: tripper.interface
3 changes: 3 additions & 0 deletions docs/api_reference/triplestore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# triplestore

::: tripper.triplestore
17 changes: 17 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Tripper

**A triplestore wrapper for Python.**

_The best ride when handling any triplestore._

## Installation

The package can be installed from [PyPI](https://pypi.org/project/tripperpy) using `pip`:

```shell
pip install tripperpy
```

## License and copyright

All files in this repository are licensed under the [MIT license](LICENSE.md) with copyright &copy; 2022 European Material Modelling Council.
86 changes: 38 additions & 48 deletions tripper/README.md → docs/overview.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Triplestore
===========
> A Python package encapsulating different triplestores using the strategy
> design pattern.
This package has by itself no dependencies outside the standard library,
but the triplestore backends may have.

The main class is Triplestore, who's `__init__()` method takes the name of the
backend to encapsulate as first argument. Its interface is strongly inspired
by rdflib.Graph, but simplified when possible to make it easy to use. Some
important differences:
# Overview

<!-- markdownlint-disable MD007 -->

A Python package encapsulating different triplestores using the strategy design pattern.

This package has by itself no dependencies outside the standard library, but the triplestore backends may have.

The main class is Triplestore, who's `__init__()` method takes the name of the backend to encapsulate as first argument.
Its interface is strongly inspired by rdflib.Graph, but simplified when possible to make it easy to use.
Some important differences:

- all IRIs are represented by Python strings
- blank nodes are strings starting with "_:"
- literals are constructed with `Literal()`
Expand All @@ -19,32 +19,30 @@ from triplestore import Triplestore
ts = Triplestore(backend="rdflib")
```

The module already provides a set of pre-defined namespaces that simplifies
writing IRIs. For example:
The module already provides a set of pre-defined namespaces that simplifies writing IRIs.
For example:

```python
from triplestore import RDFS, OWL
RDFS.subClassOf
# -> 'http://www.w3.org/2000/01/rdf-schema#subClassOf'
```

New namespaces can be created using the Namespace class, but are usually
added with the `bind()` method:
New namespaces can be created using the Namespace class, but are usually added with the `bind()` method:

```python
ONTO = ts.bind("onto", "http://example.com/onto#")
ONTO.MyConcept
# -> 'http://example.com/onto#MyConcept'
```

Namespace also support access by label and IRI checking. Both of these features
requires loading an ontology. The following example shows how to create an EMMO
namespace with IRI checking. The keyword argument `label_annotations=True` enables
access by `skos:prefLabel`, `rdfs:label` or `skos:altLabel`. The `check=True`
enables checking for existing IRIs. The `triplestore_url=...` is a resolvable URL
that can be read by the 'rdflib' backend. It is needed, because the 'rdflib'
backend is currently not able to load EMMO from the "http://emmo.info/emmo#"
namespace.
Namespace also support access by label and IRI checking.
Both of these features requires loading an ontology.
The following example shows how to create an EMMO namespace with IRI checking.
The keyword argument `label_annotations=True` enables access by `skos:prefLabel`, `rdfs:label` or `skos:altLabel`.
The `check=True` enables checking for existing IRIs.
The `triplestore_url=...` is a resolvable URL that can be read by the 'rdflib' backend.
It is needed, because the 'rdflib' backend is currently not able to load EMMO from the `http://emmo.info/emmo#` namespace.

```python
EMMO = ts.bind(
Expand All @@ -59,8 +57,7 @@ EMMO.invalid_name
# -> NoSuchIRIError: http://emmo.info/emmo#invalid_name
```

New triples can be added either with the `parse()` method (for
backends that support it) or the `add()` and `add_triples()` methods:
New triples can be added either with the `parse()` method (for backends that support it) or the `add()` and `add_triples()` methods:

```python
# en(msg) is a convenient function for adding english literals.
Expand All @@ -73,17 +70,15 @@ ts.add_triples([
])
```

For backends that support it the triplestore can be serialised using
`serialize()`:
For backends that support it the triplestore can be serialised using `serialize()`:

```python
ts.serialize("onto2.ttl")
```

A set of convenient functions exists for simple queries, including
`triples()`, `subjects()`, `predicates()`, `objects()`, `subject_predicates()`,
`subject_objects()`, `predicate_objects()` and `value()`. Except for `value()`,
they return the result as generators. For example:
A set of convenient functions exists for simple queries, including `triples()`, `subjects()`, `predicates()`, `objects()`, `subject_predicates()`, `subject_objects()`, `predicate_objects()` and `value()`.
Except for `value()`, they return the result as generators.
For example:

```python
ts.objects(subject=ONTO.MyConcept, predicate=RDFS.subClassOf)
Expand All @@ -92,31 +87,28 @@ list(ts.objects(subject=ONTO.MyConcept, predicate=RDFS.subClassOf))
# -> ['http://www.w3.org/2002/07/owl#Thing']
```

The `query()` and `update()` methods can be used to query and update the
triplestore using SPARQL.
The `query()` and `update()` methods can be used to query and update the triplestore using SPARQL.

Finally Triplestore has two specialised methods `add_mapsTo()` and
`add_function()` that simplify working with mappings. `add_mapsTo()` is
convinient for defining new mappings:
Finally Triplestore has two specialised methods `add_mapsTo()` and `add_function()` that simplify working with mappings.
`add_mapsTo()` is convinient for defining new mappings:

```python
from triplestore import Namespace
META = Namespace("http://onto-ns.com/meta/0.1/MyEntity#")
ts.add_mapsTo(ONTO.MyConcept, META.my_property)
```

It can also be used with DLite and SOFT7 data models. Here we repeat
the above with DLite:
It can also be used with DLite and SOFT7 data models.
Here we repeat the above with DLite:

```python
import dlite
meta = dlite.get_instance("http://onto-ns.com/meta/0.1/MyEntity")
ts.add_mapsTo(ONTO.MyConcept, meta, "my_property")
```

The `add_function()` describes a function and adds mappings for its
arguments and return value(s). Currently it only supports the [Function
Ontology (FnO)](https://fno.io/).
The `add_function()` describes a function and adds mappings for its arguments and return value(s).
Currently it only supports the [Function Ontology (FnO)](https://fno.io/).

```python
def mean(x, y):
Expand All @@ -130,12 +122,11 @@ ts.add_function(
)
```

## Further development

- Update the `query()` method to return the SPARQL result in a backend-independent way.
- Add additional backends. Candidates include:

Further development
-------------------
* Update the `query()` method to return the SPARQL result in a backend-
independent way.
* Add additional backends. Candidates include:
- list of tuples
- owlready2/EMMOntoPy
- OntoRec/OntoFlowKB
Expand All @@ -145,4 +136,3 @@ Further development
- Apache Jena Fuseki
- Allegrograph
- Wikidata
* Add ontological validation of physical dimension to Triplestore.mapsTo().
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ plugins:

nav:
- Home: index.md
- Overview: overview.md
- License: LICENSE.md
- Changelog: CHANGELOG.md
- ... | api_reference/**
Expand Down
Loading

0 comments on commit 8ef8b4e

Please sign in to comment.