The document discusses abstract syntax trees (AST) and code analysis tools in Python. It provides an overview of AST concepts like nodes, visitors and transformers. It also demonstrates examples of parsing, modifying and printing ASTs. Opportunities for AST applications include constant propagation, automatic logging/profiling, macros and extending Python's language. Threats include debug glitches and compiler version dependencies. Recommendations are made to improve testing tools and integrate with version control systems.
32. TRANSLATION_FUNCTIONS = set([
'_',
'gettext',
'ngettext', 'ngettext_lazy',
'npgettext', 'npgettext_lazy',
'pgettext', 'pgettext_lazy',
'ugettext', 'ugettext_lazy', 'ugettext_noop',
'ungettext', 'ungettext_lazy',
])
def visit_callfunc(self, node):
if not isinstance(node.func, astroid.Name):
# It isn't a simple name, can't deduce what function it is.
return
if node.func.name not in self.TRANSLATION_FUNCTIONS:
# Not a function we care about.
return
if not self.linter.is_message_enabled(self.MESSAGE_ID):
return
first = node.args[0]
if isinstance(first, astroid.Const):
if isinstance(first.value, basestring):
# The first argument is a constant string! All is well!
return
# Bad!
self.add_message(self.MESSAGE_ID, args=node.func.name, node=node)
ITGM, Dec 2016