Source code for onetick.lib.instance

import onetick.query as otq
import gc
from enum import Enum
import pyomd


class LoggingLevel(Enum):
    MIN = otq.OneTickLib.LOGGING_LEVEL_MIN
    LOW = otq.OneTickLib.LOGGING_LEVEL_LOW
    MEDIUM = otq.OneTickLib.LOGGING_LEVEL_MEDIUM
    MAX = otq.OneTickLib.LOGGING_LEVEL_MAX


[docs]class OneTickLib(object): """ Singleton class for otq.OneTickLib to initialize it once usage: OneTickLib() - returns existing otq.OneTickLib if it is intialized or otq.OneTickLib(None) if it is not OneTickLib(*args) - returns pyomf.OneTickLib(*args) """ __instance = None __args = None def __init__(self, *args, log_file=None): if not OneTickLib.__instance: if not args: args = (None,) OneTickLib.__instance = otq.OneTickLib(*args) if log_file: self.set_log_file(log_file) OneTickLib.__args = args elif args != OneTickLib.__args and args: raise Exception("OneTickLib was already initialized with different " "parameters: Was: {} Now: {}".format(OneTickLib.__args, args)) def __eq__(self, otl): return self.__dict__ == otl.__dict__ def __ne__(self, otl): return self.__dict__ != otl.__dict__ def __str__(self): return "Instance: {}".format(self.__instance.get_one_tick_lib())
[docs] def cleanup(self): """ Destroy otq.OneTickLib instance and reset singleton class """ del OneTickLib.__instance gc.collect() OneTickLib.__instance = None OneTickLib.__args = None
[docs] def set_log_file(self, log_file): """ Set log file for given instance of OneTickLib :param log_file: path to log file """ OneTickLib.__instance.set_log_file(str(log_file)) if hasattr(OneTickLib.__instance, 'close_log_file_in_destructor'): # we need to check it to prevent failing CI on the builds that do not have this feature OneTickLib.__instance.close_log_file_in_destructor()
[docs] def set_logging_level(self, lvl: LoggingLevel): """ Logging level can be specified by using LoggingLevel Enum class :param lvl: available values are LoggingLevel.MIN, LoggingLevel.LOW, LoggingLevel.MEDIUM or LoggingLevel.MAX :return: """ OneTickLib.__instance.set_logging_level(lvl)
[docs] def set_authentication_token(self, auth_token: str): """ Set authentication token for given instance of OneTickLib :param auth_token: authentication token """ OneTickLib.__instance.set_authentication_token(auth_token)
[docs] @staticmethod def override_config_value(config_parameter_name: str, config_parameter_value): """ Override config value of OneTickConfig :param config_parameter_name: param to override (could be both set or not set in OneTickConfig) :param config_parameter_value: new value of the param """ if OneTickLib.__instance: raise Exception('This method should be called before OneTickLib object is constructed to have affect.') pyomd.OneTickLib.override_config_value(config_parameter_name, config_parameter_value)