From 5bedb694418af3938c5dfefe3f7d8d1a75f470df Mon Sep 17 00:00:00 2001 From: latentvector <you@example.com> Date: Tue, 3 Sep 2024 16:06:18 -0400 Subject: [PATCH] module --- commune/aes.py | 40 +-------------------- commune/key.py | 19 ---------- commune/module.py | 83 +++++++++++++++++++++++++++++++++++++++----- commune/namespace.py | 18 ---------- commune/routes.yaml | 14 -------- commune/server.py | 5 ++- commune/ticket.py | 22 ------------ tests/test_server.py | 3 +- 8 files changed, 80 insertions(+), 124 deletions(-) diff --git a/commune/aes.py b/commune/aes.py index 7d47c8845..ec3dcefd3 100644 --- a/commune/aes.py +++ b/commune/aes.py @@ -44,42 +44,4 @@ def _pad(self, s): def _unpad(self, s): return s[:-ord(s[len(s)-1:])] - @classmethod - def test(cls, data=None, key='dummy'): - import torch - data = 'fammmmmdjsjfhdjfh' - print(data) - self = cls(key=key) - size_bytes = sys.getsizeof(data) - start_time = time.time() - encrypted = self.encrypt(data) - decrypted = self.decrypt(encrypted) - assert decrypted == data, f'ENCRYPTION FAILED' - seconds = time.time() - start_time - rate = size_bytes / seconds - return {'msg': 'PASSED test_encrypt_decrypt', 'rate': rate, 'seconds': seconds, 'size_bytes': size_bytes} - - @classmethod - def test_encrypt_decrypt_throughput(cls, key='dummy'): - import streamlit as st - self = cls(key=key) - test_object = [1,2,3,5]*1000000 - start_time = time.time() - encrypted = self.encrypt(test_object) - seconds = time.time() - start_time - size_bytes = sys.getsizeof(test_object) - encrypt_rate = size_bytes / seconds - - start_time = time.time() - decrypted = self.decrypt(encrypted) - seconds = time.time() - start_time - size_bytes = sys.getsizeof(test_object) - decrypt_rate = size_bytes / seconds - - st.write(f'ENCRYPT SPEED (MB per Second): {encrypt_rate//1000}') - st.write(f'DECRYPT SPEED (MB per Second): {decrypt_rate//1000}') - - print('PASSED test_encrypt_decrypt') - - return True - + \ No newline at end of file diff --git a/commune/key.py b/commune/key.py index fa4bd4d65..63b68863a 100644 --- a/commune/key.py +++ b/commune/key.py @@ -211,7 +211,6 @@ def add_key(cls, path:str, mnemonic:str = None, password:str=None, refresh:bool= if password != None: key_json = cls.encrypt(data=key_json, password=password) c.print(cls.put(path, key_json)) - cls.add_key_address(path, key.ss58_address) cls.update() return json.loads(key_json) @@ -239,23 +238,6 @@ def rename_key(self, new_path): @property def hash_pass(self): return c.hash(self.private_key) - - @classmethod - def get_hash_pass(cls, key=None, max_age=10000): - time_seed = str(time.time() // max_age) - return c.hash(str(c.get_key(key).to_json()) + time_seed) - @classmethod - def key2hash(cls, search=None, max_age=10000): - key2hash = {} - for key in cls.keys(search=search): - print(key) - try: - key_hash = cls.get_hash_pass(key) - except Exception as e: - c.print(f'failed to get hash for {key} due to {e}', color='red') - continue - key2hash[key] = key_hash - return key2hash @classmethod def mv_key(cls, path, new_path): @@ -267,7 +249,6 @@ def mv_key(cls, path, new_path): new_key = cls.get_key(new_path) return {'success': True, 'from': path , 'to': new_path, 'key': new_key} - rename_key = mv_key @classmethod diff --git a/commune/module.py b/commune/module.py index 05d7154a7..330fff8b5 100755 --- a/commune/module.py +++ b/commune/module.py @@ -43,7 +43,7 @@ class c: default_port_range = [50050, 50150] # the port range between 50050 and 50150 default_ip = local_ip = loopback = '0.0.0.0' address = '0.0.0.0:8888' # the address of the server (default) - rootpath = root_path = root = '/'.join(__file__.split('/')[:-2]) # the path to the root of the library + rootpath = root_path = root = '/'.join(__file__.split('/')[:-1]) # the path to the root of the library homepath = home_path = os.path.expanduser('~') # the home path libpath = lib_path = os.path.dirname(root_path) # the path to the library repopath = repo_path = os.path.dirname(root_path) # the path to the repo @@ -950,7 +950,7 @@ def enable_routes(cls, routes:dict=None, verbose=False): # WARNING : THE PLACE HOLDERS MUST NOT INTERFERE WITH THE KWARGS OTHERWISE IT WILL CAUSE A BUG IF THE KWARGS ARE THE SAME AS THE PLACEHOLDERS # THE PLACEHOLDERS ARE NAMED AS module_ph and fn_ph AND WILL UNLIKELY INTERFERE WITH THE KWARGS def fn_generator( *args, module_ph, fn_ph, **kwargs): - module_ph = cls.module(module_ph) + module_ph = c.get_module(module_ph) fn_type = module_ph.classify_fn(fn_ph) module_ph = module_ph() if fn_type == 'self' else module_ph return getattr(module_ph, fn_ph)(*args, **kwargs) @@ -4412,8 +4412,10 @@ def simple2objectpath(cls, return classes[-1] @classmethod - def simple2object(cls, path:str, **kwargs) -> str: - path = cls.simple2objectpath(path, **kwargs) + def simple2object(cls, path:str = None, **kwargs) -> str: + path = path or 'module' + path = c.simple2objectpath(path, **kwargs) + print(path) try: return cls.import_object(path) except: @@ -4566,14 +4568,15 @@ def local_modules(cls, search=None): object_paths = [p for p in object_paths if search in p] return sorted(list(set(object_paths))) @classmethod - def lib_tree(cls, ): - return cls.get_tree(cls.libpath) + def lib_tree(cls): + return c.get_tree(cls.libpath) @classmethod def local_tree(cls ): return cls.get_tree(cls.pwd()) @classmethod def get_tree(cls, path): + print(path) class_paths = cls.find_classes(path) simple_paths = cls.simplify_paths(class_paths) return dict(zip(simple_paths, class_paths)) @@ -4610,11 +4613,11 @@ def get_module(cls, if cache and cache_key in c.module_cache: module = c.module_cache[cache_key] return module + print(path) module = c.simple2object(path) # ensure module if verbose: c.print(f'Loaded {path} in {c.time() - t0} seconds', color='green') - if init_kwargs != None: module = module(**init_kwargs) is_module = c.is_module(module) @@ -4638,8 +4641,6 @@ def tree(cls, search=None, cache=True): if search != None: tree = {k:v for k,v in tree.items() if search in k} return tree - - return tree def overlapping_modules(self, search:str=None, **kwargs): @@ -6098,6 +6099,70 @@ def threads(cls, search:str = None): return threads + + @classmethod + def python2str(cls, input): + input = deepcopy(input) + input_type = type(input) + if input_type == str: + return input + if input_type in [dict]: + input = json.dumps(input) + elif input_type in [bytes]: + input = cls.bytes2str(input) + elif input_type in [list, tuple, set]: + input = json.dumps(list(input)) + elif input_type in [int, float, bool]: + input = str(input) + return input + + + + # JSON2BYTES + @classmethod + def dict2str(cls, data: str) -> str: + return json.dumps(data) + + @classmethod + def dict2bytes(cls, data: str) -> bytes: + return cls.str2bytes(cls.json2str(data)) + + @classmethod + def bytes2dict(cls, data: bytes) -> str: + data = cls.bytes2str(data) + return json.loads(data) + + + # BYTES LAND + + # STRING2BYTES + @classmethod + def str2bytes(cls, data: str, mode: str = 'hex') -> bytes: + if mode in ['utf-8']: + return bytes(data, mode) + elif mode in ['hex']: + return bytes.fromhex(data) + + @classmethod + def bytes2str(cls, data: bytes, mode: str = 'utf-8') -> str: + + if hasattr(data, 'hex'): + return data.hex() + else: + if isinstance(data, str): + return data + return bytes.decode(data, mode) + + + @classmethod + def str2python(cls, input)-> dict: + assert isinstance(input, str), 'input must be a string, got {}'.format(input) + try: + output_dict = json.loads(input) + except json.JSONDecodeError as e: + return input + + return output_dict c.enable_routes() # c.add_utils() diff --git a/commune/namespace.py b/commune/namespace.py index 34b7d74a8..5019a62da 100644 --- a/commune/namespace.py +++ b/commune/namespace.py @@ -213,24 +213,6 @@ def server_exists(cls, name:str, network:str = None, prefix_match:bool=False, * server_exists = bool(name in servers) return server_exists - - @classmethod - def test(cls): - network = 'test_namespace' - cls.rm_namespace(network) - namespace = cls.namespace(network=network) - assert cls.namespace(network=network) == {}, f'Namespace not empty., {namespace}' - name = 'test' - address = '0.0.0.0:8888' - cls.register_server(name=name, address=address, network=network) - namespace = cls.namespace(network=network) - assert namespace[name] == address, f'Namespace not updated. {namespace}' - cls.deregister_server(name, network=network) - assert cls.namespace(network=network) == {} - cls.rm_namespace(network) - assert cls.namespace_exists(network) == False - return {'success': True, 'msg': 'Namespace tests passed.'} - @classmethod diff --git a/commune/routes.yaml b/commune/routes.yaml index 2b23f81da..41d42fc92 100644 --- a/commune/routes.yaml +++ b/commune/routes.yaml @@ -112,20 +112,6 @@ streamlit: docker: - dlogs - images -code: - - determine_type - - process_kwargs - - python2str - - str2python - - bytes2str - - str2bytes - - str2python - - python2str - - is_generator - - dict2munch - - munch2dict - - dict2json - - json2dict server: [serve, serve_many, serve_all, kill, kill_many, kill_all, wait_for_server] pm2: diff --git a/commune/server.py b/commune/server.py index 4e4a7abbe..930fd2cc2 100644 --- a/commune/server.py +++ b/commune/server.py @@ -9,6 +9,7 @@ from starlette.middleware.base import BaseHTTPMiddleware from sse_starlette.sse import EventSourceResponse + class ServerMiddleware(BaseHTTPMiddleware): def __init__(self, app, max_bytes: int): super().__init__(app) @@ -607,7 +608,9 @@ def kill_all(cls, network='local', timeout=20, verbose=True): print(result) progress.update(1) results_list += [result] - namespace = c.namespace(network=network, update=True) + namespace = c.namespace + print(namespace) + namespace = namespace(network=network, update=True) new_n = len(servers) c.print(f'Killed {n - new_n} servers, with {n} remaining {servers}', color='red') return {'success':True, 'old_n':n, 'new_n':new_n, 'servers':servers, 'namespace':namespace} diff --git a/commune/ticket.py b/commune/ticket.py index d069ea188..004216f00 100644 --- a/commune/ticket.py +++ b/commune/ticket.py @@ -63,28 +63,6 @@ def ticket(self, data='commune', key=None, ticket_mode=False, **kwargs): create = ticket - def ticket2address(self, ticket): - """ - Get the address from a ticket - """ - return ticket['address'] - - - def is_ticket_dict(self, ticket): - if isinstance(ticket, dict): - return all([self.is_ticket(v) in ticket for v in ticket.values()]) - return False - - - def is_ticket_list(self, ticket): - if isinstance(ticket, list): - return all([self.is_ticket(v) in ticket for v in ticket]) - return False - - - def is_ticket(self, data): - return all([f in data for f in self.ticket_features]) - def verify(self, data, max_age:str=None, **kwargs): diff --git a/tests/test_server.py b/tests/test_server.py index caf047322..a388fc242 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -66,5 +66,4 @@ def test_namespace(): assert cls.namespace(network=network) == {} cls.rm_namespace(network) assert cls.namespace_exists(network) == False - return {'success': True, 'msg': 'Namespace tests passed.'} - + return {'success': True, 'msg': 'Namespace tests passed.'} \ No newline at end of file