用单位表示数字的python库

python-units的Python项目详细描述


用单位表示数字的python库

示例:

importunitsasuprintu.Unit(1,u.metre)

即将推出的功能

  • 短单位和长单位(目前只有短单位)
  • 乘数,如千(K)、兆(M)、基比(Kib)、ergs等。
  • 任意单位,如位(b)、车等。

功能

  • 基本单位
importunitsasufromrandomimportrandint# ampereprintu.Unit(randint(1,100),u.ampere)# candelaprintu.Unit(randint(1,100),u.candela)# kelvinprintu.Unit(randint(1,100),u.kelvin)# kilogramprintu.Unit(randint(1,100),u.kilogram)# metreprintu.Unit(randint(1,100),u.metre)# moleprintu.Unit(randint(1,100),u.mole)# secondprintu.Unit(randint(1,100),u.second)
  • 机组运行
importunitsasufromrandomimportrandomx=u.Unit(random(),u.second)y=u.Unit(random(),u.ampere)# additionprintx+yprinty+x# subtractionprintx-yprinty-x# multiplicationprintx*yprinty*x# division/true divisionprintx/yprinty/x# floor divisionprintx//yprinty//x# modulusprintx%y# powerprintx**yprinty**x
  • 标量和单位之间的运算
importunitsasufromrandomimportrandomx=u.Unit(random(),u.second)a=random()*10# additionprinta+yprinty+a# subtractionprinta-yprinty-a# multiplicationprinta*yprinty*a# division/true divisionprinta/yprinty/a# floor divisionprinta//yprinty//a# modulusprinta%y# powerprinta**yprinty**a
  • 导出单位
importunitsasufromrandomimportrandom# hertzprintu.Unit(random()*10,u.hertz)# newtonprintu.Unit(random()*10,u.newton)# pascalprintu.Unit(random()*10,u.pascal)# jouleprintu.Unit(random()*10,u.joule)# wattprintu.Unit(random()*10,u.watt)# coulombprintu.Unit(random()*10,u.coulomb)# voltprintu.Unit(random()*10,u.volt)# faradprintu.Unit(random()*10,u.farad)# ohmprintu.Unit(random()*10,u.ohm)# siememsprintu.Unit(random()*10,u.siemens)# weberprintu.Unit(random()*10,u.weber)# teslaprintu.Unit(random()*10,u.tesla)# henryprintu.Unit(random()*10,u.henry)# degree celciusprintu.Unit(random()*10,u.degree_celcius)# lumenprintu.Unit(random()*10,u.lumen)# luxprintu.Unit(random()*10,u.lux)# becquerelprintu.Unit(random()*10,u.becquerel)# grayprintu.Unit(random()*10,u.gray)# sievertprintu.Unit(random()*10,u.sievert)# katalprintu.Unit(random()*10,u.katal)
  • 拆包衍生单元
importunitsasufromrandomimportrandom# hertzprintu.Unit(random()*10,u.hertz).full_units# newtonprintu.Unit(random()*10,u.newton).full_units# pascalprintu.Unit(random()*10,u.pascal).full_units# jouleprintu.Unit(random()*10,u.joule).full_units# wattprintu.Unit(random()*10,u.watt).full_units# coulombprintu.Unit(random()*10,u.coulomb).full_units# voltprintu.Unit(random()*10,u.volt).full_units# faradprintu.Unit(random()*10,u.farad).full_units# ohmprintu.Unit(random()*10,u.ohm).full_units# siememsprintu.Unit(random()*10,u.siemens).full_units# weberprintu.Unit(random()*10,u.weber).full_units# teslaprintu.Unit(random()*10,u.tesla).full_units# henryprintu.Unit(random()*10,u.henry).full_units# degree celciusprintu.Unit(random()*10,u.degree_celcius).full_units# lumenprintu.Unit(random()*10,u.lumen).full_units# luxprintu.Unit(random()*10,u.lux).full_units# becquerelprintu.Unit(random()*10,u.becquerel).full_units# grayprintu.Unit(random()*10,u.gray).full_units# sievertprintu.Unit(random()*10,u.sievert).full_units# katalprintu.Unit(random()*10,u.katal).full_units
  • 任意导出单位
importunitsasuspeed=u.DerivedUnit.define('speed',metre/second)v=Unit(10,speed)
  • 任意自定义单位
from__future__importdivisionfromunitsimportBaseUnitclassCommUnit(BaseUnit):"""Template class for communication units"""def__init__(self,*args,**kwargs):super(CommUnit,self).__init__(*args,**kwargs)# redefine the base unitsself.unit_dict={'b':0,'s':0,'B':0,}@classmethoddefdefine(cls,key,value=1):"""Constructor"""obj=cls()assertkeyinobj.unit_dict.keys()assertisinstance(value,int)obj.unit_dict[key]=valuereturnobj

将按如下方式使用

bit=CommUnit.define('b')# define a bit as referring to the 'b' unitsecond=CommUnit.define('s')# a second is 's'data=Unit(32,bit)T=Unit(4,second)# data rateprintdata/T# 8.0 b·s^-1 - bits per second

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
orm如何在Java中实现规范化映射?   java以编程方式修改Xtext生成的Mwe2工作流   java正在覆盖外部文件中的现有数据,如何停止?   java在ANTLR BNF语法符号中epsilon的等价物是什么?   java如何使用Hibernate@Anyrelated注释?   代码生成生成java类并在运行时加载它   java Maven无法在本地jar文件中收集依赖项   java NetBeans IDE 8.2不显示错误消息   java Selenium web驱动程序找不到元素   java如何修复“拒绝访问属性”invoke“”的权限?   JavaApacheJClouds,从比日期更早的blob中删除blob的最佳方法   java如何比较和排序树集中的项目?   使用JavaSpring和无头/解耦CMS   java使用swagercodegen在不同的类中生成端点   Java外部Keylistener没有响应