Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 21, 2024
1 parent 65b0179 commit 9c3e557
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 213 deletions.
12 changes: 3 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@

def which(program):
def is_exe(fpath):
return (
os.path.exists(fpath)
and os.access(fpath, os.X_OK)
and os.path.isfile(fpath)
)
return os.path.exists(fpath) and os.access(fpath, os.X_OK) and os.path.isfile(fpath)

def ext_candidates(fpath):
yield fpath
Expand Down Expand Up @@ -79,7 +75,7 @@ def ext_candidates(fpath):
# 'sphinx.ext.intersphinx',
# 'sphinx.ext.coverage',
"sphinx.ext.mathjax",
"sphinx.ext.autosectionlabel"
"sphinx.ext.autosectionlabel",
# 'sphinx.ext.viewcode',
# 'sphinx.ext.githubpages',
# 'sphinx.ext.napoleon',
Expand Down Expand Up @@ -204,9 +200,7 @@ def ext_candidates(fpath):
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, "units.tex", "Units Library Documentation", "Philip Top", "manual")
]
latex_documents = [(master_doc, "units.tex", "Units Library Documentation", "Philip Top", "manual")]

# -- Options for manual page output ---------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion python/units_llnl/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .units_llnl_ext import unit, measurement,convert,convert_pu,default_unit, __doc__
from .units_llnl_ext import unit, measurement, convert, convert_pu, default_unit, __doc__
52 changes: 28 additions & 24 deletions test/python/test_conversion.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
#Copyright (c) 2019-2024,
#Lawrence Livermore National Security, LLC;
#See the top-level NOTICE for additional details. All rights reserved.
#SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2019-2024,
# Lawrence Livermore National Security, LLC;
# See the top-level NOTICE for additional details. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause

import units_llnl as u


def test_convert_units():
u1=u.unit('m')
u2=u.unit('cm')
v1=u.convert(10,u1,u2)
assert(v1==10*100)
u1 = u.unit("m")
u2 = u.unit("cm")
v1 = u.convert(10, u1, u2)
assert v1 == 10 * 100

v2 = u.convert(unit_in=u1, unit_out=u2, value=20)
assert v2 == 2000

v2=u.convert(unit_in=u1,unit_out=u2,value=20)
assert(v2==2000)

def test_convert_string():
v1=u.convert(10,'m','mm')
assert(v1==10*1000)
v1 = u.convert(10, "m", "mm")
assert v1 == 10 * 1000

v2 = u.convert(unit_out="mm", unit_in="m", value=20)
assert v2 == 20000

v2=u.convert(unit_out='mm',unit_in='m',value=20)
assert(v2==20000)

def test_convert_units_pu():
u1=u.unit('puMW')
u2=u.unit('kW')
v1=u.convert_pu(0.5,u1,u2,100)
assert(v1==50000)
u1 = u.unit("puMW")
u2 = u.unit("kW")
v1 = u.convert_pu(0.5, u1, u2, 100)
assert v1 == 50000

v2 = u.convert_pu(unit_in=u1, unit_out=u2, value=1.2, base=100)
assert v2 == 120000

v2=u.convert_pu(unit_in=u1,unit_out=u2,value=1.2,base=100)
assert(v2==120000)

def test_convert_string_pu():
v1=u.convert_pu(0.5,'puMW','kW',100)
assert(v1==50000)
v1 = u.convert_pu(0.5, "puMW", "kW", 100)
assert v1 == 50000

v2=u.convert_pu(unit_in='puMW',unit_out='kW',value=1.2,base=100)
assert(v2==120000)
v2 = u.convert_pu(unit_in="puMW", unit_out="kW", value=1.2, base=100)
assert v2 == 120000
222 changes: 119 additions & 103 deletions test/python/test_measurement.py
Original file line number Diff line number Diff line change
@@ -1,148 +1,164 @@
#Copyright (c) 2019-2024,
#Lawrence Livermore National Security, LLC;
#See the top-level NOTICE for additional details. All rights reserved.
#SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2019-2024,
# Lawrence Livermore National Security, LLC;
# See the top-level NOTICE for additional details. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause

