pyirk._core.keymanager

Key-/short-key-management helpers extracted from :mod:pyirk.core.

The symbols defined here are import-time safe: none of them needs any module-global of core while this module is being imported. Symbols that use core module-globals (ds, the compiled key regexes, the uri-stacks, uri_context etc.) at call time access them module-qualified via the _core module object, whose attributes are only read once the functions are actually called. This keeps the import monodirectional: core imports this module (early), this module only binds the (partially loaded) core module object.

Module Contents

Classes

EType

Entity types.

SType

Statement types.

VType

Dict value types.

ProcessedStmtKey

Container for processed statement key

KeyManager

Class for a flexible and comprehensible key management. Every pyirk module must have its own (passed via)

Functions

unpack_l1d

unpack a dict of length 1 :param l1d: :return:

process_key_str

In IRK there are the following kinds of keys: - a) short_key like R1234 - b) name-labeled key like R1234__my_relation (consisting of a short_key, a delimiter (__) and a label) - c) prefixed short_key like bi__R1234 - d) prefixed name-labeled key like bi__R1234__my_relation

_resolve_prefix

get uri from prefix or from passed argument or from active module

check_processed_key_label

Check if the used label of a key_str matches the actual label (R1) of that entity

ilk2nlk

convert index labeled key (R1234[“my relation”]) to name labeled key (R1234__my_relation)

u

Convenience function converting “[prefix__]I1234__my_label” to “[moduri#]I1234”. If no prefix is given the active module and builtin_entities are searched for (in this order).

pop_uri_based_key

Create a short key (int or str) (optionally with prefixes) from the reservoir.

generate_new_key

Utility function for the command line.

print_new_keys

print n random integer keys from the pregenerated list.

Data

__all__

API

pyirk._core.keymanager.__all__

[‘EType’, ‘SType’, ‘VType’, ‘ProcessedStmtKey’, ‘unpack_l1d’, ‘process_key_str’, ‘_resolve_prefix’, …

class pyirk._core.keymanager.EType(*args, **kwds)

Bases: enum.Enum

Entity types.

Initialization

ITEM

0

RELATION

1

LITERAL

2

class pyirk._core.keymanager.SType(*args, **kwds)

Bases: enum.Enum

Statement types.

Initialization

CREATION

0

EXTENSION

1

UNDEFINED

2

class pyirk._core.keymanager.VType(*args, **kwds)

Bases: enum.Enum

Dict value types.

Initialization

LITERAL

0

ENTITY

1

LIST

2

DICT

3

class pyirk._core.keymanager.ProcessedStmtKey

Container for processed statement key

short_key: str

None

etype: pyirk._core.keymanager.EType

None

stype: pyirk._core.keymanager.SType

None

vtype: pyirk._core.keymanager.VType

None

content: object

None

delimiter: str

None

label: str

None

prefix: str

None

uri: str

None

lang_indicator: str

None

original_key_str: str

None

pyirk._core.keymanager.unpack_l1d(l1d: Dict[str, object])

unpack a dict of length 1 :param l1d: :return:

pyirk._core.keymanager.process_key_str(key_str: str, check: bool = True, resolve_prefix: bool = True, mod_uri: str = None) pyirk._core.keymanager.ProcessedStmtKey

In IRK there are the following kinds of keys: - a) short_key like R1234 - b) name-labeled key like R1234__my_relation (consisting of a short_key, a delimiter (__) and a label) - c) prefixed short_key like bi__R1234 - d) prefixed name-labeled key like bi__R1234__my_relation

- e) index-labeled key like  `R1234["my relation"]`
- f) prefixed index-labeled key like  `bi__R1234["my relation"]`

See also: userdoc/overview.html#keys-in-pyirk

Also, the leading character indicates the entity type (EType).

This function expects any of these cases. :param key_str: a string like “R1234__my_relation” or “R1234” or “bi__R1234__my_relation” :param check: boolean flag; determines if the label part should be checked wrt its consistency to :param resolve_prefix: boolean flag; determines if :param mod_uri: optional uri of the module

:return: a data structure which allows to access short_key, type and label separately

pyirk._core.keymanager._resolve_prefix(pr_key: pyirk._core.keymanager.ProcessedStmtKey, passed_mod_uri: str = None) None

get uri from prefix or from passed argument or from active module

pyirk._core.keymanager.check_processed_key_label(pkey: pyirk._core.keymanager.ProcessedStmtKey) None

Check if the used label of a key_str matches the actual label (R1) of that entity

:param pkey: :return:

pyirk._core.keymanager.ilk2nlk(ilk: str) str

convert index labeled key (R1234[“my relation”]) to name labeled key (R1234__my_relation)

pyirk._core.keymanager.u(key_str: str) str

Convenience function converting “[prefix__]I1234__my_label” to “[moduri#]I1234”. If no prefix is given the active module and builtin_entities are searched for (in this order).

:param key_str: :return:

class pyirk._core.keymanager.KeyManager(minval=1000, maxval=99999, keyseed=None)

Class for a flexible and comprehensible key management. Every pyirk module must have its own (passed via)

Initialization

:param minval: int :param maxval: int :param keyseed: int; This allows a module to create its own random key order

pop(index: int = -1) int
_generate_key_numbers() None

Creates a reservoir of keynumbers, e.g. for automatically created entities. Due to the hardcoded seed value these numbers are stable between runs of the software, which simplifies development and debugging.

This function is also called after unloading a module because the respective keys are “free” again

Rationale behind random keys: During creation of knowledge bases it frees the mind of thinking too much about a meaningful order in which to create entities.

:return: list of integers

pyirk._core.keymanager.pop_uri_based_key(prefix: Optional[str] = None, prefix2: str = '') Union[int, str]

Create a short key (int or str) (optionally with prefixes) from the reservoir.

:param prefix: :param prefix2: :return:

pyirk._core.keymanager.generate_new_key(prefix, prefix2='', mod_uri=None)

Utility function for the command line.

:param prefix: :param prefix2: :param mod_uri: :return:

pyirk._core.keymanager.print_new_keys(n=30, loaded_mod=None)

print n random integer keys from the pregenerated list.

:return: