Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify code by using Graph.set and Graph.value methods #30

Open
iosonopersia opened this issue Nov 19, 2022 · 0 comments
Open

Simplify code by using Graph.set and Graph.value methods #30

iosonopersia opened this issue Nov 19, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@iosonopersia
Copy link
Collaborator

iosonopersia commented Nov 19, 2022

I just discovered a method of the rdflib.Graph class (which is extensively used by this library) which is the perfect fit for all the entities' setter methods that concern functional properties (e.g. br.has_title).

Graph.set is a convenience method that ensures no more than a single value is set for a specific subject-predicate pair by removing any preexisting values and replacing them with the new one.

Additionally, Graph.value is a convenience method that returns no more than a single value for a specific subject-predicate pair (it raises an error when more than one value exists).

Documentation for the set method.
Documentation for the value method.
Convenience methods for functional properties.

Example of a possible substitution:

    @accepts_only('literal')
    def has_title(self, string: str) -> None:
        self.remove_title()
        self._create_literal(GraphEntity.iri_title, string)

would become something similar to

    @accepts_only('literal')
    def has_title(self, string: str) -> None:
        self.g.set((self.res, GraphEntity.iri_title, Literal(string))
@iosonopersia iosonopersia added the enhancement New feature or request label Nov 19, 2022
@iosonopersia iosonopersia changed the title Simplify code by using Graph.set method Simplify code by using Graph.set and Graph.value methods Nov 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant