Skip to content
Snippets Groups Projects
Commit 3220e0e1 authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Small things.

parent da63f05e
No related branches found
No related tags found
No related merge requests found
......@@ -402,9 +402,9 @@ class CacheObject(object):
"""
def __init__(self, name, value, single=True, update=True):
self.name = name
self.update = update
self._value = value
self._single = single
self._update = update
self._pushed = False
self._updated = value is not None
......@@ -472,9 +472,10 @@ class CacheService(object):
raise LookupError("The CacheService value '{}' has already been output to the backend. The CacheOutputMode does not allow this.".format(cache_object.name))
return False
if cache_object._update:
if cache_object.update:
if not cache_object._updated:
logger.warning("Could not push value '{}' to backend because it was not updated since last push.".format(cache_object.name))
raise LookupError("Could not push value '{}' to backend because it was not updated since last push.".format(cache_object.name))
# logger.warning("Could not push value '{}' to backend because it was not updated since last push.".format(cache_object.name))
return False
return True
......
......@@ -6,12 +6,11 @@ Any new units and constants can be added to the text files "units.txt" and
"constants.txt".
"""
from __future__ import print_function
from pint import UnitRegistry
import logging
logger = logging.getLogger(__name__)
import os
import re
import logging
from pint import UnitRegistry
logger = logging.getLogger(__name__)
ureg = UnitRegistry(os.path.join(os.path.dirname(__file__), "units.txt"))
......@@ -35,12 +34,12 @@ def convert_unit(value, unit, target_unit=None):
Args:
value: The numeric value to be converted. Accepts integers, floats,
lists and numpy arrays
lists and numpy arrays
unit: The units that the value is currently given in as a string. All
units that have a corresponding declaration in the "units.txt" file
and combinations like "meter*second**-2" are supported.
units that have a corresponding declaration in the "units.txt" file
and combinations like "meter*second**-2" are supported.
target_unit: The target unit as string. Same rules as for the unit
argument. If this argument is not given, SI units are assumed.
argument. If this argument is not given, SI units are assumed.
Returns:
The given value in the target units. returned as the same data type as
......@@ -81,10 +80,10 @@ def convert_unit_function_immediate(unit, target_unit=None):
Args:
unit: The units that the value is currently given in as a string. All
units that have a corresponding declaration in the "units.txt" file
and combinations like "meter*second**-2" are supported.
units that have a corresponding declaration in the "units.txt" file
and combinations like "meter*second**-2" are supported.
target_unit: The target unit as string. Same rules as for the unit
argument. If this argument is not given, SI units are assumed.
argument. If this argument is not given, SI units are assumed.
Returns:
The given value in the target units. returned as the same data type as
......@@ -100,8 +99,9 @@ def convert_unit_function_immediate(unit, target_unit=None):
raise Exception("The dimensionality of unit '{}' does not match the dimensionality of unit '{}'. Cannot do the unit conversion.".format(unit, target_unit))
return lambda x: convert_unit(x, unit, target_unit)
#===============================================================================
#===============================================================================
class LazyF:
"""helper class for lazy evaluation of conversion function"""
def __init__(self, unit, target_unit):
......@@ -116,6 +116,8 @@ class LazyF:
self.f = convert_unit_function_immediate(self.unit, self.target_unit)
return self.f(x)
#===============================================================================
def convert_unit_function(unit, target_unit=None):
"""Returns a function that converts scalar floats from unit to target_unit
if any of the unit are user defined (usr*), then the conversion is done lazily
......@@ -127,10 +129,10 @@ def convert_unit_function(unit, target_unit=None):
Args:
unit: The units that the value is currently given in as a string. All
units that have a corresponding declaration in the "units.txt" file
and combinations like "meter*second**-2" are supported.
units that have a corresponding declaration in the "units.txt" file
and combinations like "meter*second**-2" are supported.
target_unit: The target unit as string. Same rules as for the unit
argument. If this argument is not given, SI units are assumed.
argument. If this argument is not given, SI units are assumed.
Returns:
The given value in the target units. returned as the same data type as
......@@ -142,7 +144,6 @@ def convert_unit_function(unit, target_unit=None):
return convert_unit_function_immediate(unit, target_unit)
#===============================================================================
# Testing
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment