Beispiel #1
0
employee_new = [{
    'Name': employee['Name'],
    'Salary': employee['Salary'] + 200
} if employee['Salary'] > 150 else {
    'Name': employee['Name'],
    'Salary': employee['Salary'] + 500
} for employee in employee_old]
"""
# employees_new = [
#     {'name': employee['name'], 'salary': employee['salary'] + 200} if employee['salary'] > 5000 else
#     {'name': employee['name'], 'salary': employee['salary'] + 500} for employee in employee_old]
"""
#print(employees_new)
print(employee_new)

print_info("字典推导式")
dict_old = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}
dict_new = {value: key for key, value in dict_old.items()}
print(dict_new)

print_info("集合推导式:典型用法 -- 去重")
list_old = [1, 2, 3, 5, 2, 3]
set_new = {x for x in list_old}
print(set_new)

print_info("生成器")
my_generator = (x for x in range(5))  # 注意是()
print(my_generator)
print(type(my_generator))
print('a')
print(my_generator.__next__())
Beispiel #2
0
from tools.print_info import print_info


def say(func):
    def wrapper(*args, **kwargs):
        func(*args, **kwargs)

    return wrapper


@say
def greet(name):
    print("Hello {}".format(name))


print_info("丢失原始函数信息")
print(greet.__name__)

print_info("想要不丢失原始函数信息可以用@functools.wrapper")

import functools
import time


def timer(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.perf_counter()
        value = func(*args, **kwargs)
        end_time = time.perf_counter()
        run_time = end_time - start_time
Beispiel #3
0
from tools.print_info import print_info

print_info('*args和**kwargs 接收参数')


def say(func):

    def wrapper(*args, **kwargs):
        func(*args, **kwargs)
    return wrapper


@say
def greet(name):
    print("Hello {}".format(name))


greet("xiaojing")


print(greet.__name__)
Beispiel #4
0
from tools.print_info import print_info

print_info('装饰器嵌套')


def hello(func):
    def wrapper():
        print("Hello")
        func()

    return wrapper


def welcome(func):
    def wrapper():
        print('Welcome')
        func()

    return wrapper


def say():
    print('Good')


say = hello(welcome(say))
say()

print_info("使用语法糖")

Beispiel #5
0
from tools import print_info as info


info.print_info("手动实现装饰器")
def my_decorator(my_func):

    def my_wrapper():
        print('Before the function runs')

        my_func()

        print('After the function runs')

    return my_wrapper


def my_func():
    print('I am a stand alone function, don\'t you dare modify me')


# my_func()
my_func_decorator = my_decorator(my_func)
my_func_decorator()

info.print_info("语法糖")

# 使用语法糖
@my_decorator
def my_another_func():
    print('Leave me alone')
import functools
import time
from tools.print_info import print_info

print_info("装饰方法")


def timer(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start_time = time.perf_counter()
        value = func(*args, **kwargs)
        end_time = time.perf_counter()
        run_time = end_time - start_time
        # print(func.__name__)
        # print(run_time)
        print("Finished {} in {} s".format(repr(func.__name__),
                                           round(run_time, 3)))
        return value

    return wrapper


class Calculator:
    def __init__(self, num):
        self.num = num

    @timer
    def doubled_and_add(self):
        res = sum([i * 2 for i in range(self.num)])
        print("Result : {}".format(res))