• 个人简介

    潮起...潮落...

    import time
    import msvcrt
    import threading
    import logging as log
    import base64 as b64
    def logset():
        lt=time.localtime()
        log.basicConfig(level=log.DEBUG,filename=f"{lt[0]}_{lt[1]}_{lt[2]}_{lt[3]}_{lt[4]}.log")
    def PNG_B64(target="target.png",output="op.txt"):
        '''
    png文件转Base64,主体代码参考stu231311(感谢开源)
    指定target,output来控制输入图片路径与输出
    默认输入:target.png 默认输出:op.txt'''
        try:
            with open(target, "rb") as f:
                img_data = f.read()
            b64_bytes = b64.b64encode(img_data)
            b64_str = b64_bytes.decode('utf-8')
            with open(output,"w") as f:
                f.write("data:image/png;base64,"+b64_str)
            print(f"转换完成! 输出文件:{output}")
        except FileNotFoundError as err:
            log.error(f"未找到目标文件! {target}")
    def tprint(string:str,sec:int|float=0.02):
        '''
    按时间逐字打印,输入包括string(str),sec(int,float)
    '''
        for i in string:
            print(i,end='',flush=True)
            time.sleep(sec)
        print()
    def standby():
        '''
    press any key to continue(no return)
    only workable in python.exe
    '''
        cagit=['|','/','-','\\'];i=0
        while True:
            print('\r'+cagit[i]+"   press any key to quit...",end='',flush=True)
            i=(i+1)%len(cagit)
            time.sleep(0.3)
            if msvcrt.kbhit():
                break
    def docseeker():
        '''
    用于输出当前自定义函数的所有注释(no return)
    '''
        a=globals()
        for name in a:
            if callable(a[name]) and a[name].__module__==__name__:
                print('='*20+str(name)+'='*20)
                print(a[name].__doc__)
    class NiimiSora:    #类名
        '''
    9-nine-ここのつここのかここのいろ
    白泉學園1年生
    '''#芝不仅仅士注释,试试NiimiSora.__doc__
        name=["新海天","にいみ そら","新海天","Niimi Sora"]   #类属性,在外部调用需加上<类名>.类属性
        lang={"simcn":0,"jap":1,"tricn":2,"eng":3}
        grade=1
        __hide="あたしのアルバム、見たでしよ..."#这是一个隐藏属性,不能在类外直接调用
        def __init__(self):    #类的特殊方法(构造方法),在类被调用时自动执行
            tprint("お兄ちゃん大好き~✩")   #self是类的方法所必须包含的第一个参数,其名字可以是任何东西,但必须存在一个self参数<划掉>当然你想要麻烦点我也没意见<划掉>
        def pname(self,language="simcn",secret=False):    #这是一个类的方法
            na=self.name[self.lang[language]]    #self 代表类的实例,而非类
            tprint(na+"~")
            if secret:
                self.__secret()
        def __secret(self):#这是一个隐藏方法,不能在类外直接调用
            tprint(self.__hide)
            log.info('调用了__secret')
    logset()
    niimi=NiimiSora()    #实例化类
    niimi.pname(language="simcn")# 访问类的属性和方法
    try:
        niimi.__secret()
    except AttributeError:
        log.critical('不能在外部直接调用__secret')
    try:
        a=niimi.__hide
    except AttributeError:
        log.critical("不能在外部直接调用__hide")
    print(niimi._NiimiSora__hide)    #这样是可以访问的。不过既然要访问为什么给它隐藏起来?
    
    
  • 最近活动

    This person is lazy and didn't join any contests or homework.