Module libnova.Util

Expand source code
#!/usr/bin/env python
# coding: utf-8

import json
from datetime import datetime
from types    import SimpleNamespace

def print_json_item(json_data):
    if json_data is not None:
        print(format_json_item(json_data))


def format_json_item(json_data):
    if json_data is not None:
        return json.dumps(json_data, indent=4, sort_keys=True)


def str2bool(v):
  return str(v).lower() in ("yes", "true", "1")


def obj_to_str(obj, tab_string = '\t'):
    return str(obj.__class__) + '\n' + '\n'.join(
        (
            tab_string + (str(item) + ' = ' + (
                obj_to_str(obj.__dict__[item], tab_string + '    ')
                    if
                        hasattr(obj.__dict__[item], '__dict__')
                    else
                        str(obj.__dict__[item])
            ))
            for item in sorted(obj.__dict__)
        )
    )


def dict_to_str(d, indent=0):
    for key, value in d.items():
        print('\t' * indent + str(key))
        if isinstance(value, dict):
            dict_to_str(value, indent+1)
        else:
            print('\t' * (indent+1) + str(value))


def log(message):
    print(
        '[' + datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f') + '] ' + str(message)
    )


if __name__ == "__main__":
    print('This file cannot be executed directly!')

Functions

def dict_to_str(d, indent=0)
Expand source code
def dict_to_str(d, indent=0):
    for key, value in d.items():
        print('\t' * indent + str(key))
        if isinstance(value, dict):
            dict_to_str(value, indent+1)
        else:
            print('\t' * (indent+1) + str(value))
def format_json_item(json_data)
Expand source code
def format_json_item(json_data):
    if json_data is not None:
        return json.dumps(json_data, indent=4, sort_keys=True)
def log(message)
Expand source code
def log(message):
    print(
        '[' + datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f') + '] ' + str(message)
    )
def obj_to_str(obj, tab_string='\t')
Expand source code
def obj_to_str(obj, tab_string = '\t'):
    return str(obj.__class__) + '\n' + '\n'.join(
        (
            tab_string + (str(item) + ' = ' + (
                obj_to_str(obj.__dict__[item], tab_string + '    ')
                    if
                        hasattr(obj.__dict__[item], '__dict__')
                    else
                        str(obj.__dict__[item])
            ))
            for item in sorted(obj.__dict__)
        )
    )
def print_json_item(json_data)
Expand source code
def print_json_item(json_data):
    if json_data is not None:
        print(format_json_item(json_data))
def str2bool(v)
Expand source code
def str2bool(v):
  return str(v).lower() in ("yes", "true", "1")