<jupyter_start><jupyter_text>随机点名<jupyter_code>import random
def random_choice_hacker(hackers):
    hacker=random.choice(hackers)
    return hacker
#加密
def encode_str(hackers=""):
    hackers_a=[]
    for hacker in hackers.split("\n"):
        hacker_a=[]
        for h in hacker:
            a=ord(h)
            hacker_a.append(str(a))
        hacker_a=",".join(hacker_a)
        hackers_a.append(hacker_a)
    hackers_a="\n".join(hackers_a)
    return hackers_a
#解密
def dencode_str(hackers_a=""):
    hackers=[]
    for hacker in hackers_a.split("\n"):
        hacker_c=[]
        for h in hacker.split(","):
            h=chr(int(h))
            hacker_c.append(h)
        hacker_c="".join(hacker_c)
        hackers.append(hacker_c)
    return hackers
hackers='''38472,21338
26446,24605,21473
26446,21338,38597
21608,24935,33521
35768,36335
21494,26062
26446,26149,31179
37057,26446,33418
26045,36828,23665
28504,36194
34081,38125,20043
38470,28147,39118
21016,29030,38451
26446,33620,26525
24352,26032,28982
26446,38160
32599,21531,20975
19969,19968
29579,22696
29579,23431,26480
26446,25104,26753
21331,20140,28207
29579,23425
26446,19982,20961
39640,19968,26041
38472,28751,40857'''

hacker=random_choice_hacker(dencode_str(hackers))
print(hacker)<jupyter_output><empty_output><jupyter_text>入门练习 重点掌握以下概念及代码练习<jupyter_code>#https://www.w3cschool.cn/python3/
    
# 变量 
'''Python中的变量不需要声明。每个变量在使用前都必须赋值，变量赋值以后该变量才会被创建。'''


# 条件控制
'''Python中if语句的一般形式'''



# 循环
'''Python中的循环语句 for 和 while'''

# 函数
'''Python 定义函数使用 def 关键字'''

def color():
    print("----")
    


# 类
'''Python从设计之初就已经是一门面向对象的语言，正因为如此，在Python中创建一个类和对象是很容易的。'''<jupyter_output><empty_output><jupyter_text>简单练习<jupyter_code>def color(input_image):
    print("配色...",input_image)
    main_color=[122,222,88]
    return main_color
    
def layout(image,color):
    print("布局...",image)
    poster=image+"_"+",".join([str(c) for c in color])
    return poster
 
def imageProcessing(input_image):
    print ("图像处理...",input_image)
    return input_image
    
image='test.png'
main_color=color(image)
image=imageProcessing(image)
layout(image,main_color)<jupyter_output><empty_output><jupyter_text>理解单例模式保证一个类仅有一个实例，并提供一个访问它的全局访问点<jupyter_code># 类的专有方法：
# __init__ : 构造函数，在生成对象时调用
# __call__: 函数调用

class Singleton(type):
    # __init__ : 构造函数，在生成对象时调用
    def __init__(self,class_name,class_bases,class_dic):
        super(Singleton,self).__init__(class_name,class_bases,class_dic)
        self.__instance=None

    # __call__: 函数调用
    def __call__(self, *args, **kwargs):
        if not self.__instance:
            obj=object.__new__(self)
            self.__init__(obj)
            self.__instance=obj
        return self.__instance

class MyAIDesign(object,metaclass=Singleton):
    def __init__(self):
        self.name='darksee.ai'
        self.version="0.1"
        print("--one --")

    def match_color(self,input_color=[0,0,0]):
        return [255,255,255]

    
    
    
ai1=MyAIDesign()
ai2=MyAIDesign()

print(ai1 is ai2)<jupyter_output><empty_output><jupyter_text>理解观察者模式观察者模式指的是一个对象（Subject）维持一系列依赖于它的对象（Observer），当有关状态发生变更时 Subject 对象则通知一系列 Observer 对象进行更新。在观察者模式中，Subject 对象拥有添加、删除和通知一系列 Observer 的方法等等，而 Observer 对象拥有更新方法等等。<jupyter_code># 泛化出“观察者”类
class Observer:
    def update(self):
        pass
 
 
