# units **Repository Path**: pinpang_liao/units ## Basic Information - **Project Name**: units - **Description**: python class pressure temperature unit 单位换算 - **Primary Language**: Python - **License**: LGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-04-02 - **Last Updated**: 2022-10-18 ## Categories & Tags **Categories**: utils **Tags**: None ## README # units ## 介绍 基于 [python3](https://python.org/) `float` 基类派生的常用自然环境参数应用类。 使用相关类创建的实列在应用中以内部底层`float`值参与运算,打印输出(字符串化)根据类属性设置自行组合相应单位符号并四舍五入至对应小数位。 ## 安装教程 ```bash user@machine:/u/want/anywhere/$ git clone https://gitee.com/pinpang_liao/units.git ``` ## 使用说明 1. 基本用法 ```python Python 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import units >>> dir(units) ['CELSIUS', 'FAHRENHEIT', 'HPA', 'KPA', 'Pressure', 'Temperature', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'] >>> p = units.Pressure(102567.89) >>> p # 内部存储单位为 Pa 的浮点值 102567.89 >>> print(p) # 当前设置为输出 kPa 两位小数 102.57kPa >>> p.unit # 查看当前单位设置 'kPa' >>> p.unit = units.HPA # 修改设置为 hPa >>> print(p) # 修改后的字符输出 1025.68hPa >>> p.decimal # 当前的小数位数设置 2 >>> p.decimal = 3 # 保留三位小数输出 >>> print(p) # 当前设置为输出 hPa 三位小数 1025.679hPa ``` 2. 对于已实例化的环境参数对象,再次赋值将会丢失附加特性,正确的方法是**使用相关类再次实例化** ```python >>> p = units.Pressure(102567.89) >>> print(p) # 当前设置为输出 kPa 两位小数 102.57kPa >>> p = 101010.99 # 直接赋值就丢失所有附加特性 >>> print(p) # p 当前是 float 基类的实例 101010.99 >>> p = units.Pressure(101010.99) >>> print(p) # 上面才是正确姿势 101.01kPa ``` 3. 注意事项 由于使用了新的字符串格式化特性 `f"{variable}"` ,请在 python 3.6 以上环境使用,或者更改相关 `__str__` 方法以适应其它环境。 ## TODO - 酌情添加参数约束 - 适当增加内部可转换单位 - 增加更多常用单位