1. 程式人生 > >實現資料的增刪改查

實現資料的增刪改查

一,程式要求

    對後臺的資料(以一個檔案為例)實現簡單的增刪改查功能,語句的語法為提示資訊的示例

二,程式碼部分

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Jiang Feng"
# Date: 2018/12/28
import os
def file_processing(backend_data, type, res = None):
    if type == 'grep':
        with open('aaa', 'r') as read_f:
            results 
= [] tag = False for read_line in read_f: if read_line.strip() == backend_data: tag = True continue if tag and read_line.strip().startswith('backend'): break if tag: results.append(read_line.strip())
return results elif type == 'change': with open('aaa','r') as read_f ,open('aaa_new','w') as write_f: tag = False for read_line in read_f: if read_line.strip() == backend_data: write_f.write(read_line) tag = True
for server in res: server = ' ' * 8 + server server = '%s\n' % server write_f.write(server) continue if tag and read_line.startswith('backend'): tag = False if not tag: write_f.write(read_line) os.rename('aaa', 'aaa.bak') os.rename('aaa_new', 'aaa') os.remove('aaa.bak') return '資料已經修改完成' def add(data): backend = data[0]['backend'] backend_data = 'backend %s' % backend adddata = data[0]['server'] add_list = grep(backend) if adddata in add_list: print('你想要新增的資料已經存在') else: add_list.append(adddata) return file_processing(backend_data, type='change', res=add_list) def remove(data): backend = data[0]['backend'] backend_data = 'backend %s'%backend rmdata = data[0]['server'] remove_list = grep(backend) if rmdata in remove_list: remove_list.remove(rmdata) return file_processing(backend_data, type='change', res=remove_list) else: print('你想要修改的資料不存在') def change(data): backend = data[0]['backend'] backend_data = 'backend %s'%backend old_data = data[0]['server'] new_data = data[1]['server'] grep_list = grep(backend) if not new_data or old_data not in grep_list: print('你想要修改的資料不存在') else: index = grep_list.index(old_data) grep_list[index] = new_data return file_processing(backend_data, type='change', res=grep_list) def grep(data): backend_data = 'backend %s' %data return file_processing(backend_data, type='grep') if __name__ == '__main__': tip = ''' 歡迎使用:請根據提示輸入數字 1:增(例:[{'backend':'www.baidu2.com','server':'server 2.2.2.9 2.2.2.9 weight 20 maxconn 4000'}]) 2:刪(例:[{'backend':'www.baidu2.com','server':'server 2.2.2.9 2.2.2.9 weight 20 maxconn 4000'}]) 3:改[{'backend':'www.baidu1.com','server':'server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000'},\ {'backend':'www.baidu1.com','server':'server 10.10.10.1 10.10.10.1 weight 22 maxconn 22222'}] 4:查(例:www.baidu1.com) 5:退出 ''' msg = { '1': add, '2': remove, '3': change, '4': grep, } while True: print(tip) choice = input('請輸入你的選項:') if not choice: continue if choice == '5': break data = input('請輸入程式碼:') if choice != '4': data = eval(data) res = msg[choice](data) print(res)

三,檔案中的變數

backend:為了使用查詢,獲得查詢功能返回的列表

backend_data:為了與後臺資料進行判斷

file_processing:檔案處理函式,為了實現程式的解耦性四,附上仿製的後臺資料

global
        log 127.0.0.1 local2
        daemon
        maxconn 256
        log 127.0.0.1 local2 info
defaults
        log global
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms
        option  dontlognull

listen stats :8888
        stats enable
        stats uri       /admin
        stats auth      admin:1234

frontend www.baidu.com
        bind 0.0.0.0:80
        option httplog
        option httpclose
        option  forwardfor
        log global
        acl www hdr_reg(host) -i www.oldboy.org
        use_backend www.oldboy.org if www
backend www.baidu.com
backend www.baidu1.com
        server 101.1000.7.9 101.1000.7.9 weight 20 maxconn 30
        server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
        server 2.2.2.5 2.2.2.5 weight 20 maxcoon 3333
        server 2.2.2.7 2.2.2.7 weight 20 maxcoon 3333
backend www.baidu2.com
        server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
        server 2.2.22.221 2.2.22.221 weight 20 maxconn 3333
backend www.baidu3.com
        server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
        server 2.2.2.111 2.2.2.111 weight 20 maxconn 3333

測試的示例程式碼:

增:[{'backend':'www.baidu2.com','server':'server 2.2.2.9 2.2.2.9 weight 20 maxconn 4000'}]     
刪例:[{'backend':'www.baidu2.com','server':'server 2.2.2.9 2.2.2.9 weight 20 maxconn 4000'}]
改:[{'backend':'www.baidu1.com','server':'server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000'},{'backend':'www.baidu1.com','server':'server 10.10.10.1 10.10.10.1 weight 22 maxconn 22222'}]
查:www.baidu1.com