Skip to content

Commit

Permalink
Merge f73880e into ee91173
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTimperley committed Oct 3, 2018
2 parents ee91173 + f73880e commit 2c21885
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
14 changes: 10 additions & 4 deletions houston/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,19 @@ def __getitem__(self, name: str) -> Any:
msg = "no option [{}] in state [{}]"
msg.format(name, self.__class__.__name__)
raise KeyError(msg)
return getattr(self, var._field)
return getattr(self, opt._field)

def __hash__(self) -> int:
all_opts = [self[opt.name] for opt in self.__class__.options]
all_opts.insert(0, self.__class__.__name__)
return hash(tuple(all_opts))

def __eq__(self, other: 'Configuration') -> bool:
if type(self) != type(other):
msg = "illegal comparison of configurations: [{}] vs. [{}]"
msg = msg.format(self.__class__.__name__, state.__class__.__name__)
raise Exception(msg) # FIXME use HoustonException
msg = "comparison of different classesof configs: [{}] vs. [{}]"
msg = msg.format(self.__class__.__name__, other.__class__.__name__)
logger.warning(msg)
return False
return self.__dict__ == other.__dict__

def to_dict(self) -> Dict[str, Any]:
Expand Down
5 changes: 5 additions & 0 deletions houston/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ def exact(self, other: 'State') -> bool:

__eq__ = exact

def __hash__(self) -> int:
all_vars = [self[v.name] for v in self.__class__.variables]
all_vars.append(self.time_offset)
return hash(tuple(all_vars))

def __getitem__(self, name: str) -> Any:
# FIXME use frozendict
try:
Expand Down
19 changes: 19 additions & 0 deletions test/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ class X(Configuration):
assert conf.bar == 1


def test_hash():
class X(Configuration):
foo = option(int)
class Y(Configuration):
foo = option(int)

c1 = X(foo=0)
c2 = X(foo=1)
c3 = X(foo=0)
c4 = X(foo=1)

s = {c1, c2, c3, c4}
assert s == {X(foo=0), X(foo=1)}

c5 = Y(foo=0)
assert len({c1, c5}) == 2



def test_is_frozen():
class X(Configuration):
foo = option(int)
Expand Down
12 changes: 12 additions & 0 deletions test/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ class S(State):
state.foo = 10


def test_hash():
class S(State):
foo = var(int, lambda c: 0)

s1 = S(foo=0, time_offset=0.0)
s2 = S(foo=1, time_offset=0.0)
s3 = S(foo=0, time_offset=0.0)
s4 = S(foo=1, time_offset=0.0)

assert {s1, s2, s3, s4} == {S(foo=0, time_offset=0.0), S(foo=1, time_offset=0.0)}


def test_eq():
class S(State):
foo = var(int, lambda c: 0)
Expand Down

0 comments on commit 2c21885

Please sign in to comment.