事实证明,这个条件块继续在我的代码中重复自己。任何其他方式让我的生活更轻松?当然,一个条件下执行的身体是不同的。
if self.datatype == "string":
t = "z"
elif self.datatype == "double":
t = "d"
elif self.datatype == "number":
t = "i"
elif self.datatype == "blob":
t = "z"
else:
raise EntParEx("Unknown datatype" + self.datatype)......更多的代码使用相同的条件
def emit_cpp_def(self):
s = ""
e = ""
if self.datatype == "string":
s += "static const int " + self.lenvar + " = " + self.length + ";"
s += "\nchar"
e += "[" + self.lenvar + " + 2" + "]"
elif self.datatype == "double":
s += "double"
elif self.datatype == "number":
s += "int"
elif self.datatype == "blob":
s += "char*"
else:
raise EntParEx("Unknown datatype" + self.datatype)
s += " " + self.cpp_membername;
s += e
s += ";"
return s;
def emit_cursor_def_code(self):
if self.datatype == "blob":
return ""
ret = "nvl(" + self.db_fieldname + ", "
#TODO: Add default value loading!
if self.datatype == "string":
ret += "\' \'"
elif self.datatype == "double":
ret += "-1.0"
elif self.datatype == "number":
ret += "-1"
else:
raise EntParEx("Unknown datatype" + self.datatype)
ret += "), "
return ret编辑:
我认为我需要的是像为每种类型运行特定功能的东西。不幸的是,我并不熟悉python。可以这样做吗?即
switch_datatype(function_string(), function_integer(), ...etc)这是否更糟糕?