Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

get_func_docstrings.py 4.1 KB

You have to be logged in to leave a comment. Sign In
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  1. import os
  2. import shutil
  3. import tempfile
  4. from pyls.workspace import Document, Workspace
  5. from pyls.plugins.references import pyls_references
  6. from pyls.plugins.signature import pyls_signature_help
  7. import ast
  8. import pandas as pd
  9. import inspect
  10. import ast
  11. import sys
  12. import numpy as np
  13. def get_func_docstrings(notebook_cells):
  14. cells = []
  15. lengths = []
  16. for cell in notebook_cells.code_block:
  17. cell = cell.strip('`').strip()
  18. if cell.find("%matplotlib inline") > -1:
  19. cell = cell.replace("%matplotlib inline", "")
  20. code = cell
  21. for line in cell.split("\n"):
  22. if "import" in line:
  23. try:
  24. exec(line)
  25. except Exception as e:
  26. print("Could not run:\n", line, "\n", e)
  27. if len(line.strip()) > 0 and line.strip()[0] == "!":
  28. code = code.replace(line, "")
  29. cells.append(code)
  30. lengths.append(len(code.split("\n")))
  31. lengths = np.cumsum(lengths)
  32. cb_ids = list(notebook_cells.code_block_id)
  33. cells = "\n".join(cells)
  34. cells_lines = cells.split("\n")
  35. tmp = tempfile.mkdtemp()
  36. workspace = Workspace(tmp, None)
  37. def create_file(name, content):
  38. fn = os.path.join(tmp, name)
  39. with open(fn, 'w') as f:
  40. f.write(content)
  41. workspace.put_document('file://' + fn, content, None)
  42. create_file('code.py', cells)
  43. DOC_URI = 'file://' + os.path.join(workspace.root_uri, 'code.py')
  44. doc = Document(DOC_URI, workspace)
  45. result = [] # {codeblock_id, f_name, docstring}
  46. parsed = ast.parse(cells)
  47. for node in ast.walk(parsed):
  48. if isinstance(node, ast.Call):
  49. line_call = cells_lines[node.lineno - 1][node.col_offset:node.end_col_offset]
  50. idx = line_call.find("(")
  51. position_ref = {'line': node.lineno - 1, 'character': idx - 1 + node.col_offset}
  52. position_sig = {'line': node.lineno - 1, 'character': idx + 1 + node.col_offset}
  53. name = line_call[:idx]
  54. docstr = ""
  55. try:
  56. docstr = eval('inspect.getdoc(' + name + ')')
  57. except Exception:
  58. # find source through pyls
  59. signatures = pyls_signature_help(doc, position_sig)['signatures']
  60. if len(signatures) > 0 and 'documentation' in signatures[0] and len(signatures[0]['documentation']) > 0:
  61. docstr = signatures[0]['documentation']
  62. else:
  63. try:
  64. uri = pyls_references(doc, position_ref)[0]['uri']
  65. source = []
  66. if uri.find('site-packages') >= 0:
  67. uri_split = uri.split('/')
  68. source = uri_split[uri_split.index('site-packages') + 1:]
  69. source[-1] = source[-1].split(".")[0]
  70. f_name = name.split(".")[-1]
  71. try:
  72. exec("from " + ".".join(source) + " import " + f_name)
  73. except Exception as e:
  74. pass
  75. try:
  76. exec("from " + ".".join(source[:-1]) + " import " + f_name)
  77. except Exception as e:
  78. pass
  79. try:
  80. docstr = eval('inspect.getdoc(' + f_name + ')')
  81. except Exception as e:
  82. if source[0] == "pandas":
  83. try:
  84. docstr = eval('inspect.getdoc(' + "pandas.DataFrame." + f_name + ')')
  85. except Exception as e:
  86. pass
  87. else:
  88. pass
  89. except Exception as e:
  90. print("error in pyls_references, function name", name)
  91. pass
  92. result.append({"code_block_id": cb_ids[np.where(lengths >= node.lineno - 1)[0][0]], "f_name": name,
  93. "docstring": docstr})
  94. shutil.rmtree(tmp)
  95. return result
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...