Files
seweb/base.py
Markus Zolliker dc887e68d5 move get_abs_time to base.py
+ initialize Instrument().clients
2024-10-23 16:34:55 +02:00

49 lines
1.2 KiB
Python

import sys
import time
import logging
ONEYEAR = 366 * 24 * 3600
def get_abs_time(times):
"""
Gets the absolute times for the given potential relative times. If a given timestamp is less than
one year, then the value is relative and converted into an absolute timestamp
Parameters :
times([(float)]) : an array of unix timestamps or relative duration (< 1 year) as floats
Returns :
[(float)] : an array of absolute unix timestamps as floats
"""
now = int(time.time() + 0.999)
return [t + now if t < ONEYEAR else t for t in times]
class Logger(object):
def __init__(self, logpath):
self.terminal = sys.stdout
self.log = open(logpath, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
class Instrument:
def __init__(self):
self.clients = {}
def remove(self, client):
try:
del self.clients[client.id]
except KeyError:
logging.warning('client already removed %s', client.id)
def register(self, client):
self.clients[client.id] = client
return client