Realization of Employee Information table display function

Source: Internet
Author: User
Readme:

1. Employee Information Form procedures, to achieve additions and deletions to check the operation:

1). Can be fuzzy query, syntax support at least the following 3 kinds:
Select Name,age from staff_table where age > 22
SELECT * from staff_table where dept = "IT"
SELECT * from staff_table where enroll_date like "2013"
Finally shows the number of bars found
2). Can create a new employee record, with phone to do unique key, staff_id need to increase
3). Delete the specified employee information record, enter an employee ID, and delete
4). Can modify employee information, the syntax is as follows:
UPDATE staff_table SET dept= "Market" WHERE dept = "IT"

Flow chart:

Code:

# coding:utf8import Sysimport Redef Select (Staff, field): cmd = input ("cmd>"). Strip () cmd = Cmd.replace (' From ', ' From ') cmd = Cmd.replace (' where ', ' where ') if ' * ' in cmd:for i in Field.keys (): Sys.stdout.write (str (i) + ' Print (") for line in staff:info_list = Re.split (R ', + ', Line.strip (' \ n ')) Cmd_list = Re.split (R ' [,;] + ', cmd) F_index = Cmd_list.index (' from ') Search_field = Cmd_list[1:f_index] From_field = cmd_list[f_ Index + 1]if From_field! = ' staff_table ':p rint (' \033[31;1mplease select ' staff_table ' ... \033[0m ') breakif ' where ' not in C md_list:# does not exist where condition, shows all view_list = []for i in Range (len (Search_field)): if search_field[i] = = ' * ': Vie W_list = Info_list[:]else:view_list.append (Info_list[field.get (Search_field[i])) else:print (', '. Join (V iew_list)) else:# exists where condition W_index = Cmd_list.index (' where ') Where_str = '. Join (Cmd_list[w_index + 1:]) if re.se      Arch (R ' like ', WHERE_STR):          sizeof = ' like ' where_list = Re.split (R ' like ', where_str) else:sizeof = Re.search (R ' [=><]+ ', WHERE_STR). Group () Where_list = Re.split (R ' [=><]+ ', where_str) Where_field = where_list[0                ] Value = where_list[1]if Re.search (r ' [\ ' \ ']+ ', value): # with ' and ' representing characters, processing off ' and ' value = value.replace (' \ ' ', ')  Value = Value.replace (' \ ', ') Else:value = float (value) # print (Where_str, Where_field, sizeof,  Value) # conditional judgment;, <, =, likeif sizeof = = ' > ': v = info_list[field.get (Where_field)]if float (v) >                            Value:view_list = []for i in Range (len (Search_field)): if search_field[i] = = ' * ': View_list = Info_list[:]else:view_list.append (Info_list[field.get (Search_field[i])) Els E:print (', '. Join (view_list)) elif sizeof = = ' < ': v = info_list[field.get (Where_field)]if float (v) < VA                    LueView_list = []for i in Range (len (Search_field)): if search_field[i] = = ' * ': view_list = Info_lis T[:]else:view_list.append (Info_list[field.get (Search_field[i])) else:print (', '. Join (View_list)                    ) elif sizeof = = ' = ': v = info_list[field.get (Where_field)]if field.get (where_field) = = 2:                             v = Float (v) if v = = Value:view_list = []for i in Range (len (Search_field)): if search_field[i] = = ' * ': View_list = Info_list[:]else:view_list.append (Info_list[field.get ( Search_field[i]) else:print (', '. Join (view_list)) elif sizeof = = ' like ': v = info_list[field.get (Where_field                            )]if value in v:view_list = []for i in Range (len (Search_field)): if search_field[i] = = ' * ': View_list = Info_list[:]else:view_list.append (Info_list[field.get (Search_field [i])]) Else:print (', '. JoiN (view_list)) else:passdef Add (Staff): staff.sort () num = Int (re.split (R ', ', staff[-1]) [0]) + 1phone = input ("Phone:" ). Strip () for line in staff:if phone = = line[3]:p rint ("already exists ...") return Falseif not Re.match (R ' ^\d+$ ', phone) or L En (Phone) < 7:print ("format error ...") return False name = input ("Name:"). Strip () Age = Input ("Age:"). Strip () d EPT = Input ("dept:"). Strip () Enroll_date = input ("enroll_date:"). Strip () for S in (name, age, Dept, enroll_date): If not Len (s):p rint ("Input is null ...") return Falseelse:staff.append ('%s,%s,%s,%s,%s,%s\n '% (num, name, age, Phone, dept , enroll_date)) return staffdef update (Staff, field): cmd = input ("cmd>"). Strip () up_list = [] cmd = Cmd.replac E (' Set ', ' Set ') cmd = Cmd.replace (' where ', ' where ') for line in staff:info_list = Re.split (R ', + ', Line.strip (' \ n ') Cmd_list = Re.split (R ' [,;] + ', cmd) if cmd_list[1]! = ' staff_table ':p rint (' \033[31;1mplease update ' staff_table ' ... \033[0m ') breakset_index = Cmd_list.index (' set ') Where_index = Cmd_list.index (' where ') Set_list = Re.split (R ' = ', ' ". Join (cmd_list[ Set_index + 1:where_index]) Set_field = set_list[0] Set_value = set_list[1]if Re.search (r ' [\ ' \ ']+ ', set_val UE): Set_value = set_value.replace (' \ ', ') Set_value = Set_value.replace (' \ "', ') where_li St = Re.split (R ' = ', '. Join (Cmd_list[where_index + 1:])) Where_field = where_list[0] Where_value = Where_lis  T[1]if Re.search (r ' [\ ' \ ']+ ', where_value): Where_value = where_value.replace (' \ ', ') Where_value = Where_value.replace (' \ ' ', ') if Info_list[field.get (where_field)] = = Where_value:info_list[field.get (Set_fiel    d)] = Set_valueprint (', '. Join (Info_list)) up_list.append (', '. Join (info_list)) Else:return up_listdef Delete (staff): del_id = input ("Delete ID:"). Strip () for I in Range (len): If Re.split (R ', ', Staff[i]) [0] = = Del_id:sta Ff.pop (i) print ("id:%s delete success. "% del_id) return Staffelse:return falsedef main (): staff_table = ' staff_table.txt ' field = {' staff_id ': 0,   ' Name ': 1, ' age ': 2, ' phone ': 3, ' dept ': 4, ' Enroll_date ': 5} menu = ' ' \033[33;1m--staff_table--\033[0m\033[29;1ms. Query H. Help A. Added Q. Exit E. Modify D.            Delete \033[0m ' Print (menu) while True:try:with open (staff_table, ' R ') as F:staff = F.readlines () Choice = input (">>"). Strip (). Lower () if choice = = ' h ': # Helpprint (menu) elif choice = ' Q ': Breakelif choic E = = ' s ': # Search Select (Staff, field) elif choice = = ' A ': # Addresult = Add (staff) if not result:print (' \ 033[31;1madd failed...\033[0m ') Continueelse:with open (staff_table, ' W ') as F:f . Writelines (Result) elif choice = = ' E ': # Updateresult = Update (staff, field) if not Result:print (' \033[31;1mupdate failed. ..                       \033[0m ') Continueelse:with open (staff_table, ' W ') as F:for line in result:     F.write (line + ' \ n ') elif choice = = ' d ': # Deleteresult = delete (staff) if not result:print (' \033[31;1mdelete failed. . \033[0m ') Continueelse:with open (staff_table, ' W ') as F:f.writelines (result) El Se:continueexcept:print ("\033[31;1minput error...\033[0m") continueif __name__ = = ' __main__ ': Main ()

View Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.