亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python?嵌套型partials的使用

 更新時(shí)間:2022年03月30日 14:16:38   作者:Moelimoe  
這篇文章主要介紹了python?嵌套型partials的使用,partial對(duì)象中包含partial對(duì)象的使用,下文更多詳細(xì)介紹需要的小伙伴可以參考一下

要實(shí)現(xiàn)的目標(biāo),簡(jiǎn)單示例:

from functools import partial
def func1(f):
? ? return f
def func2(f1):
? ? return f1
def func(n):
? ? return n

p = partial(func2, partial(func1, partial(func, 5)))
print(p()()())
# 輸出5

簡(jiǎn)化嵌套式的partial對(duì)象p,不要調(diào)用三次

p()
# 輸出5

可以到最后的看解決方法

場(chǎng)景:

為了實(shí)現(xiàn)一個(gè)通用性較高的sql生成方法,我寫了一個(gè)通用的轉(zhuǎn)換時(shí)間格式的方法,簡(jiǎn)略版大概如下:

def date_trunc(time_unit: str, field):
? ? return f'date_trunc("{time_unit}", `{field}`)'


print(date_trunc("WEEK", "event_date"))
print(date_trunc("DAY", "event_date"))
...

實(shí)際就是sql中的date_trunc方法

輸出:

date_trunc("WEEK", `event_date`)
date_trunc("DAY", `event_date`)

由于校驗(yàn)日期參數(shù)和日期截?cái)嗍乔昂蟀ぶ鴪?zhí)行的,我把上面的幾個(gè)方法寫進(jìn)了一個(gè)Enum對(duì)象TimeFormatter
使用partial將date_trunc方法包起來以充當(dāng)Enum的成員,實(shí)現(xiàn)用Enum類校驗(yàn)日期參數(shù),用Enum類成員的date_trunc方法執(zhí)行日期截?cái)?br />這樣在校驗(yàn)完日期參數(shù)后立馬調(diào)用它本身的date_trunc方法執(zhí)行日期截?cái)啵簣?zhí)行日期截?cái)?code>date_trunc方法時(shí)需要傳入?yún)?shù)time_unit,也就是"DAY", “WEEK”, "MONTH"等字符串

from enum import Enum
from functools import partial


def date_trunc(time_unit: str, field):?? ?# 注意這里的date_trunc和上面簡(jiǎn)略版舉例的有所不同,需要兩個(gè)參數(shù)
? ? return ?f'date_trunc("{time_unit}", `{field}`)'


class TimeFormatter(Enum):
? ? DAY = partial(date_trunc, "DAY")
? ? WEEK = partial(date_trunc, "WEEK")
? ? MONTH = partial(date_trunc, "MONTH")

? ? def __call__(self, *args, **kwargs):
? ? ? ? return self.value(*args, **kwargs)

這里的call方法讓Enum對(duì)象TimeFormatter的成員變得可以被調(diào)用(callable),關(guān)于Enum的一些用法可以參考這篇文章
到這里我依然可以正常調(diào)用我的date_trunc方法

field = "event_time"
tf_wk = TimeFormatter.__getattr__("WEEK")?? ?# 先校驗(yàn)格式
print(tf_wk(field))?? ??? ?# 傳入相應(yīng)的field對(duì)象就會(huì)執(zhí)行對(duì)應(yīng)的date_trunc方法截?cái)鄷r(shí)間
tf_day = TimeFormatter.__getattr__("DAY")?? ?# 校驗(yàn)格式
print(tf_day(field))?? ?# 執(zhí)行date_trunc

輸出:

date_trunc("WEEK", `event_time`)
date_trunc("DAY", `event_time`)

直到我想要使用二次的時(shí)間格式轉(zhuǎn)換時(shí),也就是在date_trunc之后再執(zhí)行一個(gè)from_timestamp將sql中的日期對(duì)象event_time轉(zhuǎn)換為指定的"yyyy-MM-dd"格式

from_timestamp(date_trunc("DAY", `event_time`), "yyyy-MM-dd")

發(fā)現(xiàn)好像沒那么順利地執(zhí)行時(shí)間格式轉(zhuǎn)換:

from enum import Enum
from functools import partial


def from_timestamp(field, time_fmt: str):
? ? return f'from_timestamp(`{field}`, "{time_fmt}")'


class TimeFormatter(Enum):
? ? HOUR = partial(from_timestamp, partial(date_trunc, "HOUR"))

? ? def __call__(self, *args, **kwargs):
? ? ? ? return self.value(*args, **kwargs)


tf_hour = TimeFormatter.__getattr__("HOUR")
print(tf_hour("event_hour"))

輸出:

from_timestamp(`functools.partial(<function date_trunc at 0x000002538E45E5E0>, 'HOUR')`, "event_hour")

不是想要的結(jié)果

查了一些解決辦法,有循環(huán)調(diào)用,有用組合函數(shù)(function composition)的,

最后發(fā)現(xiàn)可以用一個(gè)簡(jiǎn)單的方法解決:

from enum import Enum
from functools import partial


def date_trunc(time_unit: str, field):
? ? return f'date_trunc("{time_unit}", `{field}`)'


def from_timestamp(field, time_fmt: str):
? ? return f'from_timestamp(`{field}`, "{time_fmt}")'


def fts(time_fmt, time_unit, field):
? ? return from_timestamp(date_trunc(time_unit, field), time_fmt)


class TimeFormatter2(Enum):
? ? month = partial(fts, "yyyy-MM", "month")

? ? def __call__(self, *args, **kwargs):
? ? ? ? return self.value(*args, **kwargs)

輸出:

from_timestamp(`date_trunc("month", `acmonth`)`, "yyyy-MM")

焯!原來只要多寫一個(gè)函數(shù)就可以了!

前面簡(jiǎn)單示例的解決方法:

def nested_partials(f2, f1, n):
? ? return f2(f1(n))


p = partial(nested_partials, func2, func1)
print(p(5))

輸出:

5

到此這篇關(guān)于python 嵌套型partials的使用的文章就介紹到這了,更多相關(guān)python 嵌套型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論