Skip to content

Commit

Permalink
Merge branch '1.1.x' into 1.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
j-ittner committed Jul 21, 2021
2 parents 8dd1c0b + 8b40d86 commit 4f921db
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 70 deletions.
8 changes: 8 additions & 0 deletions RELEASE_NOTES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ This is a maintenance release to catch up with *pytools* 1.1.4.
*pytools* 1.1
-------------

1.1.5
~~~~~

- FIX: fixed a rare case where :meth:`~.Expression.eq_` returned `False` for two
equivalent expressions if one of them included an :class:`~.ExpressionAlias`
- FIX: accept any type of numerical values as leaf weights of :class:`~.LinkageTree`


1.1.4
~~~~~

Expand Down
9 changes: 0 additions & 9 deletions src/pytools/expression/base/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,5 @@ def hash_(self) -> int:
)
)

def _eq_same_type(self, other: "InfixExpression") -> bool:
return (
self.infix_ == other.infix_
and len(self.subexpressions_) == len(other.subexpressions_)
and all(
a.eq_(b) for a, b in zip(self.subexpressions_, other.subexpressions_)
)
)


__tracker.validate()
87 changes: 34 additions & 53 deletions src/pytools/expression/composite/_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Any, Tuple, TypeVar, Union, cast

from ...api import AllTracker, inheritdoc
from .. import Expression, make_expression
from .. import Expression, ExpressionAlias, make_expression
from ..atomic import Epsilon, Id
from ..base import (
BracketPair,
Expand All @@ -16,9 +16,8 @@
Invocation,
Operation,
SimplePrefixExpression,
SingletonExpression,
)
from ..operator import BinaryOperator, Operator, UnaryOperator
from ..operator import BinaryOperator, UnaryOperator
from .base import DictEntry, KeywordArgument, LambdaDefinition

log = logging.getLogger(__name__)
Expand All @@ -29,7 +28,6 @@
#

__all__ = [
"BracketedExpression",
"ListLiteral",
"TupleLiteral",
"SetLiteral",
Expand Down Expand Up @@ -67,50 +65,6 @@
#


@inheritdoc(match="[see superclass]")
class BracketedExpression(SingletonExpression, metaclass=ABCMeta):
"""
An expression surrounded by brackets.
"""

def __init__(self, brackets: BracketPair, subexpression: Any) -> None:
"""
:param brackets: the brackets enclosing this expression's subexpressions
:param subexpression: the subexpression enclosed by brackets
"""
super().__init__()
self._brackets = brackets
self._subexpression = make_expression(subexpression)

@property
def brackets_(self) -> BracketPair:
"""
The brackets enclosing this expression's subexpressions.
"""
return self._brackets

@property
def subexpression_(self) -> Expression:
"""
The subexpression enclosed by the brackets.
"""
return self._subexpression

@property
def precedence_(self) -> int:
"""[see superclass]"""
return Operator.MAX_PRECEDENCE

def hash_(self) -> int:
"""[see superclass]"""
return hash((type(self), self.brackets_, self.subexpression_.hash_()))

def _eq_same_type(self, other: "BracketedExpression") -> bool:
return self.brackets_ == other.brackets_ and self.subexpression_.eq_(
other.subexpression_
)


class ListLiteral(CollectionLiteral):
"""
A list expression.
Expand Down Expand Up @@ -230,11 +184,13 @@ def __init__(self, operator: BinaryOperator, *operands: Any) -> None:

first_operand = operands[0]

if isinstance(first_operand, BinaryOperation):
if first_operand.operator_ == operator:
# if first operand has the same operator, we flatten the operand
# noinspection PyUnresolvedReferences
operands = (*first_operand.operands_, *operands[1:])
if (
isinstance(first_operand, BinaryOperation)
and first_operand.operator_ == operator
):
# if first operand has the same operator, we flatten the operand
# noinspection PyUnresolvedReferences
operands = (*first_operand.operands_, *operands[1:])

self._operator = operator
self._operands = operands
Expand All @@ -259,6 +215,31 @@ def precedence_(self) -> int:
"""[see superclass]"""
return self.infix_.precedence

def _flattened_operands(self) -> Tuple[Expression, ...]:
operands = self.operands_
first_operand = operands[0]

while isinstance(first_operand, ExpressionAlias):
first_operand = first_operand.expression_

if (
isinstance(first_operand, BinaryOperation)
and first_operand.operator_ == self.operator_
):
return (*first_operand._flattened_operands(), *operands[1:])

return operands

def _eq_same_type(self, other: "BinaryOperation") -> bool:
if self.operator_ == other.operator_:
self_operands_ = self._flattened_operands()
other_operands_ = other._flattened_operands()
return len(self_operands_) == len(other_operands_) and all(
a.eq_(b) for a, b in zip(self_operands_, other_operands_)
)
else:
return False


#
# Invocations
Expand Down
12 changes: 4 additions & 8 deletions src/pytools/viz/dendrogram/_linkage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import numpy as np

from pytools.api import AllTracker, to_tuple
from pytools.api import AllTracker
from pytools.viz.dendrogram.base import LeafNode, LinkageNode, Node

#
Expand Down Expand Up @@ -105,16 +105,12 @@ def __init__(

def _validate_leaves(var: Sequence[Any], var_name: str):
if len(var) != n_leaves:
raise ValueError(f"expected {n_leaves} values " f"for arg {var_name}")
raise ValueError(f"expected {n_leaves} values for arg {var_name}")

self.scipy_linkage_matrix = scipy_linkage_matrix

leaf_names: Tuple[str, ...] = to_tuple(
leaf_names, element_type=str, arg_name="leaf_names"
)
leaf_weights: Tuple[float, ...] = to_tuple(
leaf_weights, element_type=float, arg_name="leaf_weights"
)
leaf_names: List[str] = [str(name) for name in leaf_names]
leaf_weights: List[float] = [float(weight) for weight in leaf_weights]

_validate_leaves(leaf_names, "leaf_labels")
_validate_leaves(leaf_weights, "leaf_weights")
Expand Down

0 comments on commit 4f921db

Please sign in to comment.