Gribouillis surely can come out with something, ask more specifically.
Here his earlier post containing importing ast, response to my code snippet:
First thing to do is to print the structure to see what it contains. I found a pretty printer here
#!/usr/bin/env python # -*-coding: utf8-*- from __future__ import (absolute_import, division, print_function, unicode_literals) __doc__ = ''' ''' import ast from astpp import dump source = """ def foo(n): p = 1 while n > 0: p *= n n -= 1 return p """ if __name__ == '__main__': result = ast.parse(source) print(dump(result)) """ -> my output Module(body=[ FunctionDef(name='foo', args=arguments(args=[ Name(id='n', ctx=Param()), ], vararg=None, kwarg=None, defaults=), body=[ Assign(targets=[ Name(id='p', ctx=Store()), ], value=Num(n=1)), While(test=Compare(left=Name(id='n', ctx=Load()), ops=[ Gt(), ], comparators=[ Num(n=0), ]), body=[ AugAssign(target=Name(id='p', ctx=Store()), op=Mult(), value=Name(id='n', ctx=Load())), AugAssign(target=Name(id='n', ctx=Store()), op=Sub(), value=Num(n=1)), ], orelse=), Return(value=Name(id='p', ctx=Load())), ], decorator_list=), ]) """
Next step: learn how to extract and use data from this
Unfortunately, I dont think there is a pymotw about ast :(
erg, my termage is escaping me >.<
but yea... best I got was the example on this page
[n.targets.id for n in ast.walk(ast.parse("x = struct()")) if isinstance(n, ast.Assign) and isinstance(n.value, ast.Call) and n.value.func.id == 'struct']
I'm intending to replace my current crappy tokenize method I developed back here
it works, but not as well as I need it to...
for right now, I'm going through some extremely complex lookback methods (such as writing my own parser) just to get the names of the things I need... >_<
Edited 2 Years Ago by Tcll
Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...
Hello All ...
Iam Getting An Error With try to excecute the stored procedure .
I have Have Sql database , the stored procedure like so :
USE [MPRS] GO /****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE ...
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
Here is the sample output that I ...