MCPcopy Create free account
hub / github.com/ActiveState/code / compile

Method compile

recipes/Python/464766_Trivia_Template/recipe-464766.py:57–97  ·  view source on GitHub ↗

compile(self) -> None Compile self into Python code.

(self)

Source from the content-addressed store, hash-verified

55 yield ref
56
57 def compile(self):
58 """
59 compile(self) -> None
60
61 Compile self into Python code.
62 """
63
64 if self.code is not None:
65 return
66
67 level = 0
68 source = ''
69
70 for token in self.tokenize():
71 indent = ' ' * level * 2
72
73 if token.startswith('###'):
74 source = source + '%s_ = _ + %s\n' % \
75 (indent, token[3:].strip())
76
77 elif token.startswith('<?') and token.endswith('?>'):
78 content = token[2:-2]
79
80 if content == 'end':
81 level = level - 1
82
83 elif content.startswith('if ') \
84 or content.startswith('for ') \
85 or content.startswith('while '):
86 source = source + '%s%s:\n' % (indent, content)
87 level = level + 1
88
89 elif content.startswith('='):
90 source = source + '%s_ = _ + %s\n' % \
91 (indent, content[1:].strip())
92
93 else:
94 source = source + '%s_ = _ + """%s"""\n' % \
95 (indent, token.replace('"""', r'\"\"\"'))
96
97 self.code = compile(source, '<string>', 'exec')

Callers 4

__init__Method · 0.95
TemplateClass · 0.45
field_extract_sectionFunction · 0.45
recipe-465757.pyFile · 0.45

Calls 6

tokenizeMethod · 0.95
compileFunction · 0.85
startswithMethod · 0.80
endswithMethod · 0.80
stripMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected