source: geniuswebcore/geniusweb/issuevalue/Bid.py@ 73

Last change on this file since 73 was 73, checked in by Bart Vastenhouw, 3 years ago

Fix for IssueValue hashcode.

File size: 2.5 KB
Line 
1from typing import Any, Dict, Union, Set
2from geniusweb.issuevalue.Value import Value
3from geniusweb.utils import toStr
4
5NoneType=type(None)
6
7class Bid:
8 def __init__(self, issuevalues:Dict[str, Value]):
9 '''
10 @param a map of issue, Value pairs. The String is the issue name.
11 '''
12 if not isinstance(issuevalues,dict):
13 raise ValueError("issuevalues must be dict, got "+str(issuevalues))
14 for (iss,val) in issuevalues.items():
15 if not isinstance(iss, str):
16 raise ValueError("Issue "+str(iss)+" must be a str")
17 if not isinstance(val, Value):
18 raise ValueError("value "+str(val)+" must be a Value, but is "+str(type(val)))
19 self._issuevalues = dict(issuevalues) #shallow copy : Value is immutable
20
21 def getIssueValues(self) -> Dict[str, Value]:
22 '''
23 @param issue name of the issue
24 @return the value for the given issue, or null if there is no value for
25 the given issue.
26 '''
27 return self._issuevalues
28
29 def getIssues(self)->Set[str]:
30 return set(self._issuevalues.keys())
31
32
33 def getValue(self, issue:str) -> Union[Value,NoneType] :
34 '''
35 @param issue name of the issue
36 @return the value for the given issue, or null if there is no value for
37 the given issue.
38 '''
39 if not issue in self._issuevalues:
40 return None
41 return self._issuevalues[issue];
42
43 def merge(self, otherbid:"Bid")->"Bid" :
44 '''
45 this this partial bid with another partial bid.
46
47 @param otherbid another partial bid.
48 @return a bid with the combined values of both partial bids.
49 @throws IllegalArgumentException if issues overlap.
50 '''
51 ourissues = set(self._issuevalues.keys())
52 ourissues = ourissues.intersection(otherbid.getIssues())
53 if len(ourissues)!=0:
54 raise ValueError(\
55 "Otherbid contains issues that are already set:"\
56 + str(ourissues))
57
58 newvalues = dict(self._issuevalues)
59 newvalues.update(otherbid._issuevalues)
60 return Bid(newvalues)
61
62 def __eq__(self, other):
63 return isinstance(other, self.__class__) and \
64 self._issuevalues == other._issuevalues
65
66 def __repr__(self) -> str:
67 return "Bid" + toStr(self._issuevalues);
68
69 def __hash__(self):
70 return hash((tuple(self._issuevalues.items())))
71
Note: See TracBrowser for help on using the repository browser.