Skip to content

Instantly share code, notes, and snippets.

@astrofrog
Created February 21, 2014 12:03
Show Gist options
  • Save astrofrog/9133160 to your computer and use it in GitHub Desktop.
Save astrofrog/9133160 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:6d823b96d1cfd69ec36f1e8bfba09700ac47475d1a4522b289d49d7cde05d03a"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Source Code Analysis"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the `ast` module to parse the source file:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import ast\n",
"source = open('hub.py', 'r').read()\n",
"s = ast.parse(source)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"s"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"<_ast.Module at 0x1063b6ed0>"
]
}
],
"prompt_number": 3,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"s.body"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"[<_ast.ImportFrom at 0x1063c5bd0>,\n",
" <_ast.Import at 0x107505710>,\n",
" <_ast.Import at 0x107505790>,\n",
" <_ast.Import at 0x107505810>,\n",
" <_ast.Import at 0x107505890>,\n",
" <_ast.Import at 0x107505910>,\n",
" <_ast.Import at 0x107505990>,\n",
" <_ast.Import at 0x107505a10>,\n",
" <_ast.Import at 0x107505a90>,\n",
" <_ast.ImportFrom at 0x107505b10>,\n",
" <_ast.ImportFrom at 0x107505b90>,\n",
" <_ast.ImportFrom at 0x107505c10>,\n",
" <_ast.ImportFrom at 0x107505c90>,\n",
" <_ast.ImportFrom at 0x107505d10>,\n",
" <_ast.ImportFrom at 0x107505d90>,\n",
" <_ast.ImportFrom at 0x107505e10>,\n",
" <_ast.ImportFrom at 0x107505f10>,\n",
" <_ast.ImportFrom at 0x1063cc050>,\n",
" <_ast.ImportFrom at 0x1063cc110>,\n",
" <_ast.ImportFrom at 0x1063cc190>,\n",
" <_ast.ImportFrom at 0x1063cc250>,\n",
" <_ast.If at 0x1063cc2d0>,\n",
" <_ast.Assign at 0x1063cc490>,\n",
" <_ast.Assign at 0x1063cc5d0>,\n",
" <_ast.ClassDef at 0x1063cc710>,\n",
" <_ast.ClassDef at 0x10746d510>]"
]
}
],
"prompt_number": 4,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"c = s.body[-1]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"c.body"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"[<_ast.Expr at 0x10746d590>,\n",
" <_ast.FunctionDef at 0x10746d610>,\n",
" <_ast.FunctionDef at 0x10746f490>,\n",
" <_ast.FunctionDef at 0x10746f6d0>]"
]
}
],
"prompt_number": 7,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"c.body[1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"<_ast.FunctionDef at 0x10746d610>"
]
}
],
"prompt_number": 9,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f = c.body[1]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"start = f.lineno # find line number\n",
"start"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": [
"1515"
]
}
],
"prompt_number": 16,
"trusted": true
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The end of the function is the line before the next block starts:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"end = c.body[2].lineno - 1\n",
"end"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 18,
"text": [
"1529"
]
}
],
"prompt_number": 18,
"trusted": true
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can check that this works (note that line numbers above are 1-based so we do -1 here):"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"lines = source.splitlines()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19,
"trusted": true
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"lines[start-1:end]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": [
"[' def handle_queue(self):',\n",
" ' try:',\n",
" ' request = self.queue_request.get_nowait()',\n",
" ' except queue.Empty: # queue is set but empty',\n",
" ' pass',\n",
" ' except AttributeError: # queue has not been set yet',\n",
" ' pass',\n",
" ' else:',\n",
" ' if isinstance(request[0], str): # To support the old protocol version',\n",
" ' samp_name = request[0]',\n",
" ' else:',\n",
" ' samp_name = request[0][\"samp.name\"]',\n",
" '',\n",
" ' self.show_dialog(samp_name, request[0], request[1], request[2])',\n",
" '']"
]
}
],
"prompt_number": 21,
"trusted": true
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment