我有下面的代碼,我認爲它很臭,因爲vi
的重複。我怎麼寫得更好?寫這段代碼的更好的方法是什麼?
vi = '([a-zA-Z0-9_\-\.]+)'
handlers = [
(r'/register', RegistrationHandler),
(r'/profiles/%s/%s' % (vi, vi), GetProfiles),
(r'/archives/%s/%s/%s/%s' % (vi, vi, vi, vi), GetArchives),
(r'/publish-profiles', PublishProfiles),
(r'/publish-bundle/%s/%s' % (vi, vi), PublishBundle),
(r'/upload-file/%s/%s/%s' % (vi, vi, vi), UploadFile),
(r"/favicon\.ico", tornado.web.StaticFileHandler, dict(path=settings['static_path'])),
]
我沒有找到答案建議優於OP的代碼。 –
也可以將'vi'簡化爲'([a-zA-Z0-9 _.-] +)'或者只是'[\ w .-]'。 – georg
@ thg435,謝謝。這也有幫助! – missingfaktor