第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python Click:全局訪問選項(xiàng)值

Python Click:全局訪問選項(xiàng)值

慕慕森 2023-11-09 21:55:46
假設(shè)我--debug/--no-debug為基本命令定義了一個(gè)標(biāo)志。該標(biāo)志將影響我的程序中許多操作的行為。現(xiàn)在我發(fā)現(xiàn)自己將此標(biāo)志作為函數(shù)參數(shù)傳遞到各處,這看起來并不優(yōu)雅。特別是當(dāng)我需要在深層調(diào)用堆棧中訪問此標(biāo)志時(shí),我必須將此參數(shù)添加到堆棧上的每個(gè)函數(shù)中。我可以創(chuàng)建一個(gè)全局變量is_debug,并在接收該標(biāo)志值的命令函數(shù)的開頭設(shè)置其值。但這對(duì)我來說似乎也不優(yōu)雅。是否有更好的方法可以使用 Click 庫使某些選項(xiàng)值全局可訪問?
查看完整描述

2 回答

?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊

有兩種方法可以實(shí)現(xiàn)此目的,具體取決于您的需要。他們最終都使用了 click?Context。

就我個(gè)人而言,我很喜歡選項(xiàng) 2,因?yàn)檫@樣我就不必修改函數(shù)簽名(而且我很少編寫多線程程序)。它聽起來也更像你正在尋找的東西。

選項(xiàng) 1:將上下文傳遞給函數(shù)

使用click.pass_context裝飾器將單擊上下文傳遞給函數(shù)。

文件:

# test1.py

import click



@click.pass_context

def some_func(ctx, bar):

? ? foo = ctx.params["foo"]

? ? print(f"The value of foo is: {foo}")



@click.command()

@click.option("--foo")

@click.option("--bar")

def main(foo, bar):

? ? some_func(bar)



if __name__ == "__main__":

? ? main()

$ python test1.py --foo 1 --bar "bbb"

The value of foo is: 1

選項(xiàng)2:click.get_current_context()

通過 直接從當(dāng)前線程中提取上下文click.get_current_context()。從 Click 5.0 開始可用。

文件:

注意:這僅在您位于當(dāng)前線程(與最初用于設(shè)置單擊命令的線程相同的線程)中時(shí)才有效。

# test2.py

import click



def some_func(bar):

? ? c = click.get_current_context()

? ? foo = c.params["foo"]

? ? print(f"The value of foo is: {foo}")



@click.command()

@click.option("--foo")

@click.option("--bar")

def main(foo, bar):

? ? some_func(bar)



if __name__ == "__main__":

? ? main()

$ python test2.py --foo 1 --bar "bbb"

The value of foo is: 1


查看完整回答
反對(duì) 回復(fù) 2023-11-09
?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊

我想讓它更加無縫,所以我將它與修改函數(shù)的全局范圍的技巧結(jié)合起來,并提出了以下裝飾器:

def with_click_params(func):

? ? @functools.wraps(func)

? ? def wrapper(*args, **kwargs):

? ? ? ? g = func.__globals__

? ? ? ? sentinel = object()


? ? ? ? ctx = click.get_current_context()

? ? ? ? oldvalues = {}

? ? ? ? for param in ctx.params:

? ? ? ? ? ? oldvalues[param] = g.get(param, sentinel)

? ? ? ? ? ? g[param] = ctx.params[param]


? ? ? ? try:

? ? ? ? ? ? return func(*args, **kwargs)

? ? ? ? finally:

? ? ? ? ? ? for param in ctx.params:

? ? ? ? ? ? ? ? if oldvalues[param] is sentinel:

? ? ? ? ? ? ? ? ? ? del g[param]

? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? g[param] = oldvalues[param]


? ? return wrapper

你可以像這樣使用它(從@dthor的答案借用示例):


@with_click_params

def some_func():

? ? print(f"The value of foo is: {foo}")

? ? print(f"The value of bar is: {bar}")



@click.command()

@click.option("--foo")

@click.option("--bar")

def main(foo, bar):

? ? some_func()



if __name__ == "__main__":

? ? main()

這是它的實(shí)際效果:


$ python test2.py --foo 1 --bar "bbb"

The value of foo is: 1

The value of bar is: bbb

注意事項(xiàng)

  • 函數(shù)只能從click原始調(diào)用堆棧調(diào)用,但這是一個(gè)有意識(shí)的選擇(即,您將對(duì)變量注入做出假設(shè))。點(diǎn)擊單元測(cè)試指南在這里應(yīng)該很有用。

  • 該函數(shù)不再是線程安全的。

也可以明確要注入的參數(shù)名稱:

def with_click_params(*params):

? ? def wrapper(func):

? ? ? ? @functools.wraps(func)

? ? ? ? def inner_wrapper(*args, **kwargs):

? ? ? ? ? ? g = func.__globals__

? ? ? ? ? ? sentinel = object()


? ? ? ? ? ? ctx = click.get_current_context()

? ? ? ? ? ? oldvalues = {}

? ? ? ? ? ? for param in params:

? ? ? ? ? ? ? ? oldvalues[param] = g.get(param, sentinel)

? ? ? ? ? ? ? ? g[param] = ctx.params[param]


? ? ? ? ? ? try:

? ? ? ? ? ? ? ? return func(*args, **kwargs)

? ? ? ? ? ? finally:

? ? ? ? ? ? ? ? for param in params:

? ? ? ? ? ? ? ? ? ? if oldvalues[param] is sentinel:

? ? ? ? ? ? ? ? ? ? ? ? del g[param]

? ? ? ? ? ? ? ? ? ? else:

? ? ? ? ? ? ? ? ? ? ? ? g[param] = oldvalue


? ? ? ? return inner_wrapper

? ? return wrapper



@with_click_params("foo")

def some_func():

? ? print(f"The value of foo is: {foo}")



@click.command()

@click.option("--foo")

@click.option("--bar")

def main(foo, bar):

? ? some_func()



if __name__ == "__main__":

? ? main()


查看完整回答
反對(duì) 回復(fù) 2023-11-09
  • 2 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)