From 30034bebd0ed8bfd2a71df14f8c7b0f3132e58f3 Mon Sep 17 00:00:00 2001 From: Innokenty Date: Fri, 13 Sep 2024 09:25:56 +0300 Subject: [PATCH 01/12] current state of pretty print --- python/hyperon/exts/snet_io/snet_gate.py | 102 ++++++++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index e2960d195..1a532eeeb 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -88,6 +88,8 @@ def __call__(self, command_a, *args_a): self.init_sdk() if command == 'get_service_callers': return args[0].generate_callers() + if command == 'get_service_callers_text': + return args[0].generate_callers_text() if command == 'create_service_space': return self.create_service_space(*args, **kwargs) if command == 'organization_list': @@ -137,9 +139,105 @@ def get_service_messages(self): def get_operation_atom(self): return OperationAtom(self.service_details[1], self) + def __pretty_print__(self, input_str): + list_of_functions = ["if", "match", "let", "let*", "unify", + "assertEqualToResult", "assertEqual", "remove-atom", "add-reduct", + "case"] # list should be possibly extended if new functions with complicated body appears in the future + str_symbols = ["'", '"', "'''"] # if we met function name, but it was in quotes, we shouldn't treat it like real function + special_symbols = [" ", "\t", "\n"] # to read word symbol by symbol till special symbol appears + line_threshold = 30 #threshold after which line break will be inserted + len_to_previous_break = 0 # to prevent duplicate \n symbols + def pp_func(str, num_of_tabs_outer): + pp_func_res = "" + _num_of_brackets = 0 + _num_of_symbols_in_line = 0 + j = 0 + _len_to_previous_break = len_to_previous_break + while j < len(str): + _symbol = str[j] + _add_symbol = _symbol + _num_of_symbols_in_line += 1 + if (_symbol == '('): + if (_num_of_brackets == 0) and (_len_to_previous_break > (num_of_tabs_outer + 2)): + _add_symbol = "\n" + "\t" * num_of_tabs_outer + _add_symbol + _num_of_symbols_in_line = 0 + _len_to_previous_break = 0 + _num_of_brackets += 1 + elif (_symbol == ')'): + _num_of_brackets -= 1 + if _num_of_brackets < 0: # we are out of function's body + pp_func_res += str[j:] + break + #in this case num_of_brackets means that we have reached end of inner (...). For example, in 'if' at leas condition is in brackets so we're putting line break there + elif (_num_of_brackets == 0) and (_len_to_previous_break > (num_of_tabs_outer + 2)): + if str[j + 1] in special_symbols: + j += 1 + _add_symbol += "\n" + "\t" * num_of_tabs_outer + _num_of_symbols_in_line = 0 + _len_to_previous_break = 0 + pp_func_res += _add_symbol + j += 1 + _len_to_previous_break += 1 + return pp_func_res + + num_of_tabs = 0 # currently num of tabs is strictly depends on number of '(' symbols + num_of_symbols_in_line = 0 + pretty_res = "" + str_detected = {"'": False, '"': False, "'''": False} # to check if we are inside of quotes or not + word_memory = "" + input_str = ' '.join(input_str.split()) # to remove every possible spaces duplication + i = 0 # since input_str can be altered during parsing process I need while not for loop + while i < len(input_str): + symbol = input_str[i] + add_symbol = symbol + num_of_symbols_in_line += 1 + if symbol in str_symbols: + word_memory = "" + str_detected[symbol] = not str_detected[symbol] + elif symbol in special_symbols: # word ended so we need to check if this is function + if word_memory in list_of_functions: + pp_func_res = pp_func(input_str[i:], num_of_tabs) # every function separately treated + input_str = input_str.replace(input_str[i + 1:], pp_func_res) # though we have ran pp_func over current function's body it is not full so we need to loop through renewed str_input + num_of_symbols_in_line = 0 + word_memory = "" + if symbol == "\n": + num_of_symbols_in_line = 0 + len_to_previous_break = 0 + elif symbol == "\t": + num_of_symbols_in_line -= 1 + elif symbol == '(': + word_memory = "" + num_of_tabs += 1 + # we will put a line break not for every '(', only if number of symbols in line exceeded threshold. PLus, to prevent several consequent line breaks, we're checking distance to previous line break + if (num_of_symbols_in_line > line_threshold) and (len_to_previous_break > (num_of_tabs + 2)): + add_symbol = "\n" + "\t" * (num_of_tabs - 1) + add_symbol + num_of_symbols_in_line = 0 + len_to_previous_break = 0 + elif symbol == ')': + word_memory = "" + num_of_tabs -= 1 + # if num_of_tabs == 0 then it should be the end of the current expression (for example, we have function's type and function definition in one string). + if (num_of_tabs == 0) and (len_to_previous_break > (num_of_tabs + 2)): + add_symbol = add_symbol + "\n" + num_of_symbols_in_line = 0 + len_to_previous_break = 0 + else: + str_flag = False + for key in str_detected: + str_flag = str_flag or str_detected[key] + # we are adding something to a memory only if this is not a part of string + word_memory += symbol * (not str_flag) + pretty_res += add_symbol + i += 1 + len_to_previous_break += 1 + pretty_res = pretty_res.replace(" ", " ") + pretty_res = pretty_res.replace("\t ", "\t") + pretty_res = pretty_res.replace("\n ", "\n") + return pretty_res + def generate_callers_text(self): - # TODO: pretty print - return "\n".join([repr(e) for e in self.generate_callers()]) + res = "\n".join([repr(e) for e in self.generate_callers()]) + return [S(self.__pretty_print__(res))] # this is not right. Currently, this is a stub def _map_type(self, t): type_map = {'bool': 'Bool', From aabda12d3ba7b2ddc57a80b978e0e4609e5c4afa Mon Sep 17 00:00:00 2001 From: DaddyWesker Date: Fri, 13 Sep 2024 16:27:20 +0300 Subject: [PATCH 02/12] Update snet_gate.py Fixed return value --- python/hyperon/exts/snet_io/snet_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index 1a532eeeb..38be0ad3a 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -237,7 +237,7 @@ def pp_func(str, num_of_tabs_outer): def generate_callers_text(self): res = "\n".join([repr(e) for e in self.generate_callers()]) - return [S(self.__pretty_print__(res))] # this is not right. Currently, this is a stub + return [ValueAtom(res)] def _map_type(self, t): type_map = {'bool': 'Bool', From e640f283c6d36d14f45316adb1d6d791ebf520f6 Mon Sep 17 00:00:00 2001 From: DaddyWesker Date: Mon, 16 Sep 2024 17:29:15 +0300 Subject: [PATCH 03/12] Update snet_gate.py --- python/hyperon/exts/snet_io/snet_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index 38be0ad3a..7381baf28 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -237,7 +237,7 @@ def pp_func(str, num_of_tabs_outer): def generate_callers_text(self): res = "\n".join([repr(e) for e in self.generate_callers()]) - return [ValueAtom(res)] + return [ValueAtom(self.__pretty_print__(res))] def _map_type(self, t): type_map = {'bool': 'Bool', From 48e9bff6827039aa8fd223cd0e70946634e8397c Mon Sep 17 00:00:00 2001 From: DaddyWesker Date: Mon, 16 Sep 2024 17:33:17 +0300 Subject: [PATCH 04/12] Update snet_gate.py --- python/hyperon/exts/snet_io/snet_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index 7381baf28..fd717178b 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -237,7 +237,7 @@ def pp_func(str, num_of_tabs_outer): def generate_callers_text(self): res = "\n".join([repr(e) for e in self.generate_callers()]) - return [ValueAtom(self.__pretty_print__(res))] + return self.__pretty_print__(res) def _map_type(self, t): type_map = {'bool': 'Bool', From 27f60b68c6c793a44f50f7d85ff0ceb9481b7402 Mon Sep 17 00:00:00 2001 From: DaddyWesker Date: Mon, 16 Sep 2024 17:33:56 +0300 Subject: [PATCH 05/12] Update snet_gate.py --- python/hyperon/exts/snet_io/snet_gate.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index fd717178b..c3ecf4c6c 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -88,8 +88,6 @@ def __call__(self, command_a, *args_a): self.init_sdk() if command == 'get_service_callers': return args[0].generate_callers() - if command == 'get_service_callers_text': - return args[0].generate_callers_text() if command == 'create_service_space': return self.create_service_space(*args, **kwargs) if command == 'organization_list': From fda9c3173f446b179af18554653be9174c3c226f Mon Sep 17 00:00:00 2001 From: Innokenty Date: Fri, 20 Sep 2024 09:06:21 +0300 Subject: [PATCH 06/12] prettyp print is being rewritten --- python/hyperon/exts/snet_io/snet_gate.py | 145 ++++++++--------------- 1 file changed, 50 insertions(+), 95 deletions(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index c3ecf4c6c..27995f5dc 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -111,6 +111,8 @@ def __init__(self, service_client): self.outputs = [] self.io_types = [] self.func_names = [] + self.len_threshold = 30 + self.current_len = 0 for method in methods: self.func_names += [method[0]] types = method[1:] @@ -137,105 +139,58 @@ def get_service_messages(self): def get_operation_atom(self): return OperationAtom(self.service_details[1], self) - def __pretty_print__(self, input_str): - list_of_functions = ["if", "match", "let", "let*", "unify", - "assertEqualToResult", "assertEqual", "remove-atom", "add-reduct", - "case"] # list should be possibly extended if new functions with complicated body appears in the future - str_symbols = ["'", '"', "'''"] # if we met function name, but it was in quotes, we shouldn't treat it like real function - special_symbols = [" ", "\t", "\n"] # to read word symbol by symbol till special symbol appears - line_threshold = 30 #threshold after which line break will be inserted - len_to_previous_break = 0 # to prevent duplicate \n symbols - def pp_func(str, num_of_tabs_outer): - pp_func_res = "" - _num_of_brackets = 0 - _num_of_symbols_in_line = 0 - j = 0 - _len_to_previous_break = len_to_previous_break - while j < len(str): - _symbol = str[j] - _add_symbol = _symbol - _num_of_symbols_in_line += 1 - if (_symbol == '('): - if (_num_of_brackets == 0) and (_len_to_previous_break > (num_of_tabs_outer + 2)): - _add_symbol = "\n" + "\t" * num_of_tabs_outer + _add_symbol - _num_of_symbols_in_line = 0 - _len_to_previous_break = 0 - _num_of_brackets += 1 - elif (_symbol == ')'): - _num_of_brackets -= 1 - if _num_of_brackets < 0: # we are out of function's body - pp_func_res += str[j:] - break - #in this case num_of_brackets means that we have reached end of inner (...). For example, in 'if' at leas condition is in brackets so we're putting line break there - elif (_num_of_brackets == 0) and (_len_to_previous_break > (num_of_tabs_outer + 2)): - if str[j + 1] in special_symbols: - j += 1 - _add_symbol += "\n" + "\t" * num_of_tabs_outer - _num_of_symbols_in_line = 0 - _len_to_previous_break = 0 - pp_func_res += _add_symbol - j += 1 - _len_to_previous_break += 1 - return pp_func_res + def __pretty_print_atoms__(self, input_atoms): - num_of_tabs = 0 # currently num of tabs is strictly depends on number of '(' symbols - num_of_symbols_in_line = 0 - pretty_res = "" - str_detected = {"'": False, '"': False, "'''": False} # to check if we are inside of quotes or not - word_memory = "" - input_str = ' '.join(input_str.split()) # to remove every possible spaces duplication - i = 0 # since input_str can be altered during parsing process I need while not for loop - while i < len(input_str): - symbol = input_str[i] - add_symbol = symbol - num_of_symbols_in_line += 1 - if symbol in str_symbols: - word_memory = "" - str_detected[symbol] = not str_detected[symbol] - elif symbol in special_symbols: # word ended so we need to check if this is function - if word_memory in list_of_functions: - pp_func_res = pp_func(input_str[i:], num_of_tabs) # every function separately treated - input_str = input_str.replace(input_str[i + 1:], pp_func_res) # though we have ran pp_func over current function's body it is not full so we need to loop through renewed str_input - num_of_symbols_in_line = 0 - word_memory = "" - if symbol == "\n": - num_of_symbols_in_line = 0 - len_to_previous_break = 0 - elif symbol == "\t": - num_of_symbols_in_line -= 1 - elif symbol == '(': - word_memory = "" - num_of_tabs += 1 - # we will put a line break not for every '(', only if number of symbols in line exceeded threshold. PLus, to prevent several consequent line breaks, we're checking distance to previous line break - if (num_of_symbols_in_line > line_threshold) and (len_to_previous_break > (num_of_tabs + 2)): - add_symbol = "\n" + "\t" * (num_of_tabs - 1) + add_symbol - num_of_symbols_in_line = 0 - len_to_previous_break = 0 - elif symbol == ')': - word_memory = "" - num_of_tabs -= 1 - # if num_of_tabs == 0 then it should be the end of the current expression (for example, we have function's type and function definition in one string). - if (num_of_tabs == 0) and (len_to_previous_break > (num_of_tabs + 2)): - add_symbol = add_symbol + "\n" - num_of_symbols_in_line = 0 - len_to_previous_break = 0 + def process_symbol_and_variable_atom(atom): + repr_atom = repr(atom) + self.current_len += len(repr_atom) + return repr_atom + + def process_grounded_atom(atom): + repr_atom = repr(atom) + self.current_len += len(repr_atom) + return repr_atom + + def check_len(depth): + if self.current_len > self.len_threshold: + self.current_len = 0 + return "\n" + "\t" * (depth - 1) + else: + return "" + + def process_atom(atom, depth): + process_res = "" + metatype = atom.get_metatype() + if metatype == AtomKind.EXPR: + self.current_len *= (depth <= 1) + process_res += ("\n" + "\t" * depth) * (depth > 1) + f"({process_expr_atom(atom, depth+1)})" + elif (metatype == AtomKind.SYMBOL) or (metatype == AtomKind.VARIABLE): + process_res += process_symbol_and_variable_atom(atom) + check_len(depth) + elif (metatype == AtomKind.GROUNDED): + process_res += process_grounded_atom(atom) + check_len(depth) else: - str_flag = False - for key in str_detected: - str_flag = str_flag or str_detected[key] - # we are adding something to a memory only if this is not a part of string - word_memory += symbol * (not str_flag) - pretty_res += add_symbol - i += 1 - len_to_previous_break += 1 - pretty_res = pretty_res.replace(" ", " ") - pretty_res = pretty_res.replace("\t ", "\t") - pretty_res = pretty_res.replace("\n ", "\n") - return pretty_res + raise Exception(f"Unexpected type of the Atom: {str(metatype)}") + return process_res + + def process_expr_atom(expr_atom, depth): + sub_atoms = expr_atom.get_children() + process_res = "" + for sub_atom in sub_atoms: + process_atom_res = process_atom(sub_atom, depth) + process_res += process_atom_res + check_len(depth) + process_res += " " + return process_res[:-1] + + res_string = "(" * (not (input_atoms[0].get_metatype() == AtomKind.EXPR)) + self.current_len = 0 + for atom in input_atoms: + res_string += process_atom(atom, 0) + res_string += "\n\n" + self.current_len = 0 + return res_string def generate_callers_text(self): - res = "\n".join([repr(e) for e in self.generate_callers()]) - return self.__pretty_print__(res) + return self.__pretty_print_atoms__(self.generate_callers()) def _map_type(self, t): type_map = {'bool': 'Bool', From 557eaa8892f5604c7c85d26404aa444180bd1003 Mon Sep 17 00:00:00 2001 From: Innokenty Date: Mon, 23 Sep 2024 07:48:50 +0300 Subject: [PATCH 07/12] rewritten pretty print --- python/hyperon/exts/snet_io/snet_gate.py | 37 ++++++++++++------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index 27995f5dc..607acadd7 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -4,13 +4,14 @@ from snet import sdk from hyperon import * + class SNetSDKWrapper: def __init__(self): self.snet_sdk = None def init_sdk(self, - private_key=os.getenv("SNET_PRIVATE_KEY", '0'*32), + private_key=os.getenv("SNET_PRIVATE_KEY", '0' * 32), eth_rpc_endpoint=os.getenv("ETH_RPC_ENDPOINT"), email=os.getenv("SNET_EMAIL"), identity_name="hyperon", @@ -32,7 +33,7 @@ def init_sdk(self, def organization_list(self): return self.snet_sdk.get_organization_list() - + def service_list(self, org_id): return self.snet_sdk.get_services_list(org_id) @@ -43,7 +44,7 @@ def create_service_client(self, org_id, service_id, free_call_token_expiry_block = int(free_call_token_expiry_block) service_client = self.snet_sdk.create_service_client( org_id=org_id, service_id=service_id, - #group_name="default_group", + # group_name="default_group", free_call_auth_token_bin=free_call_auth_token_bin, free_call_token_expiry_block=free_call_token_expiry_block) return ServiceCall(service_client) @@ -57,7 +58,7 @@ def create_service_space(self, org_id, service_id, **kwargs): space = GroundingSpaceRef() service_client = self.create_service_client(org_id, service_id, **kwargs) space.add_atom(E(S('='), E(S(org_id), S(service_id)), - service_client.get_operation_atom())) + service_client.get_operation_atom())) atoms = service_client.generate_callers() for atom in atoms: space.add_atom(atom) @@ -98,7 +99,8 @@ def __call__(self, command_a, *args_a): service_client = self.create_service_client(*args, **kwargs) return [service_client.get_operation_atom()] return [E(S('Error'), E(S('snet-sdk'), command_a, *args_a), - ValueAtom(f'unknown command {repr(command_a)}'))] + ValueAtom(f'unknown command {repr(command_a)}'))] + class ServiceCall: @@ -111,8 +113,9 @@ def __init__(self, service_client): self.outputs = [] self.io_types = [] self.func_names = [] - self.len_threshold = 30 + self.len_threshold = 50 self.current_len = 0 + self.len_to_last_eol = 0 for method in methods: self.func_names += [method[0]] types = method[1:] @@ -141,12 +144,7 @@ def get_operation_atom(self): def __pretty_print_atoms__(self, input_atoms): - def process_symbol_and_variable_atom(atom): - repr_atom = repr(atom) - self.current_len += len(repr_atom) - return repr_atom - - def process_grounded_atom(atom): + def process_svg_atom(atom): repr_atom = repr(atom) self.current_len += len(repr_atom) return repr_atom @@ -162,12 +160,12 @@ def process_atom(atom, depth): process_res = "" metatype = atom.get_metatype() if metatype == AtomKind.EXPR: - self.current_len *= (depth <= 1) - process_res += ("\n" + "\t" * depth) * (depth > 1) + f"({process_expr_atom(atom, depth+1)})" - elif (metatype == AtomKind.SYMBOL) or (metatype == AtomKind.VARIABLE): - process_res += process_symbol_and_variable_atom(atom) + check_len(depth) - elif (metatype == AtomKind.GROUNDED): - process_res += process_grounded_atom(atom) + check_len(depth) + len_to_last_eol_flag = self.current_len > 5 + self.current_len *= (depth <= 1) * (not len_to_last_eol_flag) + process_res += ("\n" + "\t" * depth) * ( + depth > 0) * len_to_last_eol_flag + f"({process_expr_atom(atom, depth + 1)})" + elif (metatype == AtomKind.SYMBOL) or (metatype == AtomKind.VARIABLE) or (metatype == AtomKind.GROUNDED): + process_res += process_svg_atom(atom) + check_len(depth) else: raise Exception(f"Unexpected type of the Atom: {str(metatype)}") return process_res @@ -196,7 +194,7 @@ def _map_type(self, t): type_map = {'bool': 'Bool', 'string': 'String', 'int32': 'Number', - 'float': 'Number'} + 'float ': 'Number'} return type_map[t] if t in type_map else t def generate_callers(self): @@ -228,6 +226,7 @@ def generate_callers(self): atoms += [metta_fun_type, function_expr] return atoms + @register_atoms() def snet_atoms(): defaultSDKAtom = OperationAtom("snet-sdk", SNetSDKWrapper(), unwrap=False) From 7e38ebe49979b3e97239e062c4b2c1e103d8bf02 Mon Sep 17 00:00:00 2001 From: Innokenty Date: Mon, 23 Sep 2024 07:57:45 +0300 Subject: [PATCH 08/12] remove redundant class var --- python/hyperon/exts/snet_io/snet_gate.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index 0cebbb72a..36046354c 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -117,7 +117,6 @@ def __init__(self, service_client): self.func_names = [] self.len_threshold = 50 self.current_len = 0 - self.len_to_last_eol = 0 for method in methods: self.func_names += [method[0]] types = method[1:] @@ -200,7 +199,7 @@ def _map_type(self, t): type_map = {'bool': 'Bool', 'string': 'String', 'int32': 'Number', - 'float ': 'Number'} + 'float': 'Number'} return type_map[t] if t in type_map else t def generate_callers(self): From 43f19190f943527972f0fb7e293e26f2eeb5cd8c Mon Sep 17 00:00:00 2001 From: Innokenty Date: Mon, 23 Sep 2024 07:59:35 +0300 Subject: [PATCH 09/12] example of generate_callers_text's usage added --- python/hyperon/exts/snet_io/test_snet_call.metta | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/hyperon/exts/snet_io/test_snet_call.metta b/python/hyperon/exts/snet_io/test_snet_call.metta index 2db17ac9f..7ecabda78 100644 --- a/python/hyperon/exts/snet_io/test_snet_call.metta +++ b/python/hyperon/exts/snet_io/test_snet_call.metta @@ -46,3 +46,6 @@ ; possible usage of open_channel_and_deposit function. 100 is the amount of AGIX and 10000 is a payment channel's TTL in blocks (snet-sdk open_channel_and_deposit &generative-lms 100 10000) + +; usage of generate_callers_text method +((py-dot &generative-lms generate_callers_text)) \ No newline at end of file From e4961b32106c641d199cb99606d32aa120c2bd96 Mon Sep 17 00:00:00 2001 From: Innokenty Date: Mon, 7 Oct 2024 07:44:33 +0300 Subject: [PATCH 10/12] move pretty print from class to outer function --- python/hyperon/exts/snet_io/snet_gate.py | 103 ++++++++++++----------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index 36046354c..e334ffad5 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -103,6 +103,60 @@ def __call__(self, command_a, *args_a): return [E(S('Error'), E(S('snet-sdk'), command_a, *args_a), ValueAtom(f'unknown command {repr(command_a)}'))] +len_threshold = 50 +current_len = 0 +def pretty_print_atoms(input_atoms): + + global len_threshold + global current_len + def process_svg_atom(atom): + global len_threshold + global current_len + repr_atom = repr(atom) + current_len += len(repr_atom) + return repr_atom + + def check_len(depth): + global len_threshold + global current_len + if current_len > len_threshold: + current_len = 0 + return "\n" + "\t" * (depth - 1) + else: + return "" + + def process_atom(atom, depth): + global len_threshold + global current_len + process_res = "" + metatype = atom.get_metatype() + if metatype == AtomKind.EXPR: + len_to_last_eol_flag = current_len > 5 + current_len *= (depth <= 1) * (not len_to_last_eol_flag) + process_res += ("\n" + "\t" * depth) * ( + depth > 0) * len_to_last_eol_flag + f"({process_expr_atom(atom, depth + 1)})" + elif (metatype == AtomKind.SYMBOL) or (metatype == AtomKind.VARIABLE) or (metatype == AtomKind.GROUNDED): + process_res += process_svg_atom(atom) + check_len(depth) + else: + raise Exception(f"Unexpected type of the Atom: {str(metatype)}") + return process_res + + def process_expr_atom(expr_atom, depth): + sub_atoms = expr_atom.get_children() + process_res = "" + for sub_atom in sub_atoms: + process_atom_res = process_atom(sub_atom, depth) + process_res += process_atom_res + check_len(depth) + process_res += " " + return process_res[:-1] + + res_string = "(" * (not (input_atoms[0].get_metatype() == AtomKind.EXPR)) + current_len = 0 + for atom in input_atoms: + res_string += process_atom(atom, 0) + res_string += "\n\n" + current_len = 0 + return res_string class ServiceCall: @@ -115,8 +169,6 @@ def __init__(self, service_client): self.outputs = [] self.io_types = [] self.func_names = [] - self.len_threshold = 50 - self.current_len = 0 for method in methods: self.func_names += [method[0]] types = method[1:] @@ -143,53 +195,8 @@ def get_service_messages(self): def get_operation_atom(self): return OperationAtom(self.service_details[1], self) - def __pretty_print_atoms__(self, input_atoms): - - def process_svg_atom(atom): - repr_atom = repr(atom) - self.current_len += len(repr_atom) - return repr_atom - - def check_len(depth): - if self.current_len > self.len_threshold: - self.current_len = 0 - return "\n" + "\t" * (depth - 1) - else: - return "" - - def process_atom(atom, depth): - process_res = "" - metatype = atom.get_metatype() - if metatype == AtomKind.EXPR: - len_to_last_eol_flag = self.current_len > 5 - self.current_len *= (depth <= 1) * (not len_to_last_eol_flag) - process_res += ("\n" + "\t" * depth) * ( - depth > 0) * len_to_last_eol_flag + f"({process_expr_atom(atom, depth + 1)})" - elif (metatype == AtomKind.SYMBOL) or (metatype == AtomKind.VARIABLE) or (metatype == AtomKind.GROUNDED): - process_res += process_svg_atom(atom) + check_len(depth) - else: - raise Exception(f"Unexpected type of the Atom: {str(metatype)}") - return process_res - - def process_expr_atom(expr_atom, depth): - sub_atoms = expr_atom.get_children() - process_res = "" - for sub_atom in sub_atoms: - process_atom_res = process_atom(sub_atom, depth) - process_res += process_atom_res + check_len(depth) - process_res += " " - return process_res[:-1] - - res_string = "(" * (not (input_atoms[0].get_metatype() == AtomKind.EXPR)) - self.current_len = 0 - for atom in input_atoms: - res_string += process_atom(atom, 0) - res_string += "\n\n" - self.current_len = 0 - return res_string - def generate_callers_text(self): - return self.__pretty_print_atoms__(self.generate_callers()) + return pretty_print_atoms(self.generate_callers()) def open_channel_and_deposit(self, amount, expiration): self.service_client.deposit_and_open_channel(amount, expiration) From 290125d4f11b1c73d6cb29431c5b893d5bae3485 Mon Sep 17 00:00:00 2001 From: DaddyWesker Date: Fri, 25 Oct 2024 09:00:46 +0300 Subject: [PATCH 11/12] Update snet_gate.py current_len and threshold into function --- python/hyperon/exts/snet_io/snet_gate.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index e334ffad5..dc2d3f83d 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -103,22 +103,19 @@ def __call__(self, command_a, *args_a): return [E(S('Error'), E(S('snet-sdk'), command_a, *args_a), ValueAtom(f'unknown command {repr(command_a)}'))] -len_threshold = 50 -current_len = 0 def pretty_print_atoms(input_atoms): - - global len_threshold - global current_len + len_threshold = 50 + current_len = 0 def process_svg_atom(atom): - global len_threshold - global current_len + nonlocal len_threshold + nonlocal current_len repr_atom = repr(atom) current_len += len(repr_atom) return repr_atom def check_len(depth): - global len_threshold - global current_len + nonlocal len_threshold + nonlocal current_len if current_len > len_threshold: current_len = 0 return "\n" + "\t" * (depth - 1) @@ -126,8 +123,8 @@ def check_len(depth): return "" def process_atom(atom, depth): - global len_threshold - global current_len + nonlocal len_threshold + nonlocal current_len process_res = "" metatype = atom.get_metatype() if metatype == AtomKind.EXPR: From ce7cba39676bd4c1da201dd56561319fe21a5df8 Mon Sep 17 00:00:00 2001 From: DaddyWesker Date: Fri, 25 Oct 2024 09:02:27 +0300 Subject: [PATCH 12/12] Update snet_gate.py --- python/hyperon/exts/snet_io/snet_gate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/hyperon/exts/snet_io/snet_gate.py b/python/hyperon/exts/snet_io/snet_gate.py index dc2d3f83d..6bce0ae24 100644 --- a/python/hyperon/exts/snet_io/snet_gate.py +++ b/python/hyperon/exts/snet_io/snet_gate.py @@ -148,7 +148,6 @@ def process_expr_atom(expr_atom, depth): return process_res[:-1] res_string = "(" * (not (input_atoms[0].get_metatype() == AtomKind.EXPR)) - current_len = 0 for atom in input_atoms: res_string += process_atom(atom, 0) res_string += "\n\n"