def query(self, query: str, *argv, **kwargs) -> pyodbc.Cursor: """ Queries the database and returns the result as a Cursor.""" logger.info(f'Querying SQL with { query }') if not self.__connection_is_valid(): logger.info("Trying to reconnect...") self.connect() cursor = self.__db.cursor() try: cursor.execute(query, *argv, **kwargs) except Exception as e: logger.error(f'Error querying SQL with { query } | { e }') raise return cursor
def connect(self) -> None: """Connects to the database. Will reuse connection if a connection is open and the connection string has not changed. """ logger.info("Connecting to database...") if self.__db is not None and self.__connection_is_valid(): logger.info( "A connection is already open! Reusing old connection.") return try: self.__db = pyodbc.connect(self.__connection_string) logger.info("Database connection successful!") except Exception as e: logger.error(f"Database connection failed! | { e }") raise
def median_distance(self): """ Returns median distance in metres (a bit more robust towards outliers) """ t = self.timestamps_in_contact() # Handle the case there is no or only one GPS point if len(t) == 0: # If no GPS points are given, all we can do is to return an unrealistic distance value logger.error( "Requesting median distance from an empty trajectory - returning unphysical value 1e6" ) return 1e6 if len(t) == 1: # For a single GPS point, the median is the distance at that point logger.warning( "Requesting median distance from a trajectory with a single point" ) return self.cd['dists'][0] weights = np.array([t[i] - t[i - 1] for i in range(1, len(t))]) weights /= sum(weights) # because len(self.cd['dists']) = len(weights) + 1, I do this transformation dists = [(self.cd['dists'][i] + self.cd['dists'][i - 1]) / 2 for i in range(1, len(t))] df = pd.DataFrame({'dists': dists, 'w': weights}) return weighted.median(df['dists'], df['w'])
if "CORONA_CONFIG_HOME" in os.environ: return os.environ["CORONA_CONFIG_HOME"] elif "APPDATA" in os.environ: return os.environ["APPDATA"] elif "XDG_CONFIG_HOME" in os.environ: return os.environ["XDG_CONFIG_HOME"] return os.path.join(os.environ["HOME"], ".config") __CONFIG__ = Config(__DEFAULT_CONFG) __CONFIG_FILE_NAME__ = "corona.conf" __CONFIG_PATH__ = os.path.join(find_config_home(), __CONFIG_FILE_NAME__) if not os.path.exists(__CONFIG_PATH__): logger.error("Config file not found!") exit(1) config = configparser.ConfigParser() config.read(__CONFIG_PATH__) for name, value in config._sections.items(): __CONFIG__.add_section(name, value) if __CONFIG__.settings.debug: logger.warning("You are running in DEBUG mode!") logger.setLevel(logging.DEBUG) __CONFIG__.cache.enabled = False if __CONFIG__.cache.enabled: if not os.path.exists(__CONFIG__.cache.location):
def silent_assert(cond, description, args): '''Assert''' if not cond: logger.error('\033[1;37;31m%s\033[0m' % '>>>>>>> Failing Assert in', description, args) return cond