day 15 - 2 内置函数练习
内置函数练习
编写 sql 查询语句功能
文件内容:
1,Eva,22,13651054608,IT
2,Vera,23,13304320533,Tearcher
3,Renault,25,1333235322,IT
实现:
装饰器登录
select name, age where age>22
select * where age>22
select * where phone like 133
select * where occ=IT(字符串比较未实现,与年龄的数字比较有冲突 待解决)
#读文件 def dir(): with open('d:/py/info.txt',encoding='utf-8')as f: for line in f: line = line.strip().split(',') yield line d =dir() #实现显示 def view(option,lis): dic = {'id':lis[0],'name':lis[1],'age':lis[2],'phone':lis[3],'occ':lis[4]} option = option.split().pop(1).split(',') for i in option: if i in dic: print(dic[i],end=' ') elif i == '*': print(lis) else: print('输入字段有误') # sel 判断 def judge(option,condition): if '>' in condition: a,b = condition.split('>') for lis in dir(): if int(d.__next__()[2]) > int(b): view(option,lis) elif '<' in condition: a,b = condition.split('<') for lis in dir(): if int(d.__next__()[2]) < int(b): view(option,lis) elif '=' in condition: a,b = condition.split('=') for lis in dir(): if int(d.__next__()[2]) == int(b): view(option,lis) #实现 like 查找功能 def like(s): if 'like' in s and '*' in s: val,con = s.split('like') val = val.split().pop().strip() con = con.strip() for lis in dir(): if con in d.__next__()[3]: print(lis) #实现登录 def login(f): def inner(): use = input('账号 >>>') pwd = input('密码 >>>') if use == 'ysg' and pwd == '123': ret = f() return ret else: print('登录失败') return inner #实现 select(主函数) @login def sel(): s = input('输入语句 >>>') #s = 'select name,age where age>22' option,condition = s.split('where') condition = condition.strip() #strs(s) #实现字符串比较的功能 like(s) #实现 like 查找功能 judge(option,condition) #实现数字比较的功能 sel()
面试题:阅读代码,说出输出结果
def extendList(val,list=[]): list.append(val) return list list = extendList(10) list2 = extendList(123,[]) list3 = extendList('a') print("list = %s"%list) print("list2 = %s"%list2) print("list3 = %s"%list3) #结果 list = [10, 'a'] list2 = [123] list3 = [10, 'a']