Skip to content

Commit

Permalink
refactor into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Sep 4, 2024
1 parent 5bedb69 commit 4415cb4
Show file tree
Hide file tree
Showing 12 changed files with 1,140 additions and 2,034 deletions.
2,038 changes: 103 additions & 1,935 deletions commune/module.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion commune/modules/data/text/truthqa/data_text_truthqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DataTextTruthQa(c.Module):
def score(self, module:str, fn='sample', kwargs={'idx': 0}):
reference_output = getattr(self.dataset, fn=fn)(**kwargs)
if isinstance(module, str):
output = c.async_call(module=module,fn=fn, **kwargs)
output = c.call(module=module,fn=fn, **kwargs)
else:
output = getattr(module,fn)(**kwargs)
if isinstance(output, dict):
Expand Down
32 changes: 31 additions & 1 deletion commune/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,34 @@ model.openrouter:
- ask
- models
chat:
- ask
- ask
utils.types:
- str2bytes
- python2str
- str2python
- bytes2str
- detailed_error
utils.os:
- cmd

utils.network:
- free_port
- port_used
- port_available
- get_port_range
- ip
- external_ip
- port_free
- used_port
- used_ports
- free_ports
- get_available_ports
- resolve_ip

utils.asyncio:
- gather
- get_event_loop
- new_event_loop



71 changes: 69 additions & 2 deletions commune/utils/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ async def async_read(path, mode='r'):

def get_new_event_loop(nest_asyncio:bool = False):
if nest_asyncio:
import nest_asyncio
nest_asyncio.apply()
set_nest_asyncio()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop
Expand All @@ -34,3 +33,71 @@ def wrapper_fn(*args, **kwargs):
return loop.run_until_complete(fn(*args, **kwargs))
return wrapper_fn




def new_event_loop(nest_asyncio:bool = True) -> 'asyncio.AbstractEventLoop':
import asyncio
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
if nest_asyncio:
set_nest_asyncio()
return loop

def set_event_loop(self, loop=None, new_loop:bool = False) -> 'asyncio.AbstractEventLoop':
import asyncio
try:
if new_loop:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
else:
loop = loop if loop else asyncio.get_event_loop()
except RuntimeError as e:
self.new_event_loop()

self.loop = loop
return self.loop

def get_event_loop(nest_asyncio:bool = True) -> 'asyncio.AbstractEventLoop':
try:
loop = asyncio.get_event_loop()
except Exception as e:
loop = new_event_loop(nest_asyncio=nest_asyncio)
return loop

def set_nest_asyncio():
import nest_asyncio
nest_asyncio.apply()


def gather(jobs:list, timeout:int = 20, loop=None)-> list:

if loop == None:
loop = get_event_loop()

if not isinstance(jobs, list):
singleton = True
jobs = [jobs]
else:
singleton = False

assert isinstance(jobs, list) and len(jobs) > 0, f'Invalid jobs: {jobs}'
# determine if we are using asyncio or multiprocessing

# wait until they finish, and if they dont, give them none

# return the futures that done timeout or not
async def wait_for(future, timeout):
try:
result = await asyncio.wait_for(future, timeout=timeout)
except asyncio.TimeoutError:
result = {'error': f'TimeoutError: {timeout} seconds'}
return result

jobs = [wait_for(job, timeout=timeout) for job in jobs]
future = asyncio.gather(*jobs)
results = loop.run_until_complete(future)

if singleton:
return results[0]
return results
10 changes: 10 additions & 0 deletions commune/utils/crypto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def is_mnemonic(s: str) -> bool:
import re
# Match 12 or 24 words separated by spaces
return bool(re.match(r'^(\w+ ){11}\w+$', s)) or bool(re.match(r'^(\w+ ){23}\w+$', s))

def is_private_key(s: str) -> bool:
import re
# Match a 64-character hexadecimal string
pattern = r'^[0-9a-fA-F]{64}$'
return bool(re.match(pattern, s))
Empty file added commune/utils/hardware.py
Empty file.
Loading

0 comments on commit 4415cb4

Please sign in to comment.