# Encoding = utf8
_ Author _ = 'admin'
# If the file is run directly
If _ name __= = "_ main __":
Print ("Main ")
# How to perform Performance Testing
Import time
Def timer (reptimes, func, * pargs, ** kargs ):
Start = time. Clock ()
For I in range (reptimes ):
Func (* pargs, ** kargs)
Return time. Clock ()-start
Def test (test ):
[I for I in range (TEST)]
Print (timer (100, test, 100 ))
# String formatting
Print ("{0}-{1}-{2}". Format (1, 1 ))
Print ("{name}-{age}". Format (name = "name", age = "Age "))
# Replace LINQ
X = range (10)
Print (STR (x ))
# Fastest parsing expression
X1 = [I for I in X if I % 2 = 0]
Print (X1)
X2 = x [0: 2]
Print (X2)
X3 = List (MAP (lambda I: I % 2 = 0 and I or none), x ))
Print (X3)
X4 = List (filter (lambda I: I % 2 = 0, x ))
Print (X4)
# Common classes
Class person (object ):
Def _ init _ (self, name, age ):
Self. _ name = Name
Self. _ age = Age
@ Property
Def name (Self ):
Return self. _ name
@ Name. setter
Def name (self, value ):
Self. _ name = (Value + "...")
@ Name. deleter
Def name (Self ):
Raise runtimeerror ("no ")
@ Staticmethod
Def run (someone ):
If isinstance (someone, person ):
Print ("Run ")
Else:
Raise runtimeerror ("only people can run ")
Def _ STR _ (Self ):
Return "my name is" + self. _ name + "I'm" + STR (self. _ age)
John = person ("John", 11)
Print (John)
Person. Run (John)
# Person. Run (1)
John. Name = "John"
Print (John)
# Del John. Name
# Demonstrate inheritance
Class man (person ):
Def _ init _ (self, name, age, sex ):
Super (man, self). _ init _ (name, age)
Self. Sex = sex
Def _ STR _ (Self ):
Return super (man, self). _ STR _ () + self. Sex
Man = man ("man", 21, "male ")
Print (man)
From ABC import abstractmethod, abstractproperty, abcmeta
# Abstract class
Class abclass ():
_ Metaclass __= abcmeta
@ Abstractmethod
Def abmethod (Self ):
Pass
@ Abstractproperty
Def abpr (Self ):
Pass
# Abentity = abclass ()
# demonstrate AOP
class trace (object):
def _ init _ (self, func):
self. func = func
def _ call _ (self, * ARGs, ** kwargs):
Print ("------ begin ------")
Print (Dir (self. func)
Print (self. func. _ code _)
self. func (* ARGs, ** kwargs)
Print ("---------- END ----------")
# demonstrate AOP
def mydecorator (func ):
def _ mydecorator (* pargs, ** kargs):
Print ("ffff")
res = func (* pargs, ** kargs)
return res
return _ mydecorator
Def complexdecorator (name ):
Def _ mydecorator (func ):
Def _ mydecorator (* pargs, ** kargs ):
Print (name)
Res = func (* pargs, ** kargs)
Return res
Return _ mydecorator
Return _ mydecorator
@ Complexdecorator ("BW ")
@ Mydecorator
@ Trace
Def demotrace ():
Print ("me ")
Demotrace ()