【发布时间】:2015-09-15 15:05:17
【问题描述】:
我正在尝试将 py++ 移植到最新版本的 python,但遇到了它抱怨的以下代码。我是 python 新手。任何帮助都非常感谢,并提前感谢。
错误:
File "C:\tp\python\python34\Lib\site-packages\pyplusplus\gui\wizard.py", line
40
, 'include_paths' : `self._parser_configuration.include_paths`
^
SyntaxError: invalid syntax
代码如下:
"""generates Py++ code from the user data"""
CODE_TEMPLATE = \
"""
import os
from pyplusplus import module_builder
#Creating an instance of class that will help you to expose your declarations
mb = module_builder.module_builder_t( [r"%(file_path)s"]
, gccxml_path=r"%(gccxml_path)s"
, working_directory=r"%(working_dir)s"
, include_paths=%(include_paths)s
, define_symbols=%(define_symbols)s )
#Well, don't you want to see what is going on?
mb.print_declarations()
#Creating code creator. After this step you should not modify/customize declarations.
mb.build_code_creator( module_name='pyplusplus' )
#Writing code to file.
mb.write_module( './bindings.cpp' )
"""
class wizard_t( object ):
"""code generator that creates Py++ code"""
def __init__( self
, parser_configuration
, source_file ):
object.__init__( self )
self._parser_configuration = parser_configuration
self._source_file = source_file
def create_code( self ):
global CODE_TEMPLATE
return CODE_TEMPLATE % {
'gccxml_path' : self._parser_configuration.gccxml_path
, 'working_dir' : self._parser_configuration.working_directory
, 'include_paths' : `self._parser_configuration.include_paths`
, 'define_symbols' : `self._parser_configuration.define_symbols`
, "file_path" : `self._source_file`
}
【问题讨论】:
-
你为什么在这些路径周围使用反引号?这些反引号仅在 Python 2 中有效,最好用
repr()调用替换。 -
非常感谢。这不是我的代码。它是 py++ 的一部分,它是使用 boost python 的 c++ 到 python 代码生成器。我正在学习 python,因为我正在尝试安装这个工具。我真的不太了解python,无法弄清楚。
-
显然该代码是为 Python 2 编写的。不过,使用反引号而不是
repr()非常晦涩(这是语言中删除语法的原因之一)。