class Color(Observer):
    def update(self, action):
        print ("更新配色规则: %s" % action)
        self.run()
    
    def run(self):
        print("配色...")
 
 
class Layout(Observer):
    def update(self, action):
        print ("更新布局规则: %s" % action)
        self.run()
    
    def run(self):
        print("布局...")
 
 
class ImageProcessing(Observer):
    def update(self, action):
        print ("更新图像处理规则: %s" % action)
        self.run()
    
    def run(self):
        print ("图像处理...")
 
 
# Subject 对象拥有添加、删除和通知一系列 Observer 的方法等等
class Subject:
    observers=[]
    action=""
    def add_observer(self,observer):
        self.observers.append(observer)
    def notify_all(self):
        for i,obs in enumerate(self.observers):
            print("\nSTEP",i+1)
            obs.update(self.action)
            

class MyAIDesign(Subject):
    def set_action(self,action="默认"):
        self.action=action
    def start_design(self):
        return True

#配色
color=Color()
#排版
layout=Layout()
#图像处理，图像裁切、图像的色彩调整……
image_processing=ImageProcessing()
 

darksee_ai=MyAIDesign()
darksee_ai.add_observer(image_processing)
darksee_ai.add_observer(color)
darksee_ai.add_observer(layout)

darksee_ai.notify_all()

# 类继承
class DerivedClassName(Base1, Base2, Base3):
    <statement-1>
    .
    .
    .
    <statement-N>
    

class Singleton(type):
    def __init__(self,class_name,class_bases,class_dic):
        super(Singleton,self).__init__(class_name,class_bases,class_dic)
        self.__instance=None

    def __call__(self, *args, **kwargs):
        if not self.__instance:
            obj=object.__new__(self)
            self.__init__(obj)
            self.__instance=obj
        return self.__instance  

# 泛化出“观察者”类
class Observer:
    def update(self):
        pass
 
 
class Color(Observer):
    def update(self, action):
        print ("触发配色: %s" % action)
        self.run()
    
    def run(self):
        print("配色...")
 
 
class Layout(Observer):
    def update(self, action):
        print ("触发布局: %s" % action)
        self.run()
    
    def run(self):
        print("布局...")

        
class ImageProcessing(Observer):
    def update(self, action):
        print ("触发图像处理: %s" % action)
        self.run()
    
    def run(self):
        print ("图像处理...")
 

 
# Subject
class Subject:
    observers=[]
    action=""
    def add_observer(self,observer):
        self.observers.append(observer)
    def notify_all(self):
        for i,obs in enumerate(self.observers):
            print("\nSTEP",i+1)
            obs.update(self.action)
            
class MyAIDesign(Subject,object,metaclass=Singleton):
    def __init__(self):
        self.name='darksee.ai'
        self.version="0.1"
        print("初始化：",self.name,self.version)
    
    def set_action(self,action):
        self.action=action
    def start_design(self):
        return True

    def match_color(self,input_color=[0,0,0]):
        return [255,255,255]
color=Color()
layout=Layout()
image_processing=ImageProcessing()
 
darksee_ai=MyAIDesign()
darksee_ai.add_observer(image_processing)
darksee_ai.add_observer(color)
darksee_ai.add_observer(layout)

if darksee_ai.start_design():
    darksee_ai.set_action("主色不变")
    darksee_ai.notify_all()

darksee_ai.set_action("人物主题")
darksee_ai.notify_all()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13182987740275465, 'fraction_numerical': 0.06023914030573634, 'mean_word_length': 3.3445101906640367, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '163987', 'license': 'Apache-2.0', 'n_tokens_mistral': 2945, 'n_tokens_neox': 2765, 'n_words': 374, 'path': 'notebook/.ipynb_checkpoints/Level01-checkpoint.ipynb', 'repo_name': 'shadowcz007/Hacking-Design-AI-2020', 'size': '6607'}