温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python怎么自动判断输入参数类型

发布时间:2021-12-17 17:34:52 来源:亿速云 阅读:501 作者:iii 栏目:大数据

这篇文章主要介绍“Python怎么自动判断输入参数类型”,在日常操作中,相信很多人在Python怎么自动判断输入参数类型问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python怎么自动判断输入参数类型”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

直接通过装饰器@typic.al,然后自动根据 Python3 的注解功能自动判断输入参数类型。

示例:

>>> import typic
>>>
>>> @typic.al
>>> def multi(a: int, b: int):
...    return a * b
...
>>> multi('2', '3')
6

>>> class DuckRegistry:
...     """A Registry for all the ducks"""
...
...     @typic.al
...     def __init__(self, *duck: Duck):
...         self._reg = {x.name: x for x in duck}
...
...     @typic.al
...     def add(self, duck: Duck):
...         self._reg[duck.name] = duck
...
...     @typic.al
...     def find(self, name: str):
...         """Try to find a duck by its name. Otherwise, try with type."""
...         if name not in self._reg:
...             matches = [x for x in self._reg.values() if x.type == name]
...             if matches:
...                 return matches[-1] if len(matches) == 1 else matches
...         return self._reg[name]
...
>>> registry = DuckRegistry({'type': 'black', 'name': 'Daffy'})
>>> registry.find('Daffy')
Duck(type=<DuckType.BLK: 'black'>, name='Daffy')
>>> registry.add({'type': 'white', 'name': 'Donald'})
>>> registry.find('Donald')
Duck(type=<DuckType.WHT: 'white'>, name='Donald')

>>> registry.add({'type': 'goose', 'name': 'Maynard'})
Traceback (most recent call last):
...
ValueError: 'goose' is not a valid DuckType

Python 让使用 ffmpeg 处理视频文件变得更简单,比如把

ffmpeg -i input.mp4 -i overlay.png -filter_complex "[0]trim=start_frame=10:end_frame=20[v0];\
   [0]trim=start_frame=30:end_frame=40[v1];[v0][v1]concat=n=2[v2];[1]hflip[v3];\
   [v2][v3]overlay=eof_action=repeat[v4];[v4]drawbox=50:50:120:120:red:t=5[v5]"\
   -map [v5] output.mp4
 

变为:

import ffmpeg

in_file = ffmpeg.input('input.mp4')
overlay_file = ffmpeg.input('overlay.png')
(
   ffmpeg
   .concat(
       in_file.trim(start_frame=10, end_frame=20),
       in_file.trim(start_frame=30, end_frame=40),
   )
   .overlay(overlay_file.hflip())
   .drawbox(50, 50, 120, 120, color='red', thickness=5)
   .output('out.mp4')
   .run()
)

到此,关于“Python怎么自动判断输入参数类型”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI