NbParser

class nbforager.NbParser(data: Dict[str, Any], strict: bool = False, **kwargs)

Dictionary parser for extracting values from a Netbox object using a chain of keys.

Netbox object may have None instead of a dictionary when a related object is absent, requiring constant data type checks. NbParser ensures the desired value is returned with the correct data type, even if the data is missing.

Raises NbParserError if strict=True and some keys are missing.

Initialize NbParser.

Parameters:
  • data (dict) – Netbox object.

  • strict (bool) – True - if data is invalid raise NbParserError, False - if data is invalid return empty data with proper type.

  • version (str) – Netbox version. Designed for compatibility with different versions.

any(*keys) Any

Get any value by keys.

Parameters:

keys – Chaining dictionary keys to retrieve the desired value.

Returns:

Value or None if the value is absent.

Return type:

Any

bool(*keys) bool

Get bool value by keys.

Parameters:

keys – Chaining dictionary keys to retrieve the desired value.

Returns:

Boolean value or False if the value is absent.

Return type:

bool

dict(*keys) Dict

Get dictionary value by keys.

Parameters:

keys – Chaining dictionary keys to retrieve the desired value.

Returns:

Dictionary value or an empty dictionary if the value is absent.

Return type:

dict

Raises:

NbParserError – If strict=True and the value is not a dictionary or key is absent.

float(*keys) float

Get float value by keys.

Parameters:

keys – Chaining dictionary keys to retrieve the desired value.

Returns:

Float value or 0.0 if the value is absent.

Return type:

float

Raises:

NbParserError – If strict=True and the value is not a digit or key is absent.

int(*keys) int

Get integer value by keys.

Parameters:

keys – Chaining dictionary keys to retrieve the desired value.

Returns:

Integer value or 0 if the value is absent.

Return type:

int

Raises:

NbParserError – If strict=True and the value is not a digit or key is absent.

list(*keys) List

Get list value by keys.

Parameters:

keys – Chaining dictionary keys to retrieve the desired value.

Returns:

List value or an empty list if the value is absent.

Return type:

list

Raises:

NbParserError – If strict=True and the value is not a list or key is absent.

str(*keys) str

Get string value by keys.

Parameters:

keys – Chaining dictionary keys to retrieve the desired value.

Returns:

String value or an empty string if the value is absent.

Return type:

str

Raises:

NbParserError – If strict=True and the value is not a string or key is absent.

strict_dict(*keys) Dict

Get dictionary value by keys in strict manner, value is mandatory.

Useful when strict=False, but you need to obtain a value in a strict manner. :param keys: Chaining dictionary keys to retrieve the desired value.

Returns:

Dictionary value.

Return type:

dict

Raises:

NbParserError – If the value is not a dictionary or key is absent or value is empty.

strict_int(*keys) int

Get integer value by keys in strict manner, value is mandatory.

Useful when strict=False, but you need to obtain a value in a strict manner. :param keys: Chaining dictionary keys to retrieve the desired value.

Returns:

Integer value.

Return type:

int

Raises:

NbParserError – If the value is not int or key is absent or value is 0.

strict_list(*keys) List

Get list value by keys in strict manner, value is mandatory.

Useful when strict=False, but you need to obtain a value in a strict manner. :param keys: Chaining dictionary keys to retrieve the desired value.

Returns:

List value.

Return type:

list

Raises:

NbParserError – If the value is not a list or key is absent or value is empty.

strict_str(*keys) str

Get string value by keys in strict manner, value is mandatory.

Parameters:

keys – Chaining dictionary keys to retrieve the desired value.

Returns:

String value.

Return type:

str

Raises:

NbParserError – If the value is not a string or key is absent or value is absent.