2020-2-6(111)

def format_string(string,formatter=None):
    
    class DefaultFormatter:
        def format(self,string):
            return str(string).title()
            
    if not formatter:
        formatter=DefaultFormatter()
    
    return formatter.format(string)
    
s="I am a girl."
print(s)
print(format_string(s))

class SecretString:
    def __init__(self,plain_string,pass_phrase):
        self.__plain_string=plain_string
        self.__pass_phrase=pass_phrase
        
    def decrypt(self,pass_phrase):
        if pass_phrase==self.__pass_phrase:
            return self.__plain_string
        else:
            return ""
            
secret_string=SecretString("hello world","psw")
print(secret_string.decrypt("psw"))
print(secret_string._SecretString__plain_string)  #名称改编
print(secret_string.__plain_string)  #无法访问私有变量

#[43]案例学习
#备注notes是存在笔记本Notebook里的短的备忘录memos;
#每一个备注都应该记录下被创建的时间,还可以添加标签;
#备注应该可以修改,也可以搜索备注。
#所有功能通过命令行实现

import time

class Note:
    '''Represent a note in the notebook'''
    last_id=0
    def __init__(self,memo,tag=''):
        self.memo=memo
        self.tag=tag
        self.create_time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
        Note.last_id+=1
        self.id=Note.last_id
        
    def match(self,filter):
        return filter in self.memo or filter in self.tag



class Notebook:
    '''Represent a collection of notes'''
    def __init__(self):
        self.notes=[]
        
    def new_note(self,memo,tag=''):
        self.notes.append(Note(memo,tag))
        
    def _find_note(self,note_id):
        for note in self.notes:
            if note.id == note_id:
                return note
        return None
    
    def modify_memo(self,note_id,memo):
        self._fine_note(note_id).memo = memo
                
    def modify_tag(self,note_id,tag):
        self._fine_note(note_id).tag = tag
                
    def search(self,filter):
        for note in self.notes:
            if note.match(filter):
                return note.id


import sys
from notebook import Notebook,Note

class Menu:
    '''Display a menu and response to choices when run'''
    def __init__(self):
        self.notebook = Notebook()
        self.choices = {
                "1":self.show_notes,
                "2":self.search_notes,
                "3":self.add_note,
                "4":self.modify_note,
                "5":self.quit
                }
    def display_menu(self):
        print("""
        Notebook Menu
        1. Show all Notes
        2. Search Notes
        3. Add Note
        4. Modify Note
        5. Quit
        """)

    def run(self):
        while True:
            self.display_menu()
            choice = input("请输入选项:")
            action = self.choices.get(choice)
            if action:
                action()
            else:
                print("%s is not a valid choice" %choice)
                
    def show_notes(self,notes=None):
        if not notes:
            notes = self.notebook.notes
        for note in notes:
            print("%s %s %s" %(note.id,note.tag,note.memo))
            
    def search_notes(self):
        filter = input("请输入关键字:")
        notes = self.notebook.search(filter)
        self.show_notes(notes)
    
    def add_note(self):
        memo = input("请输入一个备注信息:")
        self.notebook.new_note(memo)
        print("新增备注成功!")
        
    def modify_note(self):
        id = input("请输入修改的备注id:")
        memo = input("请输入修改的备注memo:")
        tag = input("请输入修改的备注tag:")
        if memo:
            self.notebook.modify_memo(id,memo)
        if tag:
            self.notebook.modify_tag(id,tag)
            
    def quit(self):
        print("Bye!")
        sys.exit(0)
        
if __name__ == "__main__":
    Menu().run()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值