Skip to content

Commit

Permalink
fix(area): Add new Area, Emissions-Factor unit types
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-p-may committed Sep 15, 2024
1 parent 43b1a99 commit 57a6460
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 6 deletions.
74 changes: 70 additions & 4 deletions ph_units/unit_types/area.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# -*- Python Version: 2.7 -*-

# T
from ph_units.unit_types._base import Base_UnitType


Expand All @@ -14,6 +14,7 @@ class MeterSquare(Base_UnitType):
"CM2": "{}*10_000",
"MM2": "{}*1_000_000",
"FT2": "{}*10.76391042",
"IN2": "{}*1550.0031",
}


Expand All @@ -22,15 +23,27 @@ class CentimeterSquare(Base_UnitType):

__symbol__ = "CM2"
__aliases__ = ["SQCM", "SQ.CM", "SQ-CM", "SQ-CENTIMETER", "SQ-CENTIMETERS", "CM²"]
__factors__ = {"MM2": "{}*100", "CM2": "{}*1", "M2": "{}*0.0001", "FT2": "{}*1"}
__factors__ = {
"MM2": "{}*100",
"CM2": "{}*1",
"M2": "{}*0.0001",
"FT2": "{}*0.001076391042",
"IN2": "{}*0.15500031",
}


class MillimeterSquare(Base_UnitType):
"""Millimeter Square"""

__symbol__ = "MM2"
__aliases__ = ["SQMM", "SQ.MM", "SQ-MM", "SQ-MILLIMETER", "SQ-MILLIMETERS", "MM²"]
__factors__ = {"MM2": "{}*1", "CM2": "{}*0.01", "M2": "{}*0.000001", "FT2": "{}*1"}
__factors__ = {
"MM2": "{}*1",
"CM2": "{}*0.01",
"M2": "{}*0.000001",
"FT2": "{}*0.000010764",
"IN2": "{}*0.0015500031",
}


class FootSquare(Base_UnitType):
Expand All @@ -46,7 +59,38 @@ class FootSquare(Base_UnitType):
"SQ-FOOT",
"FT²",
]
__factors__ = {"M2": "{}*0.09290304", "FT2": "{}*1"}
__factors__ = {
"M2": "{}*0.092903",
"CM2": "{}*929.0304",
"MM2": "{}*92_903.04",
"FT2": "{}*1",
"IN2": "{}*144",
}


class InchSquare(Base_UnitType):
"""Inch Square"""

__symbol__ = "IN2"
__aliases__ = [
"SIN",
"SI",
"SQIN",
"SQ.IN",
"SQ-IN",
"SQ-INCH",
"IN²",
]
__factors__ = {
"M2": "{}*0.00064516",
"CM2": "{}*6.4516",
"MM2": "{}*645.16",
"FT2": "{}*0.00694444",
"IN2": "{}*1",
}


# --- Concentrations ----------------------------------------------------------


class FootSquarePerPerson(Base_UnitType):
Expand All @@ -58,3 +102,25 @@ class FootSquarePerPerson(Base_UnitType):
"FT²/PERSON",
]
__factors__ = {"M2/PERSON": "{}*0.09290304", "FT2/PERSON": "{}*1"}


class CostPerFootSquared(Base_UnitType):
"""Cost Per Foot Squared"""

__symbol__ = "COST/FT2"
__aliases__ = [
"COST-FT2",
"COST/FT²",
]
__factors__ = {"COST/M2": "{}*10.76391042", "COST/FT2": "{}*1"}


class CostPerMeterSquared(Base_UnitType):
"""Cost Per Meter Squared"""

__symbol__ = "COST/M2"
__aliases__ = [
"COST-M2",
"COST/M²",
]
__factors__ = {"COST/M2": "{}*1", "COST/FT2": "{}*0.09290304"}
37 changes: 37 additions & 0 deletions ph_units/unit_types/emissions_factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,40 @@ class GramsPerKiloBtu(Base_UnitType):
"G/KBTU": "{}*1",
"G/KWH": "{}*3.4121416",
}


# --- CO2 per Area ------------------------------------------------------------
class MetricTonsPerMeterSquared(Base_UnitType):
"""MT/M2"""

__symbol__ = "MT/M2"
__aliases__ = ["MT/M²", "METRIC-TON/M2", "METRIC-TONS/M2"]
__factors__ = {
"MT/M2": "{}*1",
"KG/M2": "{}*1000",
"G/M2": "{}*1000000",
}


class KilogramsPerMeterSquared(Base_UnitType):
"""KG/M2"""

__symbol__ = "KG/M2"
__aliases__ = ["KG/M²"]
__factors__ = {
"MT/M2": "{}*0.001",
"KG/M2": "{}*1",
"G/M2": "{}*1000",
}


