shexstatements package

It’s also possible to use shexstatements in Python programs. This page gives a complete detail of the different modules that can be used in the programs.

In our first example, we take a look at the method generate_shex_from_csv, which takes as input a CSV file containing shexstatements and a delimiter. In this example, we use “,” as a delimiter.

from shexstatements.shexfromcsv import CSV

shex = CSV.generate_shex_from_csv("language.csv", delim=",")
print(shex)

In our second example, we use a data string consisting of shexstatements and make use of the function generate_shex_from_data_string. Note here, that we use “|” as a delimiter.

from shexstatements.shexfromcsv import CSV

shexstatements="""
wd|<http://www.wikidata.org/entity/>
wdt|<http://www.wikidata.org/prop/direct/>
xsd|<http://www.w3.org/2001/XMLSchema#>

@language|wdt:P31|wd:Q34770|# instance of a language
@language|wdt:P1705|LITERAL|# native name
@language|wdt:P17|.|+|# spoken in country
@language|wdt:P2989|.|+|# grammatical cases
@language|wdt:P282|.|+|# writing system
@language|wdt:P1098|.|+|# speakers
@language|wdt:P1999|.|*|# UNESCO language status
@language|wdt:P2341|.|+|# indigenous to
"""

shex = CSV.generate_shex_from_data_string(shexstatements)
print(shex)

Submodules

shexstatements.shexfromcsv

class shexstatements.shexfromcsv.CSV

This class contains functions that can be used to generate ShEx from a data string or a CSV file.

static generate_shex_from_csv(filepath, delim=', ', skip_header=False, filename=True)

This method can be used to generate ShEx from data string. However, the input data string must contain one or more lines. Each line contains ‘|’ separated values. If filepath is a string, filename should be set to false.

Parameters:
  • filepath (str) – This parameter can contain either a file path of a CSV file or shexstatements in CSV format.
  • delim (str) – a delimiter. Allowed values include ‘,’, ‘|’ and ‘;’
  • skip_header (bool) – if the first line is a header, set this value to True. By default, the value is False.
  • filename (bool) – if ‘filepath’ is a string, then this filename must be set to False
Returns:

shape expression

Return type:

shex

static generate_shex_from_data_string(data)

This method can be used to generate ShEx from data string. However, the input data string must contain one or more lines. Each line contains ‘|’ separated values.

Parameters:data (str) – shexstatements in CSV format, using “|” as a delimiter.
Returns:shape expression
Return type:shex

shexstatements.shexjfromcsv

class shexstatements.shexjfromcsv.ShExJCSV

This class contains functions that can be used to generate ShExJ from a ShEx.

static generate_shexj_from_csv(filepath, delim=', ', skip_header=False)

This method can be used to generate ShExJ from ShExStatements CSV file

Parameters:
  • filepath (str) – This parameter can contain either a file path of a CSV file or shexstatements in CSV format.
  • delim (str) – a delimiter. Allowed values include ‘,’, ‘|’ and ‘;’
  • skip_header (bool) – if the first line is a header, set this value to True. By default, the value is False.
Returns:

shape expression in JSON format (ShExJ)

Return type:

shexj

static generate_shexj_from_shexstament(shexstatement)

This method can be used to generate ShEx from data string. However, the input data string must contain one or more lines. Each line contains ‘|’ separated values.

Parameters:shexstatement (str) – shex
Returns:shape expression in JSON (ShExJ)
Return type:shexj

shexstatements.shexfromapplprofilecsv

class shexstatements.shexfromapplprofilecsv.ApplicationProfile

This class contains functions that can be used to generate ShEx from a data string or CSV application profile file.

generate_shex_from_csv(delim=', ', skip_header=False)

This method can be used to generate ShEx from application profile CSV file. However, the input file must contain one or more lines. Each line contains ‘|’ separated values. If filepath is a string, filename should be set to false.

Parameters:
  • filepath (str) – This parameter can contain either a file path of a CSV file or shexstatements in CSV format.
  • delim (str) – a delimiter. Allowed values include ‘,’, ‘|’ and ‘;’
  • skip_header (bool) – if the first line is a header, set this value to True. By default, the value is False.
Returns:

shape expression

Return type:

shex

shexstatements.errors

exception shexstatements.errors.ParserError(message)
exception shexstatements.errors.UnrecognizedCharacterError(message)