5.9. tools

Utilities and tools.

axonius_api_client.tools.listify(obj, dictkeys=False)[source]

Force an object into a list.

Notes

  • list: returns as is
  • tuple: convert to list
  • None: returns as an empty list
  • any of axonius_api_client.SIMPLE: return as a list of obj
  • dict: if dictkeys is True, return as list of keys of obj, otherwise return as a list of obj
Parameters:
  • obj (object) – object to coerce to list
  • dictkeys (bool, optional) –

    default False -

    • if True and obj is dict, return list of keys of obj
    • if False and obj is dict, return list of obj
Returns:

coerced list

Return type:

list

axonius_api_client.tools.grouper(iterable, n, fillvalue=None)[source]

Split an iterable into chunks.

Parameters:
  • iterable (typing.Iterable) – iterable to split into chunks of size n
  • n (int) – length to split iterable into
  • fillvalue (object, optional) – default None - value to use as filler for last chunk
Returns:

an iterator with chunks of length n

Return type:

typing.Iterator

axonius_api_client.tools.coerce_int(obj)[source]

Convert an object into int.

Parameters:obj (object) – object to convert to int
Returns:coerced int
Return type:int
Raises:ToolsError – if obj is not able to be converted to int
axonius_api_client.tools.coerce_bool(obj)[source]

Convert an object into bool.

Notes

  • if obj is str, will check against axonius_api_client.YES and axonius_api_client.NO
Parameters:obj (object) – object to coerce to bool
Returns:coerced bool
Return type:bool
Raises:ToolsError – obj is not able to be converted to bool
axonius_api_client.tools.is_int(obj, digit=False)[source]

Check if obj is int typeable.

Parameters:
  • obj (object) – object to check
  • digit (bool, optional) – default False - * if True obj must be (str/bytes where isdigit is True) or int * if False obj must be int
Returns:

bool reflecting if obj is int typeable

Return type:

bool

axonius_api_client.tools.join_url(url, *parts)[source]

Join a URL to any number of parts.

Parameters:
  • url (str) – str to add parts to
  • *parts (str) – str(s) to append to url
Returns:

url with parts appended

Return type:

str

axonius_api_client.tools.strip_right(obj, fix)[source]

Strip text from the right side of obj.

Parameters:
  • obj (str) or (list of str) – str(s) to strip fix from
  • fix (str) – str to remove from obj(s)
Returns:

obj with fix stripped

Return type:

(str) or (list of str)

axonius_api_client.tools.strip_left(obj, fix)[source]

Strip text from the left side of obj.

Parameters:
  • obj (str) or (list of str) – str(s) to strip fix from
  • fix (str) – str to remove from obj(s)
Returns:

obj with fix stripped

Return type:

(str) or (list of str)

axonius_api_client.tools.json_dump(obj, indent=2, sort_keys=False, error=True, **kwargs)[source]

Serialize an object into json str.

Parameters:
  • obj (object) – object to serialize into json str
  • indent (int, optional) – default 2 - json str indentation
  • sort_keys (bool, optional) –

    default False -

    • if True sort keys of dicts
    • if False leave keys of dicts sorted as is
  • error (bool, optional) –

    default True -

    • if True If json error happens, raise it
    • if False If json error happens, return original obj as is
  • **kwargs – passed to json.dumps()
Returns:

  • object: if json error happens and error is false
  • str: if no json error happens

Return type:

(str) or (object)

axonius_api_client.tools.json_load(obj, error=True, **kwargs)[source]

Deserialize a json str into an object.

Parameters:
  • obj (object) – str to deserialize into obj
  • error (bool, optional) –

    default True -

    • if True If json error happens, raise it
    • if False If json error happens, return original obj as is
  • **kwargs – passed to json.loads()
Returns:

json str serialized into python obj

Return type:

object

axonius_api_client.tools.json_reload(obj, error=False, trim=None, **kwargs)[source]

Re-serialize a json str into a pretty json str.

Parameters:
  • obj (object) – str to deserialize into obj and serialize back to str
  • error (bool, optional) –

    default False -

    • if True If json error happens, raise it
    • if False If json error happens, return original obj as is
  • **kwargs – passed to json_dump()
Returns:

  • object: if json error happens and error is false
  • str: if no json error happens

Return type:

(str) or (object)

axonius_api_client.tools.dt_parse(obj)[source]

Parse a str, datetime, or timedelta into a datetime object.

Notes

