Created
December 20, 2019 19:22
-
-
Save grandrew/b4b3f9c82b7a4929a783ea056f8fa646 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import inspect" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# this\n", | |
"# is\n", | |
"# source\n", | |
"def check_frame(i):\n", | |
" frame,filename,\\\n", | |
" line_number,function_name,lines,index = inspect.stack()[i]\n", | |
" print(filename, line_number, lines, index)\n", | |
" print(\"FULL SOURCE:\", get_frame_source_w_explicit_continuation(line_number, frame))\n", | |
" return frame" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_frame_source_w_explicit_continuation(line_number, frame):\n", | |
" \"Return reconstructed lines with explicit continuation. \\\n", | |
" line_number - lineno of the frame, frame - frame\"\n", | |
" fs_lines, offset = inspect.getsourcelines(frame)\n", | |
" real_line_number = line_number\n", | |
" rel_line_number = line_number - offset\n", | |
" # print(\"My actual line is\", fs_lines[rel_line_number])\n", | |
" full_line = fs_lines[rel_line_number].strip()\n", | |
" next_line_no = rel_line_number\n", | |
" while full_line.endswith(\"\\\\\"):\n", | |
" next_line_no += 1\n", | |
" full_line = full_line[:-1] + fs_lines[next_line_no].strip()\n", | |
" # print(\"Reconstrucred line DOWN\", full_line)\n", | |
" if rel_line_number > 0:\n", | |
" next_line_no = rel_line_number - 1\n", | |
" prev_line = fs_lines[next_line_no].strip()\n", | |
" while prev_line.endswith(\"\\\\\"):\n", | |
" real_line_number -= 1\n", | |
" full_line = prev_line[:-1] + full_line\n", | |
" if next_line_no > 0: \n", | |
" next_line_no -= 1\n", | |
" prev_line = fs_lines[next_line_no].strip()\n", | |
" else: \n", | |
" break\n", | |
" # print(\"Reconstructed FULL line\", full_line)\n", | |
" return full_line, real_line_number" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 92, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<ipython-input-90-c5d8eb1b1d0e> 6 [' line_number,function_name,lines,index = inspect.stack()[i]\\n'] 0\n", | |
"FULL SOURCE: ('frame,filename,line_number,function_name,lines,index = inspect.stack()[i]', 5)\n" | |
] | |
} | |
], | |
"source": [ | |
"f=check_frame(0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'check_frame'" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"f.f_code.co_name" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(['def check_frame(i):\\n',\n", | |
" ' frame,filename,\\\\\\n',\n", | |
" ' line_number,function_name,lines,index = \\\\\\n',\n", | |
" ' inspect.\\\\\\n',\n", | |
" ' stack()[i]\\n',\n", | |
" ' print(filename, line_number, lines, index)\\n',\n", | |
" ' return frame\\n'],\n", | |
" 4)" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"inspect.getsourcelines(f)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"s=inspect.getsource(f)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<_ast.Module at 0x7fa9ea8bd050>" | |
] | |
}, | |
"execution_count": 59, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import ast\n", | |
"ast.parse(s)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.5rc1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment