Skip to content

基于类型注解的自定义类型转换,可用于拓展 fire 解析类型

License

Notifications You must be signed in to change notification settings

luxuncang/typefire

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TypeFire

CodeFactor GitHub CodeQL

通过装饰器零入侵性的,对 fire-python 的类型约束进行拓展,此外其还适用于任何函数对象进行基于注解的类型转换

安装

pip install typefire

特性

  • 支持注解类型转换
  • 支持字符串类型注解
  • 支持 async/await
  • 零入侵性
  • 支持任意对象

Fire

from pathlib import Path
import fire


def test_fire(path: Path):
    print(type(path))
    print(path)


if __name__ == "__main__":
    fire.Fire(test_fire)

'''
python test_fire.py "/some_path_string"

<class 'str'>
/some_path_string
'''

ps: fire #260

TypeFire

from typefire import Switch, TypeFire, typefire, typeswitch
import fire
import pathlib

TypeFire.add_switch(Switch(str, pathlib.Path, lambda p: pathlib.Path(p)))

@typeswitch
def main(path: pathlib.Path, *args, **kwargs):
    print(path, type(path))

fire.Fire(main)

'''
python test_fire.py "/some_path_string"

/some_path_string <class 'pathlib.WindowsPath'>
'''
from typefire import Switch, TypeFire, typefire, typeswitch
import fire
import pathlib

TypeFire.add_switch(Switch(str, pathlib.Path, lambda p: pathlib.Path(p)))

@typefire
def main(path: pathlib.Path, *args, **kwargs):
    print(path, type(path))

main("/some_path_string")
main("--path=/some_path_string")

typeswitch

案例一:

from typing import Union

def main(data: Union[bytes, Path, str]) -> bytes:
    if isinstance(data, bytes):
        ...
    elif isinstance(data, Path):
        ...
    elif isinstance(data, str):
        ...

案例二:

from typefire import Switch, typeswitch, TypeFire
from pathlib import Path

TypeFire.add_switch(Switch(str, bytes, lambda x: open(x, 'rb').read()))
TypeFire.add_switch(Switch(Path, bytes, lambda x: open(x, 'rb').read()))

@typeswitch()
def main(data: bytes):
    print(type(data))

main(Path('test.py'))
main('test.py')
main(open('test.py', 'rb').read())

'''
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
'''

显而易见 使用 typeswitch 显的更优雅,复用性更强

qs: test

About

基于类型注解的自定义类型转换,可用于拓展 fire 解析类型

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages