Open hult1989 opened 9 years ago
把不是一目了然的语句封装成函数,即便只有一行语句,封装也能带来更好的可读性
'''如何处理嵌套的if'''
def handleSyncSosSql(wsdbpool, message):
return wsdbpool.runInteraction(_handlleSyncSos, message)
def _handlleSyncSos(txn, message):
#5,123456789abcdef,3,7,+8613800000000,+8613800000001,+8613800000002
message = message.split(',')
imei = message[1]
numbersInStick = list()
for number in message[4:]:
'''
这里可以使用条件反转
if len(number) == 0:
pass
numberInStick.append(number)
'''
if len(number) != 0:
numbersInStick.append(number)
#有时要判断一个条目是否已经在数据库中存在,可以把这个操作提取出来作为一个函数
txn.execute('select contact from temp_sos where imei = %s and sosnumber = %s', (imei, number))
contact = txn.fetchall()
if len(contact) != 0:
#如果条目已经存在,pass,这也是条件反转
#this number is both in temp_sos and in message5 from stick, it means that this number shoulde be added to the sosnumber table
txn.execute('replace into sosnumber (imei, sosnumber, contact) values(%s, %s, %s)', (imei, number, contact[0][0]))
txn.execute('delete from temp_sos where imei = %s and sosnumber = %s', (imei, number))
txn.execute('select sosnumber from sosnumber where imei = %s', (imei,))
numbersInDbEntries = txn.fetchall()
for numberEntry in numbersInDbEntries:
if numberEntry[0] not in numbersInStick:
#条件反转
#the number exists in server but not in the message from the stick, it means that this number shoule be deleted
txn.execute('delete from sosnumber where imei = %s and sosnumber = %s', (imei, numberEntry[0]))
txn.execute('delete from temp_sos where imei = %s and sosnumber = %s', (imei, numberEntry[0]))
return True
def render_POST(self, request):
payload = eval(request.content.read())
'''
switch (request.args['action'])
case 'delnumber':
case 'delnumber':
'''
if request.args['action'] == ['addnumber']:
if payload['imei'] == '0' or len(payload['imei']) == '0' or len(payload['contactentry']['sosnumber']) == 0 or payload['contactentry']['sosnumber'] == '0':
return resultValue(300)
d = selectWsinfoSql(wsdbpool, payload['imei']).addCallback(self.varifyPwd, payload)
d.addCallback(self.onSetResult, request)
d.addErrback(onError)
return NOT_DONE_YET
if request.args['action'] == ['delnumber']:
if payload['imei'] == '0' or len(payload['imei'])== '0' or len(payload['contactentry']['sosnumber']) == 0 or payload['contactentry']['sosnumber'] == '0':
return resultValue(300)
d = selectWsinfoSql(wsdbpool, payload['imei']).addCallback(self.varifyPwd, payload)
d.addCallback(self.onSetResult, request)
d.addErrback(onError)
return NOT_DONE_YET
if request.args['action'] == ['varifyadd']:
if payload['imei'] == '0' or len(payload['imei']) == '0' or len(payload['sosnumber']) == 0 or payload['sosnumber'] == '0':
return resultValue(300)
d = checkSosnumberSql(wsdbpool, payload['imei'], payload['sosnumber'])
d.addCallback(self.varifySos, request, payload)
d.addErrback(onError)
return NOT_DONE_YET
if request.args['action'] == ['varifydel']:
if payload['imei'] == '0' or len(payload['imei'] )== '0' or len(payload['sosnumber
']) == 0 or payload['sosnumber'] == '0':
return resultValue(300)
d = checkSosnumberSql(wsdbpool, payload['imei'], payload['sosnumber'])
d.addCallback(self.varifyDel, request, payload)
d.addErrback(onError)
return NOT_DONE_YET
if request.args['action'] == ['getnumber']:
d = selectSosnumberSql(wsdbpool, payload['imei'])
d.addCallback(self.onGetsos, request)
d.addErrback(onError)
return NOT_DONE_YET
if request.args['action'] == ['updatepassword']:
if payload['imei'] == '0' or len(payload['imei']) == '0' or len(payload['newadminp
wd']) == 0:
return resultValue(300)
d = selectWsinfoSql(wsdbpool, payload['imei']).addCallback(self.updatePwd, payload
)
d.addCallback(self.onSetResult, request)
d.addErrback(onError)
return NOT_DONE_YET
def varifyPwd(self, result, payload):
'''
ugly and difficult to read
'''
if len(result) == 0:
d = defer.Deferred()
d.callback('403')
return d
if result[0][2] == '0':
d = defer.Deferred()
d.callback('505')
return d
if result[0][3] != payload['adminpwd']:
d = defer.Deferred()
d.callback('402')
return d
return insertTempSosSql(wsdbpool, imei=payload['imei'], sosnumber=payload['contactentr
y']['sosnumber'], contact=payload['contactentry']['contact'])
如何组织代码源文件的结构
把所有的可配置参数都提取出来,写入配置文件,如端口号、账户、文件地址等信息 写README.md
为每个request指定正确的参数,并封装出独立的判断函数
进一步封装request,通过request的不同参数实例化不同的request子类,在这些子类的父类中声明抽象函数,然后在子类中分别实现这些函数
是这样做还是把