Finish it first!
pip install -U discord.py
裝飾器
def up(shot):
maps = shot2map(shot)
match = get_everything(shot, maps)
maps = move(match, shot, maps, [-1, 0])
shot = map2shot(maps, shot)
match = get_everything(shot, maps)
maps, shot = change(shot, maps)
shot = check_win(shot, match)
return shot
def down(shot):
maps = shot2map(shot)
match = get_everything(shot, maps)
maps = move(match, shot, maps, [1, 0])
shot = map2shot(maps, shot)
match = get_everything(shot, maps)
maps, shot = change(shot, maps)
shot = check_win(shot, match)
return shot
def left(shot):
maps = shot2map(shot)
match = get_everything(shot, maps)
maps = move(match, shot, maps, [0, -1])
shot = map2shot(maps, shot)
match = get_everything(shot, maps)
maps, shot = change(shot, maps)
shot = check_win(shot, match)
return shot
def right(shot):
maps = shot2map(shot)
match = get_everything(shot, maps)
maps = move(match, shot, maps, [0, 1])
shot = map2shot(maps, shot)
match = get_everything(shot, maps)
maps, shot = change(shot, maps)
shot = check_win(shot, match)
return shot
def init(func):
def with_logging(*args, **kwargs):
shot = args[0]
maps = shot2map(shot)
match = get_everything(shot, maps)
maps = move(match, shot, maps)
shot = map2shot(maps, shot)
match = get_everything(shot, maps)
maps, shot = change(shot, maps)
shot = check_win(shot, match)
return func(shot)
return with_logging
@init
def up(shot):
return shot
@init
def down(shot):
return shot
@init
def left(shot):
return shot
@init
def right(shot):
return shot
def logged(func):
def with_logging(*args, **kwargs):
print(func.__name__)
return func(args[0], args[0]*2)
return with_logging
@logged
def f(x,y):
return x**y
@logged
def g(x,y):
return x*y
print(f(3))
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
def lcm(a,b):
c = gcd(a,b)
return b*a//c
def f(x,y):
a = gcd(x,y)
b = lcm(x,y)
return a**b
def g(x,y):
a = gcd(x,y)
b = lcm(x,y)
return a*b
x = int(input())
y = int(input())
print(f(x,y))
print(g(x,y))
同步 VS 異步
server = Server()
result = server.handle(task) #花很多時間
print(result)
server = Server()
server.send(task) #先送工作
while not server.ok(): #檢查工作完成與否
do_other_thing() #還沒完成前可以先做其他事
result = server.recv()
print(result)
import asyncio
async def f(done, a):
while not done[a-1]:
await asyncio.sleep(1)
done[a] = 1
return
loop = asyncio.get_event_loop()
tasks = []
done = [1,0,0,0]
l = [3,1,2]
for i in l:
tasks.append(loop.create_task(f(done, i)))
loop.run_until_complete(asyncio.wait(tasks))
print(done)
日記bot
async def add_diary(ctx, date, title, content):
圖片管理系統+自訂功能
@bot.command()
async def upload(ctx):
response = requests.get(ctx.message.attachments[0].url)
file = open("sample_image.png", "wb")
file.write(response.content)
file.close()
@bot.command()
async def show_pic(ctx):
with open('sample_image.png', 'rb') as f:
picture = discord.File(f)
await ctx.send(file = picture)