Changeset 254
- Timestamp:
- 11/28/08 17:39:16
- Files:
-
- trunk/geniusql/decompile.py (deleted)
- trunk/geniusql/logic.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/geniusql/logic.py
r217 r254 56 56 57 57 You'll notice, in this first example, some extra parentheses in the final 58 lambda. The lambda has already undergone an explicit compile/decompile58 lambda. The lambda has already undergone an explicit parse/deparse 59 59 step. These differences don't affect the logic in any way, but it's 60 impossible to guess the exact original syntax when de compiling.60 impossible to guess the exact original syntax when deparsing. 61 61 62 62 However, be advised of this IMPORTANT point. When you form an Expression … … 105 105 106 106 107 De rivation (Decompilation)and Translation:108 'De riving' is the opposite of 'parsing'. The codewalk.LambdaDecompiler107 Deparsing and Translation: 108 'Deparsing' is the opposite of 'parsing'. The codewalk.LambdaDeparser 109 109 class walks a function or code object and produces equivalent Python 110 110 code in a string. 111 111 112 112 >>> e = logic.Expression(lambda x: x.a == 3 and (x.b > 1 or x.b < -10)) 113 >>> codewalk.LambdaDecompiler(e.func).code()114 'lambda x: not ((x.a == 3) and ((x.b > 1) or (x.b < -10)))'113 >>> e.code() 114 'lambda x: (x.a == 3) and ((x.b > 1) or (x.b < -10))' 115 115 116 116 However, we are not limited to Python statements of our Expression! 117 Another de compiler might produce our Expression in another language;117 Another deparser might produce our Expression in another language; 118 118 this example produces a WHERE clause for SQL (a declarative language!): 119 119 … … 121 121 x.Date > datetime.date(2004, 2, 14) and 122 122 x.Name.endswith('_')) 123 >>> ADOSQLDecompiler(e).code()123 >>> SQLServerDeparser([(None, t)], e, sqlserver.SQLServerTypeSet()).code() 124 124 "([Group] = '3' and [Date] > #2/14/2004#) and [Name] Like '%\\_'" 125 125 … … 128 128 __setstate__). You might notice that the function itself is *not* 129 129 pickled; instead, its code() method is called, which produces a 130 string representation of the function (de compilation). This makes130 string representation of the function (deparsing). This makes 131 131 pickled Expressions much more stable across Python versions than, 132 132 say, storing the function's co_code. However, this presents a problem … … 304 304 """Return source code for self.func.""" 305 305 if hasattr(self, 'func'): 306 de com= astwalk.LambdaDeparser(self.ast, env=builtins)307 return de com.code()306 dep = astwalk.LambdaDeparser(self.ast, env=builtins) 307 return dep.code() 308 308 else: 309 309 return 'function not yet loaded'
