pyirk.core

Core module of pyirk

Module Contents

Classes

Entity

Abstract parent class for both Relations and Items.

PrefixShortCut

DataStore

Provides objects to store all data that would be global otherwise

Item

KWArgManager

This class processes all keyword args for entity creation

SingleKWArgProcessor

This class processes a single keyword arg for entity creation

Relation

RelationRole

Statement types.

RawQualifier

Precursor to a real Qualifier (which is a Statement) where the subject is yet unspecified (will be the qualified Statement). Instances of this class are produced by QualifierFactory

QualifierFactory

Convenience class to create an RawQualifier. This allows syntax like:

Statement

Models a concrete (instantiated/applied) relation between entities. This is basically a dict.

QualifierStatement

RuleResult

Functions

check_type

Use the pydantic package to check for (complex) types from the typing module. If type checking passes returns True. This allows to use assert check_type(...) which allows to omit those type checks (together with other assertions) for performance reasons, e.g. with python -O ... . :param obj: the object to check :param expected_type: primitive or complex type (like typing.List[dict]) :return: True (or raise an TypeError)

wrap_function_with_search_uri_context

get_label_to_item_dict

Returns a map from labels to items. If a label occurs multiple times the last occurrence is decisive. If this is not declared as expected via known_duplicates a warning is generated.

get_label_to_relation_dict

Returns a map from labels to relations. If a label occurs multiple times the last occurrence is decisive. If this is not declared as expected via known_duplicates a warning is generated.

process_kwargs_for_entity_creation

:return: return new_kwargs, lang_related_kwargs

process_lang_related_kwargs_for_entity_creation

This function processes language related keyword args for relations which have R32__is_functional_for_each_language=True

create_item

:param key_str: “” or unique key of this item (something like I1234) :param kwargs: further relations

run_hooks

Run (previously registered) hooks after the creation of entities. This can be used for sanity checking etc.

register_hook

repl_spc_by_udsc

tolerant_removal

call sequence.remove(element) but tolerate KeyError and ValueError :param sequence: :param element: :return:

create_relation

:param key_str: “” or unique key of this relation (something like R1234); if empty key will be retrieved via inspection of the caller code

create_builtin_item

create_builtin_relation

replace_and_unlink_entity

Replace all statements where old_entity is subject or object with new relations where new_entity is sub or obj. For the “subject-case” only process those statements for which new_entity does not yet have any relations. Thus do not replace e.g. the R4__is_instance_of statement of new_entity.

is_true

format_entity_html

is_subclass

is_instance

is_subproperty

check if item is subproperty of parent_property. item == parent_p will return True as well.

Data

allowed_literal_types

RELKEYS_WITH_LITERAL_RANGE

pf

ds

YAML_VALUE

re_prefix_shortkey_suffix

re_suffix_underscore

re_suffix_square_brackets

langcode_end_pattern

VALID_HOOK_PHASES

VALID_HOOK_TYPES

_uri_stack

_search_uri_stack

_attr_name_cache

df

en

de

fr

it

es

API

pyirk.core.allowed_literal_types

()

pyirk.core.RELKEYS_WITH_LITERAL_RANGE

(‘R1’, ‘R2’, ‘R77’)

pyirk.core.check_type(obj, expected_type, strict=True)

Use the pydantic package to check for (complex) types from the typing module. If type checking passes returns True. This allows to use assert check_type(...) which allows to omit those type checks (together with other assertions) for performance reasons, e.g. with python -O ... . :param obj: the object to check :param expected_type: primitive or complex type (like typing.List[dict]) :return: True (or raise an TypeError)

class pyirk.core.Entity(base_uri)

Bases: abc.ABC

Abstract parent class for both Relations and Items.

Do not forget to call self.post_init at the end of init in subclasses.

Initialization

short_key: str

None

__call__(*args, **kwargs)
property name_labeled_key
idoc(adhoc_label: str)

