在 Python 中查找最接近的 E96 电阻值

问题

你想使用 python 以编程方式查找最接近给定精确值的 E24/E48/E96 电阻值。

解决方案

虽然有为该任务提供的在线工具,但能够将可脚本化函数集成到自定义软件中通常很有用。

我编写了 UliEngineering,一个仅 Python3 的电子库,除了许多其他有用功能外还包括电阻选择。

首先,安装 UliEngineering

然后,你可以使用 Resistor.py 中的函数计算最接近的值。

例如,如果我们想在 Python3 交互式 shell 中查找最接近 1269.5 Ω 的值:

nearest_resistor_example.py
>>> from UliEngineering.Electronics.Resistors import *
>>> nearest_resistor(1269.5, sequence=e96)
1270.0

要将值打印为可读字符串,你可以使用 EngineerIO 包:

uliengineering_format_example.py
>>> from UliEngineering.EngineerIO
>>> from UliEngineering.Electronics.Resistors import *
>>> auto_format(nearest_resistor, 1269.5, sequence=e96)
'1.27 kΩ'

注意 auto_format 需要 nearest_resistor 函数本身作为第一个参数,而不是调用该函数的结果。这是因为 Python3 类型注解用于告诉 auto_format nearest_resistor 返回单位为 Ω 的值。


Check out similar posts by category: Electronics, Python