class GramsPerMeterSquared(Base_UnitType):
"""G/M2"""

__symbol__ = "G/M2"
__aliases__ = ["G/M²"]
__factors__ = {
"MT/M2": "{}*0.000001",
"KG/M2": "{}*0.001",
"G/M2": "{}*1",
}
35 changes: 33 additions & 2 deletions tests/test_areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from ph_units.converter import convert
from ph_units.unit_type import Unit


def test_meter_squared():
Expand All @@ -13,13 +14,16 @@ def test_meter_squared():
assert convert(1, "M2", "MM2") == 1_000_000
assert convert(00.0553, "M2", "MM2") == 55_300
assert convert(1, "M2", "FT2") == pytest.approx(10.76391042)
assert convert(1, "M2", "IN2") == pytest.approx(1550.0031)


def test_centimeter_squared():
assert convert(1, "CM2", "CM2") == 1
assert convert(1, "CM²", "CM2") == 1
assert convert(1, "CM2", "M2") == 0.0001
assert convert(1, "CM2", "MM2") == 100
assert convert(1, "CM2", "FT2") == pytest.approx(0.001076391042)
assert convert(1, "CM2", "IN2") == pytest.approx(0.15500031)


def test_millimeter_squared():
Expand All @@ -28,9 +32,36 @@ def test_millimeter_squared():
assert convert(1, "MM2", "M2") == 0.000001
assert convert(34_565_678, "MM2", "M2") == 34.565678
assert convert(1, "MM2", "CM2") == 0.01
assert convert(1, "MM2", "FT2") == pytest.approx(0.000010764)
assert convert(1, "MM2", "IN2") == pytest.approx(0.0015500031)


def test_foot_squared():
assert convert(1, "FT2", "M2") == pytest.approx(0.09290304)
assert convert(1, "FT²", "M2") == pytest.approx(0.09290304)
assert convert(1, "FT2", "IN2") == pytest.approx(144)
assert convert(1, "FT2", "FT2") == 1
assert convert(1, "FT2", "MM2") == pytest.approx(92903.04)
assert convert(1, "FT2", "CM2") == pytest.approx(929.0304)
assert convert(1, "FT2", "M2") == pytest.approx(0.09290304)


def test_inch_squared():
assert convert(1, "IN2", "IN2") == 1
assert convert(1, "IN2", "FT2") == pytest.approx(0.00694444)
assert convert(1, "IN2", "MM2") == pytest.approx(645.16)
assert convert(1, "IN2", "CM2") == pytest.approx(6.4516)
assert convert(1, "IN2", "M2") == pytest.approx(0.00064516)


# --- Concentrations ----------------------------------------------------------


def test_cost_per_meter_squared():
assert convert(1, "COST/M2", "COST/M2") == 1
assert convert(1, "COST/M2", "COST/FT2") == pytest.approx(0.09290304)
assert Unit(1, "COST/M2").as_a("COST/FT2") == pytest.approx(0.09290304)


def test_cost_per_foot_squared():
assert convert(1, "COST/FT2", "COST/M2") == pytest.approx(10.76391042)
assert convert(1, "COST/FT2", "COST/FT2") == 1
assert Unit(1, "COST/FT2").as_a("COST/M2") == pytest.approx(10.76391042)
24 changes: 24 additions & 0 deletions tests/test_emissions_factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,27 @@ def test_gram_per_kBtu():
assert convert(1, "G/KBTU", "G/KWH") == pytest.approx(3.4121416)
assert convert(1, "G/KBTU", "G/BTU") == pytest.approx(0.001)
assert convert(1, "G/KBTU", "G/KBTU") == 1


# --- CO2 per Area ------------------------------------------------------------


def test_metric_ton_per_meter_squared():
assert convert(1, "MT/M2", "MT/M2") == 1
assert convert(1, "MT/M²", "MT/M²") == 1
assert convert(1, "MT/M2", "KG/M2") == 1000
assert convert(1, "MT/M2", "G/M2") == 1_000_000


def test_kilogram_per_meter_squared():
assert convert(1, "KG/M2", "MT/M2") == pytest.approx(0.001)
assert convert(1, "KG/M²", "KG/M²") == 1
assert convert(1, "KG/M2", "KG/M2") == 1
assert convert(1, "KG/M2", "G/M2") == pytest.approx(1000)


def test_gram_per_meter_squared():
assert convert(1, "G/M2", "MT/M2") == pytest.approx(0.000001)
assert convert(1, "G/M2", "KG/M2") == pytest.approx(0.001)
assert convert(1, "G/M2", "G/M2") == 1
assert convert(1, "G/M²", "G/M²") == 1

0 comments on commit 57a6460

Please sign in to comment.