Best Python code snippet using tempest_python
build_graph.py
Source:build_graph.py
1# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14import sys15import json16import logging17from collections import defaultdict18import tqdm19import redis20from redis._compat import b, unicode, bytes, long, basestring21from rediscluster.nodemanager import NodeManager22from rediscluster.crc import crc1623import argparse24import time25import pickle26import numpy as np27import scipy.sparse as sp28log = logging.getLogger(__name__)29root = logging.getLogger()30root.setLevel(logging.DEBUG)31handler = logging.StreamHandler(sys.stdout)32handler.setLevel(logging.DEBUG)33formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')34handler.setFormatter(formatter)35root.addHandler(handler)36def encode(value):37 """38 Return a bytestring representation of the value.39 This method is copied from Redis' connection.py:Connection.encode40 """41 if isinstance(value, bytes):42 return value43 elif isinstance(value, (int, long)):44 value = b(str(value))45 elif isinstance(value, float):46 value = b(repr(value))47 elif not isinstance(value, basestring):48 value = unicode(value)49 if isinstance(value, unicode):50 value = value.encode('utf-8')51 return value52def crc16_hash(data):53 return crc16(encode(data))54def get_redis(startup_host, startup_port):55 startup_nodes = [{"host": startup_host, "port": startup_port}, ]56 nodemanager = NodeManager(startup_nodes=startup_nodes)57 nodemanager.initialize()58 rs = {}59 for node, config in nodemanager.nodes.items():60 rs[node] = redis.Redis(61 host=config["host"], port=config["port"], decode_responses=False)62 return rs, nodemanager63def load_data(edge_path):64 src, dst = [], []65 with open(edge_path, "r") as f:66 for i in tqdm.tqdm(f):67 s, d, _ = i.split()68 s = int(s)69 d = int(d)70 src.append(s)71 dst.append(d)72 dst.append(s)73 src.append(d)74 src = np.array(src, dtype="int64")75 dst = np.array(dst, dtype="int64")76 return src, dst77def build_edge_index(edge_path, num_nodes, startup_host, startup_port,78 num_bucket):79 #src, dst = load_data(edge_path)80 rs, nodemanager = get_redis(startup_host, startup_port)81 dst_mp, edge_mp = defaultdict(list), defaultdict(list)82 with open(edge_path) as f:83 for l in tqdm.tqdm(f):84 a, b, idx = l.rstrip().split('\t')85 a, b, idx = int(a), int(b), int(idx)86 dst_mp[a].append(b)87 edge_mp[a].append(idx)88 part_dst_dicts = {}89 for i in tqdm.tqdm(range(num_nodes)):90 #if len(edge_index.v[i]) == 0:91 # continue92 #v = edge_index.v[i].astype("int64").reshape([-1, 1])93 #e = edge_index.eid[i].astype("int64").reshape([-1, 1])94 if i not in dst_mp:95 continue96 v = np.array(dst_mp[i]).astype('int64').reshape([-1, 1])97 e = np.array(edge_mp[i]).astype('int64').reshape([-1, 1])98 o = np.hstack([v, e])99 key = "d:%s" % i100 part = crc16_hash(key) % num_bucket101 if part not in part_dst_dicts:102 part_dst_dicts[part] = {}103 dst_dicts = part_dst_dicts[part]104 dst_dicts["d:%s" % i] = o.tobytes()105 if len(dst_dicts) > 10000:106 slot = nodemanager.keyslot("part-%s" % part)107 node = nodemanager.slots[slot][0]['name']108 while True:109 res = rs[node].hmset("part-%s" % part, dst_dicts)110 if res:111 break112 log.info("HMSET FAILED RETRY connected %s" % node)113 time.sleep(1)114 part_dst_dicts[part] = {}115 for part, dst_dicts in part_dst_dicts.items():116 if len(dst_dicts) > 0:117 slot = nodemanager.keyslot("part-%s" % part)118 node = nodemanager.slots[slot][0]['name']119 while True:120 res = rs[node].hmset("part-%s" % part, dst_dicts)121 if res:122 break123 log.info("HMSET FAILED RETRY connected %s" % node)124 time.sleep(1)125 part_dst_dicts[part] = {}126 log.info("dst_dict Done")127def build_edge_id(edge_path, num_nodes, startup_host, startup_port,128 num_bucket):129 src, dst = load_data(edge_path)130 rs, nodemanager = get_redis(startup_host, startup_port)131 part_edge_dict = {}132 for i in tqdm.tqdm(range(len(src))):133 key = "e:%s" % i134 part = crc16_hash(key) % num_bucket135 if part not in part_edge_dict:136 part_edge_dict[part] = {}137 edge_dict = part_edge_dict[part]138 edge_dict["e:%s" % i] = int(src[i]) * num_nodes + int(dst[i])139 if len(edge_dict) > 10000:140 slot = nodemanager.keyslot("part-%s" % part)141 node = nodemanager.slots[slot][0]['name']142 while True:143 res = rs[node].hmset("part-%s" % part, edge_dict)144 if res:145 break146 log.info("HMSET FAILED RETRY connected %s" % node)147 time.sleep(1)148 part_edge_dict[part] = {}149 for part, edge_dict in part_edge_dict.items():150 if len(edge_dict) > 0:151 slot = nodemanager.keyslot("part-%s" % part)152 node = nodemanager.slots[slot][0]['name']153 while True:154 res = rs[node].hmset("part-%s" % part, edge_dict)155 if res:156 break157 log.info("HMSET FAILED RETRY connected %s" % node)158 time.sleep(1)159 part_edge_dict[part] = {}160def build_infos(edge_path, num_nodes, startup_host, startup_port, num_bucket):161 src, dst = load_data(edge_path)162 rs, nodemanager = get_redis(startup_host, startup_port)163 slot = nodemanager.keyslot("num_nodes")164 node = nodemanager.slots[slot][0]['name']165 res = rs[node].set("num_nodes", num_nodes)166 slot = nodemanager.keyslot("num_edges")167 node = nodemanager.slots[slot][0]['name']168 rs[node].set("num_edges", len(src))169 slot = nodemanager.keyslot("nf:infos")170 node = nodemanager.slots[slot][0]['name']171 rs[node].set("nf:infos", json.dumps([['feats', [-1, 602], 'float32'], ]))172 slot = nodemanager.keyslot("ef:infos")173 node = nodemanager.slots[slot][0]['name']174 rs[node].set("ef:infos", json.dumps([]))175def build_node_feat(node_feat_path, num_nodes, startup_host, startup_port, num_bucket):176 assert node_feat_path != "", "node_feat_path empty!"177 feat_dict = np.load(node_feat_path)178 for k in feat_dict.keys():179 feat = feat_dict[k]180 assert feat.shape[0] == num_nodes, "num_nodes invalid"181 rs, nodemanager = get_redis(startup_host, startup_port)182 part_feat_dict = {}183 for k in feat_dict.keys():184 feat = feat_dict[k]185 for i in tqdm.tqdm(range(num_nodes)):186 key = "nf:%s:%i" % (k, i)187 value = feat[i].tobytes()188 part = crc16_hash(key) % num_bucket189 if part not in part_feat_dict:190 part_feat_dict[part] = {}191 part_feat = part_feat_dict[part]192 part_feat[key] = value193 if len(part_feat) > 100:194 slot = nodemanager.keyslot("part-%s" % part)195 node = nodemanager.slots[slot][0]['name']196 while True:197 res = rs[node].hmset("part-%s" % part, part_feat)198 if res:199 break200 log.info("HMSET FAILED RETRY connected %s" % node)201 time.sleep(1)202 part_feat_dict[part] = {}203 for part, part_feat in part_feat_dict.items():204 if len(part_feat) > 0:205 slot = nodemanager.keyslot("part-%s" % part)206 node = nodemanager.slots[slot][0]['name']207 while True:208 res = rs[node].hmset("part-%s" % part, part_feat)209 if res:210 break211 log.info("HMSET FAILED RETRY connected %s" % node)212 time.sleep(1)213 part_feat_dict[part] = {}214if __name__ == '__main__':215 parser = argparse.ArgumentParser(description='gen_redis_conf')216 parser.add_argument('--startup_port', type=int, required=True)217 parser.add_argument('--startup_host', type=str, required=True)218 parser.add_argument('--edge_path', type=str, default="")219 parser.add_argument('--node_feat_path', type=str, default="")220 parser.add_argument('--num_nodes', type=int, default=0)221 parser.add_argument('--num_bucket', type=int, default=64)222 parser.add_argument(223 '--mode',224 type=str,225 required=True,226 help="choose one of the following modes (clear, edge_index, edge_id, graph_attr)"227 )228 args = parser.parse_args()229 log.info("Mode: {}".format(args.mode))230 if args.mode == 'edge_index':231 build_edge_index(args.edge_path, args.num_nodes, args.startup_host,232 args.startup_port, args.num_bucket)233 elif args.mode == 'edge_id':234 build_edge_id(args.edge_path, args.num_nodes, args.startup_host,235 args.startup_port, args.num_bucket)236 elif args.mode == 'graph_attr':237 build_infos(args.edge_path, args.num_nodes, args.startup_host,238 args.startup_port, args.num_bucket)239 elif args.mode == 'node_feat':240 build_node_feat(args.node_feat_path, args.num_nodes, args.startup_host,241 args.startup_port, args.num_bucket)242 else:...
hosts.py
Source:hosts.py
1# Copyright 2014 NEC Corporation. All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License"); you may4# not use this file except in compliance with the License. You may obtain5# a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the12# License for the specific language governing permissions and limitations13# under the License.14import copy15list_hosts = {16 'status_code': [200],17 'response_body': {18 'type': 'object',19 'properties': {20 'hosts': {21 'type': 'array',22 'items': {23 'type': 'object',24 'properties': {25 'host_name': {'type': 'string'},26 'service': {'type': 'string'},27 'zone': {'type': 'string'}28 },29 'additionalProperties': False,30 'required': ['host_name', 'service', 'zone']31 }32 }33 },34 'additionalProperties': False,35 'required': ['hosts']36 }37}38get_host_detail = {39 'status_code': [200],40 'response_body': {41 'type': 'object',42 'properties': {43 'host': {44 'type': 'array',45 'item': {46 'type': 'object',47 'properties': {48 'resource': {49 'type': 'object',50 'properties': {51 'cpu': {'type': 'integer'},52 'disk_gb': {'type': 'integer'},53 'host': {'type': 'string'},54 'memory_mb': {'type': 'integer'},55 'project': {'type': 'string'}56 },57 'additionalProperties': False,58 'required': ['cpu', 'disk_gb', 'host',59 'memory_mb', 'project']60 }61 },62 'additionalProperties': False,63 'required': ['resource']64 }65 }66 },67 'additionalProperties': False,68 'required': ['host']69 }70}71startup_host = {72 'status_code': [200],73 'response_body': {74 'type': 'object',75 'properties': {76 'host': {'type': 'string'},77 'power_action': {'enum': ['startup']}78 },79 'additionalProperties': False,80 'required': ['host', 'power_action']81 }82}83# The 'power_action' attribute of 'shutdown_host' API is 'shutdown'84shutdown_host = copy.deepcopy(startup_host)85shutdown_host['response_body']['properties']['power_action'] = {86 'enum': ['shutdown']87}88# The 'power_action' attribute of 'reboot_host' API is 'reboot'89reboot_host = copy.deepcopy(startup_host)90reboot_host['response_body']['properties']['power_action'] = {91 'enum': ['reboot']92}93update_host = {94 'status_code': [200],95 'response_body': {96 'type': 'object',97 'properties': {98 'host': {'type': 'string'},99 'maintenance_mode': {'enum': ['on_maintenance',100 'off_maintenance']},101 'status': {'enum': ['enabled', 'disabled']}102 },103 'additionalProperties': False,104 'required': ['host', 'maintenance_mode', 'status']105 }...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!