Python Learning DAY15 Staff Information management system compiling and thinking

Source: Internet
Author: User

Employee management system, as the name implies, should have additions and deletions to check and change function. After getting the demand, should follow a certain process to write, and finally achieve the unification and compatibility of the program.


The system requirements are as follows:

The file storage format is as follows:
Id,name,age,phone,job
1,alex,22,13651054608,it
2,egon,23,13304320533,tearcher
3,nezha,25,1333235322,it

Now the employee information file needs to be added and censored.
The Foundation must do:
A. Can be queried, support three kinds of syntax:
Select Column Name 1, column Name 2, ... where column name condition
Support: greater than or equal, also support fuzzy Lookup.
Example:
Select Name,age where age>22 #> <
SELECT * WHERE Job=it # =
SELECT * Where phone like 133 #看起来像 ' abc ' in ' 1232abcahj '

#充分的利用函数
#文件处理 string Processing
#str #where Split

Advanced Selection:
B. Can create new employee record, ID order increment
C. You can delete the specified employee record and enter the employee ID directly
D. Modify Employee information
Syntax: Set column name = "New value" where condition
#先用where查找对应人的信息, then use set to modify the column name corresponding to the value "new value"

Note: To operate the Employee Information table, you must first login, login certification needs to complete with the adorner
Other requirements as far as possible with function implementation

--------------------------------------------------------------------------------------------------------------- --------
So we get the demand, first we should design the process according to the requirements, and then follow the process to write the program. The first thing you should think about is the implementation of the query function.
The code is as follows:
def cha ():
f = open (' Employee information ', encoding= ' utf-8 ') #打开文件, encoding format utf-8, file name employee information, storage format by demand
def Chaxun (a):
xinxi,tiaojian=a.split (' where ') #将获取到的内容进行分割, the split flag is ' where ', the reason for segmentation is that the condition and information can be separated by where, respectively
Xinxi = Xinxi.strip (). Strip (' select ') #将信息去空格去掉select so you can get the query information.
if ' * ' in Xinxi and ' job ' in Tiaojian: #如果得到的信息中含有 * and the condition contains job
J,d=tiaojian.strip (). Split (' = ') #那么条件按等号分割, split condition D
d = D.strip ()
For line in F: #循环每一行内容
i = Line.strip (). Split (', ') #内容去空格, separated by commas
#print (line)
job = i[4] #job在第五位
if Job==d: #拿出第四位数据对比, if equal
Print (line) #打印这行数据, which is the query, because the previous loop loop prints all the eligible data
Else:
Continue
elif ' * ' in Xinxi and ' phone ' in Tiaojian: #如果 * In the message, then phone in the condition
J, D = Tiaojian.strip (). Split (' like ') #按照like将条件分割
d = D.strip ()
For line in F:
i = Line.strip (). Split (', ')
# Print (line)
phone = i[3] #phone在数据第四位
if D in Phone: #如果分割出的条件在phone数据中
print (line) #打印这条数据
Else:
Continue