import units_llnl as u


def test_basic_measurement():
m1=u.measurement('10 m')
m2=u.measurement('2.5 s')
m3=m1/m2
m4=u.measurement('4.0 m/s')
assert(m3==m4)
m1 = u.measurement("10 m")
m2 = u.measurement("2.5 s")
m3 = m1 / m2
m4 = u.measurement("4.0 m/s")
assert m3 == m4


def test_basic_measurement2():
m1=u.measurement(10,'m')
m2=u.measurement(2.5, 's')
m3=m1/m2
m4=u.measurement(4.0, 'm/s')
assert(m3==m4)
m1 = u.measurement(10, "m")
m2 = u.measurement(2.5, "s")
m3 = m1 / m2
m4 = u.measurement(4.0, "m/s")
assert m3 == m4


def test_basic_measurement3():

u1=u.unit('m')
u2=u.unit('s')
u1 = u.unit("m")
u2 = u.unit("s")

m1=u.measurement(10,u1)
m2=u.measurement(2.5, u2)
m1 = u.measurement(10, u1)
m2 = u.measurement(2.5, u2)

m4 = u.measurement(4.0, u1 / u2)
assert m1 / m2 == m4
assert m4.units() == u1 / u2

m4=u.measurement(4.0, u1/u2)
assert(m1/m2==m4)
assert(m4.units()==u1/u2)

def test_conditions():
m1=u.measurement(1.0,'m')
m2=u.measurement(3.0,u.unit('error'))
m3=u.measurement(2.4,"infinity")
assert(not m2.is_valid())
assert(m1.is_normal())
assert(not m2.is_normal())
m1 = u.measurement(1.0, "m")
m2 = u.measurement(3.0, u.unit("error"))
m3 = u.measurement(2.4, "infinity")
assert not m2.is_valid()
assert m1.is_normal()
assert not m2.is_normal()

assert m1.is_valid()

assert(m1.is_valid())
assert not m3.is_normal()
assert m3.is_valid()

assert(not m3.is_normal())
assert(m3.is_valid())

def test_root():
m1=u.measurement('100 m^6 per second squared')
m2=m1.root(2)
m3=m1.sqrt()
m4=u.measurement('10 m*m*m/s')
assert(m2==m3)
assert(m3==m4)
m1 = u.measurement("100 m^6 per second squared")
m2 = m1.root(2)
m3 = m1.sqrt()
m4 = u.measurement("10 m*m*m/s")
assert m2 == m3
assert m3 == m4


def test_pow():
m1=u.measurement('10 m')
m2=m1**2
m3=m1**3
assert(m2==m1*m1)
assert(m3==m1*m1*m1)
m1 = u.measurement("10 m")
m2 = m1**2
m3 = m1**3
assert m2 == m1 * m1
assert m3 == m1 * m1 * m1


def test_comparisons():
m1=u.measurement('10 m')
m2=u.measurement('11 m')
assert(m1<m2)
assert(m2>m1)
assert(m1>=m1)
assert(m2<=m2)
assert(m2>=m1)
assert(m1<=m2)
assert(m1!=m2)
m1 = u.measurement("10 m")
m2 = u.measurement("11 m")
assert m1 < m2
assert m2 > m1
assert m1 >= m1
assert m2 <= m2
assert m2 >= m1
assert m1 <= m2
assert m1 != m2


def test_set_value():
m1=u.measurement('100 months')
assert(m1.value()==100)
m2=m1.set_value(14)
assert(m2.value()==14)
m1 = u.measurement("100 months")
assert m1.value() == 100
m2 = m1.set_value(14)
assert m2.value() == 14


