forked from NUS-ALSET/lambda-code-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateUserAndProblemSkills.py
489 lines (425 loc) · 16.3 KB
/
calculateUserAndProblemSkills.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import ast
from collections import defaultdict
################################################################################
# Constructs
#
# 'constructs' code features correspond to various programming constructs, such
# as slicing, list comprehension, keyword argument usage, etc.
# Note that not all constructs are independent from each other and there may
# be some overlap between them. In particular, some constructs are specialized
# versions of broader constructs. For example, there is a general construct
# 'Comprehension' and its specialized versions 'ListComprehension',
# 'SetComprehension', 'DictionaryComprehension' and 'GeneratorExpression'. Thus
# a list comprehension is counted both as a 'Comprehension' construct and as a
# 'ListComprehension' construct. Similarly a multi-target assignment
# (e.g. "a, b = 1, 2") is counted both as an 'Assignment' construct and as a
# 'MultiTargetAssignment' construct.
#
# Currently detected/recognized constructs are:
#
# Assignment Corresponds to an assignment statement.
#
# MultiTargetAssignment Corresponds to an assignment statement with
# multiple targets.
# Example:
# a, b = 1, 2
# Also counts as an Assignment construct.
#
# ChainedCompare Corresponds to a chained sequence of comparison
# expressions.
# Example:
# 1 < x <= y < 10
#
# KeywordArgumentUsage Corresponds to a function call with a keyword
# argument (argname=value syntax).
# Example:
# print('abc', end='')
#
# Subscription Referring to an item (or multiple items) of a
# sequence or mapping object.
# Example:
# a[1]
# line[4:-2]
# table['abc']
#
# Slicing Usage of slicing ( [start?:end?:stride?] ) in
# subscription.
# Example:
# items[:]
# line[1:-1]
# array[::2]
#
# IfExpression Usage of an if-expression.
# Example:
# a if a > b else b
#
# Comprehension Corresponds to any usage of list, set or dictionary
# comprehension or generator expression.
#
# FilteredComprehension Corresponds to a comprehension containing one
# or more if's.
# Example:
# [x for x in xlist if x > 0]
# Also counts as a Comprehension construct.
#
# MultilevelComprehension Corresponds to a comprehension containing two
# or more for's.
# Examples:
# [(x,y) for x in xlist for y in ylist]
# [x for y in z for x in y]
# Also counts as a Comprehension construct.
#
# ListDisplay Corresponds to a new list object, specified by
# either a list of expressions or a comprehension
# enclosed in square brackets.
# Examples:
# [1, 2, 3]
# [2*x for x in y]
#
# ListComprehension A specialized form of a ListDisplay construct. Also
# counts as a Comprehension construct.
#
# SetDisplay Corresponds to a new set object, specified by
# either a list of expressions or a comprehension
# enclosed in curly braces.
# Examples:
# {1, 2, 3}
# {x**2 for x in y}
#
# SetComprehension A specialized form of a SetDisplay construct. Also
# counts as a Comprehension construct.
#
# DictionaryDisplay Corresponds to a possibly empty series of key:datum
# pairs (possibly produced through a comprehension)
# enclosed in curly braces, defining a new dictionary
# object.
# Examples:
# {1:'a', 2:'b', 3:'c'}
# {x:bin(x) for x in y}
#
# DictionaryComprehension A specialized form of a DictionaryDisplay construct.
# Also counts as a Comprehension construct.
#
# GeneratorExpression A comprehension enclosed in parentheses, producing
# a new generator object. Also counts as a
# Comprehension construct.
#
# FunctionDef Definition of a function.
#
# ClassDef Definition of a class.
#
################################################################################
################################################################################
# Construct tester functions
################################################################################
def MultiTargetAssignment(ast_node):
'''
Detects usage of an assignment with multiple targets
Example:
a, b = 1, 2
'''
return isinstance(ast_node.targets[0], ast.Tuple)
def FilteredComprehension(ast_node):
'''
Detects usage of a comprehension construct with 1 or more if's.
Example:
[x for x in xlist if x > 0]
'''
for c in ast_node.generators:
if len(c.ifs) > 0:
return True
return False
def MultilevelComprehension(ast_node):
'''
Detects usage of a comprehension construct with 2 or more for's.
Example:
[(x,y) for x in xlist for y in ylist]
'''
return len(ast_node.generators) > 1
def ChainedCompare(ast_node):
'''
Detects usage of a chained sequence of comparisons.
Example:
1 < x < y <= 5
'''
return len(ast_node.ops) > 1
def KeywordArgumentUsage(ast_node):
'''
Detects usage of a keyword argument in a function call.
Example:
print('abc', end='')
# ^^^^^^
'''
return len(ast_node.keywords) > 0
################################################################################
# End of construct tester functions
################################################################################
comprehension = 'Comprehension', FilteredComprehension, MultilevelComprehension
# A helper function
def makeComprehensionSpec(collection_type):
return (collection_type + 'Comprehension',
collection_type + 'Display',
*comprehension)
# A map defining construct names for listed AST node types and/or checks
# that must be performed on such AST nodes. In the latter case, if a node
# satisfies the test, the name of the test function is used as the detected
# construct name.
construct_def_map = {
ast.FunctionDef : ('FunctionDef',),
ast.ClassDef : ('ClassDef',),
ast.IfExp : ('IfExpression',),
ast.Assign : ('Assignment', MultiTargetAssignment ),
ast.AugAssign : ('AugmentedAssignment',),
ast.List : ('ListDisplay',),
ast.ListComp : makeComprehensionSpec('List'),
ast.Set : ('SetDisplay',),
ast.SetComp : makeComprehensionSpec('Set'),
ast.Dict : ('DictionaryDisplay',),
ast.DictComp : makeComprehensionSpec('Dictionary'),
ast.GeneratorExp : ('GeneratorExpression', *comprehension),
ast.Compare : (ChainedCompare,),
ast.Subscript : ('Subscription',),
ast.Slice : ('Slicing',),
ast.Call : (KeywordArgumentUsage,)
}
def getAllConstructs(tree):
result = defaultdict(int)
for node in ast.walk(tree):
if type(node) in construct_def_map:
for x in construct_def_map[type(node)]:
if isinstance(x, str):
result[x] += 1
elif x(node):
result[x.__name__] += 1
return dict(result)
################################################################################
# Statements
################################################################################
def countNodesOfGivenTypes(tree, node_types):
result = defaultdict(int)
for node in ast.walk(tree):
if type(node) in node_types:
result[type(node).__name__] += 1
return dict(result)
statementNodeTypes = frozenset([ast.While,
ast.For,
ast.Return,
ast.If,
ast.Continue,
ast.Break,
ast.Try,
ast.With,
ast.Raise,
ast.Pass,
ast.Assert,
ast.Del,
ast.Yield])
# Collect all statements
def getAllStatements(tree):
return countNodesOfGivenTypes(tree, statementNodeTypes)
################################################################################
# Expressions
################################################################################
# NOTE: An implementation of getAllExpr() based on countNodesOfGivenTypes()
# NOTE: is also possible.
def getAllExpr(tree):
result = defaultdict(int)
for node in ast.walk(tree):
if isinstance(node, ast.UnaryOp) or isinstance(node, ast.BinOp) or isinstance(node, ast.BoolOp):
result[type(node.op).__name__] += 1
elif isinstance(node, ast.Compare):
for op in node.ops:
result[type(op).__name__] += 1
return dict(result)
################################################################################
# Functions
################################################################################
'''
Get all function calls from a python file
The MIT License (MIT)
Copyright (c) 2016 Suhas S G <jargnar@gmail.com>
'''
from collections import deque
class FuncCallVisitor(ast.NodeVisitor):
def __init__(self):
self._name = deque()
@property
def name(self):
#print(self._name)
return ''.join(self._name) # was ".".join() removing . option
@name.deleter
def name(self):
self._name.clear()
# Updating to only show obj for ids
def visit_Name(self, node):
self._name.appendleft(node.id)
def visit_Attribute(self, node):
try:
self._name.appendleft(node.attr)
# hacking for demonstration list of functions
#self._name.appendleft(node.value.id)
self._name.appendleft("")
#print(node.value.id)
except AttributeError:
self.generic_visit(node)
def getFuncCalls(tree):
func_calls = []
for node in ast.walk(tree):
if isinstance(node, ast.Call):
callvisitor = FuncCallVisitor()
callvisitor.visit(node.func)
func_calls.append(callvisitor.name)
result = defaultdict(int)
for item in func_calls:
result[item] += 1
return dict(result)
################################################################################
# Imports
################################################################################
def getAllImports(a):
"""Gather all imported module names"""
if not isinstance(a, ast.AST):
return set()
imports = set()
for child in ast.walk(a):
if type(child) == ast.Import:
for alias in child.names:
imports.add(alias.name)
elif type(child) == ast.ImportFrom:
for alias in child.names: # these are all functions
imports.add(child.module + "." + alias.name)
result = {}
for item in imports:
result[item] = True
return result
################################################################################
# code_features()
################################################################################
def code_features(src):
tree = ast.parse(src)
result = {
"statements": getAllStatements(tree),
"functions": getFuncCalls(tree),
"imports": getAllImports(tree),
"expressions": getAllExpr(tree),
"constructs" : getAllConstructs(tree)
}
return result
# And finally, we want to send back a dictionary of aggregate results rather than just the analysis of each solution.
def solution_features(solutions):
"""
problemSkills -> ProblemKey -> featureType -> feature -> userKey -> True
userSkills -> UserKey -> featureType -> feature -> problemKey -> True
"""
problemSkills = {}
userSkills = {}
for problemKey in solutions.keys():
for userKey in solutions[problemKey]:
src = solutions[problemKey][userKey]
analysis = code_features(src)
#print(src)
#print(analysis)
for featureType in analysis:
#print(problemKey, userKey, featureType, analysis[featureType])
for feature in analysis[featureType]:
#Add the analysis to the problemSkills dictionary.
if not problemKey in problemSkills:
problemSkills[problemKey] = {}
if not featureType in problemSkills[problemKey]:
problemSkills[problemKey][featureType] = {}
if not feature in problemSkills[problemKey][featureType]:
problemSkills[problemKey][featureType][feature] = {}
# Add userKey true to feature dictionary.
problemSkills[problemKey][featureType][feature][userKey] = True
# Add the same data to userSkills
if not userKey in userSkills:
userSkills[userKey] = {}
if not featureType in userSkills[userKey]:
userSkills[userKey][featureType] = {}
if not feature in userSkills[userKey][featureType]:
userSkills[userKey][featureType][feature] = {}
# Add problemKey true to feature dictionary.
userSkills[userKey][featureType][feature][problemKey] = True
return {"problemSkills":problemSkills, "userSkills": userSkills}
defaultGetResponse = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="app">
<h3>Change the example solution code to post for a user</h3>
<textarea v-model="solutionText">
</textarea>
<br>
<button @click="postData">Analyze</button>
<br>
<h3>Solutions to post</h3>
<pre>{{solutions}}</pre>
<hr>
<h3>Last result</h3>
<pre>{{lastResult}}</pre>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
var app = new Vue({
el: "#app",
data:{
solutionText: "print(x)",
lastResult: ""
},
computed: {
solutions: function(){
return {"problemAA":{"userBB": this.solutionText}}
}
},
methods:{
postData: function() {
var data = this.solutions;
fetch('https://ltp7y8q1ak.execute-api.ap-southeast-1.amazonaws.com/default/code_analysis', { // the URI
method: 'POST', // the method
body: JSON.stringify(data) // the body
})
.then(response => {
// we received the response and print the status code
console.log(response.status)
// return response body as JSON
return response.json()
})
.then(json => {
// print the JSON
console.log(json)
this.lastResult = json
})
}
}
})
</script>
</body>
</html>
"""
import json
# event is a dict
def lambda_handler(event, context):
dummy = {}
body = defaultGetResponse
if event['httpMethod'] == 'GET':
dummy['httpMethod'] = 'GET'
else:
if event["body"]:
dummy = json.loads(event["body"])
body = json.dumps(solution_features(dummy))
else:
dummy['httpMethod'] = 'POST'
defaultPostBody = {"problemA":{"userB":"print(x)"}}
body = json.dumps(solution_features(defaultPostBody))
result = {
"isBase64Encoded": False,
"statusCode": 200,
"headers": {"content-type": "text/html"},
"body": body
}
return result