def internal_if(cf, *args): arg0 = try_converting_to_val_even_ts(args[0]) # "ELSE" - Return the default value if len(args) == 1: return Value(arg0.value, cf=min(cf, arg0.cf), stub=arg0.is_stub, null=arg0.is_null) # "IF" - If the test evaluates to True # First catch Stub and None if arg0.is_stub: return arg0 elif arg0.is_null: arg1 = try_converting_to_val_even_ts(args[1]) if arg1.is_stub: return arg1 else: return arg0 # Does test evaluate to True? if arg0.value: # "THEN" arg1 = try_converting_to_val_even_ts(args[1]) return Value(arg1.value, cf=min(cf, arg0.cf, arg1.cf), stub=arg1.is_stub, null=arg1.is_null) # Compress the expression and recurse else: return internal_if(min(cf, arg0.cf), *args[2:])
def internal_all(a_in: Value): a = try_converting_to_val(a_in) if a.is_stub or a.is_null: return a elif list_any_false(a.value): return Value(False) elif list_contains_stub(a.value): return Stub elif list_contains_null(a.value): return Null else: return Value(list_all_true(a.value))
def internal_not(a_in: Value): a = try_converting_to_val(a_in) if a.is_stub or a.is_null: return a else: return Value(not a.value, cf=a.cf)
def try_converting_to_val_even_ts(a): if isinstance(a, TimeSeries): return a.dict[1] elif isinstance(a, Value): return a else: return Value(a)
def try_converting_to_ts(a): if isinstance(a, TimeSeries): return a elif isinstance(a, Value): return TimeSeries(a) else: return TimeSeries(Value(a))
def internal_map(f, a_in): a = try_converting_to_val(a_in) if a.is_stub or a.is_null: return a elif list_contains_null(a.value): return Null else: return Value(list(map(f, a.value)))
def internal_ts_trim(ts: dict): previous_value = Value(-1) redundant = [] for time, value in ts.items(): if internal_should_be_merged(value, previous_value): redundant.append(time) previous_value = value for time in redundant: del ts[time] return ts
def internal_list_fcn(f, a_in): a = try_converting_to_val(a_in) if a.is_stub or a.is_null: return a elif list_contains_stub(a.value): return Stub elif list_contains_null(a.value): return Null else: return Value(f([try_converting_to_val(x).value for x in a.value]))
def internal_two_list_operation_on_vals(f, a: Value, b: Value): if a.is_stub or b.is_stub: return Stub elif a.is_null or b.is_null: return Null else: alist = list_of_values_to_list(a) blist = list_of_values_to_list(b) if list_contains_stub(a.value) or list_contains_stub(b.value): return Stub elif list_contains_null(a.value) or list_contains_null(b.value): return Null else: return Value(f(alist, blist))
def internal_or(a_in: Value, b_in: Value): a = try_converting_to_val(a_in) b = try_converting_to_val(b_in) if a.value is True and b.value is True: return Value(True, cf=max(a.cf, b.cf)) elif a.value is True: return Value(True, cf=a.cf) elif b.value is True: return Value(True, cf=b.cf) elif a.value is False and b.value is False: return Value(False, cf=min(a.cf, b.cf)) elif a.value is False: return b elif b.value is False: return a elif a.is_null and b.is_null: return Value("Null", cf=max(a.cf, b.cf), null=True) elif a.is_null: return Value("Null", cf=a.cf, null=True) elif b.is_null: return Value("Null", cf=b.cf, null=True) else: return Value("Stub", cf=max(a.cf, b.cf), stub=True)
def _set_cf(v: Value, cf: Value): return Value(v.value, cf=max(min(cf.value, 1), 0), stub=v.is_stub, null=v.is_null)
def internal_get_cf(a_in: Value): return Value(try_converting_to_val(a_in).cf)
def internal_is_stub(a_in: Value): return Value(try_converting_to_val(a_in).is_stub)
def internal_process_unary_fcn_val(f, a_in: Value): a = try_converting_to_val(a_in) if a.is_stub or a.is_null: return a else: return Value(f(a.value), cf=a.cf)