File size: 2,498 Bytes
7db3210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Will open up a pytho file and split out all functions into chunks
with relevent data for embedding

To use: Open up a file and store its contents as `code_string`:
with open("RAG-accelerate/src/accelerator.py", "r") as w:
    code_string = w.read()
"""
import re
func_list=[]
func = ''
tab  = ''
brackets = {'(':0, '[':0, '{':0}
close = {')':'(', ']':'[', '}':'{'}
string=''
tab_f=''
c1=''
multiline=False
check=False
in_class = False
decorator = None
class_name = ""
for line in code_string.split('\n'):
    tab = re.findall(r'^\s*',line)[0]
    if re.findall(r'^\s*@', line):
        decorator = line
    if (re.findall(r'^\s*def', line) or re.findall(r'^\s*class', line)) and not string and not multiline:
        if re.findall(r'^\s*class', line):
            in_class = True
            class_name = re.findall(r'class\s+(\w+)\s*[\(:]', line)[0]
        elif len(line.lstrip(" ")) == 0:
            in_class = False
        func += line + '\n'
        tab_f = tab
        check=True
    if func:
        if not check:
            if sum(brackets.values()) == 0 and not string and not multiline:
                if len(tab) <= len(tab_f):
                    # Append the metadata
                    metadata = f'File: accelerator.py\nClass: {class_name}'
                    if decorator is not None:
                        func = f'{decorator}\n{func}'
                        decorator = None
                    func = f'{metadata}\nSource:\n{func}'
                    func_list.append(func)
                    func=''
                    c1=''
                    c2=''
                    continue
            func += line + '\n'
        check = False
    for c0 in line:
        if c0 == '#' and not string and not multiline:
            break
        if c1 != '\\':
            if c0 in ['"', "'"]:
                if c2 == c1 == c0 == '"' and string != "'":
                    multiline = not multiline
                    string = ''
                    continue
                if not multiline:
                    if c0 in string:
                        string = ''
                    else:
                        if not string:
                            string = c0
            if not string and not multiline:
                if c0 in brackets:
                    brackets[c0] += 1
                if c0 in close:
                    b = close[c0]
                    brackets[b] -= 1
        c2=c1
        c1=c0

for f in func_list:
    print('-'*40)
    print(f)