-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
625 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
import commune as c | ||
|
||
class Function(c.Module): | ||
@classmethod | ||
def fn_schema(cls, fn:str, | ||
defaults:bool=True, | ||
code:bool = False, | ||
docs:bool = True, | ||
version=2)->dict: | ||
''' | ||
Get function schema of function in cls | ||
''' | ||
fn_schema = {} | ||
fn = cls.get_fn(fn) | ||
fn_schema['input'] = cls.get_function_annotations(fn=fn) | ||
|
||
for k,v in fn_schema['input'].items(): | ||
v = str(v) | ||
if v.startswith('<class'): | ||
fn_schema['input'][k] = v.split("'")[1] | ||
elif v.startswith('typing.'): | ||
fn_schema['input'][k] = v.split(".")[1].lower() | ||
else: | ||
fn_schema['input'][k] = v | ||
|
||
fn_schema['output'] = fn_schema['input'].pop('return', {}) | ||
|
||
if docs: | ||
fn_schema['docs'] = fn.__doc__ | ||
if code: | ||
fn_schema['code'] = cls.fn_code(fn) | ||
|
||
fn_args = c.get_function_args(fn) | ||
fn_schema['type'] = 'static' | ||
for arg in fn_args: | ||
if arg not in fn_schema['input']: | ||
fn_schema['input'][arg] = 'NA' | ||
if arg in ['self', 'cls']: | ||
fn_schema['type'] = arg | ||
fn_schema['input'].pop(arg) | ||
if 'default' in fn_schema: | ||
fn_schema['default'].pop(arg, None) | ||
|
||
|
||
if defaults: | ||
fn_schema['default'] = cls.fn_defaults(fn=fn) | ||
for k,v in fn_schema['default'].items(): | ||
if k not in fn_schema['input'] and v != None: | ||
fn_schema['input'][k] = type(v).__name__ if v != None else None | ||
|
||
if version == 1: | ||
pass | ||
elif version == 2: | ||
defaults = fn_schema.pop('default', {}) | ||
fn_schema['input'] = {k: {'type':v, 'default':defaults.get(k)} for k,v in fn_schema['input'].items()} | ||
else: | ||
raise Exception(f'Version {version} not implemented') | ||
|
||
|
||
return fn_schema | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.