nemoguardrails.colang.v1_0.lang.utils

View as Markdown

Module Contents

Functions

NameDescription
char_splitHelper method to split a string by a given character.
extract_main_tokenHelper to extract the main token from a line
extract_topic_objectHelper to extract the object from the definition of a topic.
get_first_keyHelper to get the first key, which transpiles correctly.
get_numbered_linesHelper to returned numbered lines.
get_stripped_tokens-
params_tokenizeTokenizer specific to the params parsing.
parse_package_nameHelper to extract a normalized package name.
remove_tokenHelper to remove a token
split_argsSplit a string that represents arguments for a function.
split_maxHelper to simulate the behavior of .split(…, max_instances).
string_hashA simple string hash with an equivalent implementation in javascript.
word_splitA simple logic that splits by word but takes strings into accounts.
ws_tokenizeTokenize a text by whitespace and taking strings into account.

API

nemoguardrails.colang.v1_0.lang.utils.char_split(
text: str,
c: str,
ignore_parenthesis = False,
ignore_strings = False
) -> typing.List[str]

Helper method to split a string by a given character.

:param text: The text to split. :param c: The character to use as the separator :param ignore_parenthesis: If set, it will now account for lists i.e. starting with [], () or {} :param ignore_strings: If set, it will not take into account strings.

nemoguardrails.colang.v1_0.lang.utils.extract_main_token(
text: str
)

Helper to extract the main token from a line

nemoguardrails.colang.v1_0.lang.utils.extract_topic_object(
text: typing.Text
) -> typing.Tuple[typing.Text, typing.Optional[typing.Text]]

Helper to extract the object from the definition of a topic.

Supported expressions is_open_source is_open_source for @roboself is_open_source for companyisopensource(company is_open_source(roboself) is_open_source(@roboself)

nemoguardrails.colang.v1_0.lang.utils.get_first_key(
d: dict
)

Helper to get the first key, which transpiles correctly.

nemoguardrails.colang.v1_0.lang.utils.get_numbered_lines(
content: str
)

Helper to returned numbered lines.

Comments and empty lines are ignored.

nemoguardrails.colang.v1_0.lang.utils.get_stripped_tokens(
tokens: typing.List[str]
)
nemoguardrails.colang.v1_0.lang.utils.params_tokenize(
text
)

Tokenizer specific to the params parsing.

nemoguardrails.colang.v1_0.lang.utils.parse_package_name(
text
)

Helper to extract a normalized package name.

nemoguardrails.colang.v1_0.lang.utils.remove_token(
token: str,
line: str
)

Helper to remove a token

nemoguardrails.colang.v1_0.lang.utils.split_args(
args_str: str
) -> typing.List[str]

Split a string that represents arguments for a function.

It supports keyword arguments and also correctly handles strings and lists/dicts.

Parameters:

args_str
str

The string with the arguments e.g. ‘name=“John”, colors=[“blue”, “red”]’

Returns: List[str]

The string that correspond to each individual argument value.

nemoguardrails.colang.v1_0.lang.utils.split_max(
text,
separator,
max_instances
)

Helper to simulate the behavior of .split(…, max_instances).

This implementation is meant to transpile correctly to the JS>

nemoguardrails.colang.v1_0.lang.utils.string_hash(
s
)

A simple string hash with an equivalent implementation in javascript.

module.exports.string_hash = function(s){ let hash = 0; if (s.length === 0) return hash; for (let i = 0; i < s.length; i++) { let char = s.charCodeAt(i); hash = ((hash<<5)-hash)+char; hash = hash & hash; // Convert to 32bit integer } if (hash < 0) hash *= -1;

return hash.toString(16); }

nemoguardrails.colang.v1_0.lang.utils.word_split(
text: str,
word: str
)

A simple logic that splits by word but takes strings into accounts.

nemoguardrails.colang.v1_0.lang.utils.ws_tokenize(
text
)

Tokenize a text by whitespace and taking strings into account.