def test_decl(self): t_decl = decl("x", 1) self.assertEqual(t_decl.var, "x") self.assertEqual(t_decl.rhs, 1) t_decl = decl("x", func([], 1)) self.assertEqual(t_decl.var, "x") self.assertEqual(t_decl.rhs, func([], 1))
def test_declSequence(self): t_declSeq = declSequence([decl("x", 1), "x"]) self.assertEqual(t_declSeq.sequence, [decl("x", 1)]) self.assertEqual(t_declSeq.expr, "x") t_declSeq = declSequence([decl("x", 1), decl("x", func([], 1)), 1]) self.assertEqual(t_declSeq.sequence, [decl("x", 1), decl("x", func([], 1))]) self.assertEqual(t_declSeq.expr, 1)
def test_call(self): t_call = call(func([], 1), []) self.assertEqual(t_call.fun, func([], 1)) self.assertEqual(t_call.params, []) t_call = call(func(["x"], 1), [1]) self.assertEqual(t_call.fun, func(["x"], 1)) self.assertEqual(t_call.params, [1])
def test_parse_list(self): t_json = ["fun*", [], 1] self.assertEqual(parseList(t_json), func([], 1)) t_json = ["call", "+", 1, 2] self.assertEqual(parseList(t_json), call("+", [1, 2])) t_json = ["let", "x", "=", ["fun*", [], 1]] self.assertEqual(parseList(t_json), decl("x", func([], 1))) t_json = ["a", "b", "c"] self.assertEqual(parseList(t_json), ["a", "b", "c"])
def split_call(toy, k): global COUNT, FCOUNT if not toy.params: # Case for no argument function calls if type(toy.fun) == str: return call(toy.fun, k) elif type(toy.fun) == func: return call(value(toy.fun), k) else: return call(split(toy.fun, k), k) else: # Normal case for any-argument calls received_expr = [] function_args = [] expressions = toy.params[::-1] + [toy.fun] for i in range(FCOUNT, len(expressions) + FCOUNT): function_args.append(new_var("f", i)) FCOUNT += len(expressions) last_expr = call(function_args[-1], [k] + function_args[0:-1][::-1]) for i in range(len(expressions)): received_expr.append(receive(expressions[i])) function_args.reverse() received_expr.reverse() for i in range(len(function_args)): last_expr = call(received_expr[i], func(function_args[i], last_expr)) return last_expr
def log_prob_nested(posarr): ln_prior = lnprior(posarr, nested = True) if not np.isfinite(ln_prior): return -np.infty return ln_prior - 0.5*func(global_alfvar, posarr, prhiarr=global_prhiarr, prloarr=global_prloarr, usekeys=use_keys)
def value(toy): global COUNT if type(toy) == int: return toy else: COUNT += 1 return func([new_var("k", COUNT)] + toy.args, split(toy.body, new_var("k", COUNT)))
def test_d_func_time_1200(self): """ Test speed of func on random list with 1200 elements""" start = time.time() res = func(self.generated_1200) end = time.time() - start print(end) self.assertEqual(res, 1398) self.assertTrue(end < 2)
def func_2min(inarr): """ only optimize the first 4 parameters before running the sampler """ return func(global_alfvar, inarr, use_keys[:len(inarr)], prhiarr=global_prhiarr, prloarr=global_prloarr, )
def func_2min(inarr): """ only optimize the first 4 parameters before running the sampler """ return func(global_alfvar, inarr, prhiarr=global_prhiarr, prloarr=global_prloarr, usekeys = ['velz', 'sigma', 'logage', 'zh', 'feh', 'ah', 'ch', 'nh','nah','mgh','sih','kh','cah','tih', 'vh','crh','mnh','coh','nih','cuh','srh','bah','euh',])
def test_func(self): query_res = [("name_1", "value_1"), ("name_2", "value_2")] expected_vals = ["value_1", "value_2"] db_engine = MagicMock() execute = db_engine.execute.return_value execute.fetchall.return_value = query_res vals = func(db_engine) db_engine.execute.assert_called_with("SELECT * FROM table") execute.fetchall.assert_called_once() self.assertEqual(expected_vals, vals)
def split(toy, k): global COUNT if type(toy) == str: if toy in opList: return call(k, ops_mapping[toy]) return call(k, toy) if type(toy) == int: return call(k, toy) if type(toy) == func: return call(k, value(toy)) if type(toy) == call: return split_call(toy, k) if type(toy) == cond: conditional = cond("of-tst", split(toy.true_expression, k), split(toy.false_expression, k)) f = func(["of-tst"], conditional) return call(receive(toy.condition), f) if type(toy) == decl: toy.rhs = value(toy.rhs) return toy if type(toy) == declSequence: new_sequence = [] for declaration in toy.sequence: new_sequence.append(split(declaration, k)) toy.sequence = new_sequence toy.expr = split(toy.expr, k) return toy if type(toy) == grab: return declSequence( [decl(toy.var, func(["x", "f"], call(k, "f"))), split(toy.rhs, k)]) if type(toy) == list: if len(toy) < 1: return k # ? else: call_arg = func(["of-rst"], call(k, "of-rst")) f = func(["of-fst"], call(receive(toy[1:]), call_arg)) return call(receive(toy[0]), f) if type(toy) == stop: return stop(receive(toy.toy))
def test_02_raise(self): with self.assertRaises(AssertionError): func(2, 0.1) with self.assertRaises(AssertionError): func(0, 0) with self.assertRaises(AssertionError): func(0.4, -0.2)
def RK45(x0, parameters, h, n_var, nt): X_s = np.zeros((n_var, nt), dtype="float32") # Solution X_s[:, 0] = x0 # Initial solution # Runge-Kutta coefficients A2 = -6234157559845 / 12983515589748. A3 = -6194124222391 / 4410992767914. A4 = -31623096876824 / 15682348800105. A5 = -12251185447671 / 11596622555746. B1 = 494393426753 / 4806282396855. B2 = 4047970641027 / 5463924506627. B3 = 9795748752853 / 13190207949281. B4 = 4009051133189 / 8539092990294. B5 = 1348533437543 / 7166442652324. for n in range(nt - 1): # Step 1 dx_s1 = h * func(X_s[:, [n]], parameters) x_s1 = X_s[:, [n]] + B1 * dx_s1 # Step 2 dx_s2 = A2 * dx_s1 + h * func(x_s1, parameters) x_s2 = x_s1 + B2 * dx_s2 # Step 3 dx_s3 = A3 * dx_s2 + h * func(x_s2, parameters) x_s3 = x_s2 + B3 * dx_s3 # Step 4 dx_s4 = A4 * dx_s3 + h * func(x_s3, parameters) x_s4 = x_s3 + B4 * dx_s4 # Step 5 dx_s5 = A5 * dx_s4 + h * func(x_s4, parameters) X_s[:, n + 1] = (x_s4 + B5 * dx_s5).T return X_s
def func_2min(inarr): """ only optimize the first 4 parameters before running the sampler """ return func(global_alfvar, inarr, prhiarr=global_prhiarr, prloarr=global_prloarr, usekeys=[ 'velz', 'sigma', 'logage', 'zh', ])
def receive(toy): global COUNT if type(toy) == decl: toy.rhs = value(toy.rhs) return toy elif type(toy) == declSequence: new_sequence = [] for declaration in toy.sequence: new_sequence.append(receive(declaration)) toy.sequence = new_sequence toy.expr = receive(toy.expr) return toy else: COUNT += 1 return func(new_var("k", COUNT), split(toy, new_var("k", COUNT)))
def parseList(jso): if len(jso) > 0: if jso[0] == "fun*": body = parse(jso[2]) return func(jso[1], body) if jso[0] == "call": params = [] for parameter in jso[2:]: params.append(parse(parameter)) c = call(parse(jso[1]), params) return c if jso[0] == "if-0": return cond(parse(jso[1]), parse(jso[2]), parse(jso[3])) if jso[0] == "let": return decl(jso[1], parse(jso[3])) # No need for infix operation checking in the list section, since it cannot happen if jso[0] == "seq*": args = [] for s in jso[1:-1]: p = parse(s) args.append(p) f = call(func(rand_stringer(len(args)), parse(jso[-1])), args[::-1]) return f if jso[0] == "grab": return grab(parse(jso[1]), parse(jso[2])) if jso[0] == "stop": return stop(parse(jso[1])) if type(jso) == list: return_array = [] for item in jso: return_array.append(parse(item)) if len(return_array) > 1 and type(return_array[0]) == decl: return declSequence(return_array) return return_array return jso
def clothes(message): user = User.query.filter(User.id == message.chat.id).first() if user.city is None: bot.send_message( message.chat.id, 'Пожалуйста, установите город, нажав на кнопку' ' "Установить город"') return clothes = user.clothes weather = owm.weather_at_place(f'{user.city},Russia') w = weather.get_weather() temperature = w.get_temperature("celsius")["temp"] print(temperature) detail_status = w.get_detailed_status() new_clothes = func(temperature, clothes) for item in new_clothes: bot.send_message(message.chat.id, f'{item}')
def main(): try: a = int(input()) b = int(input()) operation = input() if operation == '+': print(a + b) elif operation == '-': print(a - b) elif operation == '*': print(a * b) elif operation == '/': print(a / b) elif operation == '%': print(func.func(a, b)) else: raise ValueError except ValueError: print('Enter two integers, each on a new line, then enter the operation character on the next line(\'+\', \'-\', \'*\', \'/\'')
class main: if __name__=="__main__": func = func() eoes = EOES() smallfunctionsofnm = SmallFuncionsOfNM() eoes.System_Statistic() # eoes.DetailedListStatistics() # eoes.Customizable_Statistics() # eoes.System_BS_Organization_Statistics() # eoes.KPI_Statistic() # smallfunctionsofnm.CrossSystemConfig() # smallfunctionsofnm.updateChannelList() # smallfunctionsofnm.add_VPN() # smallfunctionsofnm.delete_VPN() # smallfunctionsofnm.FaultWeakeningPlan() # func.CloseProcess('EXCEL,chromedriver,conhost') # 最后执行
import func func.func()
def handleStop(toy, env, store): if type(toy.toy) == str: raise errors.StopContinuation(toy.toy, env, store) raise errors.StopContinuation(call(toy.toy, func("asdasd", "asdasd")), env, store)
import sys from time import localtime import func # import module1[, module2[,... moduleN] # Python 的 from 语句让你从模块中导入一个指定的部分到当前命名空间中 # 一个模块被另一个程序第一次引入时,其主程序将运行。如果我们想在模块被引入时,模块中的某一程序块不执行, # 我们可以用__name__属性来使该程序块仅在该模块自身运行时执行。 if __name__ == '__main__': print(sys.path) print(localtime()) func.func(1, 2)
def run(toy): app_form = func(["x"], stop("x")) cps_form = call(receive(toy), app_form) interp_ast(cps_form)
self.weight_list[0][0][0], self.bias_list[0][0][0], ) self.forward_pass(x, y) self.backward_pass() print(self.loss.dx) def estimate(self, x): for i in range(self.no_of_layers): if i % 2 == 0: x = self.layer_list[i].forward_val( self.weight_list[round(i / 2)], x, self.bias_list[round(i / 2)]) else: x = self.layer_list[i].forward_val(x) return x #print("hello") nn_obj1 = network() nn_obj1.insert_layer(1, 1) #x = [[3,1.5],[2,1],[4,1.5],[3,1],[3.5,.5],[2,.5],[5.5,1],[1,1]] #y = [1,0,1,0,1,0,1,0] x = [] y = [] x, y = func.func(x, y, 10) x = np.array(x, dtype=np.float64).reshape(10, 1) y = np.array(y, dtype=np.float64).reshape(10, 1) #for i in range(10000): nn_obj1.train_network(x, y)
def test_cps(self): # Using the alpha_equals() for equality t_json = 1 parsed_json = parse(t_json) cps = receive(parsed_json) test = func(["k1"], call("k1", [1])) self.assertTrue(alpha_equal(cps, test)) t_json = "a" parsed_json = parse(t_json) cps = receive(parsed_json) test = func(["k2"], call("k2", ["a"])) self.assertTrue(alpha_equal(cps, test)) t_json = "+" parsed_json = parse(t_json) cps = receive(parsed_json) test = func(["k3"], call("k3", ops_mapping["+"])) self.assertTrue(alpha_equal(cps, test)) t_json = ["call", ["fun*", ["x", "y"], "y"], 1, 2] parsed_json = parse(t_json) cps = receive(parsed_json) test = func("k5", call(func("k6", call("k6", 2)), func("of-1", call(func("k7", call("k7", 1)), func("of-0", call(func(["k8"], call("k8", func(["k", "x", "y"], call("k", "y")))), func("of-f", call("of-f", ["k5", "of-0", "of-1"])))))))) self.assertTrue(alpha_equal(cps, test)) t_json = ["if-0", 0, 1, 2] parsed_json = parse(t_json) cps = receive(parsed_json) test = func(["k9"], call(func(["k10"], call("k10", [0])), func(["of-tst"], cond("of-tst", call("k9", [1]), call("k9", [2]))))) self.assertTrue(alpha_equal(cps, test)) t_json = [["let", "a", "=", 5], ["let", "b", "=", 6], "b"] parsed_json = parse(t_json) cps = receive(parsed_json) test = declSequence([decl("a", 5), decl("b", 6), func(["k11"], call("k11", ["b"]))]) self.assertTrue(alpha_equal(cps, test)) t_json = ["fun*", ["x"], "x"] parsed_json = parse(t_json) cps = receive(parsed_json) test = func(["k12"], call("k12", func(["k", "x"], call("k", ["x"])))) self.assertTrue(alpha_equal(cps, test)) # Unit tests against names we would generate t_json = ["fun*", ["kb"], "kb"] parsed_json = parse(t_json) cps = receive(parsed_json) test = func(["k12"], call("k12", func(["k", "kb"], call("k", ["kb"])))) self.assertTrue(alpha_equal(cps, test)) t_json = ["call", "+", 1, 2] parsed_json = parse(t_json) cps = receive(parsed_json) test = func("k13", call(func("k14", call("k14", 2)), func("of-1", call(func("k15", call("k15", 1)), func("of-0", call(func("k", call("k", ops_mapping["+"])), func("k", call("k", ["k13", "of-0", "of-1"])))))))) self.assertTrue(alpha_equal(cps, test)) t_json = ["grab", "x", ["call", "x", 10]] parsed_json = parse(t_json) cps = receive(parsed_json) test = func(["k"], declSequence([decl("x", func(["x", "f"], call("k", "f"))), call(func("kc", call("kc", 10)), func("fa", call(func("kd", call("kd", "x")), func("fb", call("fb", ["kb", "fa"])))))])) self.assertTrue(alpha_equal(cps, test)) t_json = ["stop", 10] parsed_json = parse(t_json) cps = receive(parsed_json) test = func("k", stop(func("kc", call("kc", 10)))) self.assertTrue(alpha_equal(cps, test)) t_json = ["call", "+", 1, ["stop", 10]] parsed_json = parse(t_json) cps = receive(parsed_json) test = func("k13", call(func("k", stop(func("kc", call("kc", 10)))), func("of-1", call(func("k15", call("k15", 1)), func("of-0", call(func("k", call("k", ops_mapping["+"])), func("k", call("k", ["k13", "of-0", "of-1"])))))))) self.assertTrue(alpha_equal(cps, test)) t_json = declSequence([decl("x", 15), "x"]) cps = receive(t_json) test = declSequence([decl("x", 15), func("ke", call("ke","x"))]) self.assertTrue(alpha_equal(cps, test))
def test_alpha_equals(self): self.assertTrue(alpha_equal("a", "b")) self.assertTrue(not alpha_equal(1, "b")) self.assertTrue(alpha_equal(func("a", "a"), func("b", "b"))) self.assertTrue(alpha_equal(declSequence([decl("x", func("a", "a")), "x"]), declSequence([decl("asdf", func("gfd", "gfd")), "asdf"])))
def test_func(self): t_func = func([], 1) self.assertEqual(t_func.args, []) self.assertEqual(t_func.body, 1)
def test_seq(self): t_json_input = ["seq*", ["call", "+", ["call", "*", 5, 3], 1], -1] t_json_expected = call(func(["a"], -1), [call("+", [call("*", [5, 3]), 1])]) self.assertTrue(alpha_equal(parse(t_json_input), t_json_expected))
def test_cond(self): t_cond = cond(func([], 1), 1, 0) self.assertEqual(t_cond.condition, func([], 1)) self.assertEqual(t_cond.true_expression, 1) self.assertEqual(t_cond.false_expression, 0)