MCPcopy Index your code
hub / github.com/datacamp/pythonwhat / ObjectAssignmentParser

Class ObjectAssignmentParser

pythonwhat/parsing.py:450–505  ·  view source on GitHub ↗

Find object assignmnts A parser which inherits from the basic parser to find object assignments. All assignments at top-level, as well as in if, while, for and with statements are found.

Source from the content-addressed store, hash-verified

448
449
450class ObjectAssignmentParser(Parser):
451 """Find object assignmnts
452
453 A parser which inherits from the basic parser to find object assignments.
454 All assignments at top-level, as well as in if, while, for and with statements are found.
455 """
456
457 def __init__(self):
458 self.out = {}
459 self.active_assignment = None
460
461 def visit_Name(self, node):
462 if self.active_assignment is not None:
463 if node.id not in self.out:
464 self.out[node.id] = self.get_part(node, self.active_assignment)
465 else:
466 self.out[node.id]["highlight"] = None
467 self.active_assignment = None
468
469 def visit_Attribute(self, node):
470 self.visit(node.value)
471
472 def visit_Assign(self, node):
473 self.active_assignment = node
474 self.visit_each(node.targets)
475
476 def visit_AugAssign(self, node):
477 self.active_assignment = node
478 self.visit(node.target)
479
480 def visit_If(self, node):
481 self.visit_each(node.body)
482 self.visit_each(node.orelse)
483
484 def visit_While(self, node):
485 self.visit_each(node.body)
486 self.visit_each(node.orelse)
487
488 def visit_For(self, node):
489 self.visit_each(node.body)
490 self.visit_each(node.orelse)
491
492 def visit_With(self, node):
493 self.visit_each(node.body)
494
495 def visit_Try(self, node):
496 self.visit_each(node.body)
497 self.visit_each(node.finalbody)
498
499 @staticmethod
500 def get_part(name_node, ass_node=None):
501 # either name node or simply str or name itself
502 name = getattr(name_node, "id", name_node)
503 load_name = ast.Name(id=name, ctx=ast.Load())
504 ast.fix_missing_locations(load_name)
505 return {"name": name, "node": load_name, "highlight": ass_node or name_node}
506
507

Callers 1

Calls

no outgoing calls

Tested by 1