Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Chip and (x,y) to be interchangable as keys #230

Closed
Christian-B opened this issue Nov 8, 2023 · 6 comments
Closed

Allow Chip and (x,y) to be interchangable as keys #230

Christian-B opened this issue Nov 8, 2023 · 6 comments

Comments

@Christian-B
Copy link
Member

We already have the notion that 2 Chip objects are considered equal if they have the same x and y

There also many case when the Chip object is only used to represent the x and y coordinates.

  • When they are keys in a dict
  • When comparing two Chips or locations

Currently we have to careful manage and think about when we have a Chip object and when we have a XY tuple

But do we really care?

It is technically possible to make the Chip(A,B) and the Tuple (A,B) to be "eq" and to return the same hash

Then we would not longer need to worry about when we have an XY and when we have a Chip.

@Christian-B
Copy link
Member Author

For typing there would be Three types

Chip - It is known or must be a Chip Object
XY - It is known or must be an (int, int) Tuple
X_Y It can be either a Chip or an (int, int) Tuple

@Christian-B
Copy link
Member Author

class Chip(object):
slots = ("_x", "_y")

# pylint: disable=too-many-arguments, wrong-spelling-in-docstring
def __init__(self, x: int, y: int):
    self._x = x
    self._y = y

def __eq__(self, other):
    # Equality just on X,Y; that's most useful
    if not isinstance(other, Chip):
        if isinstance(other, tuple) and len(other) == 2:
            return self._x == other[0] and self._y == other[1]
        return NotImplemented
    return self._x == other._x and self._y == other._y

def __hash__(self) -> int:
    return hash((self._x, self._y))

foo = dict()
a = Chip(2, 3)
b = Chip(4, 5)

assert a == (2, 3)
assert (2, 3) == a
foo[a] = "a"
foo[(4, 5)] = b
assert hash(a) == hash((2, 3))
k = list(foo.keys())
assert foo[a] == foo[(2, 3)]
assert foo[b] == foo[(4, 5)]
assert foo[(4, 5)] == foo[b]
assert foo[(2, 3)] == foo[a]

@Christian-B
Copy link
Member Author

A major downside possibly even killer of this idea is that the dictionaries keys() and items() methods could not longer be relied on to return all Chips or all Tuples

@Christian-B
Copy link
Member Author

class Chip(object):

__slots__ = ["_x", "_y"]

def __init__(self, x, y, bla):
    self._x = x
    self._y = y

def __iter__(self):
    yield self._x
    yield self._y

def __len__(self):
    return 2

def __eq__(self, other):
    if isinstance(other, Chip):
        return self._x == other._x and self._y == other._y
    else:
        return other == (self._x, self._y)

def __hash__(self) -> int:
    return hash((self._x, self._y))

a = Chip(1, 3, "bacon")
b = (1, 3)
(x, y) = a
print((x, y))
print(a == b)
print(b == a)
print(hash(a))
print(hash(b))

@Christian-B
Copy link
Member Author

A major downside possibly even killer of this idea is that the dictionaries keys() and items() methods could not longer be relied on to return all Chips or all Tuples

With the above where we want the dict to return (x, y) it can return Chip as (x, y) = chip works they same as (x, y) = xy

Where the dict must return a Chip mypy should catch the put of an xy tuple.

@Christian-B Christian-B mentioned this issue Mar 5, 2024
@Christian-B
Copy link
Member Author

done in #246

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant