def preprocess(s): ProcessRes = namedtuple("ProcessRes", ['string', 'is_template', 'is_sub']) is_template = (re.search(r"<.*>", s) is not None) is_sub = (re.search(r"\.", s) is not None) a = decamelize.convert(s).replace('_', ' ') a = re.sub(r"<.*>|\.", "", a) return ProcessRes(string=a, is_template=is_template, is_sub=is_sub)
def __init__(self): self.abi = {} for file_name in json.loads(get_data("config", "abi_list.json")): if file_name.split(".")[-1] != "json": continue contract_abi = json.loads(get_data("abis", file_name)) abi_dict = {} for fn in contract_abi: if "name" in fn: abi_dict[decamelize.convert(fn["name"])] = fn else: abi_dict["constructor"] = fn self.abi[file_name.split(".")[0]] = abi_dict
def code_ingest(filename, kw_pattern, nlp): class_list = [] att_list = [] method_list = [] with open(filename, 'r', encoding='utf-8') as file: code_text = file.readlines() terms_list = [] # strip strings of newlines and whitespaces code_text = [x.strip() for x in code_text] # only pull classes, attributes & methods for x in code_text: if (x.startswith('public') or x.startswith('private') or x.startswith('protected') or x.startswith('class') or x.startswith('enum')): terms_list.append(x) # remove noise by tokenising terms_list = [word_tokenize(x) for x in terms_list] # flatten list terms_list = [x for sublist in terms_list for x in sublist] # decamelize terms_list = [decamelize.convert(x) for x in terms_list] # split back those decamelised to tuple values (using only first '_') terms_list = [x.split('_') for x in terms_list] # flatten list terms_list = [x for sublist in terms_list for x in sublist] # remove any characters w/out meaning terms_list = [x for x in terms_list if (len(x) > 2 and x.isalpha())] # remove keywords terms = kw_pattern.sub('', ' '.join(terms_list)) terms_list = [x.lemma_ for x in nlp(terms)] terms = ' '.join(terms_list) return terms
def func(*args, **kwargs): if attr not in self.abi: raise ValueError("Function name not match this contract") fn = self.abi[attr] if len(fn["inputs"]) != len(args): raise ValueError("Input not match") sig = to_hex(function_abi_to_4byte_selector(fn)) input_type = [p["type"] for p in fn["inputs"]] output_type = [p["type"] for p in fn["outputs"]] data = to_hex(encode_abi(input_type, args)) if "value" in kwargs: value = to_hex(kwargs["value"]) else: value = to_hex(0) raw_output = requests.post( BASE_URL, json={ "jsonrpc": "2.0", "method": "eth_call", "params": [ { "to": self.address, "data": sig + data[2:], "value": value, }, self.block_height, ], "id": 12, }, ).json()["result"] raw_value = decode_abi(output_type, to_bytes(hexstr=raw_output)) if len(raw_value) == 1: return raw_value[0] Custom = namedtuple( "Custom", [decamelize.convert(p["name"]) for p in fn["outputs"]]) return Custom._make(raw_value)
def column_reflect(inspector, table, column_info): column_info["key"] = decamelize.convert(column_info["name"])
def test_add(self): self.assertEqual(decamelize.convert("CamelCase"), "camel_case")