from fastcore.test import *API
py_sigs
def py_sigs(
src
):
Extract class/function/method signatures from Python source
get_docstring
def get_docstring(
node, lines
):
Get docstring from source lines if present
test_py = """
def greet(name, age=10):
"Say hello"
return f"Hello {name}"
class Foo:
def __init__(self, x): self.x = x
def bar(self, y, z): return y + z
"""
py_sigs(test_py)['def greet(name, age=10):\n "Say hello" ...',
' def __init__(self, x): self.x = x ...',
' def bar(self, y, z): return y + z ...']
js_sigs
def js_sigs(
src, lang:str='javascript'
):
Extract function signatures from JS/TS source
test_js = """
function greet(name, age) { return `Hello ${name}`; }
const add = (a, b) => a + b;
class Foo {
constructor(x) { this.x = x; }
bar(y, z) { return y + z; }
}
"""
js_sigs(test_js)['function greet(name, age) {...}', 'constructor(x) {...}', 'bar(y, z) {...}']
java_sigs
def java_sigs(
src
):
Extract method signatures from Java source
test_java = """
public class Calculator {
public int add(int a, int b) { return a + b; }
private void reset() { this.value = 0; }
public String format(String template, Object... args) { return String.format(template, args); }
}
"""
java_sigs(test_java)['int add(int a, int b);',
'void reset();',
'String format(String template, Object... args);']
rust_sigs
def rust_sigs(
src
):
Extract function signatures from Rust source
test_rust = """
fn greet(name: &str) -> String { format!("Hello {}", name) }
fn add(a: i32, b: i32) -> i32 { a + b }
pub fn process(items: Vec<Item>, filter: impl Fn(&Item) -> bool) -> Vec<Item> { items.into_iter().filter(filter).collect() }
"""
rust_sigs(test_rust)['fn greet(name: &str) {...}',
'fn add(a: i32, b: i32) {...}',
'fn process(items: Vec<Item>, filter: impl Fn(&Item) -> bool) {...}']
csharp_sigs
def csharp_sigs(
src
):
Extract method signatures from C# source
test_csharp = """
public class Service {
public string GetName(int id) { return "test"; }
private void Initialize() { }
public async Task<List<Item>> FetchItems(string query, int limit) { return new List<Item>(); }
}
"""
csharp_sigs(test_csharp)['string GetName(int id);',
'void Initialize();',
'string Task(string query, int limit);']
css_selectors
def css_selectors(
src
):
Extract CSS selectors from source
test_css = """
.container { margin: 0; }
#header, .nav { display: flex; }
body > main p { color: red; }
"""
css_selectors(test_css)['.container {...}', '#header, .nav {...}', 'body > main p {...}']
go_sigs
def go_sigs(
src
):
Extract function signatures from Go source
test_go = """
func greet(name string) string { return "Hello " + name }
func add(a, b int) int { return a + b }
func (s *Server) Start(port int) error { return nil }
func (c Client) Get(url string, timeout time.Duration) (*Response, error) { return nil, nil }
"""
go_sigs(test_go)['func greet(name string) {...}',
'func add(a, b int) {...}',
'func (s *Server) Start(port int) {...}',
'func (c Client) Get(url string, timeout time.Duration) {...}']
kotlin_sigs
def kotlin_sigs(
src
):
Extract function signatures from Kotlin source
test_kotlin = """
fun greet(name: String, age: Int = 10): String { return "Hello $name" }
class Foo(val x: Int) {
fun bar(y: Int, z: Int): Int { return y + z }
}
"""
kotlin_sigs(test_kotlin)['fun greet(name: String, age: Int = 10) {...}',
'fun bar(y: Int, z: Int) {...}']
swift_sigs
def swift_sigs(
src
):
Extract function signatures from Swift source
test_swift = """
func greet(name: String, age: Int = 10) -> String { return "Hello \\(name)" }
class Foo {
var x: Int
init(x: Int) { self.x = x }
func bar(y: Int, z: Int) -> Int { return y + z }
}
"""
swift_sigs(test_swift)['func greet(name: String) {...}', 'func bar(y: Int) {...}']
lua_sigs
def lua_sigs(
src
):
Extract function signatures from Lua source
test_lua = """
function greet(name, age)
return "Hello " .. name
end
function add(a, b) return a + b end
local function helper(x) return x * 2 end
"""
lua_sigs(test_lua)['function greet(name, age) ... end',
'function add(a, b) ... end',
'function helper(x) ... end']
php_sigs
def php_sigs(
src
):
Extract function signatures from PHP source
test_php = """<?php
function greet($name, $age = 10) { return "Hello $name"; }
class Foo {
public function __construct($x) { $this->x = $x; }
public function bar($y, $z) { return $y + $z; }
}
"""
php_sigs(test_php)['function greet($name, $age = 10) {...}',
'function __construct($x) {...}',
'function bar($y, $z) {...}']
ruby_sigs
def ruby_sigs(
src
):
Extract method signatures from Ruby source
test_ruby = """
def greet(name, age = 10)
"Hello #{name}"
end
class Foo
def initialize(x)
@x = x
end
def bar(y, z) = y + z
end
"""
ruby_sigs(test_ruby)['def greet(name, age = 10) ... end',
'def initialize(x) ... end',
'def bar(y, z) ... end']
ext_sigs
def ext_sigs(
src, ext
):
Read retrieve signatures for src based on suitable langage for ext
ext_sigs(test_ruby, 'rb')['def greet(name, age = 10) ... end',
'def initialize(x) ... end',
'def bar(y, z) ... end']
file_sigs
def file_sigs(
fname
):
Read file content and retrieve signatures
for o in file_sigs('../codesigs/core.py'): print(o)def get_docstring(node, lines):
"Get docstring from source lines if present" ...
def _node_sig(node, lines): ...
def py_sigs(src):
"Extract function/method signatures from Python source" ...
def _collect(nodes): ...
def _get_sigs(src, lang, kinds, name_kind, params_kind, fmt): ...
def js_sigs(src, lang="javascript"):
"Extract function signatures from JS/TS source" ...
def java_sigs(src):
"Extract method signatures from Java source" ...
def fmt(n, nm, ps): ...
def rust_sigs(src):
"Extract function signatures from Rust source" ...
def csharp_sigs(src):
"Extract method signatures from C# source" ...
def fmt(n, nm, ps): ...
def css_selectors(src):
"Extract CSS selectors from source" ...
def go_sigs(src):
"Extract function signatures from Go source" ...
def file_sigs(fname):
"Read file content and retrieve signatures" ...