formula
codes.formula
Module for the abstract base class Formula.
Classes:
-
AggregatedComparisonFormula–Base class for aggregating comparison formulas used in the codes.
-
ComparisonFormula–Base class for comparison formulas used in the codes.
-
DoubleComparisonFormula–Base class for double comparison formulas used in the codes.
-
Formula–Abstract base class for formulas used in the codes.
codes.formula.AggregatedComparisonFormula
AggregatedComparisonFormula(*_args, **_kwargs)
Bases: ComparisonFormula
Base class for aggregating comparison formulas used in the codes. Examples: (angle < angle_max) and (height > height_min).
This class is abstract: it does not implement label, source_document or latex.
A concrete formula that aggregates other comparison formulas should subclass this and provide those,
the same way concrete subclasses of :class:ComparisonFormula do.
Such a subclass takes the parameters of the code formula it represents, and not an aggregation function
with a list of formulas. It translates the one into the other by overriding :meth:_define_aggregation,
which is where the formulas to aggregate are built.
Examples:
class SomeAggregatedComparisonFormula(AggregatedComparisonFormula): label = "1.1" source_document = "Some code document"
def __init__(self, value: float, lower_bound: float, upper_bound: float) -> None:
# The arguments are read by _define_aggregation, so nothing is passed on to the base class here.
super().__init__()
self.value = value
self.lower_bound = lower_bound
self.upper_bound = upper_bound
@classmethod
def _define_aggregation(cls, value, lower_bound, upper_bound):
return all, (
SomeLowerBoundFormula(value=value, lower_bound=lower_bound),
SomeUpperBoundFormula(value=value, upper_bound=upper_bound),
)
formula = SomeAggregatedComparisonFormula(value=5, lower_bound=1, upper_bound=10) bool(formula) # True, both bounds are satisfied formula.comparison_formulas # the two bounds, built once by _define_aggregation formula.unity_check # the largest of the two unity checks, because the aggregation is 'all'
The default implementation returns what it is given, so formulas that are already instantiated can be aggregated without such a subclass:
aggregated_formula = AggregatedComparisonFormula(all, [formula1, formula2])
Method for initializing a new instance of the class.
The aggregation function and the comparison formulas are defined and stored by __new__
through :meth:_define_aggregation, so the constructor arguments are not read again here.
Source code in blueprints/codes/formula.py
316 317 318 319 320 321 322 | |
codes.formula.AggregatedComparisonFormula.lhs
property
lhs: float
Disabled property for getting the left-hand side of the comparison, as it is not relevant for this class.
codes.formula.AggregatedComparisonFormula.rhs
property
rhs: float
Disabled property for getting the right-hand side of the comparison, as it is not relevant for this class.
codes.formula.AggregatedComparisonFormula.unity_check
property
unity_check: float
Property to present the unity check of the formula.
A unity check is the ratio between the left-hand side (lhs) and right-hand side (rhs) of a comparison formula. For an aggregated comparison formula, the unity check is determined by the aggregation function (all or any) applied to the unity checks of the individual comparison formulas:
- If aggregation is all, the unity check is the maximum of the unity checks of the individual formulas.
- If aggregation is any, the unity check is the minimum of the unity checks of the individual formulas.
A unity check <= 1 indicates the condition is satisfied. A unity check > 1 indicates the condition is not satisfied.
Examples:
formula1.unity_check = 0.9 formula2.unity_check = 1.1
aggregated_formula = AggregatedComparisonFormula(all, [formula1, formula2]) aggregated_formula.unity_check # Returns 1.1, as the maximum of the unity checks is taken for 'all' aggregation.
aggregated_formula = AggregatedComparisonFormula(any, [formula1, formula2]) aggregated_formula.unity_check # Returns 0.9, as the minimum of the unity checks is taken for 'any' aggregation.
Returns:
-
float–The unity check ratio.
codes.formula.AggregatedComparisonFormula.latex
latex(n: int = 3) -> LatexFormula
Return the latex representation of the aggregated comparison formula.
Parameters:
-
n(int, default:3) –The number of decimal places to round the result to.
Returns:
-
LatexFormula–The latex representation of the formula, given in math mode.
Source code in blueprints/codes/formula.py
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | |
codes.formula.ComparisonFormula
ComparisonFormula(*args, **kwargs)
Bases: Formula, ABC
Base class for comparison formulas used in the codes.
Source code in blueprints/codes/formula.py
21 22 23 24 | |
codes.formula.ComparisonFormula.lhs
property
lhs: float
Property for getting the left-hand side of the comparison.
Returns:
-
float–The left-hand side value of the comparison.
codes.formula.ComparisonFormula.rhs
property
rhs: float
Property for getting the right-hand side of the comparison.
Returns:
-
float–The right-hand side value of the comparison.
codes.formula.ComparisonFormula.unity_check
property
unity_check: float
Property to present the unity check of the formula.
A unity check is the ratio between the left-hand side (lhs) and right-hand side (rhs) of a comparison formula. The calculation is operator-dependent to ensure a unity check less than 1 always indicates the condition is satisfied:
- For le (<=) and lt (<): unity_check = lhs / rhs
- For ge (>=) and gt (>): unity_check = rhs / lhs
- For eq (==) and other operators: unity_check = lhs / rhs
A unity check < 1 indicates the condition is satisfied. A unity check >= 1 indicates the condition is not satisfied.
Examples:
lhs = 0.11, rhs = 0.1, Formula: lhs <= rhs, unity_check = 1.1 # NOT satisfied lhs = 0.09, rhs = 0.1, Formula: lhs <= rhs, unity_check = 0.9 # satisfied lhs = 0.2, rhs = 0.1, Formula: lhs >= rhs, unity_check = 0.5 # satisfied lhs = 0.05, rhs = 0.1, Formula: lhs >= rhs, unity_check = 2.0 # NOT satisfied
Returns:
-
float–The unity check ratio.
codes.formula.DoubleComparisonFormula
DoubleComparisonFormula(*args, **kwargs)
Bases: Formula
Base class for double comparison formulas used in the codes. Examples: angle_min < angle < angle_max or angle_min > angle > angle_max.
Note that the comparison operators must point in the same direction for both sides: - Ascending: operator.lt (<) or operator.le (<=) - Descending: operator.gt (>) or operator.ge (>=) Mixed directions (e.g., < and >) are not allowed.
Source code in blueprints/codes/formula.py
21 22 23 24 | |
codes.formula.DoubleComparisonFormula.lhs
property
lhs: float
Property for getting the left-hand side of the double comparison.
Returns:
-
float–The left-hand side value of the comparison.
codes.formula.DoubleComparisonFormula.rhs
property
rhs: float
Property for getting the right-hand side of the double comparison.
Returns:
-
float–The right-hand side value of the comparison.
codes.formula.DoubleComparisonFormula.val
property
val: float
Property for getting the middle value of the double comparison to be checked against the bounds.
Returns:
-
float–The left-hand side value of the comparison.
codes.formula.Formula
Formula(*args, **kwargs)
Bases: float, ABC
Abstract base class for formulas used in the codes.
Method for initializing a new instance of the class.
Source code in blueprints/codes/formula.py
21 22 23 24 | |
codes.formula.Formula.detailed_result
property
detailed_result: dict
Property for providing the detailed result of the formula.
Returns:
-
dict–The detailed result of the formula. Keys are strings representing the name of the partial or intermediate result. Values types will depend on the specific implementation, but must be a serializable type.
codes.formula.Formula.label
abstractmethod
property
label: str
Property for the formula label.
For example, "5.2" for formula 5.2.
Returns:
-
str–The label/number associated with the formula. This is an abstract method and must be implemented in all subclasses.
codes.formula.Formula.source_document
abstractmethod
property
source_document: str
Property for the source document.
For example, "EN 1992-1-1:2004" Try to use the official and complete name of the document including publishing year, if possible.
Returns:
-
str–The reference to the document where the formula originates. This is an abstract method and must be implemented in all subclasses.
codes.formula.Formula.latex
abstractmethod
latex(n: int = 3) -> LatexFormula
Abstract method for the latex representation of the formula, given in math mode.
Parameters:
-
n(int, default:3) –The number of decimal places to round the result to.
Returns:
-
LatexFormula–The latex representation of the formula, given in math mode. This is an abstract method and must be implemented in all subclasses.
Source code in blueprints/codes/formula.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |