Utils Module

This module contains some functions that can be called without instantiating a Converter.

tvd2rdf.utils.convert_namespace(prefix: str, uri: str) dict

Converts a prefix string and a URI string to a key value pair.

Checks are run to remove : at end of prefix if it is present, unless prefix is null string (‘’) or just ‘:’, in which case ‘:’ is used.

Raises:

TypeError – If the either parameter is not a string

Examples

>>> from tvd2rdf.utils import convert_namespace
>>> convert_namespace("ex:", "http://example.org/")
{'ex': 'http://example.org/'}
>>> convert_namespace(":","http://example.org/")
{':': 'http://example.org/'}
>>> convert_namespace("","http://example.org/") # note, no prefix.
{':': 'http://example.org/'}
tvd2rdf.utils.dump_config(config: dict, fname: str = None) str

Writes the information that determines how the TVD is processed.

If no fname is provided the config will be written to the terminal.

Parameters:
  • config (dict) – The config information.

  • fname (string) – The path/filename to which the data should be written.

Returns:

msg – A confirmation of where the data went.

Return type:

str

Raises:
  • FileNotFoundError – If the path to write the data cannot be found.

  • PermissionError – If you do not have permission to write to the required file.

  • Exception – For other errors.

Example

The following will load and write the default configuration, which can then be modified.

>>> from tvd2rdf.utils import load_config, dump_config
>>> config = load_config()
>>> dump_config(config, "config.yaml")
Config written to file config.yaml.
tvd2rdf.utils.load_config(config_fn: str = None) dict

Loads a YAML configuration file.

The configuration file contains information about the columns in the TVD and how to process them. If called with no config_fn string, the function will try to read a file called config.yaml in the working directory; if that fails a default config that is distributed with the package will be read.

Parameters:

config_fn (str) – The path/filename for the YAML config file.

Returns:

A dict structure obtained by yaml.safe_load .

Return type:

dict

Raises:
  • TypeError – If the parameter pass is not a string.

  • yaml.YAMLError – If the contents of the file cannot be read.

Example

The following will load the default, which can then be dumped in order to modify it.

>>> from tvd2rdf.utils import load_config
>>> config = load_config()
tvd2rdf.utils.str2URIRef(namespaces: dict, s: str) URIRef

Return a URIRef from a string that may be a URI or a cURI.

Raises:

ValueError – If the prefix used in a cURI is not in the namespaces dict.

Examples

Can be used with convert_namespace() to provide a value for namepaces.

>>> from tvd2rdf.utils import convert_namespace, str2URIRef
>>> ns = convert_namespace("ex:", "http://example.org/")
>>> str2URIRef(ns, "ex:Example")
rdflib.term.URIRef('http://example.org/Example')
>>> str2URIRef(ns, "http://example.org/")
rdflib.term.URIRef('http://example.org/')
tvd2rdf.utils.toLowerCamelCase(s: str) str

Converts a string to lowerCamelCase.

Examples

>>> from tvd2rdf.utils import toLowerCamelCase
>>> toLowerCamelCase("Hello World")
'helloWorld'
>>> toLowerCamelCase("alreadyInCamelCase")
'alreadyInCamelCase'
>>> toLowerCamelCase("UpperToLower Camel Case")
'upperToLowerCamelCase'
>>> toLowerCamelCase("Snakes_In_Python")
'snakesInPython'
>>> toLowerCamelCase("SHEESH-Kebabs")
'sheeshKebabs'
>>> toLowerCamelCase(42)
<stdin>:1: UserWarning: Connot convert 42 to camel case: not a string.