Skip to content

Commit

Permalink
module
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Sep 3, 2024
1 parent d226c13 commit 5bedb69
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 124 deletions.
40 changes: 1 addition & 39 deletions commune/aes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


19 changes: 0 additions & 19 deletions commune/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down
83 changes: 74 additions & 9 deletions commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 0 additions & 18 deletions commune/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions commune/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion commune/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}
Expand Down
22 changes: 0 additions & 22 deletions commune/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'}

0 comments on commit 5bedb69

Please sign in to comment.