Skip to content

Commit

Permalink
Make p into a property constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Mar 21, 2024
1 parent b6b2af0 commit b778dee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
26 changes: 18 additions & 8 deletions src/property_utils/properties/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
except ImportError:
from typing_extensions import Self # Python <= 3.10

try:
from typing import TypeAlias # Python >= 3.10 pylint: disable=ungrouped-imports
except ImportError:
from typing_extensions import TypeAlias # Python < 3.10

from property_utils.units.descriptors import (
MeasurementUnit,
Dimension,
Expand All @@ -25,6 +20,7 @@

# pylint: disable=unused-wildcard-import, wildcard-import
from property_utils.units.converters import *
from property_utils.units.units import NonDimensionalUnit
from property_utils.units.converter_types import UnitConverter, get_converter
from property_utils.exceptions.units.converter_types import UndefinedConverterError
from property_utils.exceptions.properties.property import (
Expand All @@ -37,6 +33,23 @@
from property_utils.exceptions.base import PropertyUtilsException


def p(
value: float, unit: UnitDescriptor = NonDimensionalUnit.NON_DIMENSIONAL
) -> "Property":
"""
Create a property with a value and a unit.
Default unit is non-dimensional, i.e. no unit.
>>> from property_utils.units import KELVIN
>>> p(350, KELVIN)
<Property: 350 K>
>>> p(20.23)
<Property: 20.23 >
"""
return Property(value, unit)


@dataclass
class Property:
"""
Expand Down Expand Up @@ -573,6 +586,3 @@ def _validate_comparison_input(self, other) -> None:
f"cannot compare ({other}) to ({self}); "
f"({other}) must have ({self.unit.to_generic()}) units. "
)


p: TypeAlias = Property
19 changes: 17 additions & 2 deletions src/property_utils/tests/properties/test_property.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from unittest import TestSuite, TextTestRunner

from unittest_extensions import args
from unittest_extensions import args, TestCase

from property_utils.properties.property import Property
from property_utils.properties.property import Property, p
from property_utils.units.units import NonDimensionalUnit, PressureUnit
from property_utils.exceptions.properties.property import (
PropertyExponentError,
)
Expand Down Expand Up @@ -62,6 +63,20 @@ def test_does_not_initialize_converter(self):
self.assertIsNone(self.result().unit_converter)


@add_to(property_test_suite)
class TestPropertyConstructor(TestCase):
def subject(self, **kwargs):
return p(**kwargs)

@args({"value": 5})
def test_with_no_unit(self):
self.assertEqual(self.result().unit, NonDimensionalUnit.NON_DIMENSIONAL)

@args({"value": 5, "unit": PressureUnit.BAR})
def test_with_unit(self):
self.assertEqual(self.result().unit, PressureUnit.BAR)


@add_to(property_test_suite, "eq")
class TestPropertyEq(TestProperty):

Expand Down
1 change: 1 addition & 0 deletions src/property_utils/units/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from property_utils.units.aliases import *

0 comments on commit b778dee

Please sign in to comment.