Python programming

(23.02.08)Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ : Pickle ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ ์ง๋ ฌํ™”(Serialization), ์˜ˆ์™ธ์ฒ˜๋ฆฌ

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2023. 2. 14.

pickle ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ ์ง๋ ฌํ™”(Serialization)

  • ์ง๋ ฌํ™” : pickle.dump(data,fout)
  • ์—ญ์ง๋ ฌํ™” : pickle.load(fin)

 

 

 

 

์ง๋ ฌํ™”๋ฅผ ์ด์šฉํ•œ CRUD

  • Emp ํด๋ž˜์Šค
  • ๋ฆฌ์ŠคํŠธ์— Emp๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ์ง๋ ฌํ™”

# Emp (id, name, dept, phone)
# ์ถ”๊ฐ€(a), ๋ชฉ๋ก(s), ๊ฒ€์ƒ‰(f), ์ˆ˜์ •(u), ์‚ญ์ œ(d), ์ข…๋ฃŒ(x)

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
import pickle
class Emp:
    def __init__(self, id=None, name=None, dept=None, phone=None):
        self.id = id
        self.name = name
        self.dept = dept
        self.phone = phone
        
    def __str__(self):
        return '%s\t%s\t%s\t%s'%(self.id,self.name,self.dept,self.phone)
    
    def __eq__(self, other):
        return self.id==other.id
cs

 

 

1
2
emp_list = []
 
cs

๊ธ€๋กœ๋ฒŒ ์œ„์น˜์— ์„ ์–ธํ•œ๋‹ค. ๋ฉ”์†Œ๋“œ ๋ฐ–์— ์„ ์–ธ

 

 

1
2
3
4
5
def menu():
    print()
    msg = '์ถ”๊ฐ€(a), ๋ชฉ๋ก(s), ๊ฒ€์ƒ‰(f), ์ˆ˜์ •(u), ์‚ญ์ œ(d), ์ข…๋ฃŒ(x):'
    m_in = input(msg).strip()
    return m_in
cs

 

 

1
2
3
4
5
6
7
8
def input_emp():
    sinput = input('id name dept phone:').strip()
    in_list = sinput.split()
    if len(in_list)<4:
        print('์‚ฌ์›์ •๋ณด์ž…๋ ฅ ์˜ค๋ฅ˜')
        return None
    id,name,dept,phone = in_list
    return Emp(id=id, name=name, dept=dept, phone=phone)
cs

 

 

1
2
3
4
5
import pickle
def overwrite(emp_list):
    with open('C:/Users/parkj/1Python_Projects/text/pickle_emp_crud.txt''wb'as fout:
        pickle.dump(emp_list, fout)
        print('์ง๋ ฌํ™” ์„ฑ๊ณต')
cs

 

 

1
2
3
4
5
def add():
    emp = input_emp()
    emp_list.append(emp)
    overwrite(emp_list)
    print('์‚ฌ์›์ •๋ณด ์ถ”๊ฐ€ ์„ฑ๊ณต')
cs

 

 

1
2
3
4
5
6
import pickle
def load_list():
    global emp_list
    with open('C:/Users/parkj/1Python_Projects/text/pickle_emp_crud.txt''rb'as fin:
        emp_list = pickle.load(fin)
    emp_list = sorted(emp_list, key=lambda e:e.id)
cs

 

 

1
2
3
4
5
6
7
import pickle
 
def show_list():
    load_list()
    print()
    for e in emp_list:
        print(e)
cs

 

 

1
2
3
4
5
6
7
8
9
10
11
def find(label):
    key_id = input(label+':').strip()
    key_emp = Emp(id=key_id)
    load_list()
    found = None
    if key_emp in emp_list:
        idx = emp_list.index(key_emp)
        found = emp_list[idx]
    if found is None:
        print('๊ฒ€์ƒ‰๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค')
    return found
cs

 

 

1
2
3
4
5
6
7
8
def update():
    up_emp = find('์ˆ˜์ •ํ•  ์‚ฌ์›๋ฒˆํ˜ธ')
    if up_emp is None:
        return
    new_phone = input('์ƒˆ ์ „ํ™”๋ฒˆํ˜ธ:').strip()
    up_emp.phone = new_phone
    overwrite(emp_list)
    print('์‚ฌ์›์ •๋ณด ์ˆ˜์ • ์„ฑ๊ณต')
cs

 

 

1
2
3
4
5
6
7
8
9
def delete():
    del_emp = find('์‚ญ์ œํ•  ์‚ฌ์›๋ฒˆํ˜ธ')
    load_list()
    if del_emp in emp_list:
        emp_list.remove(del_emp)
        overwrite(emp_list)
        print('์‚ฌ์›์ •๋ณด ์‚ญ์ œ ์„ฑ๊ณต')
    else:
        print('์‚ฌ์›์ •๋ณด ์‚ญ์ œ ์‹คํŒจ')
cs

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while True:
    m_in = menu()
    if m_in == 'a':
        add()
    elif m_in=='s':
        show_list()
    elif m_in=='f':
        print(find('๊ฒ€์ƒ‰ํ•  ์‚ฌ์›๋ฒˆํ˜ธ'))
    elif m_in=='u':
        update()
    elif m_in=='d':
        delete()
    elif m_in=='x':
        break
    else:
        print('๋ฉ”๋‰ด์ž…๋ ฅ ์˜ค๋ฅ˜')
        
print('---------------------ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ---------------------')
cs

 

์‹คํ–‰๊ฒฐ๊ณผ:

 

 


 

์˜ˆ์™ธ์ฒ˜๋ฆฌ

 

 

 

 

 

 

๋Œ“๊ธ€