这篇文章主要介绍“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怎么自动判断输入参数类型”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/219841.html