GithubHelp home page GithubHelp logo

typefire's Introduction

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

typefire's People

Contributors

luxuncang avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

typefire's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.