标签:负数 elf nbsp col __init__ class env lex str
1 #!/usr/bin/env python 2 # encoding: utf-8 3 4 class IllegalException(Exception): 5 ‘‘‘ 6 Custom exception types 7 ‘‘‘ 8 def __init__(self, parameter, para_value): 9 err = ‘The parameter "{0}" is not legal:{1}‘.format(parameter, para_value) 10 Exception.__init__(self, err) 11 self.parameter = parameter 12 self.para_value = para_value
1 def check_positive_int(para_list): 2 ‘‘‘ 3 @summary: check whether the parameter is positive integer 4 @param: para_list: a list,para_list: a list,[{‘key‘:‘para1‘,‘value‘:para1},{‘key‘:‘para2‘,‘value‘:para2}] 5 ‘‘‘ 6 for item in para_list: 7 if not (item[‘value‘] > 0 and isinstance(item[‘value‘], int)): 8 return item[‘key‘], ‘{0} is not a positive integer‘.format(str(item[‘value‘])) 9 return True 10 11 12 try: 13 integer = check_positive_int([{‘key‘:‘para1‘, ‘value‘:1}, {‘key‘:‘para2‘, ‘value‘:-2}]) 14 if integer != True: 15 raise(IllegalException(integer[0], integer[1])) 16 except IllegalException, x: 17 print ‘x--->‘, x 18 print ‘x.parameter--->‘, x.parameter 19 print ‘x.para_value--->‘, x.para_value 20 else: 21 print ‘NO ERROR‘
输出结果为:
1 x---> The parameter "para2" is not legal:-2 is not a positive integer 2 x.parameter---> para2 3 x.para_value---> -2 is not a positive integer
标签:负数 elf nbsp col __init__ class env lex str
原文地址:http://www.cnblogs.com/mianbaoshu/p/7810696.html