Else:
for line in F: #如果不在两个条件中, enter the third syntax
i = Line.strip (). Split (', ')
Age = i[2] #age数据在第三位
Age = Int (age) #将age数据变成int整型 in order to continue the next judgment
if Eval (Tiaojian): #用eval直接将条件信息转化成可以用的条件, you can call the age of the previous int directly
print (line) #打印查询到的年龄数据
Else:
Continue
chaxun_shuru=input (' Please enter query syntax ') #input让用户键入条件
v = Chaxun (Chaxun_shuru)
return v
if n==1: #主界面中要用到的启动条件
cha ()
--------------------------------------------------------------------------------------------------------------- ------------------
The code in the query section is written and can be commented out, because it interferes with the execution of other parts of the code, and then writes the code to create a new employee Information section as required by the customer.
The code is as follows:
def Chuang ():
f = open (' Employee information ', encoding= ' utf-8 ', mode= ' R ') #同上, first the file to process employee information first to call the file data
def cha ():
print (' Enter the employee record format you want to create, such as Alex,22,13651054608,it '
' Name,age,phone,job ')
Yuangong = input (' input: ') #用户按照创建格式来输入创建的员工信息
i=1 #先设定一个变量值为1
li=[] #设定一个空列表
print (f)
For Lin in F:
i+=1 #每次读取了一行文件中的数据, I just +1
li.append (i) #将加的1放进列表中
i =li[-1]+1 #最后得出了一个列表li, the last one +1 is the ID of the next bit
i = str (i) #将i变成字符串才可以跟字符串一起添加到信息中
if ' \ n ' not in Lin: #如果这行数据中没有 \ n
y = (' \ n ' +i+ ', ' +yuangong ') #就将i和用户输入的员工信息还有前置的 added to the data in PS: The purpose of doing this is not superfluous, because the added data, preceded by \ n Next addition, will be separated by a line /c1>
Else: and add data by row to read the ID, delete the words There's an empty line so there's a bug, no, it's at the end of the line.
y= (i+ ', ' +yuangong) #如果有的话直接添加数据不用加 \ n
f.close ()
f1 = open (' Employee information ', encoding= ' utf-8 ', mode= ' a ')
f1.write (y)
f1.close ()
cha ()
if n==2:
Chuang ()
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------
After writing the creation function, the next step is to complete the deletion function required by the customer. Similarly, to follow the ID of the deletion, the idea is clear, to try to get the information in the ID, and then find out how to call this ID to process the data
def Shan ():
Import OS
F=open (' Employee information ', encoding= ' utf-8 ', mode= ' R ')
v = f.readlines () #v接收到的是文件中每行的信息
f.close ()
y = input (' Enter the employee ID to be deleted ') #用户输入要删除的员工ID
For item in V: #for循环得到的文件数据
If Item:
if item[0] ==y: #如果循环的数据中含有用户要删除的ID
O=v.index (item) #那么用o来接收, use Index to find the index of this row of data
v[o]=v[o].lstrip (' \ n ') #索引取出, remove this value by V to remove \ nthe left
del V[o] #删除这条数据
F1=open (' Employee Information 2 ', encoding= ' utf-8 ', mode= ' W ')
F1.writelines (v) #新建文件写入除了删掉的数据的所有数据
f1.close ()
Os.remove (' Employee information ') #移除旧的文件
Os.rename (' Employee Information 2 ', ' Employee Information ') #将新文件名改成旧文件 to update status
F3=open (' Employee Information 2 ', encoding= ' utf-8 ', mode= ' W ') #再创建一个同名新文件 for next use
f3.close ()
if n==3:
Shan ()
--------------------------------------------------------------------------------------------------------------- ----------------------------------------
Delete Employee information finished, the main job is to find the key ID, then write a new file to delete the old file, and create a new file for the next use. Next, write the last feature to modify the functionality of the employee information.
The code is as follows:
def gai ():
Import OS
j=input (' Please enter the information you want to modify, syntax for set column name = new value where ') #让用户按键规则输入修改信息
Lieming,tiaojian=j.strip (). Split (' where ') #按照where分割用户输入的信息
Tiaojian=tiaojian.strip ()
Lieming,zhi=lieming.strip (). Split (' = ') #按照等号分割列名, remove the column name and value, the column name is the content property to be modified, the value is the new value modified
Zhi=zhi.strip ()
zhi=str (zhi) #将值的数据类型改为字符串
lieming= Lieming.strip (). Lstrip (' Set ') #将列名去掉左侧的set
Lieming=lieming.strip ()
F=open (' Employee information ', encoding= ' utf-8 ', mode= ' R ')
V=f.readlines ()
For line in V:
X=v.index (line) #用x接受line的索引值
if Tiaojian in line: #如果条件值在line数据中
if lieming in ' name ': #如果用户输入的列名是name的话
line=line.replace (Tiaojian,zhi) #将这一行数据中用replace方法将条件值替换成修改新值
v[x]=line #将这一行新的数据按照索引值替换v中的原数据
elif lieming in ' age ':
Line = Line.replace (Tiaojian, zhi)
v[x] = Line
elif lieming in ' phone ':
Line = Line.replace (Tiaojian, zhi)
v[x] = Line
elif lieming in ' job ':
Line = Line.replace (Tiaojian, zhi)
v[x] = Line
f.close ()
F2=open (' Employee Information 2 ', encoding= ' utf-8 ', mode= ' W ')
F2.writelines (v)
f2.close ()
Os.remove (' Employee information ')
Os.rename (' Employee Information 2 ', ' Employee information ')
F3=open (' Employee Information 2 ', encoding= ' utf-8 ', mode= ' W ')
f3.close ()
if n==4:
gai ()
--------------------------------------------------------------------------------------------------------------- --------------------------------

After writing all the functions of the program, after the completion of the next is the integration, want users to use the loop, it is necessary to add a loop plus exit.
The code below begins with all the function functions:
While True:
Print (' Employee Information Management system ')
Print (' 1. Find employee Information ')
Print (' 2. Create a new employee record ')
Print (' 3. Delete the specified employee record ')
Print (' 4. Modify employee Information ')
Print (' 5. Exit ')
n = input (' Please enter function number ')
N=int (N)
Let the user follow the function of the function number selection, also includes the Exit function
All functions and loops have been completed, so for a long time to decorate with a decorator, only log in once login system
--------------------------------------------------------------------------------------------------------------- -------------------------------
flag = False
def Login (func):
def Inner (*args,**kwargs):
Global Flag
While True:
if not flag:
a = input (' username: ')
B = input (' password ')
with open (' User login ', encoding= ' utf-8 ') as F:
For line in F:
Name,pwd=line.strip (). Split (' | ')
if name = = A and pwd ==b:
print (' login successful ')
flag = True
If flag:
Res=func (*args,**kwargs)
return res

return inner
Usage in the previous blog has instructions


Add the adorner to all functions with syntactic sugar so that the employee information management system is completed

Python Learning DAY15 Staff Information management system compiling and thinking

Related Article

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.