The adorner generator generates and runs the Fibonacci iterator built-in function json and pickle

Source: Internet
Author: User
Tags ldap pow

Decorative Device

Adorner:
Definition: The essence is a function, (decorating other functions)
is to add additional functionality to other functions
Principle: 1. Cannot modify the source code of the decorated function
2. Cannot modify the calling method of the decorated function
(meaning I changed a man to a woman, but he didn't know it)

Implement the Adorner Knowledge Reserve:
1. The function is "variable"
2. Higher-order functions
3. Nesting functions
Higher order high number + nested function = = "Decorator

--------------------------------------------------------I'm a cute split-line--------------------------------------------------------- -------------

# Author:james

# # #函数据变量

# wrong function not defined
#def foo ():
# Print ("in the Foo")
# Bar () function
#foo ()

# It's right.
# def bar ():
# # Print ("in the Bar")
# def foo ():
# # Print ("in the Foo")
# # Bar ()
# # foo ()

# # Is this OK? I know the code in Java is a line down, and Python is a
# def foo ():
# Print ("in the Foo")
# Bar ()
# def bar ():
# Print ("in the Bar")
# foo () probably because the function foo is in the last

# There's a reason to guess (wrong)
#def foo ():
# Print ("in the Foo")
# Bar ()
#foo () It could be Foo. This function ended prematurely, so I couldn't find bar.
#def Bar ():
# Print ("in the Bar")

#匿名函数
#calc = Lambda x:x*3
#print (Calc (3))

--------------------------------------------------------I'm a cute split-line--------------------------------------------------------- -------------

 # author:james 
#import Time
# High-order function
# A very round example
# def bar ():
# Print ("in the Bar")
#
# def TES T (func):
# Print (func)
# func () #加上这个func相当于 func = Bar
# test (bar) #内存地址 ...


# decorated
# def bar ():
# Print ("in the Bar")
#
# def Test (func): #装饰器
# start_time = Ti Me.time ()
# func () #加上这个func相当于 func = bar so that's Bar
# stop_time = Time.time ()
# Print ("The Func run Ti Me is%s "% (start_time--stop_time))
#test (bar) #内存地址:
#bar () #附加了一个时间统计时间

-------------------------------------------------------- I'm a lovely split line----------------------------------------------------------------------

Import Time
def bar ():
Time.sleep (3)
Print ("in the Bar")
def test2 (func):
Print (func) #func等于bar的内存地址
return func #又返回了bar的内存地址了
#print (test2 (bar)) #bar传进去 func equals the memory address of Bar
#t =test2 (bar)
#print (t)
#t () #可以
# Use bar to override
Bar = test2 (bar)
Bar ()

# Author:james
#嵌套函数

def foo ():
Print ("In the Foo")
def bar ():
# (nested with def defined in a function) The local variable attribute cannot be called externally
Print ("In the Bar")
Bar ()
Foo ()

#这个不叫嵌套
#def test1 ():
# Test2 () # This is called calling not nesting
#test1 ()

--------------------------------------------------------I'm a cute split-line--------------------------------------------------------- -------------

# Author:james
Import time
user,passwd = ' James ', ' qwe123 '
def auth (auth_type):
Print ("What is inside the auth inside func?") : ", Auth_type)
def outer_wrapper (func):
def wrapper (*args, **kwargs):
Print ("wrapper func args?") ", *args, **kwargs)
if Auth_type = = "Local":
Username = input ("username:"). Strip ()
Password = input ("Password:"). Strip ()
If user = = Username and passwd = = password:
Print ("\033[32;1muser has passed authentication\033[0m")
res = func (*args, **kwargs) # from home, function ends here
Print ("---after Authenticaion")
return res
Else
Exit ("\033[31;1minvalid username or password\033[0m")
elif Auth_type = = "LDAP":
Print ("LDAP")

Return wrapper
Return Outer_wrapper

def index ():
Print ("Welcome to Index page")
@auth (auth_type= "local") # home = wrapper ()
Def home ():
Print ("Welcome to Home Page")
Return "From Home"

@auth (auth_type= "LDAP")
Def BBS ():
Print ("Welcome to BBS page")

Index ()
Print (Home ()) #相当于调用wrapper
BBS ()
--------------------------------------------------------I'm a cute split-line--------------------------------------------------------- -------------
# Author:james
Import Time #时间统计
# def deco (func): #这里要传参
# start = Time.time () #开始时间
# func ()
# stop = Time.time () #结束时间
# print ("The Func Run is%s"% (Start-stop))
# def test1 ():
# print ("in the Test1")
# def test2 ():
# Time.sleep (3)
# print ("in the Test2")
# Deco (test1)
# Deco (TEST2)

# #这就在没动test1和test2的代码情况下加了附属功能

#高阶函数和嵌套
def timer (func): #timer (TSET1) func=test1 func= memory address
def deco (name,age): #也可以用 *args **kwargs
Start = Time.time () #开始时间 func (name,age) #直接运行 run test1 stop = time.time () #结束时间
Print ("The Func Run is%s"% (Start-stop))
Return deco# back
# @timer # Referencing the timer method
# def test1 ():
# Time.sleep (3) #睡三秒
# print ("in the Test1")
@timer # wrong because test2 equals timer (test2) test2 Fanc equals test2
#deoc直接返回内存地址, so TEST2=DEOC test2 () =DEOC (), so to pass parameters in Func
def test2 (name,age):
Print ("Test2:", Name,age)
# test1 ()
Test2 ("James", 22)
Fibonacci and generator initial knowledge exception handling

 # Author:james 
#斐波那契 and generator first Knowledge exception handling
Def FBNQ (max): # ten
N,a,b = 0,0,1
While n < max : # n<10
#print (b) #每次循环就打印b
Yield B
A, b = b,a+b
# a,b=1, 2
# t = (b,b+a)
#意思是 a=b b+a
N = n + 1
Return "Done"

#a = f BNQ (Ten)
G = FBNQ (6)
while True:
Try:
x = Next (g)
Print ("G:", X)
except Stopiterat Ion as E:
Print ("Generator Reurn value:", E.value)
Break
# print (a.__next__ ())
# print ("Here you can do some Things can go in and out ")
# Print (a.__next__ ())
# Print (a.__next__ ())
# Print (a.__next__ ())
# Print (a.__next__ ())
# Print (a.__next__ ())
# Print (a.__next__ ())
# Print (a.__next__ ())
# Print (a.__next__ ())
# Print (a.__ Next__ ())
#
Print ("=====start loop====")
# for I in a:
# print (i)

# Author:james
# Generator and run count
#凡是可作用于for循环的对象都是Iterable类型 (iterator)
# The iterator type is inert (genetic) that acts on the next () object.
# collection data types such as List dict str etc. are iterable but not iterator, you cannot get a iterator object through the ITER () function

--------------------------------------------------------I'm a cute split-line--------------------------------------------------------- -------------


Generator and run


Import time
DEF consumer (name):
Print ("%s ready to eat buns!") "%name)
While True:
Baozi = yield
Print ("The bun came [%s] came, was [%s] ate"% (baozi,name))
# c = Consumer ("Simon Chicken")
# c.__next__ ()
# B1 = "Red bean paste"
# C.send (B1)
# c.__next__ ()
def producer (NAEM):
c = Consumer ("a") #第一步先声明
C2 = Consumer ("B")
c.__next__ () #第二步
C2.__next__ ()
Print ("Lao Tzu began to prepare buns!") ")
For I in range: #第三步 loop
Time.sleep (1)
Print ("Made two buns!") ")
C.send (i)
C2.send (i)

Producer ("James")
--------------------------------------------------------I'm a cute split-line--------------------------------------------------------- -------------
Built-in functions
# Author:james
#python中自带的函数

# anonymous functions
# def SAYBI (n): # sample version
# print (n)
# Saybi (3)

#pow What and what many times side

# (Lambda N:print (n)) (5) #表态的写法

#calc = lambda n:print (n) #这里不可以for, but can be used if or ternary
#calc (5)

# calc = Lambda N:3 if n<5 else n
# Print (Calc (55))

#filter Filtration
# res = filter (lambda n:n>5,range (10))
# for I in Res:
# Print (i)

#不可变集合跟元组一样 not variable
#a = Frozenset ([1,1,1,1,1,1,15,415,4568,1561])

Mode of #整个程序的所有变得格式换为K V
# Efficient binary Find
#print (Globals ())

#不常用 locals
# def Test ():
# local = 333
# print (Locals ())
# test ()
# print (Locals ())
# print (Locals (). Get ("Local_var"))

# POW What's what's two Times Square
# round to a decimal point

#字典是无序的, sorted by sorted, sorted by K list
#value来排序也可以的
#a = {6:2,8:0,1:4,-5:6,99:11,4:22}
#print (Sorted (A.items ()))
#print (Sorted (A.items (), Key=lambda x:x[1]))
#print (a)

# Zip (Zip)
# a = [1,2,3,4,5]
# b = ["A", "B", "C", "D", "E"]
# for I in Zip (A, B):
# Print (i)

#import Decorator
__import__ (' decorator ')
                                        
--------------------------------------------------------I'm a cute split-line--------------------------------------------------------- -------------Top Top                                             
JSON and PICKLE

Instantiation of

# Author:james
#import JSON
Import Pickle
def sayhi (name):
Print ("Hello", name)
info = {
"Name": "James",
"Age": 22,
"Func": Sayhi

}
f = open ("Test.text", "WB")
Pickle.dump (info,f) #f. Write (Pickle.dumps (info))

F.close ()
--------------------------------------------------------I'm a cute split-line--------------------------------------------------------- -------------    

Anti-instantiation

# Author:james
#import JSON
#json和pickle一样的 difference in the invocation of the time a little more s
Import Pickle
def sayhi (name):
Print ("Hello", name)
f = open ("Test.text", "RB")
#data = Pickle.loads (F.read ())
data = Pickle.load (f)
Print (data["func"] ("James"))

--------------------------------------------------------I'm a cute split-line--------------------------------------------------------- -------------   
Instantiation 2
# Author:james
Import JSON

def sayhi (name):
Print ("Hello", name)
info = {
"Name": "James",
"Age": 22,
# "Func": Sayhi

}
f = open ("Test2.text", "W")
F.write (Json.dumps (info))

info["Age"]=23
F.write (Json.dumps (info)) #dumps可以好多次但是loads这可以一次

F.close ()
--------------------------------------------------------I'm a cute split-line--------------------------------------------------------- -------------   
Anti-instantiation 2
# Author:james
Import JSON

f = open ("Test2.text", "R")
data = Json.loads (F.read ()) #dumps可以好多次但是loads这可以一次

Print (data["func"] ("James"))






The adorner generator generates and runs the Fibonacci iterator built-in function json and pickle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.