Parameters:obj (str or datetime.datetime or datetime.timedelta) – object or list of objects to parse
Returns:parsed datetime
Return type:datetime.datetime
axonius_api_client.tools.dt_now(delta=None, tz=tzutc())[source]

Get the current datetime in for a specific tz.

Parameters:
Returns:

datetime of now

Return type:

datetime.datetime

axonius_api_client.tools.dt_sec_ago(obj, exact=False)[source]

Get number of seconds ago a given datetime was.

Parameters:obj (object) – will be parsed by dt_parse() into a datetime obj
Returns:int of secs ago obj was
Return type:int
axonius_api_client.tools.dt_min_ago(obj)[source]

Get number of minutes ago a given datetime was.

Parameters:obj (object) – will be parsed by dt_sec_ago() into seconds ago
Returns:int of minutes ago obj was
Return type:int
axonius_api_client.tools.dt_within_min(obj, n=None)[source]

Check if given datetime is within the past n minutes.

Parameters:
  • obj (object) – will be parsed by dt_min_ago() into minutes ago
  • n (int or str, optional) – default None - int of dt_min_ago(obj) should be greater than or equal to
Returns:

bool representing if obj is within the past n minutes

Return type:

bool

axonius_api_client.tools.get_path(obj)[source]

Convert a str into a fully resolved & expanded Path object.

Parameters:obj (str or pathlib.Path) – obj to convert into expanded and resolved absolute Path obj
Returns:resolved path
Return type:pathlib.Path
axonius_api_client.tools.path_read(obj, binary=False, is_json=False, **kwargs)[source]

Read data from a file.

Notes

  • obj will be parsed by path()
  • if path filename ends with “.json”, data will be deserialized using json_load()
Parameters:
  • obj (str or pathlib.Path) – path to read data form
  • binary (bool, optional) –

    default False -

    • if True read the data as binary
    • if False read the data as str
  • is_json (bool, optional) – default False - * if True deserialize data using json_load() * if False do not deserialize data
  • **kwargs – passed to json_load()
Returns:

resolved path and data read from path

Return type:

tuple of (pathlib.Path, object)

Raises:

ToolsError – path does not exist as file

axonius_api_client.tools.path_write(obj, data, overwrite=False, binary=False, binary_encoding='utf-8', is_json=False, make_parent=True, protect_file=384, protect_parent=448, **kwargs)[source]

Write data to a file.

Notes

  • obj will be parsed by path()
  • if obj filename ends with “.json”, serializes data using json_dump().
Parameters:
  • obj (str or pathlib.Path) – path to write data to
  • data (str or bytes or object) – data to write to obj
  • overwrite (bool, optional) –

    default False -

    • if True overwrite obj
    • if False do not overwrite obj, raise exception if it already exists
  • binary (bool, optional) –

    default False -

    • if True write the data as binary
    • if False write the data as str
  • binary_encoding (str, optional) – default "utf-8" - encoding to use when switching from str/bytes
  • is_json (bool, optional) –

    default False -

    • if True Serialize data using json_load() before writing
    • if False Do not serialize data before writing
  • make_parent (bool, optional) –

    default True -

    • if True If the parent directory does not exist, create it
    • if False If the parent directory does not exist, raise exception
  • protect_file (oct) – default 0o600 (read/write to owner only) - octal mode of permissions to set on file
  • protect_dir (oct) – default 0o700 (read/write/execute to owner only) - octal mode of permissions to set on parent directory when creating
  • **kwargs – passed to json_dump()
Returns:

tuple of (resolved path obj, method used to write data to file)

Return type:

tuple of (pathlib.Path, method)

Raises:
  • ToolsError – path exists as file and overwrite is False
  • ToolsError – if parent path does not exist and make_parent is False
axonius_api_client.tools.longest_str(obj)[source]

Badwolf.

axonius_api_client.tools.split_str(obj, split=', ', strip=None, do_strip=True, lower=True, empty=False)[source]

Split a string or list of strings into a list of strings.

axonius_api_client.tools.echo_ok(msg, tmpl=True, **kwargs)[source]

Pass.

axonius_api_client.tools.echo_warn(msg, tmpl=True, **kwargs)[source]

Pass.

axonius_api_client.tools.echo_error(msg, abort=True, tmpl=True, **kwargs)[source]

Pass.

axonius_api_client.tools.sysinfo()[source]

Pass.

axonius_api_client.tools.calc_percent(part, whole)[source]

Pass.