idoc means “inline doc”. This function allows to attach a label to entities when using them in code because it returns just the Entity-object itself. Thus one can use the following expressions interchangeably: I1234 and I1234.idoc("human readable item name")

Note that there is a shortcut to this function: `I1234[“human readable item name”]

:return: self

__getitem__(adhoc_label)

This magic method overloads the […]-operator. See docs for idoc for more information.

:param adhoc_label: :return: self

__getattr__(attr_name)
__setattr__(attr_name: str, attr_value: Any)
__process_attribute_name(attr_name: str, exception_type=AttributeError) pyirk._core.keymanager.ProcessedStmtKey
__eq__(other)
__post_init__()
_perform_inheritance()

Transfer method prototypes from parent to child classes

:return:

_perform_instantiation()

Convert all method prototypes from class-item into methods of instance-item :return:

_get_relation_contents(rel_uri: str, lang_indicator=None)
classmethod add_method_to_class(func)

Used to add methods to the class from the builtin_entities module. This mechanism (adding the method later) allows to keep the dependency monodirectional

add_method(func: callable, name: pyirk._core.keymanager.Optional[str] = None)

Add a method to this instance (self). If there are R4 relations pointing from child items to self, this method is also inherited to those child items.

:param func: :param name: the name under which the callable object should be accessed :return:

_set_relations_from_init_kwargs(**kwargs)

This method is called explicitly from the init-method of subclasses after preprocessing the kwargs

:param kwargs: :return:

set_multiple_relations(relation: pyirk._core.serialization.Union[pyirk.core.Relation, str], obj_seq: pyirk._core.serialization.Union[tuple, list], *args, **kwargs) pyirk._core.mod_management.List[pyirk.core.Statement]

Convenience function to create multiple Statements at once

set_relation(relation: pyirk._core.serialization.Union[pyirk.core.Relation, str], obj, scope: pyirk.core.Entity = None, proxyitem: pyirk._core.keymanager.Optional[pyirk.core.Item] = None, qualifiers: pyirk._core.keymanager.Optional[pyirk._core.mod_management.List[pyirk.core.RawQualifier]] = None, prevent_duplicate=False) pyirk._core.keymanager.Optional[pyirk.core.Statement]

Allows to add a relation after the item was created.

:param relation: Relation-Entity (or its short_key) :param obj: target (object) of the relation (where self is the subject) :param scope: Entity for the scope in which the relation is defined :param proxyitem: optional item to which the Statement is associated (e.g. an equation-instance) :param qualifiers: optional list of RawQualifiers (see docstring of this class) :param prevent_duplicate bool; prevent the creation of a statement which already exists. :return:

_set_relation(rel_uri: str, rel_content: object, scope: pyirk._core.keymanager.Optional[pyirk.core.Entity] = None, qualifiers: pyirk._core.keymanager.Optional[list] = None, proxyitem: pyirk._core.keymanager.Optional[pyirk.core.Item] = None) pyirk.core.Statement
get_relations(key_str_or_uri: pyirk._core.keymanager.Optional[str] = None, return_subj: bool = False, return_obj: bool = False) pyirk._core.serialization.Union[pyirk._core.keymanager.Dict[str, list], list]

Return all Statement instance where this item is subject

:param key_str_or_uri: optional; either a verbose key_str (of a builtin entity) or a full uri; if passed only return the result for this key :param return_subj: default False; if True only return the subject(s) of the relation edges, not the whole statement

:return: either the whole dict or just one value (of type list)

get_inv_relations(key_str_or_uri: pyirk._core.keymanager.Optional[str] = None, return_subj: bool = False, return_obj: bool = False) pyirk._core.serialization.Union[pyirk._core.keymanager.Dict[str, list], list]

Return all Statement instance where this item is object

:param key_str_or_uri: optional; either a verbose key_str (of a builtin entity) or a full uri; if passed only return the result for this key :param return_subj: default False; if True only return the subject(s) of the relation edge(s), not the whole statement :param return_obj: default False; if True only return the object(s) of the relation edge(s), not the whole statement

:return: either the whole dict or just one value (of type list)

static _return_relations(base_dict, key_str_or_uri: str, return_subj: bool = False, return_obj: bool = False) pyirk._core.serialization.Union[pyirk._core.keymanager.Dict[str, list], list]

:param base_dict: either ds.statements or ds.inv_statements :param key_str_or_uri: optional; either a verbose key_str (of a builtin entity) or a full uri; if passed only return the result for this key :param return_subj: default False; if True only return the subject(s) of the relation edge(s), not the whole statement :param return_obj: default False; if True only return the object(s) of the relation edge(s), not the whole statement :return:

overwrite_statement(rel_key_str_or_uri: str, new_obj: pyirk.core.Entity, qualifiers=None) pyirk.core.Statement
finalize()

Method which is intended to be explicitly called if an (automatically created) entity is finished.

Background: some entities like evaluated mappings are manipulated after creation. Hooks like consistency-checking have to be executed afterwards.

__hash__()

Defining a hash method allows to use Entities as keys in dicts, or create sets of them.

update_relations(**kwargs)
pyirk.core.wrap_function_with_search_uri_context(func, uri=None)
class pyirk.core.PrefixShortCut
__getattribute__(prefix_name: str) Any
pyirk.core.pf

‘PrefixShortCut(…)’

class pyirk.core.DataStore

Provides objects to store all data that would be global otherwise

Initialization

initialize_hooks() dict
get_item_by_label(label) pyirk.core.Entity

Search over all item and return the first item which has the provided label. Useful during interactive debugging. Not useful for production!

get_entity_by_key_str(key_str, mod_uri=None) pyirk.core.Entity

:param key_str: str like I1234 or I1234__some_label :param mod_uri: optional uri of the module; if None the active module is assumed

:return: corresponding entity

get_entity_by_uri(uri: str, etype=None, strict=True) pyirk._core.serialization.Union[pyirk.core.Entity, None]
static _default_subject_filter(entity)

used to prevent items from scopes showing up inside the results of get_subjects_for_relation.

get_subjects_for_relation(rel_uri: str, filter=None)
get_statements(entity_uri: str, rel_uri: str) pyirk._core.mod_management.List[pyirk.core.Statement]

self.statements maps an entity_key to an inner_dict. The inner_dict maps an relation_key to a Statement or List[Statement].

:param entity_uri: :param rel_uri: :return:

set_statement(stm: pyirk.core.Statement) None

Insert a Statement into the relevant data structures of the DataStorage (self)

This method does not handle the dual relation. It must be created and stored separately.

:param stm: Statement instance :return:

get_uri_for_prefix(prefix: str) str
preprocess_query(query, sanity_check=True)
append_scope(scope)

Called when enter-ing a scoping context manager

remove_scope(scope)

Called when exit-ing a scoping context manager

get_current_scope()
pyirk.core.ds

‘DataStore(…)’

pyirk.core.YAML_VALUE

None

pyirk.core.get_label_to_item_dict(known_duplicates: list = None)

Returns a map from labels to items. If a label occurs multiple times the last occurrence is decisive. If this is not declared as expected via known_duplicates a warning is generated.

:param known_duplicates: sequence of labels which are known to occur multiple times

pyirk.core.get_label_to_relation_dict(known_duplicates: list = None)

Returns a map from labels to relations. If a label occurs multiple times the last occurrence is decisive. If this is not declared as expected via known_duplicates a warning is generated.

:param known_duplicates: sequence of labels which are known to occur multiple times

pyirk.core.re_prefix_shortkey_suffix

‘compile(…)’

pyirk.core.re_suffix_underscore

‘compile(…)’

pyirk.core.re_suffix_square_brackets

‘compile(…)’

pyirk.core.langcode_end_pattern

‘compile(…)’

class pyirk.core.Item(base_uri: str, key_str: str, **kwargs)

Bases: pyirk.core.Entity

__repr__()
pyirk.core.process_kwargs_for_entity_creation(entity_key: str, kwargs: dict) tuple[dict, dict]

:return: return new_kwargs, lang_related_kwargs

class pyirk.core.KWArgManager(entity_key: str, kwargs: dict)

This class processes all keyword args for entity creation

Initialization

process()
class pyirk.core.SingleKWArgProcessor(kwam: pyirk.core.KWArgManager, kwarg_name: str, kwarg_value: str)

This class processes a single keyword arg for entity creation

Initialization

handle_kwarg_stage1()

Determine new_key

handle_kwarg_stage2()
dispatch_value_multiplicity_for_rk_with_lr()

Situation for relkeys with literal range: self.kwarg_value might be a ‘scalar’ value or list of ‘scalar’ values. This method handles the difference and then calls the actual processing

handle_rk_with_lr(scalar_kwarg_value)

‘rk’ means relkeys ‘lr’ means literal range

Background: Relation keys like R1, R2 and R77 are used in triples where the object is a Literal. R1__has_label, R2__has_description are functional (R32__is_functional_for_each_language). R77__has_alternative_label is not functional (neither R22__is_functional nor R32).

This function handles the different cases

_handle_kwarg_for_functional_rel(scalar_kwarg_value)
_check_for_valid_language(scalar_kwarg_value, first_value=False)
_handle_value(kwarg_value, lang_related_value_list=None) pyirk._core.serialization.Literal

This function processes language related keyword args for relations which have R32__is_functional_for_each_language=True

pyirk.core.create_item(key_str: str = '', **kwargs) pyirk.core.Item

:param key_str: “” or unique key of this item (something like I1234) :param kwargs: further relations

:return: newly created item

class pyirk.core.Relation(base_uri: str, short_key: str, **kwargs)

Bases: pyirk.core.Entity

__repr__()
class pyirk.core.RelationRole(*args, **kwds)

Bases: pyirk._core.keymanager.Enum

Statement types.

Initialization

SUBJECT

0

PREDICATE

1

OBJECT

2

pyirk.core.VALID_HOOK_PHASES

[‘post-create’, ‘post-finalize’]

pyirk.core.VALID_HOOK_TYPES

[‘post-create-entity’, ‘post-create-item’, ‘post-create-relation’, ‘post-finalize-entity’, ‘post-fin…

pyirk.core.run_hooks(entity: pyirk.core.Entity, phase: str) None

Run (previously registered) hooks after the creation of entities. This can be used for sanity checking etc.

pyirk.core.register_hook(type_str: str, func: callable) None
pyirk.core.repl_spc_by_udsc(txt: str) str
class pyirk.core.RawQualifier(rel: pyirk.core.Relation, obj: pyirk._core.serialization.Union[pyirk._core.serialization.Literal, pyirk.core.Entity])

Precursor to a real Qualifier (which is a Statement) where the subject is yet unspecified (will be the qualified Statement). Instances of this class are produced by QualifierFactory

Initialization

__repr__()
class pyirk.core.QualifierFactory(relation: pyirk.core.Relation, registry_name: pyirk._core.keymanager.Optional[str] = None)

Convenience class to create an RawQualifier. This allows syntax like:

start_date = QualifierFactory(R1234["start date"])
# ...
I2746["Rudolf Kalman"].set_relation(R1833["has employer"], I7301["ETH Zürich"], qualifiers=[start_date(1973)])

Initialization

:param relation: :param registry_name: optional str; if not None this is the key under which this QF is stored in ds.qff_dict.

__call__(obj)
class pyirk.core.Statement(relation: pyirk.core.Relation = None, relation_tuple: tuple = None, role: pyirk.core.RelationRole = None, corresponding_entity: pyirk.core.Entity = None, corresponding_literal=None, scope=None, qualifiers: pyirk._core.keymanager.Optional[pyirk._core.serialization.Union[pyirk._core.mod_management.List[pyirk.core.RawQualifier], pyirk._core.mod_management.List[pyirk.core.QualifierStatement]]] = None, proxyitem: pyirk._core.keymanager.Optional[pyirk.core.Item] = None)

Models a concrete (instantiated/applied) relation between entities. This is basically a dict.

Initialization

:param relation: :param relation_tuple: :param role: RelationRole.SUBJECT for normal and RelationRole.OBJECT for inverse statements :param corresponding_entity: This is the entity on the “other side” of the relation (depending of role) or None in case that other side is a literal :param corresponding_literal: This is the literal on the “other side” of the relation (depending of role) or :param scope: None in case that other side is an Entity :param qualifiers: list of relation edges, that describe self more precisely (cf. wikidata qualifiers) :param proxyitem: associated item; e.g. a equation-item

property key_str
__repr__()
_process_qualifiers(qlist: pyirk._core.serialization.Union[pyirk._core.mod_management.List[pyirk.core.RawQualifier], pyirk._core.mod_management.List[pyirk.core.QualifierStatement]], scope: pyirk._core.keymanager.Optional[pyirk.core.Entity] = None) None
is_qualifier()
get_first_qualifier_obj_with_rel(key=None, uri=None, tolerate_key_error=False)

Remove this Statement instance from all data structures in the global data storage :return:

class pyirk.core.QualifierStatement(*args, **kwargs)

Bases: pyirk.core.Statement

pyirk.core.tolerant_removal(sequence, element)

call sequence.remove(element) but tolerate KeyError and ValueError :param sequence: :param element: :return:

pyirk.core.create_relation(key_str: str = '', **kwargs) pyirk.core.Relation

:param key_str: “” or unique key of this relation (something like R1234); if empty key will be retrieved via inspection of the caller code

:param kwargs: further relations (e.g. R1__has_label etc.)

:return: newly created relation

pyirk.core.create_builtin_item(*args, **kwargs) pyirk.core.Item
pyirk.core.create_builtin_relation(*args, **kwargs) pyirk.core.Relation
pyirk.core._uri_stack

[]

pyirk.core._search_uri_stack

[]

pyirk.core._attr_name_cache: dict

None

Replace all statements where old_entity is subject or object with new relations where new_entity is sub or obj. For the “subject-case” only process those statements for which new_entity does not yet have any relations. Thus do not replace e.g. the R4__is_instance_of statement of new_entity.

Then unlink old_entity.

pyirk.core.df

‘LanguageCode(…)’

pyirk.core.en

‘LanguageCode(…)’

pyirk.core.de

‘LanguageCode(…)’

pyirk.core.fr

‘LanguageCode(…)’

pyirk.core.it

‘LanguageCode(…)’

pyirk.core.es

‘LanguageCode(…)’

class pyirk.core.RuleResult

Initialization

add_statement(stm: pyirk.core.Statement)
add_statements(stms: pyirk._core.mod_management.List[pyirk.core.Statement])
add_entity(entity: pyirk.core.Entity)
extend(part: pyirk.core.RuleResult)
add_partial(part: pyirk.core.RuleResult)
__repr__()
property rule

Convenience property for easy access to the corresponding rule

get_new_triples() list[tuple[pyirk.core.Entity]]
pyirk.core.is_true(subject: pyirk.core.Entity, predicate: pyirk.core.Relation, object) tuple[bool, None]
pyirk.core.format_entity_html(e: pyirk.core.Entity)
pyirk.core.is_subclass(item: pyirk.core.Item, parent_item: pyirk.core.Item)
pyirk.core.is_instance(item: pyirk.core.Item, parent_item: pyirk.core.Item)
pyirk.core.is_subproperty(item: pyirk.core.Item, parent_property: pyirk.core.Item)

check if item is subproperty of parent_property. item == parent_p will return True as well.