format_symbol
def format_symbol(
name, signature, doc, decorators:NoneType=None, is_method:bool=False
):format the information in markdown
Once you collect symbols, you want to format it as a markdown list
format the information in markdown
Next we parse symbols in the module that we want to list.
For example, this is how a Class will be parsed:
mock_class = extract_node('''
@decorator
class TestClass:
"""
Class docstring
with multiple lines
"""
@staticmethod
def method1(arg1):
"""
Method1 docstring
with multiple lines
"""
pass
def method2(self):
"""Single line docstring"""
pass
@property
def myprop(self):
"My docstring"
return 'isaac'
''')
# Process the class
result = process_class(mock_class, "TestClass", True)
# # Test equality
# test_eq(result, (
# 'class',
# 'TestClass',
# '\n Class docstring\n with multiple lines\n ',
# ['decorator'],
# [
# ('method1', 'method1(arg1)', '\n Method1 docstring\n with multiple lines\n ', ['staticmethod']),
# ('method2', 'method2(self)', 'Single line docstring', [])
# ]
# ))
result('class',
'TestClass',
'\n Class docstring\n with multiple lines\n ',
['decorator'],
[('method1',
'method1(arg1)',
'\n Method1 docstring\n with multiple lines\n ',
['staticmethod']),
('method2', 'method2(self)', 'Single line docstring', []),
('myprop', 'myprop', 'My docstring', ['property'])])
code1 = """
class Color(Enum):
"A enum for selecting colors"
RED='red'
BLUE='blue'
GREEN='green'
def get_color(self):
"Color method for testing"
return self.value
@property
def get_name(self):
"name property for testing"
return self.value
"""
code2 = """
class VEnum(Enum):
def __str__(self): pass
class Size(VEnum):
"A size picker"
small='small'
medium='medium'
large='large'
"""
result1 = process_enum(extract_node(code1), "Color", True)
result2 = process_enum(extract_node(code2), "Size", True)
print(result1)
print(result2)('enum', 'Color', (['RED', 'BLUE', 'GREEN'], [('get_color', 'get_color(self)', 'Color method for testing', []), ('get_name', 'get_name', 'name property for testing', ['property'])]), 'A enum for selecting colors', [])
('enum', 'Size', (['small', 'medium', 'large'], []), 'A size picker', [])
Extract all public symbols
Extract all public symbols
Format an enum class in markdown
- `class Color(Enum)`
A enum for selecting colors
Members: RED, BLUE, GREEN
- `get_color(self)`
Color method for testing
- `@property get_name`
name property for testing
Call self as a function.
Here is a preview of the fastcore library, for instance:
# fastcore Module Documentation
## fastcore.aio
> Bridging async and sync code: `run_sync`, `iter_sync`, `ctx_sync`, `maybe_await`, and `then`
>
> Docs: https://fastcore.fast.ai/aio.html.md
- `def run_sync(coro)`
Run coroutine `coro` to completion from sync code and return its result
- `def iter_sync(agen)`
Iterate async generator `agen` from sync code
- `@contextmanager def ctx_sync(acm)`
Use async context manager `acm` in a plain `with` block
- `def maybe_await(o)`
Await `o` if needed, and return it
We can generate our list of symbols as a markdown file like so:
Generate a list of symbols corresponding to a python package in a markdown format.