def test_set_units():
m1=u.measurement('100 months')
assert(m1.units()==u.unit('month'))
m2=m1.set_units(u.unit('day'))
assert(m2.units()==u.unit('day'))
m1 = u.measurement("100 months")
assert m1.units() == u.unit("month")
m2 = m1.set_units(u.unit("day"))
assert m2.units() == u.unit("day")


def test_value_as():
m1=u.measurement('20 weeks')
assert(m1.value_as('day')==20*7)
u1=u.unit('hr')
assert(m1.value_as(u1)==20*7*24)
m1 = u.measurement("20 weeks")
assert m1.value_as("day") == 20 * 7
u1 = u.unit("hr")
assert m1.value_as(u1) == 20 * 7 * 24


def test_convert_to():
m1=u.measurement('20 weeks')
m2=m1.convert_to('day')
assert(m2.value()==20*7)
u1=u.unit('hr')
m3=m1.convert_to('hr')
assert(m3.value()==20*7*24)

m4=m1.convert_to_base()
assert(m4.units()==u.unit('s'))
assert(m4.units().multiplier()==1.0)
assert(m4.value()==20*7*24*3600)
m1 = u.measurement("20 weeks")
m2 = m1.convert_to("day")
assert m2.value() == 20 * 7
u1 = u.unit("hr")
m3 = m1.convert_to("hr")
assert m3.value() == 20 * 7 * 24

m4 = m1.convert_to_base()
assert m4.units() == u.unit("s")
assert m4.units().multiplier() == 1.0
assert m4.value() == 20 * 7 * 24 * 3600


def test_as_unit():
m1=u.measurement('15 seconds')
m2=u.measurement(4,m1.as_unit())
m1 = u.measurement("15 seconds")
m2 = u.measurement(4, m1.as_unit())

assert m2.value() == 4
assert m2.value_as("s") == 60

assert(m2.value()==4)
assert(m2.value_as('s')==60)

def test_add_sub():
m1=u.measurement('15 seconds')
m2=u.measurement(1,'minute')
m3=m2-m1
assert(m3.value()==0.75)
m1 = u.measurement("15 seconds")
m2 = u.measurement(1, "minute")
m3 = m2 - m1
assert m3.value() == 0.75

m4 = m3 + m2 + m1
assert m4 == u.measurement(120, "second")

m4=m3+m2+m1
assert(m4==u.measurement(120,'second'))

def test_mult():
m1=u.measurement('2 meters')
m2=u.measurement(3,'meters')
m3=m2*m1
assert(m3.value()==6)
m1 = u.measurement("2 meters")
m2 = u.measurement(3, "meters")
m3 = m2 * m1
assert m3.value() == 6

m4=3*m3
assert(m4==u.measurement(18,'meters squared'))
m4 = 3 * m3
assert m4 == u.measurement(18, "meters squared")

m5 = m3 * 3
assert m5 == u.measurement(18, "meters squared")

m5=m3*3
assert(m5==u.measurement(18,'meters squared'))

def test_div():
m1=u.measurement('10 meters')
m2=u.measurement(2,'seconds')
m3=m1/m2
assert(m3.value()==5)
m1 = u.measurement("10 meters")
m2 = u.measurement(2, "seconds")
m3 = m1 / m2
assert m3.value() == 5

m4 = 10 / m3
assert m4 == u.measurement(2, "s/m")

m4=10/m3
assert(m4==u.measurement(2,'s/m'))
m5 = m3 / 2.5
assert m5 == u.measurement(2, "m/s")

m5=m3/2.5
assert(m5==u.measurement(2,'m/s'))

def test_string():
m1=u.measurement('10 lb')
assert(m1.to_string()=='10 lb')
s3=f"the measurement is {m1}"
assert(s3=="the measurement is 10 lb")
m1 = u.measurement("10 lb")
assert m1.to_string() == "10 lb"
s3 = f"the measurement is {m1}"
assert s3 == "the measurement is 10 lb"
Loading

0 comments on commit 9c3e557

Please sign in to comment.