我正在升级我的旧项目, 之前的Python版本是2.2,所以它有点跳转到2.7。
现在最多的东西都在工作......我有很多has_key检查我必须将它们更改为新的'in'运算符。
我遇到了一些OverflowError exeptions,例如“:Python long convert to C long”和“:Python int太大而无法转换为C long”。
我已经搜索了6个小时以来的错误,但我没有得到它......
每次我将我的dict的子元素复制到我的元素dict中,我得到一个过低的错误,比如“:Python int太大而无法转换为C long”。
难道我不能在dict中添加大对象吗?
self.elems[name] = child # gives overflowexception
孩子是一个对象
parent.childs[idx] = GridSlotWindow()
我简直无法弄清楚为什么这与python 2.2一起工作但不能用python 2.7, 当我在大字典上“ _ 包含 _ ”时,我甚至会出现过度错误。
def checkkeylst(self, name, value, key_list):
for DataKey in key_list:
if DataKey not in value:
print "Failed to find key", "[" + name + "/" + DataKey + "]"
return FALSE
return TRUE
此代码提供了长到长的溢出异常......
也许我应该将脚本文件转换为xml文件并用库解析它们(我觉得会更容易)
我现在无法思考......这绝对会让我的大脑死亡:(
如果有人能给我一个提示错误是什么以及如何修复它会很棒。
编辑: 应该完成整个过程的代码部分
###################################################################################################
## Python Script Loader
###################################################################################################
class ScriptWindow(Window):
def __init__(self, layer = "UI"):
Window.__init__(self, layer)
self.Children = []
self.ElementDictionary = {}
def __del__(self):
Window.__del__(self)
def ClearDictionary(self):
self.Children = []
self.ElementDictionary.clear()
def InsertChild(self, name, child):
self.ElementDictionary[name] = child
def IsChild(self, name):
return name in self.ElementDictionary
def GetChild(self, name):
return self.ElementDictionary[name]
def GetChild2(self, name):
return self.ElementDictionary.get(name, None)
class PythonScriptLoader(object):
BODY_KEY_LIST = ( "x", "y", "width", "height" )
#####
DEFAULT_KEY_LIST = ( "type", "x", "y", )
WINDOW_KEY_LIST = ( "width", "height", )
IMAGE_KEY_LIST = ( "image", )
EXPANDED_IMAGE_KEY_LIST = ( "image", )
ANI_IMAGE_KEY_LIST = ( "images", )
SLOT_KEY_LIST = ( "width", "height", "slot", )
CANDIDATE_LIST_KEY_LIST = ( "item_step", "item_xsize", "item_ysize", )
GRID_TABLE_KEY_LIST = ( "start_index", "x_count", "y_count", "x_step", "y_step", )
EDIT_LINE_KEY_LIST = ( "width", "height", "input_limit", )
COMBO_BOX_KEY_LIST = ( "width", "height", "item", )
TITLE_BAR_KEY_LIST = ( "width", )
HORIZONTAL_BAR_KEY_LIST = ( "width", )
BOARD_KEY_LIST = ( "width", "height", )
BOARD_WITH_TITLEBAR_KEY_LIST = ( "width", "height", "title", )
BOX_KEY_LIST = ( "width", "height", )
BAR_KEY_LIST = ( "width", "height", )
LINE_KEY_LIST = ( "width", "height", )
SLOTBAR_KEY_LIST = ( "width", "height", )
GAUGE_KEY_LIST = ( "width", "color", )
SCROLLBAR_KEY_LIST = ( "size", )
LIST_BOX_KEY_LIST = ( "width", "height", )
def __init__(self):
self.Clear()
def Clear(self):
self.ScriptDictionary = { "SCREEN_WIDTH" : wndMgr.GetScreenWidth(), "SCREEN_HEIGHT" : wndMgr.GetScreenHeight() }
self.InsertFunction = 0
def LoadScriptFile(self, window, FileName):
self.Clear()
print "===== Load Script File : %s\n" % (FileName)
try:
execfile(FileName, self.ScriptDictionary)
except:
import dbg
import exception
dbg.TraceError("Failed to load script file : %s" % (FileName))
exception.Abort("LoadScriptFile")
Body = self.ScriptDictionary["window"]
self.CheckKeyList("window", Body, self.BODY_KEY_LIST)
window.ClearDictionary()
self.InsertFunction = window.InsertChild
window.SetPosition(int(Body["x"]), int(Body["y"]))
window.SetSize(int(Body["width"]), int(Body["height"]))
if "style" in Body:
for StyleList in Body["style"]:
window.AddFlag(StyleList)
self.LoadChildren(window, Body)
def LoadChildren(self, parent, dicChildren):
if "children" not in dicChildren:
return FALSE
Index = 0
ChildrenList = dicChildren["children"]
parent.Children = range(len(ChildrenList))
for ElementValue in ChildrenList:
try:
Name = ElementValue["name"]
except KeyError:
Name = ElementValue["name"] = "NONAME"
try:
Type = ElementValue["type"]
except KeyError:
Type = ElementValue["type"] = "window"
if FALSE == self.CheckKeyList(Name, ElementValue, self.DEFAULT_KEY_LIST):
del parent.Children[Index]
continue
if Type == "window":
parent.Children[Index] = ScriptWindow()
parent.Children[Index].SetParent(parent)
self.LoadElementWindow(parent.Children[Index], ElementValue)
elif Type == "button":
parent.Children[Index] = Button()
parent.Children[Index].SetParent(parent)
self.LoadElementButton(parent.Children[Index], ElementValue)
elif Type == "radio_button":
parent.Children[Index] = RadioButton()
parent.Children[Index].SetParent(parent)
self.LoadElementButton(parent.Children[Index], ElementValue)
elif Type == "toggle_button":
parent.Children[Index] = ToggleButton()
parent.Children[Index].SetParent(parent)
self.LoadElementButton(parent.Children[Index], ElementValue)
elif Type == "mark":
parent.Children[Index] = MarkBox()
parent.Children[Index].SetParent(parent)
self.LoadElementMark(parent.Children[Index], ElementValue)
elif Type == "image":
parent.Children[Index] = ImageBox()
parent.Children[Index].SetParent(parent)
self.LoadElementImage(parent.Children[Index], ElementValue)
elif Type == "expanded_image":
parent.Children[Index] = ExpandedImageBox()
parent.Children[Index].SetParent(parent)
self.LoadElementExpandedImage(parent.Children[Index], ElementValue)
elif Type == "ani_image":
parent.Children[Index] = AniImageBox()
parent.Children[Index].SetParent(parent)
self.LoadElementAniImage(parent.Children[Index], ElementValue)
elif Type == "slot":
parent.Children[Index] = SlotWindow()
parent.Children[Index].SetParent(parent)
self.LoadElementSlot(parent.Children[Index], ElementValue)
elif Type == "candidate_list":
parent.Children[Index] = CandidateListBox()
parent.Children[Index].SetParent(parent)
self.LoadElementCandidateList(parent.Children[Index], ElementValue)
elif Type == "grid_table":
parent.Children[Index] = GridSlotWindow()
parent.Children[Index].SetParent(parent)
self.LoadElementGridTable(parent.Children[Index], ElementValue)
elif Type == "text":
parent.Children[Index] = TextLine()
parent.Children[Index].SetParent(parent)
self.LoadElementText(parent.Children[Index], ElementValue)
elif Type == "editline":
parent.Children[Index] = EditLine()
parent.Children[Index].SetParent(parent)
self.LoadElementEditLine(parent.Children[Index], ElementValue)
elif Type == "titlebar":
parent.Children[Index] = TitleBar()
parent.Children[Index].SetParent(parent)
self.LoadElementTitleBar(parent.Children[Index], ElementValue)
elif Type == "horizontalbar":
parent.Children[Index] = HorizontalBar()
parent.Children[Index].SetParent(parent)
self.LoadElementHorizontalBar(parent.Children[Index], ElementValue)
elif Type == "board":
parent.Children[Index] = Board()
parent.Children[Index].SetParent(parent)
self.LoadElementBoard(parent.Children[Index], ElementValue)
elif Type == "board_with_titlebar":
parent.Children[Index] = BoardWithTitleBar()
parent.Children[Index].SetParent(parent)
self.LoadElementBoardWithTitleBar(parent.Children[Index], ElementValue)
elif Type == "thinboard":
parent.Children[Index] = ThinBoard()
parent.Children[Index].SetParent(parent)
self.LoadElementThinBoard(parent.Children[Index], ElementValue)
elif Type == "box":
parent.Children[Index] = Box()
parent.Children[Index].SetParent(parent)
self.LoadElementBox(parent.Children[Index], ElementValue)
elif Type == "bar":
parent.Children[Index] = Bar()
parent.Children[Index].SetParent(parent)
self.LoadElementBar(parent.Children[Index], ElementValue)
elif Type == "line":
parent.Children[Index] = Line()
parent.Children[Index].SetParent(parent)
self.LoadElementLine(parent.Children[Index], ElementValue)
elif Type == "slotbar":
parent.Children[Index] = SlotBar()
parent.Children[Index].SetParent(parent)
self.LoadElementSlotBar(parent.Children[Index], ElementValue)
elif Type == "gauge":
parent.Children[Index] = Gauge()
parent.Children[Index].SetParent(parent)
self.LoadElementGauge(parent.Children[Index], ElementValue)
elif Type == "scrollbar":
parent.Children[Index] = ScrollBar()
parent.Children[Index].SetParent(parent)
self.LoadElementScrollBar(parent.Children[Index], ElementValue)
elif Type == "thin_scrollbar":
parent.Children[Index] = ThinScrollBar()
parent.Children[Index].SetParent(parent)
self.LoadElementScrollBar(parent.Children[Index], ElementValue)
elif Type == "small_thin_scrollbar":
parent.Children[Index] = SmallThinScrollBar()
parent.Children[Index].SetParent(parent)
self.LoadElementScrollBar(parent.Children[Index], ElementValue)
elif Type == "sliderbar":
parent.Children[Index] = SliderBar()
parent.Children[Index].SetParent(parent)
self.LoadElementSliderBar(parent.Children[Index], ElementValue)
elif Type == "listbox":
parent.Children[Index] = ListBox()
parent.Children[Index].SetParent(parent)
self.LoadElementListBox(parent.Children[Index], ElementValue)
elif Type == "listbox2":
parent.Children[Index] = ListBox2()
parent.Children[Index].SetParent(parent)
self.LoadElementListBox2(parent.Children[Index], ElementValue)
elif Type == "listboxex":
parent.Children[Index] = ListBoxEx()
parent.Children[Index].SetParent(parent)
self.LoadElementListBoxEx(parent.Children[Index], ElementValue)
else:
Index += 1
continue
parent.Children[Index].SetWindowName(Name)
if 0 != self.InsertFunction:
self.InsertFunction(Name, parent.Children[Index])
if "style" in ElementValue:
for StyleList in ElementValue["style"]:
parent.Children[Index].AddFlag(StyleList)
self.LoadChildren(parent.Children[Index], ElementValue)
Index += 1
def CheckKeyList(self, name, value, key_list):
for DataKey in key_list:
if DataKey not in value:
print "Failed to find data key", "[" + name + "/" + DataKey + "]"
return FALSE
return TRUE
要加载的示例文件:
imports screenConfig
window = {
"name" : "questiondlg",
"x" : SCREEN_WIDTH/2 - 125,
"y" : SCREEN_HEIGHT/2 - 52,
"width" : 280,
"height" : 75,
"children" :
(
{
"name" : "board",
"type
...
"children" :
(
{
"name" : "message",
"type" : "text",
"x" : 0,
"y" ...
},
{
"name" : "countdown_message",
...
},
),
},
),
}
它缩短了一点,但应该很清楚
EDIT2:
login.py(行:510)
def __LoadScript(self, fileName):
try:
scriptLoader = ui.PythonScriptLoader()
scriptLoader.LoadScriptFile(self, fileName)
except:
import exception
exception.Abort("LoginWindow.__LoadScript")
错误跟踪重新加入:
login.py(line:510) __LoadScript
ui.py(line:2648) LoadScriptFile
ui.py(line:2835) LoadChildren
ui.py(line:2659) LoadChildren
LoginWindow.__LoadScript - <type 'exceptions.OverflowError'>:Python int too large to convert to C long
谢谢