def setup_rand_nas(self, request): dep_low, dep_up = request.param[0] wid_low, wid_up = request.param[1] maxbd_low, maxbd_up = request.param[2] depth = random.randint(dep_low, dep_up) width = random.randint(wid_low, wid_up) max_bdepth = random.randint(maxbd_low, maxbd_up) return Nas(depth=depth, width=width, max_branch_depth=max_bdepth)
def setup_nas(self, request): m_best, opt_k, seed = request.param return Nas(m_best=m_best, opt_best_k=opt_k, randseed=seed)
def test_eliminate(self, setup_nas, pool_size_exp): nas = setup_nas pool_size = 10 ** pool_size_exp pool = [i for i in range(pool_size)] scores = [random.random() for i in range(pool_size)] scores_bak = scores.copy() mid_val = nas._Nas__eliminate(pool, scores) for i in range(len(pool)): num = pool[i] assert(scores[i] is scores_bak[num]) assert(scores[i] >= mid_val) @pytest.mark.run def test_run(self, setup_rand_nas): nas = setup_rand_nas nas.run() if __name__ == '__main__': dep_low, dep_up = TEST_RAND_NAS[0][0] wid_low, wid_up = TEST_RAND_NAS[0][1] maxbd_low, maxbd_up = TEST_RAND_NAS[0][2] depth = random.randint(dep_low, dep_up) width = random.randint(wid_low, wid_up) max_bdepth = random.randint(maxbd_low, maxbd_up) print("d: {0}, w: {1}, mbd: {2}".format(depth, width, max_bdepth)) # nas = Nas(depth=depth, width=width, max_branch_depth=max_bdepth, randseed=100) nas = Nas() nas.run()
from nas import Nas if __name__ == '__main__': nas = Nas(randseed=1000) nas.run()
def help_info(): print "console.py nas [upload|ls] {dir}" print "console.py oss [upload] {dir}" exit(0) if __name__ == "__main__": if len(sys.argv) < 2: help_info() module = sys.argv[1] if module == "nas": if len(sys.argv) < 4: help_info() cmd = sys.argv[2] nas = Nas() if cmd == "upload": src_dir = sys.argv[3] if not src_dir.startswith("/"): print "source dir must start with /" help_info() nas.copy_to_nas(src_dir) elif cmd == "ls": dir = sys.argv[3] nas.ls_dir(dir) else: help_info() elif module == "oss": pass else: help_info()
def main_func(event, content): # Ignore the retry process from Slack if 'X-Slack-Retry-Num' in event['params']['header']: print("this is redirect requests!") return 204 content_type = type(event['body']) if content_type is str: parsed_event = parse_lambda_event_str(event) # setup all need informations nas_user_id = parsed_event['user_id'] nas_user_name = parsed_event['user_name'] team_id = parsed_event['team_id'] sent_channel_id = parsed_event['channel_id'] command = parsed_event['command'] if command == '/nas': sended_message = parsed_event['text'].split() receive_user_name = sended_message[0].lstrip('@') receive_user_id = bring_slack_id_from_slack_name(receive_user_name) if command == '/use_nas_gacha_ticket': use_ticket = parsed_event['text'].split()[0] if content_type is dict: # setup all need informations nas_user_id = event['body']['event']['user'] nas_user_name = bring_slack_name_from_slack_id(nas_user_id) receive_user_id = event['body']['event']['item_user'] receive_user_name = bring_slack_name_from_slack_id(receive_user_id) team_id = event['body']['team_id'] sent_channel_id = event['body']['event']['item']['channel'] send_stamp = event['body']['event']['reaction'] command = '/nas_stamp' if command == '/nas': # check can send nas message nas_obj = Nas(nas_user_id, nas_user_name, team_id) if nas_obj.chack_self_portrait(receive_user_id) is True: print('self portrait') send_user_slack_text = '自画自賛乙' post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok if nas_obj.check_can_send_nas() is False: print('nas send limit') send_user_slack_text = '今週はもうnasを送れないよ!' post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok if receive_user_id == '': print('the user not exist.') send_user_slack_text = 'そのユーザは存在しないよ!' post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok # forming message message = "" for word in sended_message[1:]: message += word # setup slack text for send user sended_nas_num = nas_obj.sended_nas_num() remain_nas = NAS_LIMIT - sended_nas_num nas_bonus = nas_obj.nas_bonus() nas_status = nas_obj.nas_status() send_user_slack_text = "コマンドからnasを送れたよ!\n今週の残りnas数: {0}\n先週からのボーナス: {1}\nあなたの残りnasは{2}です.".format(remain_nas, nas_bonus, nas_status) # setup slack text for receive user receive_user_slack_text = "<@{0}> {1}さんからのメッセージです。\n {2}".format(receive_user_name, nas_user_name, message) # send nas message post_public_message_to_slack(receive_user_slack_text, PUBLIC_NAS_CHANNEL_ID) # for receive user post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user # create nas record nas_obj.nas_message(receive_user_id, receive_user_name) return requests.codes.ok if command == '/nas_stamp': nas_obj = Nas(nas_user_id, nas_user_name, team_id) if nas_obj.chack_self_portrait(receive_user_id) is True: print('self portrait') send_user_slack_text = '自画自賛乙' post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok if nas_obj.check_can_send_nas() is False: print('nas send limit') send_user_slack_text = '今週はもうnasを送れないよ!' post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok # set write user info STAMP_CONFIG.set('user_info', 'nas_user_id', nas_user_id) STAMP_CONFIG.set('user_info', 'nas_user_name', nas_user_name) STAMP_CONFIG.set('user_info', 'receive_user_id', receive_user_id) STAMP_CONFIG.set('user_info', 'receive_user_name', receive_user_name) if nas_obj.nas_stamp(receive_user_id, receive_user_name, send_stamp) is False: return requests.codes.ok # setup slack text for send user sended_nas_num = nas_obj.sended_nas_num() remain_nas = NAS_LIMIT - sended_nas_num nas_bonus = nas_obj.nas_bonus() nas_status = nas_obj.nas_status() send_user_slack_text = STAMP_CONFIG[send_stamp]['confirm_message'] + \ "\n今週の残りnas数: {0}\n先週からのボーナス: {1}\nあなたの残りnasは{2}です.".format(remain_nas, nas_bonus, nas_status) # setup slack text for receive user receive_user_slack_text = STAMP_CONFIG[send_stamp]['send_message'] # send nas message post_private_message_to_slack(receive_user_slack_text, PUBLIC_NAS_CHANNEL_ID, receive_user_id) # for receive user post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok if command == '/nas_rank': receive_all_nas_group_by_user = calc_nas_ranking_this_week() rank_count = 1 send_user_slack_text = "今週のnasランキング\n順位 ユーザ名 貰ったnas数\n" # setup slack text for send user for user_name, receive_nas in receive_all_nas_group_by_user.items(): send_user_slack_text += str(rank_count) + ". " + str(user_name) + "\t" + str(receive_nas) + "\n" rank_count += 1 post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok if command == '/nas_st': nas_obj = Nas(nas_user_id, nas_user_name, team_id) # setup slack text for send user sended_nas_num = nas_obj.sended_nas_num() remain_nas = NAS_LIMIT - sended_nas_num nas_bonus = nas_obj.nas_bonus() nas_status = nas_obj.nas_status() send_user_slack_text = "今週の残りnas数: {0}\n先週からのボーナス: {1}\nあなたの残りnasは{2}です.".format(remain_nas, nas_bonus, nas_status) # send nas message post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok if command == '/nas_gacha': nas_obj = Nas(nas_user_id, nas_user_name, team_id) if nas_obj.check_can_run_gacha() is False: print("can't run nas gacha") send_user_slack_text = 'ガチャの残り回数がもう無いよ!もっとnasを貰ってきてね。' post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok send_user_slack_text = "デュルデュルデュルデュル..." post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user gacha_result = nas_obj.nas_gacha() time.sleep(3) # setup slack text for send user remain_nas_gacha = nas_obj.nas_gacha_status() if gacha_result != '': send_user_slack_text = "ドン!今回の結果はあたりでした!\n当たった景品 {0}\n残りのガチャ回数は{1}回です".format(gacha_result, remain_nas_gacha) # send nas message post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user else: send_user_slack_text = "ドン!今回の結果ははずれでした!\n残りのガチャ回数は{0}回です".format(remain_nas_gacha) post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok if command == '/nas_gacha_status': nas_obj = Nas(nas_user_id, nas_user_name, team_id) remain_nas_gacha = nas_obj.nas_gacha_status() until_next_time_nas_num = nas_obj.calc_until_next_time_nas_num() send_user_slack_text = "残りのガチャ回数は{0}回です\n回せるようになるまで{1}個のnasを受け取る必要があります".format(remain_nas_gacha, until_next_time_nas_num) # send nas message post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user if command == '/nas_gacha_tickets': nas_obj = Nas(nas_user_id, nas_user_name, team_id) has_tickets = nas_obj.check_nas_gacha_tickets() send_user_slack_text = "持っているチケットリスト\n" for prize_name, has_num in has_tickets.items(): send_user_slack_text += "{0} : {1} \n".format(prize_name, has_num) # send nas message post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok if command == '/use_nas_gacha_ticket': nas_obj = Nas(nas_user_id, nas_user_name, team_id) result = nas_obj.use_nas_gacha_tickets(use_ticket) if result is False: send_user_slack_text = "チケットの消費に失敗しました。" post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok send_user_slack_text = "{0}のチケットを消費しました!".format(use_ticket) post_private_message_to_slack(send_user_slack_text, sent_channel_id, nas_user_id) # for send user return requests.codes.ok