Python Streamer: Log all of the Values of an Object

Log object is an enhanced method to abstract having to write a bunch of log statements to stream all of the values of an object with multiple data points at a specific time.

The log_object method expects one parameter, obj:

  • obj is either a list, dict, or simple object with attributes.
    • If obj is a list, it will use the key name list_n unless the optional key_prefix is supplied, then it will use key_prefix_n where n - in both cases - is an index.
    • If obj is a dict, it will use the key name dict_key where unless the optionalkey_prefix is supplied, then it will use key_prefix_key where key - in both cases - is the key of the dictionary value.
    • If obj is a simple object, it will iterate over the objects attributes and produce values for keys with the name of the key as obj_attr unless the key_prefix is supplied, then it will use key_prefix_attr. In all cases, attr is the attribute name.
  • key_prefix is an optional string that, when supplied, overrrides the default key prefixes.
  • epoch is an optional number that represents the current time in epoch format.

Here is a working example.

NOTE: log_object will log multiple keys and values, but will override the epoch timestamp of each value so that there is no cpu or iteration skew in the timestamp reported for when those values were logged and streamed.

Feedback and Knowledge Base