UnitSystem

class opstool.pre.UnitSystem(length='m', force='kn', time='sec')[source]

Bases: object

A class for unit conversion. All unit factors are transformed into the base unit system as specified by the following length, force, and time parameters.

Parameters

length: str, default=”m”

Length unit base. Optional [“inch”, “ft”, “mm”, “cm”, “m”, “km”].

force: str, default=”kN”

Force unit base. Optional [“lb”(“lbf”), “kip”, “n”, “kn”, “mn”, “kgf”, “tonf”].

time: str, default=”sec”

Time unit base. Optional [“sec”].

Note

  • Mass and stress units can be automatically determined based on length and force units, optional mass units include [“mg”, “g”, “kg”, “ton”(“t”), “slug”], and optional stress units include [“pa”, “kpa”, “mpa”, “gpa”, “bar”, “psi”, “ksi”, “psf”, “ksf”].

  • You can enter any uppercase and lowercase forms, such as kn and kN, mpa and MPa are equivalent.

  • You can add a number (int) after the unit to indicate a power, such as .m3 for m*m*m.

  • You can use key indexing, such as: unit[“m^3”], unit[“kN/m^2”], unit[“N*sec^2/m”], unit[“MPa”]

Examples

>>> UNIT = UnitSystem(length="m", force="kN", time="min")
>>> # Call the __repr__ method, print the UnitSystem object information
>>> print(UNIT)
>>> # Call the print method, print all common units
>>> UNIT.print()
>>> # use key indexing
>>> print("N/mm2", UNIT["N/mm2"])
>>> print("N*mm/m^2", UNIT["N*mm/m^2"])
>>> print("MPa", UNIT["MPa"])
>>> # Show some unit conversion effects
>>> print("Length:", UNIT.mm, UNIT.Mm2, UNIT.cm, UNIT.m, UNIT.M2, UNIT.inch, UNIT.Ft)
>>> print("Force", UNIT.n, UNIT.kN, UNIT.kN2, UNIT.lbf, UNIT.kip)
>>> print("Stress", UNIT.mpa, UNIT.kpa, UNIT.pa, UNIT.psi, UNIT.ksi)
>>> print("Mass", UNIT.g, UNIT.kg, UNIT.ton, UNIT.slug)
>>> print("Time", UNIT.msec, UNIT.min, UNIT.hour, UNIT.day, UNIT.year)

Methods

print

Show all unit conversion coefficients with colorful output

Attributes

force

length

time

print()[source]

Show all unit conversion coefficients with colorful output