util module

Utility functions which may be useful for clients.

Note

Do not change any code in this file! If you want different behavior for these functions, implement them yourself in client.py.

util.compute_edits(old, new)

Compute the in-place edits needed to convert from old to new

Returns a list [(index_1,change_1), (index_2,change_2)...] where index_i is an offset into old, and change_1 is the new bytes to replace.

For example, calling compute_edits("abcdef", "qbcdzw") will return [(0, "q"), (4, "zw")].

That is, the update should be preformed as (abusing notation):

new[index:index+len(change)] = change

Parameters:
  • old (str) – The old data
  • new (str) – The new data
Returns:

A list of tuples (index_i, change_i)

util.from_json_string(s)

Convert a JSON string back into a basic Python object.

This function can correctly handle deserializing back into RSA key objects.

This uses the JSON library to load the object from a string. For more information on JSON in Python, see the JSON library in the Python standard library.

Parameters:

s (str) – A JSON string

Returns:

The Python object deserialized from s

Raises:
  • JSONDecodeError – If s is not a valid JSON document.
  • TypeError – If s isn’t a string.
util.to_json_string(obj)

Convert basic Python objects into a JSON-serialized string.

This can be useful for converting objects like lists or dictionaries into string format, instead of deriving your own data format.

This function can correctly handle serializing RSA key objects.

This uses the JSON library to dump the object to a string. For more information on JSON in Python, see the JSON library in the Python standard library.

Parameters:obj – A JSON-serializable Python object
Returns:A JSON-serialized string for obj
Raises:TypeError – If obj isn’t JSON serializable.